Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In the parallel program in Figure 12.8 for computing the dot product, the sum of the partial products is computed by one processor after all
In the parallel program in Figure 12.8 for computing the dot product, the sum of the partial products is computed by one processor after all threads have reached the barrier. Modify the program so that each thread adds its partial sum incrementally to a global sum for the dot product. To prevent two or more threads from trying to update the global sum at the same time synchronization is required. One method is to use a shared counter variable whose value is the identifier of the thread that is granted exclusive access to the variable for the global sum. After the thread with exclusive access has updated the global sum, it can simply increment this counter to grant exclusive access to another thread Reference of fig.12.8 #include #include "thread.h" Routines for input/output./ Routines for thread creation/synchronization. #define N 100 #define P Number of elements in each vector./ Number of processors for parallel execution. double aN], bIN: double partial_sumsP Array of results computed by threads. Barrier bar Vectors for computing the dot product. * /* Shared variable to support barrier synchronization. void ParallelFunction (void) int my id, i, start, end; double s; my_id- get my_thread id O: Get unique identifier for this thread. start (N/P) my id; end (N/P)*(my_id +1)-;N is assumed to be evenly divisible by P.I s-0.0; for i starti init barrier (&bar): for (iz1; i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started