--- trunk/OOPSE-3.0/test/math/testRandNumGen.cpp 2005/03/02 07:32:15 2075 +++ trunk/OOPSE-3.0/test/math/testRandNumGen.cpp 2005/03/09 17:58:33 2098 @@ -1,65 +1,76 @@ +#include #include #include #include -#include "utils/simError.h" +#include +#include + +#include "math/SeqRandNumGen.hpp" +#ifdef IS_MPI +#include #include "math/ParallelRandNumGen.hpp" +#endif +using namespace oopse; +using namespace std; -void RandNumGenTestCase::testUniform(){ - MTRand randNumGen(823645754); - - const int N = 16; +void testUniform(){ + SeqRandNumGen randNumGen(823645754); + const int N = 100; std::vector histogram(N, 0); - const int num = 1000000; + const unsigned long int num = 10000000; for (int i = 0; i histogram(size , 0); vector normalizedHistogram(size); - for (int i = 0; i < num; ++i) { + for (unsigned long int i = 0; i < num; ++i) { int index = static_cast(randNumGen.randNorm(mean, variance) / interval); ++histogram[index]; } std::transform(histogram.begin(), histogram.end(), normalizedHistogram.begin(), std::bind2nd(std::divides(), num)); std::ofstream gaussian("gaussian.dat"); - for (int i = 0; i < num; ++i) { - gaussian << i << "\t" << normalizedHistogram[i] << std::endl; + for (int i = 0; i < size; ++i) { + gaussian << i*interval << "\t" << normalizedHistogram[i] << std::endl; } } - -void RandNumGenTestCase::testParallelRandNumGen(){ - const int seed = 324271632; - const int nloops = 1000000; +#ifdef IS_MPI +void testParallelRandNumGen(){ + const unsigned long int seed = 324271632; + const unsigned long int nloops = 1000000; MPI_Status istatus; ParallelRandNumGen mpiRandNumGen(seed); const int masterNode = 0; - if (worldRank = masterNode) { + int myRank; + MPI_Comm_rank( MPI_COMM_WORLD, &myRank ); + if (myRank == masterNode) { - MTRand singleRandNumGen(seed); - + SeqRandNumGen singleRandNumGen(seed); + std::ofstream singleOs("single.dat"); + std::ofstream parallelOs("parallel.dat"); int nProcessors; MPI_Comm_size(MPI_COMM_WORLD, &nProcessors); std::vector mpiRandNums(nProcessors); std::vector singleRandNums(nProcessors); - for (int i = 0; i < nloops; ++i) { + for (unsigned long int i = 0; i < nloops; ++i) { mpiRandNums[masterNode] = mpiRandNumGen.randInt(); for (int j = 0; j < nProcessors; ++j) { @@ -70,7 +81,10 @@ void RandNumGenTestCase::testParallelRandNumGen(){ singleRandNums[j] = mpiRandNumGen.randInt(); } - assert(mpiRandNums, singleRandNums); + for (int j = 0; j < nProcessors; ++j) { + singleOs << singleRandNums[j] << "\n"; + parallelOs << singleRandNums[j] << "\n"; + } } @@ -78,7 +92,7 @@ void RandNumGenTestCase::testParallelRandNumGen(){ } else { unsigned long int randNum; - for (int i = 0; i < nloops; ++i) { + for (unsigned long int i = 0; i < nloops; ++i) { randNum = mpiRandNumGen.randInt(); MPI_Send(&randNum, 1, MPI_INT, masterNode, i, MPI_COMM_WORLD); } @@ -86,12 +100,11 @@ void RandNumGenTestCase::testParallelRandNumGen(){ } } - - +#endif int main(int argc, char* argv[]) { - - MPI_Init(argc, argv); - +#ifdef IS_MPI + MPI_Init(&argc, &argv); + std::cout << "begin test" << std::endl; if (worldRank == 0 ) { testUniform(); testGaussian(); @@ -100,4 +113,11 @@ int main(int argc, char* argv[]) { testParallelRandNumGen(); MPI_Finalize(); +#else + std::cout << "begin test" <