Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this project, you are asked to write two independent programs, thread_atomic.c and thread_reduce.c, each of which uses m computational threads to concurrently calculate the

In this project, you are asked to write two independent programs, thread_atomic.c and thread_reduce.c, each of which uses m computational threads to concurrently calculate the sum of Gregory-Leibniz terms from 1 to n, where m and n are powers of 2 and are specified in the command line. For the program thread_atomic.c, it gets the values m and n from the command line arguments and converts them to two integers, respectively. Next, it creates m threads using pthread_create() and each thread computes the sum of n/ Gregory-Leibniz terms. Calculating - Maths Careers using the Gregory-Leibniz Series: =4(11/3+1/51/7+1/9) If you continued this pattern forever you would be able to calculate exactly. If however you start to add up the first few terms, you will begin to get an approximation for . The problem with the series above is that you need to add up a lot of terms in order to get an accurate approximation of . You need to add up more than 300 terms in order to produce accurate to two decimal places! For the program thread_atomic.c, it gets the values m and n from the command line arguments and converts them to two integers, respectively. Next, it creates m threads using pthread_create() and each thread computes the sum of n/ terms in the Gregory-Leibniz Series. Namely, the first thread (i.e. thread 0) computes the sum of term 1 to n/, the second thread (i.e. thread 1) computes the sum of the terms from n/ + 1 to 2n/, etc. The n-th term of the Gregory-Leibniz series (1)^+1 (1/(21)) When a thread finishes its computation, it should print its 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: yazheng@spirit:~/Spr23CIS545/p1$ ./thread_atomic.exe 16 1024 thr 0: 3.125969 thr 1: 0.007812 thr 2: 0.002604 thr 3: 0.001302 thr 7: 0.000279 thr 9: 0.000174 thr 5: 0.000521 thr 6: 0.000372 thr 10: 0.000142 thr 4: 0.000781 thr 8: 0.000217 thr 13: 0.000086 thr 12: 0.000100 thr 14: 0.000074 thr 11: 0.000118 thr 15: 0.000065 Pi value is: 3.140616 The program thread_ reduce.c is similar to thread_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 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, in order to add two elements in the array, thread 4 should wait for thread 0 done, thread 5 has to wait for thread 1 done, threads 6 needs to wait for thread 2 done, and thread 7 should wait for thread 3 done. In the second reduction step, thread 6 waits for thread 4 finished, and thread 7 waits for thread 5 finished. In the third step, thread 7 waits for thread 6 done and then prints the the whole array. Hint: to find its partner thread ID during the ith reduction step, a thread can use its ID XORed with 2^r-i, where r = log(m). Note that the main thread just calls 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

More Books

Students also viewed these Databases questions