Answered step by step
Verified Expert Solution
Question
1 Approved Answer
bach>./thr_atomic 265536 thr 0: 352703.926537 thr 1: 486164.553017 sum of quadruple roots: 838868.479554 The program thr_reduce. c is similar to thr atomic. c except that
bach>./thr_atomic 265536 thr 0: 352703.926537 thr 1: 486164.553017 sum of quadruple roots: 838868.479554 The program thr_reduce. c is similar to thr atomic. c except that you need to use the parallel reduction approach to combine the partial sums. That is, your program uses a shared global array and each computational thread stores its partial sum, but not prints, in an array element indexed on its thread ID. Then, half of these threads call pthread_join() to wait for their corresponding partner threads completion, and then each of these threads can add two numbers in the array together. This reduction procedure will be performed log2m times and each time the number of the active threads will be reduced half. Finally, there will be only one active thread and this thread should print the whole array. For example, assume that there are 8 computational threads. In the first reduction step, in order to add two elements in the array, thread 1 should wait for thread 0 done, thread 3 has to wait for thread 2 done, threads 5 needs to wait for thread 4 done, and thread 7 should wait for thread 6 done. In the second reduction step, thread 3 waits for thread 1 finished, and thread 7 waits for thread 5 finished. In the third step, thread 7 waits for thread 3 done and then prints the the whole array. Hint: to find its partner thread ID during the i th reduction step, a thread can use its ID XORed with 2i1. Furthermore, you have to use bit-shift operations instead of calling log() or pow () functions to find the partner thread ID. Note that the main thread just calls pthread_exit () after creating m threads. It does not need to wait for these threads finished. Calling pthread_exit () in the main thread will allow other threads to continue execution
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