1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
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 <mpif.h>
#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)
C---
integer gtype
C---
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
C--- node 0 is reading in a generator type
if (myid .eq. 0) then
#include "genf_types_menu.h"
print *,'Type in a generator type (integers: 0,1,2,3,4,5): '
read *, gtype
endif
call MPI_BCAST(gtype,1, MPI_INTEGER,0,MPI_COMM_WORLD,ierror)
C---
if (myid .eq. 0) then
streamnum = 0 ! process 0 sends stream to process 1
nstreams = 1
seed = 985456376
C
stream = init_sprng(gtype,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
|