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