Skip to content

Commit 45905e1

Browse files
committed
Overhauled MPIMCI and added method to load seeds from file
1 parent a926270 commit 45905e1

File tree

5 files changed

+53
-9
lines changed

5 files changed

+53
-9
lines changed

examples/ex2/main.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ 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");
71+
6972
if (myrank == 0) cout << "ndim = " << mci->getNDim() << endl;
7073

7174
// set the integration range to [-1:3]
@@ -84,7 +87,6 @@ int main() {
8487

8588
if (myrank == 0) cout << "initial walker position = " << mci->getX(0) << endl;
8689

87-
8890
// initial MRT2 step
8991
double * step = new double[ndim];
9092
step[0] = 0.5;

examples/ex2/rseed.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../res/rseed.txt

src/MCIntegrator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ void MCI::setIRange(const double * const * irange)
526526
}
527527

528528

529-
void MCI::setSeed(const long seed)
529+
void MCI::setSeed(const uint_fast64_t seed) // fastest unsigned integer which is at least 64 bit (as expected by rgen)
530530
{
531531
_rgen.seed(seed);
532532
}

src/MCIntegrator.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class MCI
8888
~MCI(); //Destructor
8989

9090
// --- Setters
91-
void setSeed(const long seed);
91+
void setSeed(const uint_fast64_t seed);
9292

9393
void setIRange(const double * const * irange);
9494

src/MPIMCI.hpp

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,63 @@
22
#define MPIMCI
33

44
#include <stdexcept>
5+
#include <string>
6+
#include <fstream>
57
#include <mpi.h>
68
#include "MCIntegrator.hpp"
79

810
namespace MPIMCI
911
{
10-
int init() // return mpi rank of process
12+
// return my rank
13+
int rank()
14+
{
15+
int myrank;
16+
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
17+
return myrank;
18+
}
19+
20+
// return size
21+
int size()
22+
{
23+
int size;
24+
MPI_Comm_size(MPI_COMM_WORLD, &size);
25+
return size;
26+
}
27+
28+
// init MPI and return rank of process
29+
int init()
1130
{
1231
int isinit;
1332
MPI_Initialized(&isinit);
1433
if (isinit==1) throw std::runtime_error("MPI already initialized!");
1534
MPI_Init(NULL, NULL);
16-
int myrank;
17-
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
18-
return myrank;
35+
return rank();
36+
}
37+
38+
// set different random seeds per thread from a file
39+
void setSeed(MCI * const mci, const std::string filename)
40+
{
41+
int myrank = rank();
42+
int nranks = size();
43+
44+
uint_fast64_t seeds[nranks];
45+
uint_fast64_t myseed;
46+
if (myrank == 0) {
47+
std::ifstream seedfile;
48+
seedfile.open(filename);
49+
50+
for (int i=0; i<nranks; ++i) {
51+
seedfile >> seeds[i];
52+
}
53+
seedfile.close();
54+
}
55+
56+
MPI_Scatter(seeds, 1, MPI_UNSIGNED_LONG, &myseed, 1, MPI_UNSIGNED_LONG, 0, MPI_COMM_WORLD);
57+
mci->setSeed(myseed);
1958
}
2059

60+
61+
// integrate in parallel and accumulate results
2162
void integrate(MCI * const mci, const long &Nmc, double * average, double * error, bool findMRT2step=true, bool initialdecorrelation=true, bool use_mpi=true) // by setting use_mpi to false you can use this without requiring MPI
2263
{
2364
if (use_mpi) {
@@ -28,8 +69,7 @@ namespace MPIMCI
2869
MPI_Finalized(&isfinal);
2970
if (isfinal==1) throw std::runtime_error("MPI already finalized!");
3071

31-
int nranks;
32-
MPI_Comm_size(MPI_COMM_WORLD, &nranks);
72+
int nranks = size();
3373

3474
// the results are stored in myAverage/Error and then reduced into the same average/error for all processes
3575
double myAverage[mci->getNObsDim()];
@@ -52,6 +92,7 @@ namespace MPIMCI
5292
}
5393
}
5494

95+
// finalize MPI
5596
void finalize()
5697
{
5798
// make sure the user has MPI in the correct state

0 commit comments

Comments
 (0)