In this part, you will add all elements in an array in parallel. 1. Write your code using the following template, and mo
Posted: Fri Apr 29, 2022 7:00 am
In this part, you will add all elements in an array in parallel. 1. Write your code using the following template, and modify it as necessary, explaining your modifications. #include <stdio.h> int main() { const int N=100; int a[N]; //initialize for (int i=0; i<n; i++) a = i; // compute sum int local_sum, sum; #pragma omp parallel private (local_sum) shared (sum) { local_sum =0; //the array is distributde statically between threads #pragma omp for schedule (static, 1) for (int i=0; i<n; i++) { local_sum += a; } //each thread calculated its local_sum. All threads have to add to //the global sum. It is critical that this operation is atomic. #pragma omp critical sum += local_sum; } printf ("sum=%d should be %d\n", sum, N* (N-1)/2); } 2. Compile and run this program FIVE times, explain the differences in the outcomes. 3. Rewrite the code without using OpenMP constructs. Compare and explain the speed differences.