Code:
#include
#include
#include
int main(int argc, char** argv) {
int num_threads = 8;
int ping_pong_count = 1000000;
double start_time, end_time;
int i, j;
//Initialize the OpenMP environment
omp_set_num_threads(num_threads);
//Start the timer
start_time = omp_get_wtime();
//Parallel region
#pragma omp parallel shared(ping_pong_count)
{
for (i = 0; i < ping_pong_count; i++) {
//Send a message to the next node
#pragma omp critical
{
printf(“Thread %d is sending a message to the next node\n”, omp_get_thread_num());
fflush(stdout);
}
//Wait for a message from the previous node
#pragma omp critical
{
printf(“Thread %d is waiting for a message from the previous node\n”, omp_get_thread_num());
fflush(stdout);
}
}
}
//Stop the timer
end_time = omp_get_wtime();
printf(“Time taken for %d ping pong messages: %f seconds\n”, ping_pong_count, end_time – start_time);
return 0;
}