| 1 | #include "math/RandNumGenTestCase.hpp" | 
| 2 |  | 
| 3 | // Registers the fixture into the 'registry' | 
| 4 | CPPUNIT_TEST_SUITE_REGISTRATION( RandNumGenTestCase ); | 
| 5 |  | 
| 6 | using namespace oopse; | 
| 7 |  | 
| 8 | void RandNumGenTestCase::testUniform(){ | 
| 9 | MTRand randNumGen(823645754); | 
| 10 |  | 
| 11 | const int N = 16; | 
| 12 | std::vector<unsigned long int> histogram(N, 0); | 
| 13 | const int num = 1000000; | 
| 14 | for (int i = 0; i <num; ++i) { | 
| 15 | ++histogram[randNumGen.randInt(N -1 )]; // rantInt returns an integer in [0, N-1] | 
| 16 | } | 
| 17 |  | 
| 18 | int avg = num / N; | 
| 19 | double tolerance = 0.01; | 
| 20 | for (int i = 0; i < num; ++i) { | 
| 21 | if ((histogram[i] - avg) /avg > tolerance) { | 
| 22 |  | 
| 23 | } | 
| 24 | } | 
| 25 | } | 
| 26 |  | 
| 27 | void RandNumGenTestCase::testGaussian(){ | 
| 28 | MTRand randNumGen(823645754); | 
| 29 | double mean = 3.0; | 
| 30 | double variance = 1.0; | 
| 31 | const int num = 1000000; | 
| 32 | double interval = 0.001; | 
| 33 | unsigned long int histogram[1000]; | 
| 34 | for (int i = 0; i < num; ++i) { | 
| 35 | int index = static_cast<int>(randNumGen.randNorm(mean, variance) / interval); | 
| 36 | ++histogram[index]; | 
| 37 | } | 
| 38 |  | 
| 39 | //fitting | 
| 40 | } | 
| 41 |  | 
| 42 | void RandNumGenTestCase::testMPIRNG(){ | 
| 43 | #ifdef IS_MPI | 
| 44 | const int seed = 324271632; | 
| 45 | const int nloops = 1000000; | 
| 46 | MPI_Status istatus; | 
| 47 | ParallelRandNumGen mpiRandNumGen(seed); | 
| 48 | const int masterNode = 0; | 
| 49 | if (worldRank = masterNode) { | 
| 50 |  | 
| 51 | MTRand singleRandNumGen(seed); | 
| 52 |  | 
| 53 | int nProcessors; | 
| 54 | MPI_Comm_size(MPI_COMM_WORLD, &nProcessors); | 
| 55 | std::vector<unsigned long int> mpiRandNums(nProcessors); | 
| 56 | std::vector<unsigned long int> singleRandNums(nProcessors); | 
| 57 |  | 
| 58 | for (int i = 0; i < nloops; ++i) { | 
| 59 | mpiRandNums[masterNode] = mpiRandNumGen.randInt(); | 
| 60 |  | 
| 61 | for (int j = 0; j < nProcessors; ++j) { | 
| 62 | if (j != masterNode) { | 
| 63 | MPI_Recv(&mpiRandNums[j], 1, MPI_UNSIGNED_LONG, j, i, MPI_COMM_WORLD, &istatus); | 
| 64 | } | 
| 65 |  | 
| 66 | singleRandNums[j] = mpiRandNumGen.randInt(); | 
| 67 |  | 
| 68 | assert(mpiRandNums[j], singleRandNums[j]); | 
| 69 | } | 
| 70 |  | 
| 71 |  | 
| 72 | } | 
| 73 |  | 
| 74 | mpiRandNumGen.randInt(); | 
| 75 |  | 
| 76 |  | 
| 77 | } else { | 
| 78 |  | 
| 79 | unsigned long int randNum; | 
| 80 | for (int i = 0; i < nloops; ++i) { | 
| 81 | randNum = mpiRandNumGen.randInt(); | 
| 82 | MPI_Send(&randNum, 1, MPI_INT, masterNode, i, MPI_COMM_WORLD); | 
| 83 | } | 
| 84 |  | 
| 85 | } | 
| 86 |  | 
| 87 | #endif | 
| 88 | } | 
| 89 |  |