int main() { int streamnum, nstreams, seed, i; Sprng *stream; double rn; int j; int gtype; /*--- */ /*--- reading in a generator type */ #include "gen_types_menu.h" printf("Type in a generator type (integers: 0,1,2,3,4,5): "); scanf("%d", >ype); /* for(j = 0; j < 6; j++){ */ /************************** Initialization *******************************/ streamnum = 0; nstreams = 1; seed = make_sprng_seed(); /* make new seed each time program is run */ stream = SelectType(gtype); stream->init_sprng(streamnum,nstreams,seed,SPRNG_DEFAULT); /*initialize stream*/ printf(" Printing information about new stream\n"); stream->print_sprng(); /************************ print random numbers ***************************/ printf(" Printing 3 random numbers in [0,1):\n"); for (i=0;i<3;i++) { rn = stream->sprng(); /* generate double precision random number */ printf("%f\n", rn); } stream->free_sprng(); /* free memory used to store stream state */ return 0; }
int main() { int streamnum, nstreams; double rn; int irn; int i, j; int gtype; Sprng * ptr; streamnum = 0; nstreams = 1; #include "gen_types_menu.h" /* header file generated by Make */ printf("Type in a generator type (integers: 0,1,2,3,4,5): "); scanf("%d", >ype); ptr = SelectType(gtype); ptr->init_sprng(streamnum, nstreams, SEED, SPRNG_DEFAULT); printf("\n --------------------------------------------------------\n"); printf(" Print information about new stream:\n"); ptr->print_sprng(); printf(" Printing 3 random numbers in [0,1):\n"); for (i=0;i<3;i++){ rn = ptr->sprng(); /* generate a double precision random number */ printf("%f\n",rn); } printf(" Printing 3 random integers in [0,2^31):\n"); for (i=0;i<3;i++) { irn = ptr->isprng(); /* generate an integer random number */ printf("%16d\n",irn); } ptr->free_sprng(); /* free memory used to store stream state */ return 0; }
int main(int argc, char *argv[]) { int streamnum, nstreams; Sprng *stream; double rn; int i, myid, nprocs, len; MPI_Status status; char *packed; int gtype; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &myid); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); if(nprocs < 2) { fprintf(stderr,"ERROR: At least 2 processes required\n"); MPI_Finalize(); exit(1); } 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 ); if (myid==0) { streamnum = 0; nstreams = 1; stream = SelectType(gtype); stream->init_sprng(streamnum,nstreams,SEED,SPRNG_DEFAULT); printf("\n\nProcess %d: Print information about stream:\n",myid); stream->print_sprng(); printf("Process %d: Print 2 random numbers in [0,1):\n", myid); for (i=0;i<2;i++) { rn = stream->sprng(); printf("Process %d: %f\n", myid, rn); } len = stream->pack_sprng(&packed); MPI_Send(&len, 1, MPI_INT, 1, 0, MPI_COMM_WORLD); MPI_Send(packed, len, MPI_BYTE, 1, 0, MPI_COMM_WORLD); free(packed); nstreams = stream->free_sprng(); printf(" Process 0 sends stream to process 1\n"); printf(" %d generators now exist on process 0\n", nstreams); } else if(myid == 1) { MPI_Recv(&len, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status); if ((packed = (char *) malloc(len)) == NULL) { fprintf(stderr,"ERROR: process %d: Cannot allocate memory\n", myid); MPI_Finalize(); exit(1); } MPI_Recv(packed, len, MPI_BYTE, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status); stream = SelectType(gtype); stream->unpack_sprng(packed); printf(" Process 1 has received the packed stream\n"); printf("Process %d: Print information about stream:\n",myid); stream->print_sprng(); free(packed); printf(" Process 1 prints 2 numbers from received stream:\n"); for (i=0;i<2;i++) { rn = stream->sprng(); printf("Process %d: %f\n", myid, rn); } stream->free_sprng(); } MPI_Finalize(); return 0; }