The full program is runsprng.cpp. Lines illustrating use of the sprng library and MPI Send and Recv functions are shown below.
#include "runsprng.h"
/* runsprng.h */
#define SIMPLE_SPRNG //simple interface to sprng
#define USE_MPI //use MPI in sprng
#include "/home/psiders/include/sprng.h" //SPRNG header file must be included
#define SEED 985456376 //seed recommended in sprng 1.0
int gen = 0 ; //0 through 4 selects generator
init_sprng(gen,SEED,SPRNG_DEFAULT); // initialize stream
double rannum ;
....
rannum = sprng() ;
#define REPORTRanVal 3
#define REPORTRanAvg 5
int main () {
MPI::COMM_WORLD.Send(RanValProc, numrans, MPI::DOUBLE, server, REPORTRanVal) ;
MPI::COMM_WORLD.Send(&RanAvgProc, 1, MPI::DOUBLE, server, REPORTRanAvg ) ;
RanValProc is a double-precision vector of length numrans.
RanAvgProc is a double-precision scalar.
"server" (=0, int) specifies the destination of the Send.
REPORTRanVal and REPORTRanAVg are integer "tag"s that server will match in the Recv function.
int nrecvd = 0 ; //number of workers from which data have been received
while (nrecvd < numworkers) {
MPI::COMM_WORLD.Recv(RanValProc, numrans, MPI::DOUBLE, MPI_ANY_SOURCE,
REPORTRanVal, status) ;
sender = status.Get_source() ;
...
nrecvd++ ;
}
/*These receives loop through the workers in numerical order*/
for (int worker=1; worker<numprocs; worker++) {
MPI::COMM_WORLD.Recv(&RanAvgProc, 1, MPI::DOUBLE, worker,
REPORTRanAvg, status) ;
RanAverage[worker] = RanAvgProc ;
}
The first loop receives data in the order it is sent.Output from the command [psiders@beowulf www]$ mpirun -np 6 runsprng
Random number generator 0 (lfg)
id: 1 2 3 4 5
0 0.44148 0.81419 0.67244 0.06508 0.04786
20000 0.99797 0.78137 0.27664 0.50201 0.41230
40000 0.39637 0.38296 0.86886 0.23288 0.71488
60000 0.40581 0.47516 0.25676 0.11400 0.59770
80000 0.00248 0.64053 0.95231 0.38301 0.10299
100000 0.44026 0.31129 0.40963 0.80981 0.41615
average 0.49995 0.49842 0.49934 0.49823 0.49905
End of random number test output.
0.047488 seconds elapsed on 5 workers for generator 0 (lfg).
........
Complete output is available in runsprng.log.