Skip to content

Commit 54909e7

Browse files
committed
Fixed error in MPI example. Basic wrapper functionality stable.
1 parent c7bf1f5 commit 54909e7

File tree

4 files changed

+30
-29
lines changed

4 files changed

+30
-29
lines changed

examples/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@
55
## Example 1
66

77
`ex1/`: integration of a 1-dimensional quadratic function, without and with a sampling function.
8+
9+
10+
## Example 2
11+
12+
`ex2/`: like ex2, but using the MPI wrapper for parallel integration.

examples/testmpi/main.cpp renamed to examples/ex2/main.cpp

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,18 @@ class NormalizedLine: public MCISamplingFunctionInterface{
5050
int main() {
5151
using namespace std;
5252

53-
// intro
54-
cout << "We want to compute the integral" << endl;
55-
cout << " Integral[-1:3] dx (4x - x^2)" << endl << endl;
56-
cout << "Notice that we can re-write the integral as" << endl;
57-
cout << " Integral[-1:3] dx (5*sign(x)*(4-x) * |x|/5)" << endl;
58-
cout << "where g(x)=|x|/5 is a candidate sampling function since it's positive on the domain [-1:3] and its integral on the domain is equal to 1." << endl << endl;
53+
int myrank = MPIMCI::init(); // first run custom MPI init
5954

60-
cout << "We start by initializing MPI and setting the MCI:" << endl;
61-
62-
MPIMCI::init();
63-
64-
// --- From now on we run in parallel ---
65-
66-
int myrank = MPI::COMM_WORLD.Get_rank();
55+
if (myrank == 0) {
56+
// intro
57+
cout << "We want to compute the integral" << endl;
58+
cout << " Integral[-1:3] dx (4x - x^2)" << endl << endl;
59+
cout << "Notice that we can re-write the integral as" << endl;
60+
cout << " Integral[-1:3] dx (5*sign(x)*(4-x) * |x|/5)" << endl;
61+
cout << "where g(x)=|x|/5 is a candidate sampling function since it's positive on the domain [-1:3] and its integral on the domain is equal to 1." << endl << endl;
62+
63+
cout << "We start by initializing MPI and setting the MCI:" << endl;
64+
}
6765

6866
const int ndim = 1;
6967
MCI * mci = new MCI(ndim);
@@ -174,22 +172,20 @@ int main() {
174172

175173
MPIMCI::finalize(mci); // also deletes all mci
176174

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;
177179

178180

179-
// --- From now on we are single threaded again ---
180-
181-
cout << "The integral gives as result = " << average[0] << " +- " << error[0] << endl;
182-
cout << "--------------------------------------------------------" << endl << endl;
183-
184-
185-
// final comments
186-
cout << "Using a sampling function in this case gives worse performance. In fact, the error bar is larger." << endl;
187-
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;
188-
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;
189184

190-
// deallocate
191-
delete[] average;
192-
delete[] error;
185+
// deallocate
186+
delete[] average;
187+
delete[] error;
188+
}
193189

194190
// end
195191
return 0;

examples/testmpi/run.sh renamed to examples/ex2/run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ echo "--------------------------------------------------------------------------
3737
echo ""
3838
echo ""
3939
echo ""
40-
mpirun -np 4 exe
40+
mpirun -np $1 exe

src/MCIWrapperMPI.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#include <mpi.h>
2-
#include <iostream>
32
#include "MCIntegrator.hpp"
43

54
namespace MPIMCI
65
{
7-
void init()
6+
int init() // return mpi rank of process
87
{
98
if (MPI::Is_initialized()) throw std::runtime_error("MPI already initialized!");
109
MPI::Init();
1110
MPI::COMM_WORLD.Set_errhandler(MPI::ERRORS_THROW_EXCEPTIONS);
11+
return MPI::COMM_WORLD.Get_rank();
1212
}
1313

1414
void integrate(MCI * const mci, const long &Nmc, double * average, double * error)

0 commit comments

Comments
 (0)