/** same as l_random, but for integer */ inline int i_random(int maxint) { /* delivers an integer between 0 and maxint-1 */ int temp; temp = (int)( ( (double) maxint * l_random() )* AM ); return temp; }
inline double d_random(void) { /* delivers a uniform double between 0 and 1 */ double temp; iy = l_random(); if ((temp = AM * iy) > RNMX) temp = RNMX; return temp; }
TEST(base_random, get_range) { bk::random l_random(2); bk::uint l_test_count = 20; // { //bk::uint l_max = 0; //bk::uint l_test_count = 2000000; //for(bk::uint i = 0; i < l_test_count; ++i) { // bk::uint l_v = l_random.get(); // l_max = bk::max(l_max, l_v); //} //EXPECT_EQ(bk::random::max, l_max); } // { const bk::uint l_max0 = 10; bk::uint l_min1 = 10, l_max1 = 0; for(bk::uint i = 0; i < l_test_count; ++i) { bk::uint l_v = l_random.get(l_max0); l_min1 = bk::min(l_min1, l_v); l_max1 = bk::max(l_max1, l_v); } EXPECT_EQ(0U, l_min1); EXPECT_EQ(l_max0, l_max1); } // { const bk::sint l_min0 = -5, l_max0 = 5; bk::sint l_min1 = 5, l_max1 = -5; for(bk::uint i = 0; i < l_test_count; ++i) { bk::sint l_v = l_random.get(l_min0, l_max0); l_min1 = bk::min(l_min1, l_v); l_max1 = bk::max(l_max1, l_v); } EXPECT_EQ(l_min0, l_min1); EXPECT_EQ(l_max0, l_max1); } // { bk::real l_rmin = -100, l_rmax = 100; bk::real l_r0 = l_random.get(l_rmax); EXPECT_LE(l_r0, l_rmax); } // { bk::real l_rmin = -100, l_rmax = 100; bk::real l_r1 = l_random.get(l_rmin, l_rmax); EXPECT_GE(l_r1, l_rmin); EXPECT_LE(l_r1, l_rmax); } }
// base/random TEST(base_random, set_seed) { bk::random l_random(12345); bk::uint l_test_count = 20; bk::uint_array l_res0, l_res1; for(bk::uint i = 0; i < l_test_count; ++i) { l_res0.push_back(l_random.get()); } l_random.seed = 12345; for(bk::uint i = 0; i < l_test_count; ++i) { l_res1.push_back(l_random.get()); } for(bk::uint i = 0; i < l_test_count; ++i) { EXPECT_EQ(l_res0[i], l_res1[i]); } }
/* test the random number generator */ void main(int argc, char **argv) { long seed = 7; long lo = 2; long hi = 15; long count = 50; int i; long temp; s_l_random( seed, lo, hi); printf("\n %d random numbs between %d and %d\n",count,lo,hi); for(i=0; i<count; i++){ if(i%5==0)printf("\n"); temp = l_random(); printf(" %8d ",temp); } printf("\n"); }
int main(int argc, char **argv) { int buffer_count; /* length of message to bounce */ int buff_size_bytes; /* length of message to bounce in bytes */ int iterations; /* Number of iterations to time */ long *buffer; /* buffer to bounce between processes */ long *incoming; /* buffer to bounce between processes */ double *timings,*tp; /* arrays of times for each iteration */ double wt_overhead(); /* MPI_Wtime correction factor in Seconds */ double t0, twtime; /* initial time and timer call time */ int my_ID; /* process id (or in MPI-speak, the rank) */ int numP; /* number of processes */ MPI_Status stat; /* MPI status parameter */ int iters, i; int cpy_count; #ifdef DEBUG long *result, *res; /* Hold original buffer for testing */ long diff; /* holds sum of differences for error testing */ double check_sum; /* check sum of random-filled buffer */ #endif /********************************************************************* ** Initialize the MPI environment *********************************************************************/ MPI_Init(&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD, &my_ID); MPI_Comm_size(MPI_COMM_WORLD, &numP); if(numP != 2){ if(my_ID == 0){ printf(" Ping Pong only runs with 2 process\n"); printf(" Wrong number of processes (%d) ... abort \n",numP); } MPI_Abort(MPI_COMM_WORLD, 1); } #ifdef DEBUG printf("\nMPI Ping Pong Test \n"); printf("\n after MPI_Init and MPI_Comm_rank\n"); #endif /********************************************************************* ** pass input parameters on to slave *********************************************************************/ if(my_ID == 0){ if (argc == 3){ buffer_count = atoi(*++argv); iterations = atoi(*++argv); } else{ printf("Usage: %s <message_size in longs> <iterations> \n",*argv); printf(" program will proceed with default values\n"); buffer_count = 50; iterations = 20; } #ifdef DEBUG printf("\n MPI ping/pong with %d longs",buffer_count); printf(" and %d iterations \n",iterations); #endif buff_size_bytes = buffer_count * sizeof(long); MPI_Send (&buffer_count, 1, MPI_INT, 1, MTag_1, MPI_COMM_WORLD); MPI_Send (&iterations, 1, MPI_INT, 1, MTag_2, MPI_COMM_WORLD); } else if (my_ID==1){ MPI_Recv (&buffer_count, 1, MPI_INT, 0, MTag_1, MPI_COMM_WORLD, &stat); MPI_Recv (&iterations, 1, MPI_INT, 0, MTag_2, MPI_COMM_WORLD, &stat); #ifdef DEBUG printf("\n ID %d ping/pong with %d longs",my_ID,buffer_count); printf(" and %d iterations \n",iterations); #endif } if (my_ID == 0){ /***************************************************************** ** Time the timer and allocate memory used by node 0 only . *****************************************************************/ twtime = wt_overhead(iterations); // twtime = 0.0; #ifdef DEBUG printf("\n clock correction factor is time is %f\n",twtime); #endif if ( (incoming= (long *)malloc(buffer_count* sizeof(long))) == False){ printf(" Could not allocate buffer of %d longs\n",buffer_count); exit (1); } if((tp=timings=(double *)malloc((unsigned)(iterations*sizeof(double))))==False){ printf(" Could not allocate space for times array \n"); exit (1); } #ifdef DEBUG printf("\nDEBUG is on - communications will be tested\n"); if ( (result = (long *)malloc(buff_size_bytes)) == False){ printf("Could not allocate result buff of %d longs\n",buffer_count); exit (1); } #endif } /****************************************************************** ** Allocate memory used on both nodes ******************************************************************/ if ( (buffer = (long *)malloc(buffer_count * sizeof(long))) == False){ printf(" Could not allocate buffer of %d longs\n",buffer_count); exit (1); } /********************************************************************* ** Fill buffer up with junk. *********************************************************************/ if(my_ID == 0){ s_l_random( SEED, low_value, hi_value ); for (i=0; i<buffer_count; i++) buffer[i] = l_random(); #ifdef DEBUG check_sum = 0.0; for (i=0; i<buffer_count; i++) { result[i] = buffer[i]; check_sum += (double)buffer[i]; } printf("\n %d ping-pong data checksum %f\n",my_ID,check_sum); #endif } /********************************************************************* ** Synchronize everyone and collect timeing data. *********************************************************************/ #ifdef DEBUG printf("\n ID %d do the barrier and start ping-poinging\n",my_ID); #endif MPI_Barrier (MPI_COMM_WORLD); if (my_ID == 0){ for( iters = iterations ; iters-- ; ) { t0 = MPI_Wtime(); MPI_Send (buffer, buffer_count, MPI_LONG, 1, MTag_3, MPI_COMM_WORLD); MPI_Recv (incoming, buffer_count, MPI_LONG, 1, MTag_4, MPI_COMM_WORLD, &stat); *tp = MPI_Wtime(); *tp++ -= ( t0 + twtime); #ifdef DEBUG for (diff=0, i=0; i<buffer_count; i++) diff+= abs(result[i]-incoming[i]); if(diff > 0) printf("\n error in MPI snd/rcv: diff = %ld\n",diff); #endif } /* end ID 0 loop */ } else { for( iters = iterations ; iters-- ; ) { MPI_Recv (buffer, buffer_count, MPI_LONG, 0, MTag_3, MPI_COMM_WORLD, &stat); MPI_Send (buffer, buffer_count, MPI_LONG, 0, MTag_4, MPI_COMM_WORLD); }/* end ID 1 loop */ } /********************************************************************* ** Produce and output statistics *********************************************************************/ if(my_ID == 0){ printf("Round trip ping-pong times \n"); printf("\n %d iterations ",iterations); printf(" of %d byte messages\n\n",buff_size_bytes); time_stats (timings, iterations, buff_size_bytes); } /********************************************************************* ** ** this is not working right now. Until I debug this code, I will ** comment it out. ** ** Do a memcpy of the same size message to provide a point of ** comparison for the communiation data. This will be particularly ** relevant when we are running on a shared memory system. *********************************************************************/ // MPI_Barrier (MPI_COMM_WORLD); // if(my_ID == 0){ // // s_l_random( SEED, low_value, hi_value ); // tp = timings; // cpy_count = buffer_count * sizeof(long); // for (i=0; i<buffer_count; i++) { // incoming[i] = 0L; // buffer[i] = l_random(); // } // // for( iters = iterations ; iters-- ; ) { // t0 = MPI_Wtime(); // // incoming = (long *) memcpy(incoming, buffer, cpy_count); // buffer = (long *) memcpy(buffer, incoming, cpy_count); // // *tp = MPI_Wtime(); // *tp++ -= ( t0 + twtime); // //#ifdef DEBUG // for (diff=0, i=0; i<buffer_count; i++) diff+= abs(result[i]-incoming[i]); // if(diff > 0) printf("\n error in memcpy : diff = %ld\n",diff); //#endif // // } // // printf("\n\nRound trip memcpy times \n"); // time_stats (timings, iterations, buff_size_bytes); // } /********************************************************************* ** Wait for end and then shut down MPI *********************************************************************/ MPI_Barrier(MPI_COMM_WORLD); MPI_Finalize(); } /* end of main */