Skip to content

Commit 1043f2b

Browse files
committed
Allow use of offset to control from which point on to read the seeds
1 parent 45905e1 commit 1043f2b

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

examples/ex2/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ int main() {
6666
const int ndim = 1;
6767
MCI * mci = new MCI(ndim);
6868

69-
// if you want to seed the different MPI threads from a file, for reproducible results
70-
MPIMCI::setSeed(mci, "rseed.txt");
69+
// seed the different MPI threads from a file
70+
MPIMCI::setSeed(mci, "rseed.txt", 0); // offset x -> start from the x-th seed in file
7171

7272
if (myrank == 0) cout << "ndim = " << mci->getNDim() << endl;
7373

src/MPIMCI.hpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace MPIMCI
3636
}
3737

3838
// set different random seeds per thread from a file
39-
void setSeed(MCI * const mci, const std::string filename)
39+
void setSeed(MCI * const mci, const std::string &filename, const int &offset = 0) // with offset you can control how many seeds to skip initially
4040
{
4141
int myrank = rank();
4242
int nranks = size();
@@ -47,7 +47,13 @@ namespace MPIMCI
4747
std::ifstream seedfile;
4848
seedfile.open(filename);
4949

50+
for (int i=0; i<offset; ++i) {
51+
if (seedfile.eof()) {throw std::runtime_error("Chosen seed offset is already larger than the number of seeds in seed file.");}
52+
uint_fast64_t skip;
53+
seedfile >> skip;
54+
}
5055
for (int i=0; i<nranks; ++i) {
56+
if (seedfile.eof()) {throw std::runtime_error("Seed file doesn't provide enough seeds for the chosen number of ranks and offset.");}
5157
seedfile >> seeds[i];
5258
}
5359
seedfile.close();
@@ -65,9 +71,9 @@ namespace MPIMCI
6571
// make sure the user has MPI in the correct state
6672
int isinit, isfinal;
6773
MPI_Initialized(&isinit);
68-
if (isinit==0) throw std::runtime_error("MPI not initialized!");
74+
if (isinit==0) {throw std::runtime_error("MPI not initialized!");}
6975
MPI_Finalized(&isfinal);
70-
if (isfinal==1) throw std::runtime_error("MPI already finalized!");
76+
if (isfinal==1) {throw std::runtime_error("MPI already finalized!");}
7177

7278
int nranks = size();
7379

@@ -98,9 +104,9 @@ namespace MPIMCI
98104
// make sure the user has MPI in the correct state
99105
int isinit, isfinal;
100106
MPI_Initialized(&isinit);
101-
if (isinit==0) throw std::runtime_error("MPI not initialized!");
107+
if (isinit==0) {throw std::runtime_error("MPI not initialized!");}
102108
MPI_Finalized(&isfinal);
103-
if (isfinal==1) throw std::runtime_error("MPI already finalized!");
109+
if (isfinal==1) {throw std::runtime_error("MPI already finalized!");}
104110

105111
MPI_Finalize();
106112
}

0 commit comments

Comments
 (0)