An approximation to the value of π can be calculated from the following expression
where the answer becomes more accurate with increasing N. As each term is independent, the summation over i can be parallelized nearly trivially.
Starting from the serial code pi.cpp (or pi.F90 for Fortran), make a version that performs the calculation parallel with two processes.
-
Divide the range over N in two, so that rank 0 does i=1, 2, ... , N/2 and rank 1 does i=N/2 + 1, N/2 + 2, ... , N.
-
Both tasks calculate their own partial sums
-
Once finished with the calculation, rank 1 sends its partial sum to rank 0, which then calculates the final result and prints it out.
-
Compare the result to the that of the serial calculation, do you get exactly the same result? If not, can you explain why?