ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-3.0/test/math/testRandNumGen.cpp
Revision: 2098
Committed: Wed Mar 9 17:58:33 2005 UTC (19 years, 4 months ago) by tim
File size: 3614 byte(s)
Log Message:
fix a bug in SectionParser (lineNo is not updated)

File Contents

# User Rev Content
1 tim 2098 #include <functional>
2 tim 2075 #include <cassert>
3     #include <fstream>
4     #include <algorithm>
5 tim 2098 #include <vector>
6     #include <iostream>
7    
8     #include "math/SeqRandNumGen.hpp"
9     #ifdef IS_MPI
10     #include <mpi.h>
11 tim 2075 #include "math/ParallelRandNumGen.hpp"
12 tim 2098 #endif
13     using namespace oopse;
14     using namespace std;
15 tim 2075
16 tim 2098 void testUniform(){
17     SeqRandNumGen randNumGen(823645754);
18     const int N = 100;
19 tim 2075 std::vector<unsigned long int> histogram(N, 0);
20 tim 2098 const unsigned long int num = 10000000;
21 tim 2075 for (int i = 0; i <num; ++i) {
22 tim 2098 int index = randNumGen.randInt(N -1 );
23     ++histogram[index]; // rantInt returns an integer in [0, N-1]
24 tim 2075 }
25 tim 2098 std::ofstream uniform("uniform.dat");
26     double avg = num / N;
27 tim 2075 double tolerance = 0.01*avg;
28 tim 2098 for (int i = 0; i < N; ++i) {
29     //assert((histogram[i] - avg) /avg <= tolerance);
30 tim 2075 uniform << i << "\t" << histogram[i] << std::endl;
31     }
32     }
33    
34 tim 2098 void testGaussian(){
35     SeqRandNumGen randNumGen(823645754);
36 tim 2075 double mean = 100.0;
37     double variance = 1.0;
38 tim 2098 const unsigned long int num = 1000000;
39 tim 2075 double interval = 0.1;
40     const int size = 2000;
41     vector<unsigned long int> histogram(size , 0);
42     vector<double> normalizedHistogram(size);
43 tim 2098 for (unsigned long int i = 0; i < num; ++i) {
44 tim 2075 int index = static_cast<int>(randNumGen.randNorm(mean, variance) / interval);
45     ++histogram[index];
46     }
47    
48     std::transform(histogram.begin(), histogram.end(), normalizedHistogram.begin(), std::bind2nd(std::divides<double>(), num));
49     std::ofstream gaussian("gaussian.dat");
50 tim 2098 for (int i = 0; i < size; ++i) {
51     gaussian << i*interval << "\t" << normalizedHistogram[i] << std::endl;
52 tim 2075 }
53     }
54 tim 2098 #ifdef IS_MPI
55     void testParallelRandNumGen(){
56     const unsigned long int seed = 324271632;
57     const unsigned long int nloops = 1000000;
58 tim 2075 MPI_Status istatus;
59     ParallelRandNumGen mpiRandNumGen(seed);
60     const int masterNode = 0;
61 tim 2098 int myRank;
62     MPI_Comm_rank( MPI_COMM_WORLD, &myRank );
63     if (myRank == masterNode) {
64 tim 2075
65 tim 2098 SeqRandNumGen singleRandNumGen(seed);
66     std::ofstream singleOs("single.dat");
67     std::ofstream parallelOs("parallel.dat");
68 tim 2075 int nProcessors;
69     MPI_Comm_size(MPI_COMM_WORLD, &nProcessors);
70     std::vector<unsigned long int> mpiRandNums(nProcessors);
71     std::vector<unsigned long int> singleRandNums(nProcessors);
72    
73 tim 2098 for (unsigned long int i = 0; i < nloops; ++i) {
74 tim 2075 mpiRandNums[masterNode] = mpiRandNumGen.randInt();
75    
76     for (int j = 0; j < nProcessors; ++j) {
77     if (j != masterNode) {
78     MPI_Recv(&mpiRandNums[j], 1, MPI_UNSIGNED_LONG, j, i, MPI_COMM_WORLD, &istatus);
79     }
80    
81     singleRandNums[j] = mpiRandNumGen.randInt();
82     }
83    
84 tim 2098 for (int j = 0; j < nProcessors; ++j) {
85     singleOs << singleRandNums[j] << "\n";
86     parallelOs << singleRandNums[j] << "\n";
87     }
88 tim 2075 }
89    
90    
91    
92     } else {
93    
94     unsigned long int randNum;
95 tim 2098 for (unsigned long int i = 0; i < nloops; ++i) {
96 tim 2075 randNum = mpiRandNumGen.randInt();
97     MPI_Send(&randNum, 1, MPI_INT, masterNode, i, MPI_COMM_WORLD);
98     }
99    
100     }
101    
102     }
103 tim 2098 #endif
104 tim 2075 int main(int argc, char* argv[]) {
105 tim 2098 #ifdef IS_MPI
106     MPI_Init(&argc, &argv);
107     std::cout << "begin test" << std::endl;
108 tim 2075 if (worldRank == 0 ) {
109     testUniform();
110     testGaussian();
111     }
112    
113     testParallelRandNumGen();
114    
115     MPI_Finalize();
116 tim 2098 #else
117     std::cout << "begin test" <<std::endl;
118     testUniform();
119     testGaussian();
120     #endif
121     std::cout << "test done" << std::endl;
122     return 0;
123 tim 2075 }