2. Summing up to PI () – 35 % This programming problem involves multithreading (
ID: 3847356 • Letter: 2
Question
2. Summing up to PI () – 35 % This programming problem involves multithreading (Chapter 4) and uses synchronization mechanisms from Chapter 5. Write a multithreaded application called pie in C or C++ for Linux with the Pthreads library that computes an approximation of the number using an 'infinite series' with N+1 terms. The series sum is partitioned in T non-overlapping partial sums, each computed by a separate child thread. This program exemplifies data parallelism. Numbers T and N are passed to the pie program as command line parameters. The main(argc, argv[]) call gets these parameters in argv[1] and argv[2] as strings. Use the Nilakantha approximation formula for (http://en.wikipedia.org/wiki/Pi): = 3 + 4/(2*3*4) – 4/(4*5*6) + 4/(6*7*8) - 4/(8*9*10) + …. + k*4/((2*i)* (2*i+1)*( 2*i+2))+... where k = 1 if i is odd and k = -1 if i is even and i goes from 1 to N. Name your source file pie.c. The program takes two command line parameters. The first parameter, N is the upper limit of the number sequence to sum. The second parameter T is the number of child threads that compute the partial sums. N should be always greater than T, N>T. Otherwise the program should display an error message and do exit(1). Run it like this from a shell: ./pie N T For instance, if you run command: ./pie 100 4 the parent thread in main() will create 4 child threads, each getting an index from 0 to 3. Thread 0 computes the partial sum for i going from 0 to 24 Thread 1 computes the partial sum for i going from 25 to 49 Thread 2 computes the partial sum for i going from 50 to 74 Thread 3 computes the partial sum for i going from 75 to 99 Important: In general, a child thread with index j (0 to T-1) will compute a partial sum for i going from N*j/T+1 to (N/T)*(j+1)-1 and will store it to a local variable. Note that * and / are used on ints. The thread with index T-1 (with the highest index) will compute the partial sum for i going from N/T*(T-1) to N-1. A global variable gpie MUST be declared as: double gpie = 0; to store the current approximation of , After completing the computation of its partial sum, each thread must update the shared global variable gpie in a critical section: gpie = gpie + myPartialSum; myPartialSum is a local variable in the current thread. The child thread that updates gpie must protect the critical section with a pthread mutex. (Think about why is it better to update the shared global variable only once per child thread – with the partial sum – instead of adding directly each series term to the shared global variable gpie.) After the completion of the critical section, a child thread will end its execution. The main thread will wait for all child threads to finish (using pthread join), after which it will print the result, as follows: printf(“pi computed with %d terms in %d threads is %d ”, N, T, gpie);); where gpie is the sum of all partial sums.
Explanation / Answer
Hi,
I have heavily commented the code for deeper understanding.
Note that in order to compile the code you have to include option -lpthread like
gcc pie.c -o pie -lpthread
and run using ./pie
Feel free to comment if you have any queries. Give a thumbs up if the answer helped you.
//Code starts here
//Code ends here
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.