call MPI_Init(ierr) world = MPI_COMM_WORLD call MPI_Comm_size(world,numprocs,ierr) MPI_Comm_rank(world,myid,ierr) randServer = numprocs-1 if (myid .eq. 0) then print *,"Enter epsilon" read(*,*)epsilon end if call MPI_Comm_group( world, world_group, ierr ) !get group of everyone ranks(0) = randServer ! exclude the last call MPI_Group_excl( world_group, 1, ranks, worker_group, ierr ) ! get new group call MPI_Comm_create( world, worker_group, workers, ierr) ! get new comm if (myid .eq. randServer) then ! I am the rand server call MPI_Recv(request, 1, MPI_INT, MPI_ANY_SOURCE, NEED_NUMBERS, world, mesgStatus, ierr) do while (request > 0) do i=0,CHUNKSIZE randNums(i) = rand() end do call MPI_Send(randNums, CHUNKSIZE, MPI_INT, mesgStatus(MPI_SOURCE), RANDOM_NUMBERS, world, ierr) call MPI_Recv(request, 1, MPI_INT, MPI_ANY_SOURCE, NEED_NUMBERS, world, mesgStatus, ierr) end do else ! I am a worker process request = 1 done = 0 in = 0 out = 0 call MPI_Send( request, 1, MPI_INT, randServer, NEED_NUMBERS, world, ierr ) ! request a string of random numbers call MPI_Comm_rank( workers, workerNum, ierr ) do while (.not.done) request = 1 call MPI_Recv( randNums, CHUNKSIZE, MPI_INT, randServer, RANDOM_NUMBER, world, mesgStatus ) do i=1, CHUNKSIZE,2 x = (randNums(i)/max) * 2 - 1 y = (randNums(i+1)/max) * 2 - 1 if (x*x + y*y .lt 1.0) then in=in+1 else out=out+1 end if end do call MPI_Reduce(in, totalin, 1, MPI_INT, MPI_SUM, 0, workers) MPI_Reduce(out, totalout, 1, MPI_INT, MPI_SUM, 0, workers) ! count total of outs if (myid .eq. 0) then calculatedPi = (4.0*totalin)/(totalin + totalout) error = abs( calculatedPi-3.141592653589793238462643) if ((error .lt. epsilon) .or. (totalin+totalout).gt.1000000000)then done=.true. request = 0 else done=.false. request = 1 end if print *,calculatedPi call MPI_Send( request, 1, MPI_INT, randServer, NEED_NUMBERS, world, ierr ) call MPI_Bcast( done, 1, MPI_INT, 0, workers ) else call MPI_Bcast( done, 1, MPI_INT, 0, workers ) if (.not.done) then call MPI_Send( request, 1, MPI_INT, randServer, NEED_NUMBERS, world ) end if end if if (myid .eq. 0)print *, "npoints:", totalin+totalout," in:" totalin, , "out:",totalout call MPI_Finalize()