| 1 | 
#include <cassert> | 
| 2 | 
#include <fstream> | 
| 3 | 
#include <algorithm> | 
| 4 | 
#include "utils/simError.h" | 
| 5 | 
#include "math/ParallelRandNumGen.hpp" | 
| 6 | 
 | 
| 7 | 
void RandNumGenTestCase::testUniform(){ | 
| 8 | 
    MTRand randNumGen(823645754); | 
| 9 | 
 | 
| 10 | 
    const int N = 16; | 
| 11 | 
    std::vector<unsigned long int> histogram(N, 0); | 
| 12 | 
    const int num = 1000000; | 
| 13 | 
    for (int i = 0; i <num; ++i) { | 
| 14 | 
        ++histogram[randNumGen.randInt(N -1 )]; // rantInt returns an integer in [0, N-1]         | 
| 15 | 
    } | 
| 16 | 
 | 
| 17 | 
    std::ofstream uniform("uniform.dat") | 
| 18 | 
    int avg = num / N; | 
| 19 | 
    double tolerance = 0.01*avg; | 
| 20 | 
    for (int i = 0; i < num; ++i) { | 
| 21 | 
        assert((histogram[i] - avg) /avg <= tolerance); | 
| 22 | 
        uniform << i << "\t" << histogram[i] << std::endl; | 
| 23 | 
    } | 
| 24 | 
} | 
| 25 | 
 | 
| 26 | 
void RandNumGenTestCase::testGaussian(){       | 
| 27 | 
    MTRand randNumGen(823645754); | 
| 28 | 
    double mean = 100.0; | 
| 29 | 
    double variance = 1.0; | 
| 30 | 
    const int num = 1000000; | 
| 31 | 
    double interval = 0.1; | 
| 32 | 
    const int size = 2000; | 
| 33 | 
    vector<unsigned long int> histogram(size , 0); | 
| 34 | 
    vector<double> normalizedHistogram(size); | 
| 35 | 
    for (int i = 0; i < num; ++i) { | 
| 36 | 
        int index = static_cast<int>(randNumGen.randNorm(mean, variance) / interval); | 
| 37 | 
        ++histogram[index];         | 
| 38 | 
    } | 
| 39 | 
 | 
| 40 | 
    std::transform(histogram.begin(), histogram.end(), normalizedHistogram.begin(), std::bind2nd(std::divides<double>(), num)); | 
| 41 | 
    std::ofstream gaussian("gaussian.dat"); | 
| 42 | 
    for (int i = 0; i < num; ++i) { | 
| 43 | 
        gaussian << i << "\t" << normalizedHistogram[i] << std::endl; | 
| 44 | 
    }     | 
| 45 | 
} | 
| 46 | 
 | 
| 47 | 
void RandNumGenTestCase::testParallelRandNumGen(){       | 
| 48 | 
    const int seed = 324271632; | 
| 49 | 
    const int nloops = 1000000; | 
| 50 | 
    MPI_Status istatus; | 
| 51 | 
    ParallelRandNumGen mpiRandNumGen(seed); | 
| 52 | 
    const int masterNode = 0; | 
| 53 | 
    if (worldRank = masterNode) { | 
| 54 | 
 | 
| 55 | 
        MTRand singleRandNumGen(seed); | 
| 56 | 
 | 
| 57 | 
        int nProcessors; | 
| 58 | 
        MPI_Comm_size(MPI_COMM_WORLD, &nProcessors); | 
| 59 | 
        std::vector<unsigned long int> mpiRandNums(nProcessors); | 
| 60 | 
        std::vector<unsigned long int> singleRandNums(nProcessors); | 
| 61 | 
 | 
| 62 | 
        for (int i = 0; i < nloops; ++i) { | 
| 63 | 
            mpiRandNums[masterNode] = mpiRandNumGen.randInt(); | 
| 64 | 
         | 
| 65 | 
            for (int j = 0; j < nProcessors; ++j) { | 
| 66 | 
                if (j != masterNode) { | 
| 67 | 
                    MPI_Recv(&mpiRandNums[j], 1, MPI_UNSIGNED_LONG, j, i, MPI_COMM_WORLD, &istatus); | 
| 68 | 
                } | 
| 69 | 
 | 
| 70 | 
                singleRandNums[j] = mpiRandNumGen.randInt(); | 
| 71 | 
            } | 
| 72 | 
 | 
| 73 | 
            assert(mpiRandNums, singleRandNums); | 
| 74 | 
        } | 
| 75 | 
 | 
| 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 | 
} | 
| 89 | 
 | 
| 90 | 
 | 
| 91 | 
int main(int argc, char* argv[]) { | 
| 92 | 
 | 
| 93 | 
    MPI_Init(argc, argv); | 
| 94 | 
 | 
| 95 | 
    if (worldRank == 0 ) { | 
| 96 | 
        testUniform(); | 
| 97 | 
        testGaussian(); | 
| 98 | 
    } | 
| 99 | 
 | 
| 100 | 
    testParallelRandNumGen(); | 
| 101 | 
     | 
| 102 | 
    MPI_Finalize(); | 
| 103 | 
} |