Arcane  4.2.3.0
Developer documentation
Loading...
Searching...
No Matches
MpiAdapter.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* MpiAdapter.h (C) 2000-2026 */
9/* */
10/* Implementation of messages with MPI. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_MESSAGEPASSINGMPI_MPIADAPTER_H
13#define ARCCORE_MESSAGEPASSINGMPI_MPIADAPTER_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arccore/trace/TraceAccessor.h"
18
19#include "arccore/message_passing_mpi/MessagePassingMpiGlobal.h"
20#include "arccore/message_passing_mpi/internal/MessagePassingMpiEnum.h"
21#include "arccore/message_passing/PointToPointMessageInfo.h"
22#include "arccore/message_passing/Request.h"
23#include "arccore/collections/CollectionsGlobal.h"
24
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
30namespace Arcane::MessagePassing::Mpi
31{
33
34/*---------------------------------------------------------------------------*/
35/*---------------------------------------------------------------------------*/
36
46class ARCCORE_MESSAGEPASSINGMPI_EXPORT MpiAdapter
47: public TraceAccessor
48, public IRequestCreator
49{
50 public:
51
52 class RequestSet;
53 struct SubRequestInfo;
54
55 public:
56
57 MpiAdapter(ITraceMng* msg, IStat* stat,
58 MPI_Comm comm, MpiLock* mpi_lock,
59 IMpiProfiling* mpi_prof = nullptr);
60 MpiAdapter(const MpiAdapter& rhs) = delete;
61 MpiAdapter& operator=(const MpiAdapter& rhs) = delete;
62
63 protected:
64
65 ~MpiAdapter() override;
66
67 public:
68
70 void destroy();
71
72 public:
73
74 void broadcast(void* buf, Int64 nb_elem, Int32 root, MPI_Datatype datatype);
75 void allGather(const void* send_buf, void* recv_buf,
76 Int64 nb_elem, MPI_Datatype datatype);
77 void gather(const void* send_buf, void* recv_buf,
78 Int64 nb_elem, Int32 root, MPI_Datatype datatype);
79 void allGatherVariable(const void* send_buf, void* recv_buf, const int* recv_counts,
80 const int* recv_indexes, Int64 nb_elem, MPI_Datatype datatype);
81 void gatherVariable(const void* send_buf, void* recv_buf, const int* recv_counts,
82 const int* recv_indexes, Int64 nb_elem, Int32 root, MPI_Datatype datatype);
83 void scatterVariable(const void* send_buf, const int* send_count, const int* send_indexes,
84 void* recv_buf, Int64 nb_elem, Int32 root, MPI_Datatype datatype);
85 void allToAll(const void* send_buf, void* recv_buf, Int32 count, MPI_Datatype datatype);
86 void allToAllVariable(const void* send_buf, const int* send_counts,
87 const int* send_indexes, void* recv_buf, const int* recv_counts,
88 const int* recv_indexes, MPI_Datatype datatype);
89 void reduce(const void* send_buf, void* recv_buf, Int64 count, MPI_Datatype datatype, MPI_Op op, Int32 root);
90 void allReduce(const void* send_buf, void* recv_buf, Int64 count, MPI_Datatype datatype, MPI_Op op);
91 void scan(const void* send_buf, void* recv_buf, Int64 count, MPI_Datatype datatype, MPI_Op op);
92 void scanExclusive(const void* send_buf, void* recv_buf, Int64 count, MPI_Datatype datatype, MPI_Op op);
93 void directSendRecv(const void* send_buffer, Int64 send_buffer_size,
94 void* recv_buffer, Int64 recv_buffer_size,
95 Int32 proc, Int64 elem_size, MPI_Datatype data_type);
96
97 Request directSend(const void* send_buffer, Int64 send_buffer_size,
98 Int32 proc, Int64 elem_size, MPI_Datatype data_type,
99 int mpi_tag, bool is_blocked);
100
102 Request sendNonBlockingNoStat(const void* send_buffer, Int64 send_buffer_size,
103 Int32 proc, MPI_Datatype data_type, int mpi_tag);
104
105 Request directRecv(void* recv_buffer, Int64 recv_buffer_size,
106 Int32 source_rank, Int64 elem_size, MPI_Datatype data_type,
107 int mpi_tag, bool is_blocked);
108
110 Request receiveNonBlockingNoStat(void* recv_buffer, Int64 recv_buffer_size,
111 Int32 source_rank, MPI_Datatype data_type, int mpi_tag);
112
113 Request directSendPack(const void* send_buffer, Int64 send_buffer_size,
114 Int32 proc, int mpi_tag, bool is_blocked);
115
116 void probeRecvPack(UniqueArray<Byte>& recv_buffer, Int32 proc);
117
118 MessageId probeMessage(PointToPointMessageInfo message);
119
120 MessageSourceInfo legacyProbeMessage(PointToPointMessageInfo message);
121
122 Request directRecv(void* recv_buffer, Int64 recv_buffer_size,
123 MessageId message, Int64 elem_size, MPI_Datatype data_type,
124 bool is_blocked);
125
126 Request directRecvPack(void* recv_buffer, Int64 recv_buffer_size,
127 Int32 proc, int mpi_tag, bool is_blocking);
128
129 void waitAllRequests(ArrayView<Request> requests);
130
131 private:
132
133 bool _waitAllRequestsMPI(ArrayView<Request> requests, ArrayView<bool> indexes,
134 ArrayView<MPI_Status> mpi_status);
135
136 public:
137
138 void waitSomeRequests(ArrayView<Request> requests,
139 ArrayView<bool> indexes,
140 bool is_non_blocking);
141
142 void waitSomeRequestsMPI(ArrayView<Request> requests,
143 ArrayView<bool> indexes,
144 ArrayView<MPI_Status> mpi_status, bool is_non_blocking);
145
146 public:
147
149 int commRank() const { return m_comm_rank; }
150
152 int commSize() const { return m_comm_size; }
153
154 IMessagePassingMng* commSplit(bool keep);
155
156 void freeRequest(Request& request);
157 bool testRequest(Request& request);
158
159 void enableDebugRequest(bool enable_debug_request);
160
161 MpiLock* mpiLock() const { return m_mpi_lock; }
162
163 Request nonBlockingBroadcast(void* buf, Int64 nb_elem, Int32 root, MPI_Datatype datatype);
164 Request nonBlockingAllGather(const void* send_buf, void* recv_buf, Int64 nb_elem, MPI_Datatype datatype);
165 Request nonBlockingGather(const void* send_buf, void* recv_buf, Int64 nb_elem, Int32 root, MPI_Datatype datatype);
166
167 Request nonBlockingAllToAll(const void* send_buf, void* recv_buf, Int32 count, MPI_Datatype datatype);
168 Request nonBlockingAllReduce(const void* send_buf, void* recv_buf, Int64 count, MPI_Datatype datatype, MPI_Op op);
169 Request nonBlockingAllToAllVariable(const void* send_buf, const int* send_counts,
170 const int* send_indexes, void* recv_buf, const int* recv_counts,
171 const int* recv_indexes, MPI_Datatype datatype);
172
173 Request nonBlockingBarrier();
174 void barrier();
175
176 int toMPISize(Int64 count);
177
179 Request buildRequest(int ret, MPI_Request request);
180
181 public:
182
184 void setRequestErrorAreFatal(bool v);
185 bool isRequestErrorAreFatal() const;
186
188 void setPrintRequestError(bool v);
189 bool isPrintRequestError() const;
190
192 void setTraceMPIMessage(bool v) { m_is_trace = v; }
193 bool isTraceMPIMessage() const { return m_is_trace; }
194
200 void setCheckRequest(bool v);
201 bool isCheckRequest() const;
202
203 public:
204
205 void setMpiProfiling(IMpiProfiling* mpi_profiling);
206 void setProfiler(IProfiler* profiler);
207 IMpiProfiling* getMpiProfiling() const;
208 IProfiler* profiler() const;
209
210 public:
211
212 ITimeMetricCollector* timeMetricCollector() const { return m_metric_collector; }
213 void setTimeMetricCollector(ITimeMetricCollector* v) { m_metric_collector = v; }
214
215 bool isAllowNullRankForAnySource() const { return m_is_allow_null_rank_for_any_source; }
216
217 public:
218
219 void initializeWindowCreator(MPI_Comm comm_machine);
220 MpiMachineShMemWinBaseInternalCreator* windowCreator() const;
221
222 private:
223
224 IStat* m_stat = nullptr;
225 MpiLock* m_mpi_lock = nullptr;
226 IMpiProfiling* m_mpi_prof = nullptr;
227 ITimeMetricCollector* m_metric_collector = nullptr;
228 MPI_Comm m_communicator;
229 int m_comm_rank = A_PROC_NULL_RANK;
230 int m_comm_size = 0;
231 Int64 m_nb_all_reduce = 0;
232 Int64 m_nb_reduce = 0;
233 bool m_is_trace = false;
234 RequestSet* m_request_set = nullptr;
236 MPI_Request m_empty_request1;
237 MPI_Request m_empty_request2;
238 int m_recv_buffer_for_empty_request[1];
239 int m_send_buffer_for_empty_request2[1];
240 int m_recv_buffer_for_empty_request2[1];
241
242 // If true, allows using the null rank (A_NULL_RANK) to specify MPI_ANY_SOURCE
243 // This is the default in Arccore versions before July 2024.
244 // Starting from 2025, it will have to be prohibited.
245 // The environment variable ARCCORE_ALLOW_NULL_RANK_FOR_MPI_ANY_SOURCE will allow
246 // temporarily maintaining a compatible mode.
247 bool m_is_allow_null_rank_for_any_source = true;
248
250
251 private:
252
253 void _trace(const char* function);
254 void _addRequest(MPI_Request request);
255 void _removeRequest(MPI_Request request);
256 void _checkFatalInRequest();
257 MessageId _probeMessage(MessageRank source, MessageTag tag, bool is_blocking);
258 MessageSourceInfo _legacyProbeMessage(MessageRank source, MessageTag tag, bool is_blocking);
259 bool _handleEndRequests(ArrayView<Request> requests, ArrayView<bool> done_indexes,
260 ArrayView<MPI_Status> status);
261 void _checkHasNoRequests();
262 MessageSourceInfo _buildSourceInfoFromStatus(const MPI_Status& status);
263};
264
265/*---------------------------------------------------------------------------*/
266/*---------------------------------------------------------------------------*/
267
268} // namespace Arcane::MessagePassing::Mpi
269
270/*---------------------------------------------------------------------------*/
271/*---------------------------------------------------------------------------*/
272
273#endif
Declarations of types for the 'base' component of Arccore.
Modifiable view of an array of type T.
Interface managing statistics on execution.
Interface of the message passing manager.
Interface of a profiler for message exchanges.
Definition IProfiler.h:32
Interface for a request creator.
Definition Request.h:57
Information about the source of a message.
Abstraction interface for MPI operations. Primarily used to employ a decorator for MPI functions in o...
MpiAdapter(ITraceMng *msg, IStat *stat, MPI_Comm comm, MpiLock *mpi_lock, IMpiProfiling *mpi_prof=nullptr)
void setTraceMPIMessage(bool v)
Indicates if messages are displayed for each MPI call.
Definition MpiAdapter.h:192
int commRank() const
Rank of this instance in the communicator.
Definition MpiAdapter.h:149
int commSize() const
Number of ranks in the communicator.
Definition MpiAdapter.h:152
void destroy()
Destroys the instance. It should no longer be used afterward.
MPI_Comm m_communicator
MPI Communicator.
Definition MpiAdapter.h:228
Request sendNonBlockingNoStat(const void *send_buffer, Int64 send_buffer_size, Int32 proc, MPI_Datatype data_type, int mpi_tag)
Non-blocking version of send without temporal statistics.
MPI_Request m_empty_request1
Empty requests. See MpiAdapter.cc for more information.
Definition MpiAdapter.h:236
void _addRequest(MPI_Request request)
Request receiveNonBlockingNoStat(void *recv_buffer, Int64 recv_buffer_size, Int32 source_rank, MPI_Datatype data_type, int mpi_tag)
Non-blocking version of receive without temporal statistics.
void _removeRequest(MPI_Request request)
Information for sending/receiving a point-to-point message.
Reference to an instance.
TraceAccessor(ITraceMng *m)
Constructs an accessor via the trace manager m.
1D data vector with value semantics (STL style).
std::int64_t Int64
Signed integer type of 64 bits.
std::int32_t Int32
Signed integer type of 32 bits.