Computing p with Message Passing
Notes:
- thread identification first
- explicit work sharing
- all variables are private
- explicit data exchange (reduce)
- all code is parallel
- number of processors is specified outside of code
double pi, l, ls = 0.0, w = 1.0/N; int i, mid, nth;
MPI_comm_rank(MPI_COMM_WORLD,&mid);
MPI_comm_size(MPI_COMM_WORLD,&nth);
for(i=mid; i<N; i += nth) {
MPI_reduce(&ls,&pi,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);
if(mid == 0) printf(“pi is %f\n”,pi*w);