# | Line 1 | Line 1 | |
---|---|---|
1 | < | /* |
1 | > | /* |
2 | * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. | |
3 | * | |
4 | * The University of Notre Dame grants you ("Licensee") a | |
# | Line 47 | Line 47 | namespace oopse { | |
47 | ||
48 | ||
49 | ||
50 | < | int ParallelRandNumGen::nCreatedRNG_ = 0; |
50 | > | int ParallelRandNumGen::nCreatedRNG_ = 0; |
51 | ||
52 | < | ParallelRandNumGen::ParallelRandNumGen( const uint32& oneSeed) { |
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(); |
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 | < | |
67 | > | MPI_Comm_rank( MPI_COMM_WORLD, &myRank_); |
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); |
72 | > | mtRand_ = new MTRand(newSeed, nProcessors, myRank_); |
73 | ||
74 | ++nCreatedRNG_; | |
75 | < | } |
75 | > | } |
76 | ||
77 | < | ParallelRandNumGen::ParallelRandNumGen() { |
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); |
83 | > | MPI_Comm_rank( MPI_COMM_WORLD, &myRank_); |
84 | > | mtRand_ = new MTRand(nProcessors, myRank_); |
85 | ||
86 | seed(); /** @todo calling virtual function in constructor is not a good design */ | |
87 | < | } |
87 | > | } |
88 | ||
89 | ||
90 | < | void ParallelRandNumGen::seed( const uint32 oneSeed ) { |
90 | > | void ParallelRandNumGen::seed( const uint32 oneSeed ) { |
91 | ||
92 | const int masterNode = 0; | |
93 | int seed = oneSeed; | |
94 | MPI_Bcast(&seed, 1, MPI_UNSIGNED_LONG, masterNode, MPI_COMM_WORLD); | |
95 | ||
96 | if (seed != oneSeed) { | |
97 | < | sprintf(painCave.errMsg, |
98 | < | "Using different seed to initialize ParallelRandNumGen.\n"); |
99 | < | painCave.isFatal = 1;; |
100 | < | simError(); |
97 | > | sprintf(painCave.errMsg, |
98 | > | "Using different seed to initialize ParallelRandNumGen.\n"); |
99 | > | painCave.isFatal = 1;; |
100 | > | simError(); |
101 | } | |
102 | ||
103 | int newSeed = oneSeed +nCreatedRNG_; | |
104 | mtRand_->seed(newSeed); | |
105 | ||
106 | ++nCreatedRNG_; | |
107 | < | } |
107 | > | } |
108 | ||
109 | < | void ParallelRandNumGen::seed() { |
109 | > | void ParallelRandNumGen::seed() { |
110 | ||
111 | std::vector<uint32> bigSeed; | |
112 | int size; | |
113 | const int masterNode = 0; | |
114 | if (worldRank == masterNode) { | |
115 | < | bigSeed = mtRand_->generateSeeds(); |
116 | < | size = bigSeed.size(); |
117 | < | MPI_Bcast(&size, 1, MPI_INT, masterNode, MPI_COMM_WORLD); |
118 | < | MPI_Bcast(&bigSeed[0], size, MPI_UNSIGNED_LONG, masterNode, MPI_COMM_WORLD); |
115 | > | bigSeed = mtRand_->generateSeeds(); |
116 | > | size = bigSeed.size(); |
117 | > | MPI_Bcast(&size, 1, MPI_INT, masterNode, MPI_COMM_WORLD); |
118 | > | MPI_Bcast(&bigSeed[0], size, MPI_UNSIGNED_LONG, masterNode, MPI_COMM_WORLD); |
119 | ||
120 | }else { | |
121 | < | MPI_Bcast(&size, 1, MPI_INT, masterNode, MPI_COMM_WORLD); |
122 | < | bigSeed.resize(size); |
123 | < | MPI_Bcast(&bigSeed[0], size, MPI_UNSIGNED_LONG, masterNode, MPI_COMM_WORLD); |
121 | > | MPI_Bcast(&size, 1, MPI_INT, masterNode, MPI_COMM_WORLD); |
122 | > | bigSeed.resize(size); |
123 | > | MPI_Bcast(&bigSeed[0], size, MPI_UNSIGNED_LONG, masterNode, MPI_COMM_WORLD); |
124 | } | |
125 | ||
126 | if (bigSeed.size() == 1) { | |
127 | < | mtRand_->seed(bigSeed[0]); |
127 | > | mtRand_->seed(bigSeed[0]); |
128 | } else { | |
129 | < | mtRand_->seed(&bigSeed[0], bigSeed.size()); |
129 | > | mtRand_->seed(&bigSeed[0], bigSeed.size()); |
130 | } | |
131 | ||
132 | ++nCreatedRNG_; | |
133 | < | } |
133 | > | } |
134 | ||
135 | ||
136 | } |
– | Removed lines |
+ | Added lines |
< | Changed lines |
> | Changed lines |