The value of PI can be calculated in a number of ways. In this example, we are estimating PI using following way:
- Each task executes locally its portion of the loop a number of times.
iterations = 10000
circle_count = 0
do j = 1,iterations
generate 2 random numbers between 0 and 1
xcoordinate = random1
ycoordinate = random2
if (xcoordinate, ycoordinate) inside circle
then circle_count = circle_count + 1
PI = 4.0*circle_count/iterations
- One task acts as master and collects the results through the BSP communication interface.
1) Each process computes the value of Pi locally, and 2) sends it to master task using send() function. Then, 3) the master task can recieve the messages using sync() function. Finally, we can calculate the average value of sum of PI values from each peers as below: