Question: To compile this program, include the option -fopenmp in your compile command. There are NUM_THREADS threads that each increments their own int in a loop.

To compile this program, include the option -fopenmp in your compile command. There are NUM_THREADS threads that each increments their own int in a loop. Next, write a script that runs the above code varying the number of threads by 1 from 1 to 8 in bash

 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include  #include  #include "omp.h" int main(int argc, char *argv[]) { int NUM_THREADS = 1; if (argc == 2) { NUM_THREADS = atoi(argv[1]); } if (NUM_THREADS == 0) { std::cerr << "ERROR: invalid number of threads" << std::endl; exit(1); } int *array = new int[NUM_THREADS]; for (int i = 0; i < NUM_THREADS; i++) array[i] = 0; struct timeval tv1, tv2; gettimeofday(&tv1, 0); #pragma omp parallel num_threads(NUM_THREADS) { int index = omp_get_thread_num(); for(int i = 0; i < 1e9; i++) array[index]++; } gettimeofday(&tv2, 0); std::cout << "Time: " << (double)(tv2.tv_usec - tv1.tv_usec) / 1000000 + (double)(tv2.tv_sec - tv1.tv_sec) << std::endl; delete[] array; return 0; } 

To compile this program, include the option -fopenmp in your compile command. There are NUM_THREADS threads that each increments their own int in a loop. Next, write a script that runs the above code varying the number of threads by 1 from 1 to 8.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!