Skip to content
Snippets Groups Projects
Commit 23744d1d authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Begin Messenger infrastructure

The goal of this class is to encapsulate MPI calls and to provide a same API for
non-parallel and parallel builds
parent 982152e0
No related branches found
No related tags found
1 merge request!11Feature/mpi
...@@ -9,6 +9,7 @@ add_library( ...@@ -9,6 +9,7 @@ add_library(
BacktraceManager.cpp BacktraceManager.cpp
ConsoleManager.cpp ConsoleManager.cpp
FPEManager.cpp FPEManager.cpp
Messenger.cpp
PastisOStream.cpp PastisOStream.cpp
PastisUtils.cpp PastisUtils.cpp
RevisionInfo.cpp RevisionInfo.cpp
......
#include <Messenger.hpp>
#include <PastisOStream.hpp>
#include <pastis_config.hpp>
#ifdef PASTIS_HAS_MPI
#include <mpi.h>
#endif // PASTIS_HAS_MPI
Messenger* Messenger::m_instance = nullptr;
void Messenger::create(int& argc, char* argv[])
{
if (Messenger::m_instance == nullptr) {
Messenger::m_instance = new Messenger(argc, argv);
} else {
std::cerr << "Messenger already created\n";
std::exit(1);
}
}
void Messenger::destroy()
{
// One allows multiple destruction to handle unexpected code exit
if (Messenger::m_instance != nullptr) {
delete Messenger::m_instance;
Messenger::m_instance = nullptr;
}
}
Messenger::
Messenger(int& argc, char* argv[])
{
#ifdef PASTIS_HAS_MPI
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &m_rank);
MPI_Comm_size(MPI_COMM_WORLD, &m_size);
if (m_rank != 0) {
pout.setOutput(null_stream);
perr.setOutput(null_stream);
}
#endif // PASTIS_HAS_MPI
}
Messenger::
~Messenger()
{
MPI_Finalize();
}
#ifndef MESSENGER_HPP
#define MESSENGER_HPP
#include <PastisMacros.hpp>
class Messenger
{
private:
static Messenger* m_instance;
Messenger(int& argc, char* argv[]);
int m_rank{0};
int m_size{1};
public:
static void create(int& argc, char* argv[]);
static void destroy();
PASTIS_INLINE
const int& rank() const
{
return m_rank;
}
PASTIS_INLINE
const int& size() const
{
return m_size;
}
Messenger(const Messenger&) = delete;
~Messenger();
};
#endif // MESSENGER_HPP
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
#include <RevisionInfo.hpp> #include <RevisionInfo.hpp>
#include <BuildInfo.hpp> #include <BuildInfo.hpp>
#include <Messenger.hpp>
#include <rang.hpp> #include <rang.hpp>
#include <FPEManager.hpp> #include <FPEManager.hpp>
...@@ -14,30 +16,9 @@ ...@@ -14,30 +16,9 @@
#include <CLI/CLI.hpp> #include <CLI/CLI.hpp>
#include <pastis_config.hpp>
#ifdef PASTIS_HAS_MPI
#include <mpi.h>
#endif // PASTIS_HAS_MPI
std::string initialize(int& argc, char* argv[]) std::string initialize(int& argc, char* argv[])
{ {
#ifdef PASTIS_HAS_MPI Messenger::create(argc, argv);
MPI_Init(&argc, &argv);
{
const int mpi_rank
=[](){
int mpi_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
return mpi_rank;
}();
if (mpi_rank != 0) {
pout.setOutput(null_stream);
perr.setOutput(null_stream);
}
}
#endif // PASTIS_HAS_MPI
long unsigned number = 10; long unsigned number = 10;
std::string filename; std::string filename;
...@@ -97,6 +78,7 @@ std::string initialize(int& argc, char* argv[]) ...@@ -97,6 +78,7 @@ std::string initialize(int& argc, char* argv[])
try { try {
app.parse(argc, argv); app.parse(argc, argv);
} catch (const CLI::ParseError &e) { } catch (const CLI::ParseError &e) {
Messenger::destroy();
std::exit(app.exit(e, pout(), perr())); std::exit(app.exit(e, pout(), perr()));
} }
...@@ -124,30 +106,6 @@ std::string initialize(int& argc, char* argv[]) ...@@ -124,30 +106,6 @@ std::string initialize(int& argc, char* argv[])
void finalize() void finalize()
{ {
#ifdef PASTIS_HAS_MPI
MPI_Barrier(MPI_COMM_WORLD);
const int mpi_rank
=[](){
int mpi_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
return mpi_rank;
}();
const int mpi_size
=[](){
int mpi_size;
MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
return mpi_size;
}();
pout() << rang::fgB::green << "Terminating process " << rang::fg::reset
<< rang::fgB::yellow << mpi_rank << rang::fg::reset << " of "
<< rang::style::bold << mpi_size << rang::style::reset << '\n';;
#endif // PASTIS_HAS_MPI
Kokkos::finalize(); Kokkos::finalize();
Messenger::destroy();
#ifdef PASTIS_HAS_MPI
MPI_Finalize();
#endif // PASTIS_HAS_MPI
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment