1 |
/* |
2 |
* 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 |
#endif |
46 |
|
47 |
namespace oopse { |
48 |
|
49 |
int ParallelRandNumGen::nCreatedRNG_ = 0; |
50 |
|
51 |
ParallelRandNumGen::ParallelRandNumGen(const uint32& oneSeed) { |
52 |
|
53 |
const int masterNode = 0; |
54 |
int seed = oneSeed; |
55 |
#ifdef IS_MPI |
56 |
MPI_Bcast(&seed, 1, MPI_UNSIGNED_LONG, masterNode, MPI_COMM_WORLD); |
57 |
#endif |
58 |
if (seed != oneSeed) { |
59 |
sprintf(painCave.errMsg, |
60 |
"Using different seed to initialize ParallelRandNumGen.\n"); |
61 |
painCave.isFatal = 1;; |
62 |
simError(); |
63 |
} |
64 |
|
65 |
int nProcessors; |
66 |
#ifdef IS_MPI |
67 |
MPI_Comm_size(MPI_COMM_WORLD, &nProcessors); |
68 |
MPI_Comm_rank( MPI_COMM_WORLD, &myRank_); |
69 |
#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 |
int newSeed = oneSeed + nCreatedRNG_; |
78 |
mtRand_ = new MTRand(newSeed, nProcessors, myRank_); |
79 |
|
80 |
++nCreatedRNG_; |
81 |
} |
82 |
|
83 |
ParallelRandNumGen::ParallelRandNumGen() { |
84 |
|
85 |
std::vector<uint32> bigSeed; |
86 |
const int masterNode = 0; |
87 |
int nProcessors; |
88 |
#ifdef IS_MPI |
89 |
MPI_Comm_size(MPI_COMM_WORLD, &nProcessors); |
90 |
MPI_Comm_rank( MPI_COMM_WORLD, &myRank_); |
91 |
#else |
92 |
nProcessors = 1; |
93 |
myRank_ = 0; |
94 |
#endif |
95 |
mtRand_ = new MTRand(nProcessors, myRank_); |
96 |
|
97 |
seed(); /** @todo calling virtual function in constructor is |
98 |
not a good design */ |
99 |
} |
100 |
|
101 |
|
102 |
void ParallelRandNumGen::seed( const uint32 oneSeed ) { |
103 |
|
104 |
const int masterNode = 0; |
105 |
int seed = oneSeed; |
106 |
#ifdef IS_MPI |
107 |
MPI_Bcast(&seed, 1, MPI_UNSIGNED_LONG, masterNode, MPI_COMM_WORLD); |
108 |
#endif |
109 |
if (seed != oneSeed) { |
110 |
sprintf(painCave.errMsg, |
111 |
"Using different seed to initialize ParallelRandNumGen.\n"); |
112 |
painCave.isFatal = 1;; |
113 |
simError(); |
114 |
} |
115 |
|
116 |
int newSeed = oneSeed +nCreatedRNG_; |
117 |
mtRand_->seed(newSeed); |
118 |
|
119 |
++nCreatedRNG_; |
120 |
} |
121 |
|
122 |
void ParallelRandNumGen::seed() { |
123 |
|
124 |
std::vector<uint32> bigSeed; |
125 |
int size; |
126 |
const int masterNode = 0; |
127 |
#ifdef IS_MPI |
128 |
if (worldRank == masterNode) { |
129 |
#endif |
130 |
|
131 |
bigSeed = mtRand_->generateSeeds(); |
132 |
size = bigSeed.size(); |
133 |
|
134 |
#ifdef IS_MPI |
135 |
MPI_Bcast(&size, 1, MPI_INT, masterNode, MPI_COMM_WORLD); |
136 |
MPI_Bcast(&bigSeed[0], size, MPI_UNSIGNED_LONG, masterNode, MPI_COMM_WORLD); |
137 |
}else { |
138 |
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 |
} |
142 |
#endif |
143 |
|
144 |
if (bigSeed.size() == 1) { |
145 |
mtRand_->seed(bigSeed[0]); |
146 |
} else { |
147 |
mtRand_->seed(&bigSeed[0], bigSeed.size()); |
148 |
} |
149 |
|
150 |
++nCreatedRNG_; |
151 |
} |
152 |
} |