1 |
gezelter |
507 |
/* |
2 |
tim |
388 |
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
3 |
|
|
* |
4 |
|
|
* The University of Notre Dame grants you ("Licensee") a |
5 |
|
|
* non-exclusive, royalty free, license to use, modify and |
6 |
|
|
* redistribute this software in source and binary code form, provided |
7 |
|
|
* that the following conditions are met: |
8 |
|
|
* |
9 |
|
|
* 1. Acknowledgement of the program authors must be made in any |
10 |
|
|
* publication of scientific results based in part on use of the |
11 |
|
|
* program. An acceptable form of acknowledgement is citation of |
12 |
|
|
* the article in which the program was described (Matthew |
13 |
|
|
* A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher |
14 |
|
|
* J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented |
15 |
|
|
* Parallel Simulation Engine for Molecular Dynamics," |
16 |
|
|
* J. Comput. Chem. 26, pp. 252-271 (2005)) |
17 |
|
|
* |
18 |
|
|
* 2. Redistributions of source code must retain the above copyright |
19 |
|
|
* notice, this list of conditions and the following disclaimer. |
20 |
|
|
* |
21 |
|
|
* 3. Redistributions in binary form must reproduce the above copyright |
22 |
|
|
* notice, this list of conditions and the following disclaimer in the |
23 |
|
|
* documentation and/or other materials provided with the |
24 |
|
|
* distribution. |
25 |
|
|
* |
26 |
|
|
* This software is provided "AS IS," without a warranty of any |
27 |
|
|
* kind. All express or implied conditions, representations and |
28 |
|
|
* warranties, including any implied warranty of merchantability, |
29 |
|
|
* fitness for a particular purpose or non-infringement, are hereby |
30 |
|
|
* excluded. The University of Notre Dame and its licensors shall not |
31 |
|
|
* be liable for any damages suffered by licensee as a result of |
32 |
|
|
* using, modifying or distributing the software or its |
33 |
|
|
* derivatives. In no event will the University of Notre Dame or its |
34 |
|
|
* licensors be liable for any lost revenue, profit or data, or for |
35 |
|
|
* direct, indirect, special, consequential, incidental or punitive |
36 |
|
|
* damages, however caused and regardless of the theory of liability, |
37 |
|
|
* arising out of the use of or inability to use software, even if the |
38 |
|
|
* University of Notre Dame has been advised of the possibility of |
39 |
|
|
* such damages. |
40 |
|
|
*/ |
41 |
|
|
|
42 |
|
|
#include "math/ParallelRandNumGen.hpp" |
43 |
|
|
#ifdef IS_MPI |
44 |
|
|
#include <mpi.h> |
45 |
gezelter |
1241 |
#endif |
46 |
tim |
388 |
|
47 |
|
|
namespace oopse { |
48 |
|
|
|
49 |
gezelter |
507 |
int ParallelRandNumGen::nCreatedRNG_ = 0; |
50 |
tim |
388 |
|
51 |
gezelter |
1241 |
ParallelRandNumGen::ParallelRandNumGen(const uint32& oneSeed) { |
52 |
tim |
388 |
|
53 |
tim |
392 |
const int masterNode = 0; |
54 |
|
|
int seed = oneSeed; |
55 |
gezelter |
1241 |
#ifdef IS_MPI |
56 |
tim |
392 |
MPI_Bcast(&seed, 1, MPI_UNSIGNED_LONG, masterNode, MPI_COMM_WORLD); |
57 |
gezelter |
1241 |
#endif |
58 |
tim |
392 |
if (seed != oneSeed) { |
59 |
gezelter |
507 |
sprintf(painCave.errMsg, |
60 |
|
|
"Using different seed to initialize ParallelRandNumGen.\n"); |
61 |
|
|
painCave.isFatal = 1;; |
62 |
|
|
simError(); |
63 |
tim |
392 |
} |
64 |
|
|
|
65 |
tim |
388 |
int nProcessors; |
66 |
gezelter |
1241 |
#ifdef IS_MPI |
67 |
tim |
388 |
MPI_Comm_size(MPI_COMM_WORLD, &nProcessors); |
68 |
tim |
398 |
MPI_Comm_rank( MPI_COMM_WORLD, &myRank_); |
69 |
gezelter |
1241 |
#else |
70 |
|
|
nProcessors = 1; |
71 |
|
|
myRank_ = 0; |
72 |
|
|
#endif |
73 |
|
|
//In order to generate independent random number stream, the |
74 |
|
|
//actual seed used by random number generator is the seed passed |
75 |
|
|
//to the constructor plus the number of random number generators |
76 |
|
|
//which are already created. |
77 |
tim |
392 |
int newSeed = oneSeed + nCreatedRNG_; |
78 |
tim |
398 |
mtRand_ = new MTRand(newSeed, nProcessors, myRank_); |
79 |
gezelter |
1241 |
|
80 |
tim |
388 |
++nCreatedRNG_; |
81 |
gezelter |
507 |
} |
82 |
tim |
388 |
|
83 |
gezelter |
507 |
ParallelRandNumGen::ParallelRandNumGen() { |
84 |
tim |
388 |
|
85 |
|
|
std::vector<uint32> bigSeed; |
86 |
|
|
const int masterNode = 0; |
87 |
|
|
int nProcessors; |
88 |
gezelter |
1241 |
#ifdef IS_MPI |
89 |
tim |
388 |
MPI_Comm_size(MPI_COMM_WORLD, &nProcessors); |
90 |
tim |
398 |
MPI_Comm_rank( MPI_COMM_WORLD, &myRank_); |
91 |
gezelter |
1241 |
#else |
92 |
|
|
nProcessors = 1; |
93 |
|
|
myRank_ = 0; |
94 |
|
|
#endif |
95 |
tim |
398 |
mtRand_ = new MTRand(nProcessors, myRank_); |
96 |
tim |
388 |
|
97 |
gezelter |
1241 |
seed(); /** @todo calling virtual function in constructor is |
98 |
|
|
not a good design */ |
99 |
gezelter |
507 |
} |
100 |
tim |
388 |
|
101 |
|
|
|
102 |
gezelter |
507 |
void ParallelRandNumGen::seed( const uint32 oneSeed ) { |
103 |
tim |
388 |
|
104 |
|
|
const int masterNode = 0; |
105 |
|
|
int seed = oneSeed; |
106 |
gezelter |
1241 |
#ifdef IS_MPI |
107 |
tim |
388 |
MPI_Bcast(&seed, 1, MPI_UNSIGNED_LONG, masterNode, MPI_COMM_WORLD); |
108 |
gezelter |
1241 |
#endif |
109 |
tim |
388 |
if (seed != oneSeed) { |
110 |
gezelter |
507 |
sprintf(painCave.errMsg, |
111 |
|
|
"Using different seed to initialize ParallelRandNumGen.\n"); |
112 |
|
|
painCave.isFatal = 1;; |
113 |
|
|
simError(); |
114 |
tim |
388 |
} |
115 |
gezelter |
1241 |
|
116 |
tim |
388 |
int newSeed = oneSeed +nCreatedRNG_; |
117 |
|
|
mtRand_->seed(newSeed); |
118 |
gezelter |
1241 |
|
119 |
tim |
388 |
++nCreatedRNG_; |
120 |
gezelter |
507 |
} |
121 |
tim |
388 |
|
122 |
gezelter |
507 |
void ParallelRandNumGen::seed() { |
123 |
tim |
388 |
|
124 |
|
|
std::vector<uint32> bigSeed; |
125 |
|
|
int size; |
126 |
|
|
const int masterNode = 0; |
127 |
gezelter |
1241 |
#ifdef IS_MPI |
128 |
tim |
388 |
if (worldRank == masterNode) { |
129 |
gezelter |
1241 |
#endif |
130 |
|
|
|
131 |
gezelter |
507 |
bigSeed = mtRand_->generateSeeds(); |
132 |
|
|
size = bigSeed.size(); |
133 |
gezelter |
1241 |
|
134 |
|
|
#ifdef IS_MPI |
135 |
gezelter |
507 |
MPI_Bcast(&size, 1, MPI_INT, masterNode, MPI_COMM_WORLD); |
136 |
|
|
MPI_Bcast(&bigSeed[0], size, MPI_UNSIGNED_LONG, masterNode, MPI_COMM_WORLD); |
137 |
tim |
388 |
}else { |
138 |
gezelter |
507 |
MPI_Bcast(&size, 1, MPI_INT, masterNode, MPI_COMM_WORLD); |
139 |
|
|
bigSeed.resize(size); |
140 |
|
|
MPI_Bcast(&bigSeed[0], size, MPI_UNSIGNED_LONG, masterNode, MPI_COMM_WORLD); |
141 |
tim |
388 |
} |
142 |
gezelter |
1241 |
#endif |
143 |
tim |
388 |
|
144 |
|
|
if (bigSeed.size() == 1) { |
145 |
gezelter |
507 |
mtRand_->seed(bigSeed[0]); |
146 |
tim |
388 |
} else { |
147 |
gezelter |
507 |
mtRand_->seed(&bigSeed[0], bigSeed.size()); |
148 |
tim |
388 |
} |
149 |
|
|
|
150 |
|
|
++nCreatedRNG_; |
151 |
gezelter |
1241 |
} |
152 |
tim |
388 |
} |