-
Notifications
You must be signed in to change notification settings - Fork 0
/
PRNGSetup.cpp
79 lines (70 loc) · 2.16 KB
/
PRNGSetup.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*******************************************************************************
GPU OPTIMIZED MONTE CARLO (GOMC) 2.31
Copyright (C) 2018 GOMC Group
A copy of the GNU General Public License can be found in the COPYRIGHT.txt
along with this program, also can be found at <http://www.gnu.org/licenses/>.
********************************************************************************/
#include <stdio.h> //for exit
#include "Reader.h" //For file I/O
#include "PRNGSetup.h" //header spec
#include "ConfigSetup.h" //For restart, gen kind settings
#include "StrLib.h" //for Compare
namespace prng_setup
{
const std::string STEP_STR = "STEP";
void PRNGInitData::HandleError(std::string const& mode)
{
std::cerr << "ERROR: Attempt to initialize generator in mode \""
<< mode << "\" failed!" << std::endl << std::endl;
exit(1);
}
//Init from file.
void PRNGInitData::Init(Reader & prngSeed, const ulong resStep)
{
Goto(prngSeed, resStep);
Read(prngSeed);
prng = new MTRand();
prng->load(loadArray);
}
void PRNGInitData::Goto(Reader & prngSeed, const ulong resStep)
{
std::string varName;
ulong step;
while (prngSeed.Read(varName)) {
if (str::compare(varName, STEP_STR))
prngSeed.file >> step;
if (step == resStep) break;
}
}
void PRNGInitData::Read(Reader & prngSeed)
{
uint temp, i = 0;
//Init seed array.
loadArray = new MTRand::uint32[MTRand::N + 1];
//Read seed;
for (; i < MTRand::N; i++) {
prngSeed.file >> temp;
loadArray[i] = (MTRand::uint32)(temp);
}
//Read left
prngSeed.file >> temp;
loadArray[i] = (MTRand::uint32)(temp);
}
}
const std::string PRNGSetup::seedFileAlias =
"Seed file for Mersenne Twister pseudo-random number generator";
void PRNGSetup::Init(config_setup::RestartSettings const& restart,
config_setup::PRNGKind const& genConf,
std::string const& name)
{
if (genConf.IsRand())
prngMaker.Init();
else if (genConf.IsSeed())
prngMaker.Init(genConf.seed);
else if (genConf.IsRestart()) {
Reader prngSeedFile(name, seedFileAlias);
prngMaker.Init(prngSeedFile, restart.step);
}
if (prngMaker.prng == NULL)
prngMaker.HandleError(genConf.kind);
}