In this part, you will add all elements in an array in parallel. 1. Write your code using the following template, and mo

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
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

Post by answerhappygod »

In This Part You Will Add All Elements In An Array In Parallel 1 Write Your Code Using The Following Template And Mo 1
In This Part You Will Add All Elements In An Array In Parallel 1 Write Your Code Using The Following Template And Mo 1 (88.04 KiB) Viewed 40 times
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!
Post Reply