14#include "arccore/message_passing_mpi/StandaloneMpiMessagePassingMng.h"
16#include "arccore/message_passing/Dispatchers.h"
17#include "arccore/message_passing/Stat.h"
18#include "arccore/trace/ITraceMng.h"
19#include "arccore/base/ReferenceCounter.h"
20#include "arccore/base/BFloat16.h"
21#include "arccore/base/Float16.h"
23#include "arccore/message_passing_mpi/MpiDatatype.h"
24#include "arccore/message_passing_mpi/internal/MpiMessagePassingMng.h"
25#include "arccore/message_passing_mpi/internal/MpiAdapter.h"
26#include "arccore/message_passing_mpi/internal/MpiTypeDispatcher.h"
27#include "arccore/message_passing_mpi/internal/MpiControlDispatcher.h"
28#include "arccore/message_passing_mpi/internal/MpiSerializeDispatcher.h"
33namespace Arcane::MessagePassing::Mpi
39class ARCCORE_MESSAGEPASSINGMPI_EXPORT StandaloneMpiMessagePassingMng2
40:
public MpiMessagePassingMng
43 friend class StandaloneMpiMessagePassingMng;
47 explicit StandaloneMpiMessagePassingMng2(
Impl* p);
51 ~StandaloneMpiMessagePassingMng2()
override;
56 static MpiMessagePassingMng*
create(MPI_Comm comm,
bool clean_comm =
false);
74 static MpiMessagePassingMng* _create(
Impl* p);
84 explicit Impl(MPI_Comm mpi_comm,
bool clean_comm =
false)
85 : m_communicator(mpi_comm)
86 , m_clean_comm(clean_comm)
91 explicit Impl(
int* argc,
char** argv[],
int wanted_thread_level)
92 : m_communicator(MPI_COMM_WORLD)
95 int r = MPI_Init_thread(argc, argv, wanted_thread_level, &provided);
97 ARCCORE_FATAL(
"Error when calling MPI_Init_thread err={0}", r);
98 if (provided != wanted_thread_level) {
100 ::MPI_Comm_rank(m_communicator, &rank);
102 std::cerr <<
"WARNING: MPI_Init_thread provided level (" << provided
103 <<
") is not the wanted level (" << wanted_thread_level <<
")\n";
105 m_do_finalize =
true;
112 m_adapter->destroy();
115 std::cerr <<
"ERROR: msg=" << ex <<
"\n";
118 delete m_dispatchers;
122 MPI_Comm_free(&m_communicator);
129 m_trace_mng = Arccore::arccoreCreateDefaultTraceMng();
130 MPI_Comm mpi_comm = m_communicator;
131 ::MPI_Comm_rank(mpi_comm, &m_comm_rank);
132 ::MPI_Comm_size(mpi_comm, &m_comm_size);
135 m_adapter =
new MpiAdapter(m_trace_mng.get(), m_stat, mpi_comm, mpi_lock);
138 m_dispatchers->setDeleteDispatchers(
true);
150 IStat* m_stat =
nullptr;
153 int m_comm_rank = A_NULL_RANK;
154 int m_comm_size = A_NULL_RANK;
155 MPI_Comm m_communicator = MPI_COMM_NULL;
156 bool m_clean_comm =
false;
157 bool m_do_finalize =
false;
163StandaloneMpiMessagePassingMng2::
164StandaloneMpiMessagePassingMng2(
Impl* p)
165: MpiMessagePassingMng(p->buildInfo())
168 setTraceMng(p->m_trace_mng.get());
174StandaloneMpiMessagePassingMng2::
175~StandaloneMpiMessagePassingMng2()
182 template <
typename DataType>
void
187 x->setDestroyDatatype(
true);
188 dispatchers->setDispatcher(x);
191 template <
typename DataType>
void
195 MPI_Datatype mpi_dt = MpiBuiltIn::datatype(DataType());
197 _createAndSetCustomDispatcher<DataType>(dispatchers, mpm, adapter, dt);
205MpiMessagePassingMng* StandaloneMpiMessagePassingMng2::
208 auto mpm =
new StandaloneMpiMessagePassingMng2(p);
209 auto adapter = p->m_adapter;
210 auto dsp = p->m_dispatchers;
212 _createAndSetDispatcher<char>(dsp, mpm, adapter);
213 _createAndSetDispatcher<signed char>(dsp, mpm, adapter);
214 _createAndSetDispatcher<unsigned char>(dsp, mpm, adapter);
215 _createAndSetDispatcher<short>(dsp, mpm, adapter);
216 _createAndSetDispatcher<unsigned short>(dsp, mpm, adapter);
217 _createAndSetDispatcher<int>(dsp, mpm, adapter);
218 _createAndSetDispatcher<unsigned int>(dsp, mpm, adapter);
219 _createAndSetDispatcher<long>(dsp, mpm, adapter);
220 _createAndSetDispatcher<unsigned long>(dsp, mpm, adapter);
221 _createAndSetDispatcher<long long>(dsp, mpm, adapter);
222 _createAndSetDispatcher<unsigned long long>(dsp, mpm, adapter);
223 _createAndSetDispatcher<float>(dsp, mpm, adapter);
224 _createAndSetDispatcher<double>(dsp, mpm, adapter);
225 _createAndSetDispatcher<long double>(dsp, mpm, adapter);
227 dsp->setDispatcher(
new MpiControlDispatcher(adapter, mpm));
228 dsp->setDispatcher(
new MpiSerializeDispatcher(adapter, mpm));
230 MPI_Datatype uint8_datatype = MpiBuiltIn::datatype(uint8_t{});
233 MPI_Datatype mpi_datatype;
234 MPI_Type_contiguous(2, uint8_datatype, &mpi_datatype);
235 MPI_Type_commit(&mpi_datatype);
236 auto* x =
new MpiDatatype(mpi_datatype,
false,
new StdMpiReduceOperator<BFloat16>(
true));
237 _createAndSetCustomDispatcher<BFloat16>(dsp, mpm, adapter, x);
241 MPI_Datatype mpi_datatype;
242 MPI_Type_contiguous(2, uint8_datatype, &mpi_datatype);
243 MPI_Type_commit(&mpi_datatype);
244 auto* x =
new MpiDatatype(mpi_datatype,
false,
new StdMpiReduceOperator<Float16>(
true));
245 _createAndSetCustomDispatcher<Float16>(dsp, mpm, adapter, x);
254create(MPI_Comm mpi_comm,
bool clean_comm)
256 Impl* p =
new Impl(mpi_comm, clean_comm);
264createRef(MPI_Comm mpi_comm,
bool clean_comm)
266 MpiMessagePassingMng* v =
create(mpi_comm, clean_comm);
274createAndInitializeMPI(
int* argc,
char** argv[],
int wanted_thread_level)
276 Impl* p =
new Impl(argc, argv, wanted_thread_level);
288create(MPI_Comm mpi_comm,
bool clean_comm)
297createRef(MPI_Comm mpi_comm,
bool clean_comm)
317 return StandaloneMpiMessagePassingMng2::createAndInitializeMPI(argc, argv, wanted_thread_level);
#define ARCCORE_FATAL(...)
Macro throwing a FatalErrorException.
Base class for an exception.
Interface of the dispatcher container.
Interface of the message passing manager.
Statistics on parallelism.
Encapsulation of an MPI_Datatype.
MPI implementation of the message exchange manager.
static MpiMessagePassingMng * create(MPI_Comm comm, bool clean_comm=false)
Creates a manager associated with the communicator comm.
static Ref< IMessagePassingMng > createRef(MPI_Comm comm, bool clean_comm=false)
Creates a manager associated with the communicator comm.
static Ref< IMessagePassingMng > createAndInitializeMPI(int *argc, char **argv[], int wanted_thread_level)
Initialize MPI and creates a manager associated with the MPI_WORLD_COMM communicator.
static Ref< IMessagePassingMng > createRef(MPI_Comm comm, bool clean_comm=false)
Creates a manager associated with the communicator comm.
static Ref< IMessagePassingMng > createWorldRef()
Creates a manager associated with the MPI_WORLD_COMM communicator.
static IMessagePassingMng * create(MPI_Comm comm, bool clean_comm=false)
Creates a manager associated with the communicator comm.
Statistics manager for parallelism.
Reference to an instance.
Encapsulation of a pointer with a reference counter.
auto makeRef(InstanceType *t) -> Ref< InstanceType >
Creates a reference on a pointer.