Skip to content

Commit 9015dbc

Browse files
committed
Reduce->Allreduce, necessary for VMC
1 parent f85bb8e commit 9015dbc

File tree

2 files changed

+26
-32
lines changed

2 files changed

+26
-32
lines changed

examples/ex2/main.cpp

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ int main() {
6363
cout << "We start by initializing MPI and setting the MCI:" << endl;
6464
}
6565

66-
const int ndim = 1;
66+
const int ndim = 1;
6767
MCI * mci = new MCI(ndim);
6868

6969
if (myrank == 0) cout << "ndim = " << mci->getNDim() << endl;
@@ -119,12 +119,9 @@ int main() {
119119

120120
// integrate
121121
const long Nmc = 1000000;
122-
double * average;
123-
double * error;
124-
if (myrank == 0) { // allocate only for root
125-
average = new double[mci->getNObsDim()];
126-
error = new double[mci->getNObsDim()];
127-
}
122+
double * average = new double[mci->getNObsDim()];
123+
double * error = new double[mci->getNObsDim()];
124+
128125
MPIMCI::integrate(mci, Nmc, average, error);
129126

130127
if (myrank == 0) {
@@ -156,6 +153,16 @@ int main() {
156153
// integrate
157154
MPIMCI::integrate(mci, Nmc, average, error);
158155

156+
if (myrank == 0) {
157+
cout << "The integral gives as result = " << average[0] << " +- " << error[0] << endl;
158+
cout << "--------------------------------------------------------" << endl << endl;
159+
160+
161+
// final comments
162+
cout << "Using a sampling function in this case gives worse performance. In fact, the error bar is larger." << endl;
163+
cout << "This implies that the variance of the re-factored f(x) written for introducing a sampling function, is larger than the original f(x)." << endl;
164+
}
165+
159166
// deallocate per-thread allocations
160167
delete sf;
161168

@@ -170,22 +177,13 @@ int main() {
170177
delete[] irange[0];
171178
delete[] irange;
172179

173-
MPIMCI::finalize(mci); // also deletes all mci
174-
175-
// from now on just root thread
176-
if (myrank == 0) {
177-
cout << "The integral gives as result = " << average[0] << " +- " << error[0] << endl;
178-
cout << "--------------------------------------------------------" << endl << endl;
179-
180+
delete[] average;
181+
delete[] error;
180182

181-
// final comments
182-
cout << "Using a sampling function in this case gives worse performance. In fact, the error bar is larger." << endl;
183-
cout << "This implies that the variance of the re-factored f(x) written for introducing a sampling function, is larger than the original f(x)." << endl;
183+
delete mci;
184184

185-
// deallocate
186-
delete[] average;
187-
delete[] error;
188-
}
185+
// finalize MPI
186+
MPIMCI::finalize();
189187

190188
// end
191189
return 0;

src/MPIMCI.hpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,46 +15,42 @@ namespace MPIMCI
1515
return MPI::COMM_WORLD.Get_rank();
1616
}
1717

18-
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
18+
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
1919
{
2020
if (use_mpi) {
2121
// make sure the user has MPI in the correct state
2222
if (!MPI::Is_initialized()) throw std::runtime_error("MPI not initialized!");
2323
if (MPI::Is_finalized()) throw std::runtime_error("MPI already finalized!");
2424

25-
const int myrank = MPI::COMM_WORLD.Get_rank();
2625
const int nranks = MPI::COMM_WORLD.Get_size();
2726

28-
// the results are stored in myAverage/Error and then reduced into average/error for root process
27+
// the results are stored in myAverage/Error and then reduced into the same average/error for all processes
2928
double myAverage[mci->getNObsDim()];
3029
double myError[mci->getNObsDim()];
3130

3231
mci->integrate(Nmc, myAverage, myError, findMRT2step, initialdecorrelation);
3332

3433
for (int i=0; i<mci->getNObsDim(); ++i) {
35-
MPI::COMM_WORLD.Reduce(&myAverage[i], &average[i], 1, MPI::DOUBLE, MPI::SUM, 0);
34+
MPI::COMM_WORLD.Allreduce(&myAverage[i], &average[i], 1, MPI::DOUBLE, MPI::SUM);
3635

3736
myError[i] *= myError[i];
38-
MPI::COMM_WORLD.Reduce(&myError[i], &error[i], 1, MPI::DOUBLE, MPI::SUM, 0);
37+
MPI::COMM_WORLD.Allreduce(&myError[i], &error[i], 1, MPI::DOUBLE, MPI::SUM);
3938

40-
if (myrank == 0) {
41-
average[i] /= nranks;
42-
error[i] = sqrt(error[i]) / nranks;
43-
}
39+
average[i] /= nranks;
40+
error[i] = sqrt(error[i]) / nranks;
4441
}
4542
}
4643
else {
4744
mci->integrate(Nmc, average, error, findMRT2step, initialdecorrelation); // regular single thread call
4845
}
4946
}
5047

51-
void finalize(MCI * mci)
48+
void finalize()
5249
{
5350
// make sure the user has MPI in the correct state
5451
if (!MPI::Is_initialized()) throw std::runtime_error("MPI not initialized!");
5552
if (MPI::Is_finalized()) throw std::runtime_error("MPI already finalized!");
5653

57-
delete mci;
5854
MPI::Finalize();
5955
}
6056
};

0 commit comments

Comments
 (0)