Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this project, you are asked to write two independent programs, thr atomic.c and thr reduce.c, each of which uses computational threads to concurrently calculate

image text in transcribed

In this project, you are asked to write two independent programs, thr atomic.c and thr reduce.c, each of which uses computational threads to concurrently calculate the sum of quadruple rools from I to where and are powers of 2 and are specified in the command line For the program thr atomic.c. it gets the values and from the command line arguments and converts them to two integers, respectively. Next, it creates a threads using pthread create and each thread computes the Sum ofnm quadruple roots. Namely the first thread the thread O) computes the sum of quadruple roots from I tom . the second thread (ie thread computes the sum of the quadruple roots from + 1 to 2/m, etc When a thread finishes its computation it should print ils partial sum and atomically add it to a shared global variable. Note that your program needs to use pthread barrier wait to let the main thread know that all of the m computational threads have done the atomic additions and hence it can print the result. Below is an example of running your thread program: bach> /thr_atomic 2 65536 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 ap- proach to combine the partial sums. That is your program uses a shared global array and each computational thread just stores, but not prints, its partial sum 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 log(m) 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 the adding two partial sums in the array), thread o waits for thread I done, thread 2 waits for thread 3 done, threads 4 waits for thread 5 done, and thread 6 waits for thread 7 done. In the second reduction step, thread o waits for thread 2 finished, and thread 4 waits for thread 6 finished. In the third step. thread o waits for thread 4 done and then prints the whole array. Hint to find its partner thread ID during the ith reduction step, a thread can use its ID XORed with ! Note that the main thread has to call pthread.exit after creating m threads. It does not need to wait for these threads Calling pthread.exit() in the main thread will allow other threads to continue execution In this project, you are asked to write two independent programs, thr atomic.c and thr reduce.c, each of which uses computational threads to concurrently calculate the sum of quadruple rools from I to where and are powers of 2 and are specified in the command line For the program thr atomic.c. it gets the values and from the command line arguments and converts them to two integers, respectively. Next, it creates a threads using pthread create and each thread computes the Sum ofnm quadruple roots. Namely the first thread the thread O) computes the sum of quadruple roots from I tom . the second thread (ie thread computes the sum of the quadruple roots from + 1 to 2/m, etc When a thread finishes its computation it should print ils partial sum and atomically add it to a shared global variable. Note that your program needs to use pthread barrier wait to let the main thread know that all of the m computational threads have done the atomic additions and hence it can print the result. Below is an example of running your thread program: bach> /thr_atomic 2 65536 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 ap- proach to combine the partial sums. That is your program uses a shared global array and each computational thread just stores, but not prints, its partial sum 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 log(m) 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 the adding two partial sums in the array), thread o waits for thread I done, thread 2 waits for thread 3 done, threads 4 waits for thread 5 done, and thread 6 waits for thread 7 done. In the second reduction step, thread o waits for thread 2 finished, and thread 4 waits for thread 6 finished. In the third step. thread o waits for thread 4 done and then prints the whole array. Hint to find its partner thread ID during the ith reduction step, a thread can use its ID XORed with ! Note that the main thread has to call pthread.exit after creating m threads. It does not need to wait for these threads 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro SQL Server Administration

Authors: Peter Carter

1st Edition

1484207106, 9781484207109

More Books

Students also viewed these Databases questions