program flip !!! 10-June-2005 !Modified From: Richard Gerber NERSC 04/25/01 c........Flip a coin several times, and calculate heads number c........needs the number of tries in "flip.input" include 'mpif.h' c........initialize MPI call mpi_init(ierr) call mpi_comm_rank(mpi_comm_world,iam,ierr) call mpi_comm_size(mpi_comm_world,nproc,ierr) c........set a value for ntimes iheads = 0 if (iam.eq.0) ntimes = 25000 c...... Get User Input if (iam.eq.0) then open(unit=12,file='flip.input',status='old') read(12,*) ntimes print*,' Flipping coin the default: ',ntimes, 1 ' times on each task.' close(unit=12) endif c.......Broadcast the user's input to all tasks icount = 1 iroot = 0 mpi_type = MPI_INTEGER CALL MPI_BCAST(ntimes, icount, mpi_type, iroot, 1 MPI_COMM_WORLD, ierror) c.......Use random numbers to "flip" the coin iseed = 18*iam ranno = rand(iseed) DO i=1,ntimes iseed = (iseed * 100 )* ranno ranno = rand(iseed) IF(ranno.gt.0.5) iheads=iheads+1 END DO c.......Report the results PRINT *, 'Processor ',iam,' got ',iheads,' heads.' c.......Add up all the results from all the tasks and collect on task 0 CALL MPI_REDUCE(iheads,itotHeads,1,mpi_type,MPI_SUM,iroot, 1 MPI_COMM_WORLD,ierror) c.......Calculate summary info on task 0 and print out IF(iam.eq.0) THEN pct = 100.0*FLOAT(itotHeads)/FLOAT(ntimes*nproc) PRINT *, 'Heads came up ',pct,' percent of the time.' END IF c......close out MPI call mpi_finalize(ierr) stop end