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
|
/***************************************************************************/
/* Demonstrates sprng use in C++ with MPI */
/* A distinct stream is created on each process, then prints a few */
/* random numbers. */
/***************************************************************************/
#include <stdio.h>
#include <mpi.h> // MPI header file
#define SIMPLE_SPRNG // simple interface
#define USE_MPI // use MPI to find number of processes
#include "sprng.h" // SPRNG header file
#define SEED 985456376
main(int argc, char *argv[])
{
double rn;
int i, myid;
int gtype; /*--- */
/*************************** MPI calls ***********************************/
MPI_Init(&argc, &argv); /* Initialize MPI */
MPI_Comm_rank(MPI_COMM_WORLD, &myid); /* find process id */
/************************** Initialization *******************************/
if(myid == 0)
{
#include "gen_types_menu.h"
printf("Type in a generator type (integers: 0,1,2,3,4,5): ");
scanf("%d", >ype);
}
MPI_Bcast(>ype,1,MPI_INT,0,MPI_COMM_WORLD ); /*--- broadcast gen type */
init_sprng(gtype,SEED,SPRNG_DEFAULT); /* initialize stream */
printf("Process %d, print information about stream:\n", myid);
print_sprng();
/************************ print random numbers ***************************/
for (i=0;i<3;i++)
{
rn = sprng(); /* generate double precision random number */
printf("Process %d, random number %d: %.14f\n", myid, i+1, rn);
}
MPI_Finalize(); /* Terminate MPI */
}
|