Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#pragma once
#include <cassert>
#include <mpi.h>
class MpiEnvironment { // wrapper for creating and destroying the environment
int _worldRank{-1};
int _worldSize{-1};
bool _isMaster{false};
public:
int worldRank() const { return _worldRank; }
int worldSize() const { return _worldSize; }
bool isMaster() const { return _isMaster; }
friend void swap(MpiEnvironment& first, MpiEnvironment& second) noexcept {
using std::swap;
swap(first._worldRank, second._worldRank);
swap(first._worldSize, second._worldSize);
swap(first._isMaster, second._isMaster);
}
MpiEnvironment(int& argc, char* argv[]) {
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &_worldRank);
MPI_Comm_size(MPI_COMM_WORLD, &_worldSize);
_isMaster = {_worldRank == 0};
}
~MpiEnvironment() {
if (_worldRank != -1) { MPI_Finalize(); }
}
MpiEnvironment(MpiEnvironment&) = delete;
MpiEnvironment& operator=(MpiEnvironment&) = delete;
MpiEnvironment(MpiEnvironment&& other) noexcept { swap(*this, other); }
MpiEnvironment& operator=(MpiEnvironment&& other) noexcept {
swap(*this, other);
return *this;
}
};