ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/math/ParallelRandNumGen.cpp
Revision: 2076
Committed: Wed Mar 2 15:36:14 2005 UTC (19 years, 4 months ago) by tim
File size: 4610 byte(s)
Log Message:
remove default parameter from MersenneTwister.hpp which causes all kinds of trouble.
Refactory random number generator

File Contents

# Content
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
46 namespace oopse {
47
48
49
50 int ParallelRandNumGen::nCreatedRNG_ = 0;
51
52 ParallelRandNumGen::ParallelRandNumGen( const uint32& oneSeed) {
53
54 const int masterNode = 0;
55 int seed = oneSeed;
56 MPI_Bcast(&seed, 1, MPI_UNSIGNED_LONG, masterNode, MPI_COMM_WORLD);
57
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 MPI_Comm_size(MPI_COMM_WORLD, &nProcessors);
67
68 //In order to generate independent random number stream, the actual seed used by random
69 //number generator is the seed passed to the constructor plus the number of random number
70 //generators which are already created.
71 int newSeed = oneSeed + nCreatedRNG_;
72 mtRand_ = new MTRand(newSeed, nProcessors, worldRank);
73
74 ++nCreatedRNG_;
75 }
76
77 ParallelRandNumGen::ParallelRandNumGen() {
78
79 std::vector<uint32> bigSeed;
80 const int masterNode = 0;
81 int nProcessors;
82 MPI_Comm_size(MPI_COMM_WORLD, &nProcessors);
83 mtRand_ = new MTRand(nProcessors, worldRank);
84
85 seed(); /** @todo calling virtual function in constructor is not a good design */
86 }
87
88
89 void ParallelRandNumGen::seed( const uint32 oneSeed ) {
90
91 const int masterNode = 0;
92 int seed = oneSeed;
93 MPI_Bcast(&seed, 1, MPI_UNSIGNED_LONG, masterNode, MPI_COMM_WORLD);
94
95 if (seed != oneSeed) {
96 sprintf(painCave.errMsg,
97 "Using different seed to initialize ParallelRandNumGen.\n");
98 painCave.isFatal = 1;;
99 simError();
100 }
101
102 int newSeed = oneSeed +nCreatedRNG_;
103 mtRand_->seed(newSeed);
104
105 ++nCreatedRNG_;
106 }
107
108 void ParallelRandNumGen::seed() {
109
110 std::vector<uint32> bigSeed;
111 int size;
112 const int masterNode = 0;
113 if (worldRank == masterNode) {
114 bigSeed = mtRand_->generateSeeds();
115 size = bigSeed.size();
116 MPI_Bcast(&size, 1, MPI_INT, masterNode, MPI_COMM_WORLD);
117 MPI_Bcast(&bigSeed[0], size, MPI_UNSIGNED_LONG, masterNode, MPI_COMM_WORLD);
118
119 }else {
120 MPI_Bcast(&size, 1, MPI_INT, masterNode, MPI_COMM_WORLD);
121 bigSeed.resize(size);
122 MPI_Bcast(&bigSeed[0], size, MPI_UNSIGNED_LONG, masterNode, MPI_COMM_WORLD);
123 }
124
125 if (bigSeed.size() == 1) {
126 mtRand_->seed(bigSeed[0]);
127 } else {
128 mtRand_->seed(&bigSeed[0], bigSeed.size());
129 }
130
131 ++nCreatedRNG_;
132 }
133
134
135 }
136
137 #endif

Properties

Name Value
svn:executable *