In this part, you will add all elements in an array in parallel. 1. Write your code using the following template, and mo
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
In this part, you will add all elements in an array in parallel. 1. Write your code using the following template, and mo
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.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!