Skip to content

Commit 8db0892

Browse files
committed
Switched used MPI bindings to C style (C++ is deprecated and caused problems on one machine)
1 parent 9015dbc commit 8db0892

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

src/MPIMCI.hpp

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
#ifndef MPIMCI
22
#define MPIMCI
33

4-
4+
#include <stdexcept>
55
#include <mpi.h>
66
#include "MCIntegrator.hpp"
77

88
namespace MPIMCI
99
{
1010
int init() // return mpi rank of process
1111
{
12-
if (MPI::Is_initialized()) throw std::runtime_error("MPI already initialized!");
13-
MPI::Init();
14-
MPI::COMM_WORLD.Set_errhandler(MPI::ERRORS_THROW_EXCEPTIONS);
15-
return MPI::COMM_WORLD.Get_rank();
12+
int isinit;
13+
MPI_Initialized(&isinit);
14+
if (isinit==1) throw std::runtime_error("MPI already initialized!");
15+
MPI_Init(NULL, NULL);
16+
int myrank;
17+
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
18+
return myrank;
1619
}
1720

1821
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
1922
{
2023
if (use_mpi) {
2124
// make sure the user has MPI in the correct state
22-
if (!MPI::Is_initialized()) throw std::runtime_error("MPI not initialized!");
23-
if (MPI::Is_finalized()) throw std::runtime_error("MPI already finalized!");
25+
int isinit, isfinal;
26+
MPI_Initialized(&isinit);
27+
if (isinit==0) throw std::runtime_error("MPI not initialized!");
28+
MPI_Finalized(&isfinal);
29+
if (isfinal==1) throw std::runtime_error("MPI already finalized!");
2430

25-
const int nranks = MPI::COMM_WORLD.Get_size();
31+
int nranks;
32+
MPI_Comm_size(MPI_COMM_WORLD, &nranks);
2633

2734
// the results are stored in myAverage/Error and then reduced into the same average/error for all processes
2835
double myAverage[mci->getNObsDim()];
@@ -31,10 +38,10 @@ namespace MPIMCI
3138
mci->integrate(Nmc, myAverage, myError, findMRT2step, initialdecorrelation);
3239

3340
for (int i=0; i<mci->getNObsDim(); ++i) {
34-
MPI::COMM_WORLD.Allreduce(&myAverage[i], &average[i], 1, MPI::DOUBLE, MPI::SUM);
41+
MPI_Allreduce(&myAverage[i], &average[i], 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
3542

3643
myError[i] *= myError[i];
37-
MPI::COMM_WORLD.Allreduce(&myError[i], &error[i], 1, MPI::DOUBLE, MPI::SUM);
44+
MPI_Allreduce(&myError[i], &error[i], 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
3845

3946
average[i] /= nranks;
4047
error[i] = sqrt(error[i]) / nranks;
@@ -48,10 +55,13 @@ namespace MPIMCI
4855
void finalize()
4956
{
5057
// make sure the user has MPI in the correct state
51-
if (!MPI::Is_initialized()) throw std::runtime_error("MPI not initialized!");
52-
if (MPI::Is_finalized()) throw std::runtime_error("MPI already finalized!");
58+
int isinit, isfinal;
59+
MPI_Initialized(&isinit);
60+
if (isinit==0) throw std::runtime_error("MPI not initialized!");
61+
MPI_Finalized(&isfinal);
62+
if (isfinal==1) throw std::runtime_error("MPI already finalized!");
5363

54-
MPI::Finalize();
64+
MPI_Finalize();
5565
}
5666
};
5767

0 commit comments

Comments
 (0)