#include "MpiEnvironment.hpp" #include void MpiEnvironment::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::MpiEnvironment(int& argc, char* argv[]) { MPI_Init(&argc, &argv); _worldRank = [] { int r; MPI_Comm_rank(MPI_COMM_WORLD, &r); return static_cast(r); }(); _worldSize = [] { int s; MPI_Comm_size(MPI_COMM_WORLD, &s); return static_cast(s); }(); _isMaster = {_worldRank == 0}; // We want the program to stop on I/O errors MPI_File_set_errhandler(MPI_FILE_NULL, MPI_ERRORS_ARE_FATAL); } MpiEnvironment::~MpiEnvironment() { if (_worldRank != std::numeric_limits::max()) { MPI_Finalize(); } } MpiEnvironment::MpiEnvironment(MpiEnvironment&& other) noexcept { swap(*this, other); } MpiEnvironment& MpiEnvironment::operator=(MpiEnvironment&& other) noexcept { swap(*this, other); return *this; }