Digital Lab Assignment-4 Parallel Distribution and Computing
Digital Lab Assignment-4 Parallel Distribution and Computing
Name-Ranuj Pradhan
Reg.No-20BCE2459
CRISTIAN ALGORITHM
The Cristian's algorithm is a time synchronization
protocol used in distributed systems. It is designed to
synchronize the clocks of different nodes in a network to
a common time reference. The algorithm was proposed
by Leslie Lamport and is named after its co-author,
Cristian Flaviu.
CODE-
#include <iostream>
#include <chrono>
#include <omp.h>
return 0;
}
#include <iostream>
#include <chrono>
#include <omp.h>
int main() {
int num_servers = 4;
double server_times[num_servers] = {10.0, 15.0, 12.0, 9.0};
double client_time = 0.0;
double total_time = 0.0;
double avg_time = 0.0;
return 0;
}
Output-
2. Berkeley Algorithm
int main() {
int id, n;
int max_delay = 5;
omp_set_num_threads(4);
return 0;
}
#include <iostream>
#include <omp.h>
#include <chrono>
#include <thread>
using namespace std;
int main() {
int id, n;
int max_delay = 5;
omp_set_num_threads(4);
cout << "Process " << id << " enters critical section at " <<
local_clock << endl;
this_thread::sleep_for(chrono::seconds(rand() % max_delay));
cout << "Process " << id << " leaves critical section at " <<
local_clock << endl;
}
}
return 0;
}
Output-
Code-
#include <iostream>
#include <mpi.h>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
vector<int> workload(size);
for (int i = 0; i < size; i++) {
workload[i] = rand() % 10;
}
if (rank == 0) {
workload[dest]++;
}
}
// receiver processes
else {
for (int i = 0; i < task_count; i++) {
int task;
MPI_Recv(&task, 1, MPI_INT, 0, i, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
cout << "Process " << rank << " received task " <<
task << endl;
}
}
MPI_Finalize();
return 0;
}
.#include <iostream>
#include <mpi.h>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
vector<int> workload(size);
for (int i = 0; i < size; i++) {
workload[i] = rand() % 10;
}
workload[dest]++;
}
}
// receiver processes
else {
for (int i = 0; i < task_count; i++) {
int task;
MPI_Recv(&task, 1, MPI_INT, 0, i, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
cout << "Process " << rank << " received task " << task << endl;
}
}
MPI_Finalize();
return 0;
}
Output-