Question: 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

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

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!