main(int argc, char *argv[]) { int streamnum, commNum, nstreams, *stream, *commonStream; double rn; int i, myid, nprocs; /************************** MPI calls ***********************************/ MPI_Init(&argc, &argv); /* Initialize MPI */ MPI_Comm_rank(MPI_COMM_WORLD, &myid); /* find process id */ MPI_Comm_size(MPI_COMM_WORLD, &nprocs); /* find number of processes */ /****************** Initialization values *******************************/ streamnum = myid; /*This stream is different on each process*/ commNum = nprocs; /* This stream is common to all processes */ nstreams = nprocs + 1; /* extra stream is common to all processes*/ /*********************** Initialize streams *****************************/ /* This stream is different on each process */ stream = init_sprng(streamnum,nstreams,SEED,SPRNG_DEFAULT); printf("Process %d: Print information about new stream\n", myid); print_sprng(stream); /* This stream is identical on each process */ commonStream = init_sprng(commNum,nstreams,SEED,SPRNG_DEFAULT); printf("Process %d: This stream is identical on all processes\n", myid); print_sprng(commonStream); /*********************** print random numbers ***************************/ for (i=0;i<2;i++) /* random numbers from distinct stream */ { rn = sprng(stream); /* generate double precision random number*/ printf("Process %d, random number (distinct stream) %d: %f\n", myid, i+1, rn); } for (i=0;i<2;i++) /* random number from common stream */ { rn = sprng(commonStream); /*generate double precision random number */ printf("Process %d, random number (shared stream) %d: %f\n", myid, i+1, rn); } /*************************** free memory ********************************/ free_sprng(stream); /* free memory used to store stream state */ free_sprng(commonStream); MPI_Finalize(); /* terminate MPI */ }
main() { int streamnum, nstreams, seed, *stream, i; double rn; /************************** Initialization *******************************/ streamnum = 0; nstreams = 1; seed = make_sprng_seed(); /* make new seed each time program is run */ stream = init_sprng(streamnum,nstreams,seed,SPRNG_DEFAULT); /*initialize stream*/ printf(" Printing information about new stream\n"); print_sprng(stream); /************************ print random numbers ***************************/ printf(" Printing 3 random numbers in [0,1):\n"); for (i=0;i<3;i++) { rn = sprng(stream); /* generate double precision random number */ printf("%f\n", rn); } free_sprng(stream); /* free memory used to store stream state */ }
main() { int streamnum, nstreams, *stream, **new; double rn; int i, irn, nspawned; 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); /****************** Initialization values *******************************/ streamnum = 0; nstreams = 1; stream = init_sprng(gtype,streamnum,nstreams,SEED,SPRNG_DEFAULT); /* initialize stream */ printf(" Print information about stream:\n"); print_sprng(stream); /*********************** print random numbers ***************************/ printf(" Printing 2 random numbers in [0,1):\n"); for (i=0;i<2;i++) { rn = sprng(stream); /* generate double precision random number*/ printf("%f\n", rn); } /**************************** spawn streams *****************************/ printf(" Spawned two streams\n"); nspawned = 2; nspawned = spawn_sprng(stream,2,&new); /* spawn 2 streams */ if(nspawned != 2) { fprintf(stderr,"Error: only %d streams spawned\n", nspawned); exit(1); } printf(" Information on first spawned stream:\n"); print_sprng(new[0]); printf(" Information on second spawned stream:\n"); print_sprng(new[1]); printf(" Printing 2 random numbers from second spawned stream:\n"); for (i=0;i<2;i++) { rn = sprng(new[1]); /* generate a random number */ printf("%f\n", rn); } /*************************** free memory ********************************/ free_sprng(stream); /* free memory used to store stream state */ free_sprng(new[0]); /* free memory used to store stream state */ free_sprng(new[1]); /* free memory used to store stream state */ free(new); }
main(int argc, char *argv[]) { int streamnum, nstreams, seed, *stream, i, myid, nprocs; double rn; int gtype; /*--- */ /*************************** MPI calls ***********************************/ MPI_Init(&argc, &argv); /* Initialize MPI */ MPI_Comm_rank(MPI_COMM_WORLD, &myid); /* find process id */ MPI_Comm_size(MPI_COMM_WORLD, &nprocs); /* find number of processes */ /************************** Initialization *******************************/ streamnum = myid; nstreams = nprocs; /* one stream per processor */ seed = make_sprng_seed(); /* make new seed each time program is run */ /*--- node 0 is reading in a generator type */ 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 ); /* Seed should be the same on all processes */ printf("Process %d: seed = %16d\n", myid, seed); stream = init_sprng(gtype,streamnum,nstreams,seed,SPRNG_DEFAULT); /*initialize stream*/ printf("\n\nProcess %d: Print information about stream:\n",myid); print_sprng(stream); /************************ print random numbers ***************************/ for (i=0;i<3;i++) { rn = sprng(stream); /* generate double precision random number */ printf("process %d, random number %d: %f\n", myid, i+1, rn); } free_sprng(stream); /* free memory used to store stream state */ MPI_Finalize(); /* Terminate MPI */ }
/*--- change from initialize() to minitialize() ---*/ void minitialize(int rng_type, int seed, int param, int use_blocks) { int i, j, temp; nsites = lattice_size*lattice_size; prob = 1 - exp(-2.0*J); for(i=0; i<10; i++) for(j=0; j<10; j++) Energy[i][j] = Cv[i][j] = 0.0; spin = malloc(nsites*sizeof(int)); stack = malloc(nsites*sizeof(int)); if(!spin || !stack) { printf("\n\tMemory allocation failure, program exits!\n"); exit(-1); } for(i=0; i<nsites; i++) /* randomly initialize system */ spin[i]=(RNG()>prob)?1:-1; /* here assume that expo is integer exponent of 2 */ temp = mask=lattice_size-1; exponent = 0; /* expo = log_2(lattice_size) */ while(temp) { exponent++; temp >>= 1; } /* initialize generator */ genptr = (int **) malloc(nsites*sizeof(int *)); genptr[0] = init_sprng(rng_type,0,nsites,seed,param); /*--- add rng_type ---*/ print_sprng(genptr[0]); for(i=1; i<nsites; i++) #ifdef PARALLEL genptr[i]=init_sprng(rng_type,i,nsites,seed,param); /*--- add rng_type ---*/ #else genptr[i]=genptr[0]; #endif }
main(int argc, char *argv[]) { int streamnum, nstreams, *stream; double rn; int i, myid, nprocs; /*************************** MPI calls ***********************************/ MPI_Init(&argc, &argv); /* Initialize MPI */ MPI_Comm_rank(MPI_COMM_WORLD, &myid); /* find process id */ MPI_Comm_size(MPI_COMM_WORLD, &nprocs); /* find number of processes */ /************************** Initialization *******************************/ streamnum = myid; nstreams = nprocs; /* one stream per processor */ stream = init_sprng(streamnum,nstreams,SEED,SPRNG_DEFAULT); /* initialize stream */ printf("Process %d, print information about stream:\n", myid); print_sprng(stream); /*********************** print random numbers ****************************/ for (i=0;i<3;i++) { rn = sprng(stream); /* generate double precision random number */ printf("Process %d, random number %d: %.14f\n", myid, i+1, rn); } /*************************** free memory *********************************/ free_sprng(stream); /* free memory used to store stream state */ MPI_Finalize(); /* Terminate MPI */ }
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 *******************************/ /*--- node 0 is reading in a generator type */ 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 ); init_sprng(gtype,SEED,SPRNG_DEFAULT); /* initialize stream */ printf("\n\nProcess %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 */ }
main(int argc, char *argv[]) { double rn; int i, myid, len, nprocs; MPI_Status status; char *packed; int gtype; /*--- */ /************************** MPI calls ************************************/ MPI_Init(&argc, &argv); /* Initialize MPI */ MPI_Comm_rank(MPI_COMM_WORLD, &myid); /* find process id */ MPI_Comm_size(MPI_COMM_WORLD, &nprocs); /* find number of processes */ if(nprocs < 2) { fprintf(stderr,"ERROR: At least 2 processes required\n"); MPI_Finalize(); exit(1); } /*--- node 0 is reading in a generator type */ 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 */ if (myid==0) /*********** process 0 sends stream to process 1 ***********/ { init_sprng(gtype,SEED,SPRNG_DEFAULT); /*initialize stream */ printf("\n\nProcess %d: Print information about stream:\n",myid); print_sprng(); printf("Process %d: Print 2 random numbers in [0,1):\n", myid); for (i=0;i<2;i++) { rn = sprng(); /* generate double precision random number */ printf("Process %d: %f\n", myid, rn); } len = pack_sprng(&packed); /* pack stream into an array */ /* inform process 1 how many bytes process 0 will send. */ MPI_Send(&len, 1, MPI_INT, 1, 0, MPI_COMM_WORLD); MPI_Send(packed, len, MPI_BYTE, 1, 0, MPI_COMM_WORLD); /* send stream */ free(packed); /* free storage for array */ printf(" Process 0 sends stream to process 1\n"); } else if(myid == 1) /****** process 1 receives stream from process 0 *****/ { init_sprng(gtype,SEED,SPRNG_DEFAULT); /*initialize stream */ MPI_Recv(&len, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status); /* receive buffer size required */ if ((packed = (char *) malloc(len)) == NULL) /* allocate array */ { 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); /* receive packed stream */ unpack_sprng(packed); /* unpack stream */ printf(" Process 1 has received the packed stream\n"); printf("Process %d: Print information about stream:\n",myid); print_sprng(); free(packed); /* free array of packed stream */ printf(" Process 1 prints 2 numbers from received stream:\n"); for (i=0;i<2;i++) { rn = sprng(); /* generate double precision random number */ printf("Process %d: %f\n", myid, rn); } } MPI_Finalize(); /* terminate MPI */ }