C C Demonstrates passing a stream to another process C Process 0 initializes a random number stream and prints a few random C numbers. It then passes this stream to process 1, which recieves it C and prints a few random numbers from this stream. C C Uncomment the following line to get the interface with pointer checking C #define CHECK_POINTERS program messagef_mpi implicit none #include #include "sprng_f.h" integer seed,i,myid, nprocs, len, junk integer streamnum, nstreams, ierror SPRNG_POINTER stream real*8 rn integer status(MPI_STATUS_SIZE) integer packed(MAX_PACKED_LENGTH) call MPI_INIT(ierror) call MPI_COMM_SIZE(MPI_COMM_WORLD, nprocs, ierror) call MPI_COMM_RANK(MPI_COMM_WORLD, myid, ierror) if (nprocs .lt. 2) then print *, 'ERROR: At least 2 processes required' call MPI_FINALIZE(ierror) call exit(1) endif if (myid .eq. 0) then streamnum = 0 ! process 0 sends stream to process 1 nstreams = 1 seed = 985456376 C stream = init_sprng(streamnum,nstreams,seed,SPRNG_DEFAULT) write(6,"('Process',i2,': Print information about stream:')") & myid junk = print_sprng(stream) write(6,"('Process',i2,': Print 2 random numbers in [0,1):')") & myid do 100 i = 1, 2 rn = sprng(stream) write(6, "('Process', i2, ': ', f8.6)") myid, rn 100 continue len = pack_sprng(stream, packed) ! inform process 1 how many bytes process 0 will send. call MPI_SEND(len,1,MPI_INTEGER,1,0,MPI_COMM_WORLD,ierror) call MPI_SEND(packed,len,MPI_INTEGER,1,0, & MPI_COMM_WORLD,ierror) nstreams = free_sprng(stream) print*, 'Process 0 sends stream to process 1' write(6,"(i2,' generators now exist on process 0')")nstreams elseif (myid .eq. 1) then call MPI_RECV(len,1,MPI_INTEGER,0,MPI_ANY_TAG,MPI_COMM_WORLD, & status, ierror) call MPI_RECV(packed,len,MPI_INTEGER,0,MPI_ANY_TAG, & MPI_COMM_WORLD,status, ierror) stream = unpack_sprng(packed) print *, 'Process 1 has received the packed stream' write(6, 250) myid 250 format('Process',i2,': Print information about stream:') junk = print_sprng(stream) print *, 'Process 1 prints 2 numbers from received stream:' do 200 i = 1, 2 rn = sprng(stream) write(6, "('Process', i2, ': ', f8.6)") myid, rn 200 continue junk = free_sprng(stream) endif call MPI_FINALIZE(ierror) end