Skip to content

Commit 39efe0a

Browse files
committed
Usability overhaul, first iteration
1 parent f0a933f commit 39efe0a

File tree

9 files changed

+143
-194
lines changed

9 files changed

+143
-194
lines changed

examples/ex1/main.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,7 @@ int main() {
9999

100100

101101
// target acceptance rate
102-
double * targetacceptrate = new double[1];
103-
targetacceptrate[0] = 0.7;
104-
mci->setTargetAcceptanceRate(targetacceptrate);
102+
mci->setTargetAcceptanceRate(0.7);
105103

106104
cout << "Acceptance rate = " << mci->getTargetAcceptanceRate() << endl;
107105
cout << endl << endl;
@@ -192,8 +190,6 @@ int main() {
192190

193191
delete obs;
194192

195-
delete[] targetacceptrate;
196-
197193
delete[] step;
198194

199195
delete[] initpos;

examples/ex1/run.sh

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,5 @@
11
#!/bin/bash
2-
source ../../config.sh
3-
OS_NAME=$(uname)
4-
5-
# Delete old compiled files
6-
\rm -f exe
7-
\rm -f *.o
8-
9-
#runtime dynamic library path
10-
RPATH="$(pwd)/../.."
11-
12-
FLAGS_TO_USE=$OPTFLAGS
13-
14-
# Build the main executable
15-
echo "$CC $FLAGS $FLAGS_TO_USE -I$(pwd)/../../src/ -c *.cpp"
16-
$CC $FLAGS $FLAGS_TO_USE -Wall -I$(pwd)/../../src/ -c *.cpp
17-
18-
case ${OS_NAME} in
19-
"Darwin")
20-
echo "$CC $FLAGS $FLAGS_TO_USE -L$(pwd)/../.. -o exe *.o -l${LIBNAME}"
21-
$CC $FLAGS $FLAGS_TO_USE -L$(pwd)/../.. -o exe *.o -l${LIBNAME}
22-
;;
23-
"Linux")
24-
echo "$CC $FLAGS $FLAGS_TO_USE -L$(pwd)/../.. -Wl,-rpath=${RPATH} -o exe *.o -l${LIBNAME}"
25-
$CC $FLAGS $FLAGS_TO_USE -L$(pwd)/../.. -Wl,-rpath=${RPATH} -o exe *.o -l${LIBNAME}
26-
;;
27-
esac
28-
29-
echo "Rebuilt the executable file"
30-
echo ""
31-
echo ""
32-
33-
# Run the debugging executable
34-
echo "Ready to run!"
35-
echo ""
36-
echo "--------------------------------------------------------------------------"
37-
echo ""
38-
echo ""
39-
echo ""
40-
# valgrind --leak-check=full --track-origins=yes ./exe
41-
./exe
2+
ORIGDIR=$(pwd)
3+
cd ../../build/examples
4+
./ex1.exe
5+
cd "${ORIGDIR}"

examples/ex2/main.cpp

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

69-
// this can be used to 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
71-
7269
if (myrank == 0) cout << "ndim = " << mci->getNDim() << endl;
7370

7471
// set the integration range to [-1:3]
@@ -95,9 +92,7 @@ int main() {
9592
if (myrank == 0) cout << "MRT2 step = " << mci->getMRT2Step(0) << endl;
9693

9794
// target acceptance rate
98-
double * targetacceptrate = new double[1];
99-
targetacceptrate[0] = 0.7;
100-
mci->setTargetAcceptanceRate(targetacceptrate);
95+
mci->setTargetAcceptanceRate(0.7);
10196

10297
if (myrank == 0) {
10398
cout << "Acceptance rate = " << mci->getTargetAcceptanceRate() << endl;
@@ -170,8 +165,6 @@ int main() {
170165

171166
delete obs;
172167

173-
delete[] targetacceptrate;
174-
175168
delete[] step;
176169

177170
delete[] initpos;

examples/ex2/rseed.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/ex2/run.sh

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,5 @@
11
#!/bin/bash
2-
source ../../config.sh
3-
OS_NAME=$(uname)
4-
5-
# Delete old compiled files
6-
\rm -f exe
7-
\rm -f *.o
8-
9-
#runtime dynamic library path
10-
RPATH="$(pwd)/../.."
11-
12-
FLAGS_TO_USE=$OPTFLAGS
13-
14-
# Build the main executable
15-
echo "$MPICC $FLAGS $FLAGS_TO_USE -I$(pwd)/../../src/ -c *.cpp"
16-
$MPICC $FLAGS $FLAGS_TO_USE -Wall -I$(pwd)/../../src/ -c *.cpp
17-
18-
case ${OS_NAME} in
19-
"Darwin")
20-
echo "$MPICC $FLAGS $FLAGS_TO_USE -L$(pwd)/../.. -o exe *.o -l${LIBNAME}"
21-
$MPICC $FLAGS $FLAGS_TO_USE -L$(pwd)/../.. -o exe *.o -l${LIBNAME}
22-
;;
23-
"Linux")
24-
echo "$MPICC $FLAGS $FLAGS_TO_USE -L$(pwd)/../.. -Wl,-rpath=${RPATH} -o exe *.o -l${LIBNAME}"
25-
$MPICC $FLAGS $FLAGS_TO_USE -L$(pwd)/../.. -Wl,-rpath=${RPATH} -o exe *.o -l${LIBNAME}
26-
;;
27-
esac
28-
29-
echo "Rebuilt the executable file"
30-
echo ""
31-
echo ""
32-
33-
# Run the debugging executable
34-
echo "Ready to run!"
35-
echo ""
36-
echo "--------------------------------------------------------------------------"
37-
echo ""
38-
echo ""
39-
echo ""
40-
mpirun -np $1 exe
2+
ORIGDIR=$(pwd)
3+
cd ../../build/examples
4+
mpirun -np $1 ./ex2.exe
5+
cd "${ORIGDIR}"

include/mci/MCIntegrator.hpp

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,19 @@ class MCI
2121
std::uniform_real_distribution<double> _rd; //after initialization (done in the constructor) can be used with _rd(_rgen)
2222

2323
int _ndim; // number of dimensions
24+
double ** _rrange; // ranges for random initialization
2425
double ** _irange; // integration ranges
2526
double _vol; // Integration volume
2627

2728
double * _xold; // walker position
2829
double * _xnew; //walker proposed position
2930
double * _mrt2step; // M(RT)^2 random step
3031

31-
double _targetaccrate; // accepted and rejected moves
32-
int _acc, _rej; // the MC integration will be done sampling from this pdf
32+
int _NfindMRT2steps; // how many MRT2 step adjustment iterations to do before integrating
33+
int _NdecorrelationSteps; // how many decorrelation steps to do before integrating
34+
int _nblocks; // how many blocks to use for error estimation (0 for auto -> high RAM usage)
35+
36+
double _targetaccrate; // desired acceptance ratio
3337

3438
std::vector<MCISamplingFunctionInterface *> _pdf; //vector of sampling functions
3539
bool _flagpdf; // did the user provide a sampling function?
@@ -39,6 +43,7 @@ class MCI
3943

4044
std::vector<MCICallBackOnAcceptanceInterface *> _cback; // Vector of observable functions
4145

46+
int _acc, _rej; // internal counters
4247
int _ridx; // running index, which keeps track of the number of MC steps
4348
int _bidx; // index of the current block/datax element
4449
double ** _datax; // array that will contain all the measured observable (or block averages if used)
@@ -70,32 +75,34 @@ class MCI
7075
void applyPBC(double * v);
7176
void computeNewX();
7277
void updateX();
73-
void doStepMRT2(bool * flagacc); //use this if there is a pdf
78+
bool doStepMRT2(); //use this if there is a pdf, returns whether step was accepted or not
7479

75-
void findMRT2Step(const int &NfindMRT2stepIterations = -1);
80+
void findMRT2Step();
81+
void initialDecorrelation();
7682

7783
void sample(const long &npoints, const bool &flagobs, const long &stepsPerBlock = 1);
7884

79-
void initialDecorrelation(const int &NdecorrelationSteps = -1);
80-
8185
void storeObservables();
8286
void storeWalkerPositions();
8387

84-
8588
public:
8689
MCI(const int & ndim); //Constructor, need the number of dimensions
8790
~MCI(); //Destructor
8891

8992
// --- Setters
9093
void setSeed(const uint_fast64_t seed);
9194

92-
void setIRange(const double * const * irange);
95+
void setRRange(const double * const * rrange); // if set, initialize walkers within these bounds on every newRandomX(), else use irange
96+
void setIRange(const double * const * irange); // keep walkers within these bounds during integration (defaults to full range of double floats)
9397

9498
void setX(const double * x);
9599
void newRandomX(); // use if you want to take a new random _xold
96-
void setMRT2Step(const double * mrt2step);
97100

98-
void setTargetAcceptanceRate(const double * targetaccrate);
101+
void setMRT2Step(const double * mrt2step);
102+
void setNfindMRT2steps(const int niterations /* -1 == auto, 0 == disabled */){_NfindMRT2steps=niterations;} // how many MRT2 step adjustment iterations to do before integrating
103+
void setNdecorrelationSteps(const int nsteps /* -1 == auto, 0 == disabled */){_NdecorrelationSteps=nsteps;} // how many decorrelation steps to do before integrating
104+
void setNBlocks(const int nblocks /* 0 == auto -> high RAM usage */){_nblocks=nblocks;} // how many blocks to use for error estimation
105+
void setTargetAcceptanceRate(const double targetaccrate);
99106

100107
void addObservable(MCIObservableFunctionInterface * obs);
101108
void clearObservables();
@@ -111,9 +118,13 @@ class MCI
111118

112119
// --- Getters
113120
int getNDim(){return _ndim;}
121+
double getRRange(const int &i, const int &j){return *(*(_rrange+i)+j);}
114122
double getIRange(const int &i, const int &j){return *(*(_irange+i)+j);}
115123
double getX(const int &i){return *(_xold+i);}
116124
double getMRT2Step(const int &i){return *(_mrt2step+i);}
125+
int getNfindMRT2steps(){return _NfindMRT2steps;}
126+
int getNdecorrelationSteps(){return _NdecorrelationSteps;}
127+
int getNBlocks(){return _nblocks;}
117128

118129
MCIObservableFunctionInterface * getObservable(const int &i){return _obs[i];}
119130
int getNObs(){return _obs.size();}
@@ -130,13 +141,8 @@ class MCI
130141

131142
// --- Integrate
132143

133-
// Wrapper for easiness/compatibility. Using automatic methods for findMRT2step and initial decorrelation (if enabled).
134-
void integrate(const long &Nmc, double * average, double * error, bool findMRT2step=true, bool initialdecorrelation = true, size_t nblocks = 0); // nblocks == 0 means using auto-blocking (no RAM benefit)
135-
136-
// Actual integrate implemention. With integer controls: -1 -> auto, 0 -> disabled, >0 -> use fixed step/iteration counts, e.g. useful for parallel computation
137-
void integrate(const long &Nmc, double * average, double * error, int NfindMRT2stepIterations, int NdecorrelationSteps, size_t nblocks = 0);
138-
139-
144+
// Actual integrate implemention. With flags to skip the configured step adjustment/decorrelation.
145+
void integrate(const long &Nmc, double * average, double * error, const bool doFindMRT2step = true, const bool doDecorrelation = true);
140146
};
141147

142148
#endif

include/mci/MPIMCI.hpp

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace MPIMCI
1313
{
1414
// return my rank
15-
int rank()
15+
int myrank() // we cannot use rank() here because it collides with "using namespace std"
1616
{
1717
int myrank;
1818
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
@@ -34,14 +34,15 @@ namespace MPIMCI
3434
MPI_Initialized(&isinit);
3535
if (isinit==1) throw std::runtime_error("MPI already initialized!");
3636
MPI_Init(NULL, NULL);
37-
return rank();
37+
38+
return myrank();
3839
}
3940

4041
// set different random seeds per thread from a file
4142
void setSeed(MCI * const mci, const std::string &filename, const int &offset = 0) // with offset you can control how many seeds to skip initially
4243
{
43-
int myrank = rank();
44-
int nranks = size();
44+
int myrank = MPIMCI::myrank();
45+
int nranks = MPIMCI::size();
4546

4647
uint_fast64_t seeds[nranks];
4748
uint_fast64_t myseed;
@@ -70,47 +71,34 @@ namespace MPIMCI
7071

7172
// integrate in parallel and accumulate results
7273

73-
void integrate(MCI * const mci, const long &Nmc, double * average, double * error, int NfindMRT2stepIterations, int NdecorrelationSteps, size_t nblocks=0, bool use_mpi=true) // by setting use_mpi to false you can use this without requiring MPI
74+
void integrate(MCI * const mci, const long &Nmc, double * average, double * error, const bool doFindMRT2Step = true, const bool doDecorrelation = true)
7475
{
75-
if (use_mpi) {
76-
// make sure the user has MPI in the correct state
77-
int isinit, isfinal;
78-
MPI_Initialized(&isinit);
79-
if (isinit==0) {throw std::runtime_error("MPI not initialized!");}
80-
MPI_Finalized(&isfinal);
81-
if (isfinal==1) {throw std::runtime_error("MPI already finalized!");}
76+
// make sure the user has MPI in the correct state
77+
int isinit, isfinal;
78+
MPI_Initialized(&isinit);
79+
if (isinit==0) {throw std::runtime_error("MPI not initialized!");}
80+
MPI_Finalized(&isfinal);
81+
if (isfinal==1) {throw std::runtime_error("MPI already finalized!");}
8282

83-
int nranks = size();
83+
int nranks = size();
8484

85-
// the results are stored in myAverage/Error and then reduced into the same average/error for all processes
86-
double myAverage[mci->getNObsDim()];
87-
double myError[mci->getNObsDim()];
85+
// the results are stored in myAverage/Error and then reduced into the same average/error for all processes
86+
double myAverage[mci->getNObsDim()];
87+
double myError[mci->getNObsDim()];
8888

89-
mci->integrate(Nmc, myAverage, myError, NfindMRT2stepIterations, NdecorrelationSteps, nblocks);
89+
mci->integrate(Nmc, myAverage, myError, doFindMRT2Step, doDecorrelation);
9090

91-
for (int i=0; i<mci->getNObsDim(); ++i) {
92-
MPI_Allreduce(&myAverage[i], &average[i], 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
91+
for (int i=0; i<mci->getNObsDim(); ++i) {
92+
MPI_Allreduce(&myAverage[i], &average[i], 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
9393

94-
myError[i] *= myError[i];
95-
MPI_Allreduce(&myError[i], &error[i], 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
94+
myError[i] *= myError[i];
95+
MPI_Allreduce(&myError[i], &error[i], 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
9696

97-
average[i] /= nranks;
98-
error[i] = sqrt(error[i]) / nranks;
99-
}
100-
}
101-
else {
102-
mci->integrate(Nmc, average, error, NfindMRT2stepIterations, NdecorrelationSteps, nblocks); // regular single thread call
97+
average[i] /= nranks;
98+
error[i] = sqrt(error[i]) / nranks;
10399
}
104100
}
105101

106-
void integrate(MCI * const mci, const long &Nmc, double * average, double * error, bool findMRT2step=true, bool initialdecorrelation=true, size_t nblocks=0, bool use_mpi=true) // auto-mode-wrapper
107-
{
108-
int stepsMRT2 = findMRT2step ? -1 : 0;
109-
int stepsDecorr = initialdecorrelation ? -1 : 0;
110-
integrate(mci, Nmc, average, error, stepsMRT2, stepsDecorr, nblocks, use_mpi);
111-
}
112-
113-
114102
// finalize MPI
115103
void finalize()
116104
{

0 commit comments

Comments
 (0)