Skip to content

Commit e7fd787

Browse files
committed
improve examples
1 parent 432b77f commit e7fd787

File tree

5 files changed

+62
-68
lines changed

5 files changed

+62
-68
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,11 @@ Note that we build out-of-tree, so the compiled library and executable files can
4646
# First steps
4747

4848
You may want to read `doc/user_manual.pdf` to get a quick overview of the libraries functionality. However, it is not guaranteed to be perfectly up-to-date and accurate. Therefore, the best way to get your own code started is by studying the examples in `examples/`. See `examples/README.md` for further guidance.
49+
50+
51+
# Multi-threading: MPI
52+
53+
This library supports multi-threaded MC integration with a distributed-memory paradigm, thanks to Message Passing interface (MPI).
54+
55+
To be able to use this feature, just compile the library with a MPI implementation present on your system. The header `MPIMCI.hpp` provides convenient functions
56+
for using MCI++ with MPI. For example usage, look into example ex2.

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
mkdir -p build && cd build
55
cmake -DCMAKE_CXX_COMPILER="${CXX_COMPILER}" -DUSER_CXX_FLAGS="${CXX_FLAGS}" -DUSE_COVERAGE="${USE_COVERAGE}" ..
66

7-
if [ "$1" == "" ]; then
7+
if [ "$1" = "" ]; then
88
make -j$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || getconf _NPROCESSORS_ONLN 2>/dev/null)
99
else
1010
make -j$1

examples/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ link_libraries(mci)
22
add_executable(ex1.exe ex1/main.cpp)
33
if (MPI_FOUND)
44
add_executable(ex2.exe ex2/main.cpp)
5-
target_link_libraries(ex2.exe ${MPI_CXX_LIBRARIES})
65
endif()

examples/ex1/main.cpp

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ int main() {
6363

6464
// declare a 1-dimensional integrator
6565
const int ndim = 1;
66-
MCI * mci = new MCI(ndim);
66+
MCI mci(ndim);
6767

68-
cout << "ndim = " << mci->getNDim() << endl;
68+
cout << "ndim = " << mci.getNDim() << endl;
6969

7070

7171

@@ -74,34 +74,34 @@ int main() {
7474
irange[0] = new double[2];
7575
irange[0][0] = -1.;
7676
irange[0][1] = 3.;
77-
mci->setIRange(irange);
77+
mci.setIRange(irange);
7878

79-
cout << "irange = [ " << mci->getIRange(0, 0) << " ; " << mci->getIRange(0, 1) << " ]" << endl;
79+
cout << "irange = [ " << mci.getIRange(0, 0) << " ; " << mci.getIRange(0, 1) << " ]" << endl;
8080

8181

8282

8383
// initial walker position
84-
double * initpos = new double[ndim];
84+
double initpos[ndim];
8585
initpos[0] = -0.5;
86-
mci->setX(initpos);
86+
mci.setX(initpos);
8787

88-
cout << "initial walker position = " << mci->getX(0) << endl;
88+
cout << "initial walker position = " << mci.getX(0) << endl;
8989

9090

9191

9292
// initial MRT2 step
93-
double * step = new double[ndim];
93+
double step[ndim];
9494
step[0] = 0.5;
95-
mci->setMRT2Step(step);
95+
mci.setMRT2Step(step);
9696

97-
cout << "MRT2 step = " << mci->getMRT2Step(0) << endl;
97+
cout << "MRT2 step = " << mci.getMRT2Step(0) << endl;
9898

9999

100100

101101
// target acceptance rate
102-
mci->setTargetAcceptanceRate(0.7);
102+
mci.setTargetAcceptanceRate(0.7);
103103

104-
cout << "Acceptance rate = " << mci->getTargetAcceptanceRate() << endl;
104+
cout << "Acceptance rate = " << mci.getTargetAcceptanceRate() << endl;
105105
cout << endl << endl;
106106

107107

@@ -117,22 +117,22 @@ int main() {
117117

118118
// observable
119119
MCIObservableFunctionInterface * obs = new Parabola(ndim);
120-
mci->addObservable(obs);
120+
mci.addObservable(obs);
121121

122-
cout << "Number of observables set = " << mci->getNObs() << endl;
122+
cout << "Number of observables set = " << mci.getNObs() << endl;
123123

124124

125125

126126
// sampling function
127-
cout << "Number of sampling function set = " << mci->getNSampF() << endl;
127+
cout << "Number of sampling function set = " << mci.getNSampF() << endl;
128128

129129

130130

131131
// integrate
132132
const long Nmc = 1000000;
133-
double * average = new double[mci->getNObsDim()];
134-
double * error = new double[mci->getNObsDim()];
135-
mci->integrate(Nmc, average, error);
133+
double average[mci.getNObsDim()];
134+
double error[mci.getNObsDim()];
135+
mci.integrate(Nmc, average, error);
136136

137137
cout << "The integral gives as result = " << average[0] << " +- " << error[0] << endl;
138138
cout << "--------------------------------------------------------" << endl << endl;
@@ -153,23 +153,23 @@ int main() {
153153
// observable
154154
delete obs;
155155
obs = new NormalizedParabola(ndim);
156-
mci->clearObservables(); // we first remove the old observable
157-
mci->addObservable(obs);
156+
mci.clearObservables(); // we first remove the old observable
157+
mci.addObservable(obs);
158158

159-
cout << "Number of observables set = " << mci->getNObs() << endl;
159+
cout << "Number of observables set = " << mci.getNObs() << endl;
160160

161161

162162

163163
// sampling function
164164
MCISamplingFunctionInterface * sf = new NormalizedLine(ndim);
165-
mci->addSamplingFunction(sf);
165+
mci.addSamplingFunction(sf);
166166

167-
cout << "Number of sampling function set = " << mci->getNSampF() << endl;
167+
cout << "Number of sampling function set = " << mci.getNSampF() << endl;
168168

169169

170170

171171
// integrate
172-
mci->integrate(Nmc, average, error);
172+
mci.integrate(Nmc, average, error);
173173

174174
cout << "The integral gives as result = " << average[0] << " +- " << error[0] << endl;
175175
cout << "--------------------------------------------------------" << endl << endl;
@@ -183,23 +183,14 @@ int main() {
183183

184184

185185
// deallocate
186-
delete[] average;
187-
delete[] error;
188186

189187
delete sf;
190188

191189
delete obs;
192190

193-
delete[] step;
194-
195-
delete[] initpos;
196-
197191
delete[] irange[0];
198192
delete[] irange;
199193

200-
delete mci;
201-
202-
203194

204195
// end
205196
return 0;

examples/ex2/main.cpp

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -64,38 +64,38 @@ int main() {
6464
}
6565

6666
const int ndim = 1;
67-
MCI * mci = new MCI(ndim);
67+
MCI mci(ndim);
6868

69-
if (myrank == 0) cout << "ndim = " << mci->getNDim() << endl;
69+
if (myrank == 0) cout << "ndim = " << mci.getNDim() << endl;
7070

7171
// set the integration range to [-1:3]
7272
double ** irange = new double*[ndim];
7373
irange[0] = new double[2];
7474
irange[0][0] = -1.;
7575
irange[0][1] = 3.;
76-
mci->setIRange(irange);
76+
mci.setIRange(irange);
7777

78-
if (myrank == 0) cout << "irange = [ " << mci->getIRange(0, 0) << " ; " << mci->getIRange(0, 1) << " ]" << endl;
78+
if (myrank == 0) cout << "irange = [ " << mci.getIRange(0, 0) << " ; " << mci.getIRange(0, 1) << " ]" << endl;
7979

8080
// initial walker position
81-
double * initpos = new double[ndim];
81+
double initpos[ndim];
8282
initpos[0] = -0.5;
83-
mci->setX(initpos);
83+
mci.setX(initpos);
8484

85-
if (myrank == 0) cout << "initial walker position = " << mci->getX(0) << endl;
85+
if (myrank == 0) cout << "initial walker position = " << mci.getX(0) << endl;
8686

8787
// initial MRT2 step
88-
double * step = new double[ndim];
88+
double step[ndim];
8989
step[0] = 0.5;
90-
mci->setMRT2Step(step);
90+
mci.setMRT2Step(step);
9191

92-
if (myrank == 0) cout << "MRT2 step = " << mci->getMRT2Step(0) << endl;
92+
if (myrank == 0) cout << "MRT2 step = " << mci.getMRT2Step(0) << endl;
9393

9494
// target acceptance rate
95-
mci->setTargetAcceptanceRate(0.7);
95+
mci.setTargetAcceptanceRate(0.7);
9696

9797
if (myrank == 0) {
98-
cout << "Acceptance rate = " << mci->getTargetAcceptanceRate() << endl;
98+
cout << "Acceptance rate = " << mci.getTargetAcceptanceRate() << endl;
9999
cout << endl << endl;
100100

101101
// first way of integrating
@@ -106,20 +106,25 @@ int main() {
106106

107107
// observable
108108
MCIObservableFunctionInterface * obs = new Parabola(ndim);
109-
mci->addObservable(obs);
109+
mci.addObservable(obs);
110110

111111
if (myrank == 0) {
112-
cout << "Number of observables set = " << mci->getNObs() << endl;
112+
cout << "Number of observables set = " << mci.getNObs() << endl;
113113
// sampling function
114-
cout << "Number of sampling function set = " << mci->getNSampF() << endl;
114+
cout << "Number of sampling function set = " << mci.getNSampF() << endl;
115115
}
116116

117117
// integrate
118118
const long Nmc = 1000000;
119-
double * average = new double[mci->getNObsDim()];
120-
double * error = new double[mci->getNObsDim()];
119+
double average[mci.getNObsDim()];
120+
double error[mci.getNObsDim()];
121121

122-
MPIMCI::integrate(mci, Nmc, average, error);
122+
// ! set fixed amount of findMRT2 and decorrelation steps !
123+
// ! this is very important for efficient parallel execution !
124+
mci.setNfindMRT2steps(50);
125+
mci.setNdecorrelationSteps(5000);
126+
127+
MPIMCI::integrate(&mci, Nmc, average, error);
123128

124129
if (myrank == 0) {
125130
cout << "The integral gives as result = " << average[0] << " +- " << error[0] << endl;
@@ -134,21 +139,21 @@ int main() {
134139
// observable
135140
delete obs;
136141
obs = new NormalizedParabola(ndim);
137-
mci->clearObservables(); // we first remove the old observable
138-
mci->addObservable(obs);
142+
mci.clearObservables(); // we first remove the old observable
143+
mci.addObservable(obs);
139144

140-
if (myrank == 0) cout << "Number of observables set = " << mci->getNObs() << endl;
145+
if (myrank == 0) cout << "Number of observables set = " << mci.getNObs() << endl;
141146

142147

143148
// sampling function
144149
MCISamplingFunctionInterface * sf = new NormalizedLine(ndim);
145-
mci->addSamplingFunction(sf);
150+
mci.addSamplingFunction(sf);
146151

147-
if (myrank == 0) cout << "Number of sampling function set = " << mci->getNSampF() << endl;
152+
if (myrank == 0) cout << "Number of sampling function set = " << mci.getNSampF() << endl;
148153

149154

150155
// integrate
151-
MPIMCI::integrate(mci, Nmc, average, error);
156+
MPIMCI::integrate(&mci, Nmc, average, error);
152157

153158
if (myrank == 0) {
154159
cout << "The integral gives as result = " << average[0] << " +- " << error[0] << endl;
@@ -165,18 +170,9 @@ int main() {
165170

166171
delete obs;
167172

168-
delete[] step;
169-
170-
delete[] initpos;
171-
172173
delete[] irange[0];
173174
delete[] irange;
174175

175-
delete[] average;
176-
delete[] error;
177-
178-
delete mci;
179-
180176
// finalize MPI
181177
MPIMCI::finalize();
182178

0 commit comments

Comments
 (0)