Arcane  4.1.12.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 directSendRecv(const void* send_buffer, Int64 send_buffer_size,
93 void* recv_buffer, Int64 recv_buffer_size,
94 Int32 proc, Int64 elem_size, MPI_Datatype data_type);
95
96 Request directSend(const void* send_buffer, Int64 send_buffer_size,
97 Int32 proc, Int64 elem_size, MPI_Datatype data_type,
98 int mpi_tag, bool is_blocked);
99
101 Request sendNonBlockingNoStat(const void* send_buffer, Int64 send_buffer_size,
102 Int32 proc, MPI_Datatype data_type, int mpi_tag);
103
104 Request directRecv(void* recv_buffer, Int64 recv_buffer_size,
105 Int32 source_rank, Int64 elem_size, MPI_Datatype data_type,
106 int mpi_tag, bool is_blocked);
107
109 Request receiveNonBlockingNoStat(void* recv_buffer, Int64 recv_buffer_size,
110 Int32 source_rank, MPI_Datatype data_type, int mpi_tag);
111
112 Request directSendPack(const void* send_buffer, Int64 send_buffer_size,
113 Int32 proc, int mpi_tag, bool is_blocked);
114
115 void probeRecvPack(UniqueArray<Byte>& recv_buffer, Int32 proc);
116
117 MessageId probeMessage(PointToPointMessageInfo message);
118
119 MessageSourceInfo legacyProbeMessage(PointToPointMessageInfo message);
120
121 Request directRecv(void* recv_buffer, Int64 recv_buffer_size,
122 MessageId message, Int64 elem_size, MPI_Datatype data_type,
123 bool is_blocked);
124
125 Request directRecvPack(void* recv_buffer, Int64 recv_buffer_size,
126 Int32 proc, int mpi_tag, bool is_blocking);
127
128 void waitAllRequests(ArrayView<Request> requests);
129
130 private:
131
132 bool _waitAllRequestsMPI(ArrayView<Request> requests, ArrayView<bool> indexes,
133 ArrayView<MPI_Status> mpi_status);
134
135 public:
136
137 void waitSomeRequests(ArrayView<Request> requests,
138 ArrayView<bool> indexes,
139 bool is_non_blocking);
140
141 void waitSomeRequestsMPI(ArrayView<Request> requests,
142 ArrayView<bool> indexes,
143 ArrayView<MPI_Status> mpi_status, bool is_non_blocking);
144
145 public:
146
148 int commRank() const { return m_comm_rank; }
149
151 int commSize() const { return m_comm_size; }
152
153 MpiMessagePassingMng* commSplit(bool keep);
154
155 void freeRequest(Request& request);
156 bool testRequest(Request& request);
157
158 void enableDebugRequest(bool enable_debug_request);
159
160 MpiLock* mpiLock() const { return m_mpi_lock; }
161
162 Request nonBlockingBroadcast(void* buf, Int64 nb_elem, Int32 root, MPI_Datatype datatype);
163 Request nonBlockingAllGather(const void* send_buf, void* recv_buf, Int64 nb_elem, MPI_Datatype datatype);
164 Request nonBlockingGather(const void* send_buf, void* recv_buf, Int64 nb_elem, Int32 root, MPI_Datatype datatype);
165
166 Request nonBlockingAllToAll(const void* send_buf, void* recv_buf, Int32 count, MPI_Datatype datatype);
167 Request nonBlockingAllReduce(const void* send_buf, void* recv_buf, Int64 count, MPI_Datatype datatype, MPI_Op op);
168 Request nonBlockingAllToAllVariable(const void* send_buf, const int* send_counts,
169 const int* send_indexes, void* recv_buf, const int* recv_counts,
170 const int* recv_indexes, MPI_Datatype datatype);
171
172 Request nonBlockingBarrier();
173 void barrier();
174
175 int toMPISize(Int64 count);
176
178 Request buildRequest(int ret, MPI_Request request);
179
180 public:
181
183 void setRequestErrorAreFatal(bool v);
184 bool isRequestErrorAreFatal() const;
185
187 void setPrintRequestError(bool v);
188 bool isPrintRequestError() const;
189
191 void setTraceMPIMessage(bool v) { m_is_trace = v; }
192 bool isTraceMPIMessage() const { return m_is_trace; }
193
199 void setCheckRequest(bool v);
200 bool isCheckRequest() const;
201
202 public:
203
204 void setMpiProfiling(IMpiProfiling* mpi_profiling);
205 void setProfiler(IProfiler* profiler);
206 IMpiProfiling* getMpiProfiling() const;
207 IProfiler* profiler() const;
208
209 public:
210
211 ITimeMetricCollector* timeMetricCollector() const { return m_metric_collector; }
212 void setTimeMetricCollector(ITimeMetricCollector* v) { m_metric_collector = v; }
213
214 bool isAllowNullRankForAnySource() const { return m_is_allow_null_rank_for_any_source; }
215
216 public:
217
218 void initializeWindowCreator(MPI_Comm comm_machine);
219 MpiMachineShMemWinBaseInternalCreator* windowCreator() const;
220
221 private:
222
223 IStat* m_stat = nullptr;
224 MpiLock* m_mpi_lock = nullptr;
225 IMpiProfiling* m_mpi_prof = nullptr;
226 ITimeMetricCollector* m_metric_collector = nullptr;
227 MPI_Comm m_communicator;
228 int m_comm_rank = A_PROC_NULL_RANK;
229 int m_comm_size = 0;
230 Int64 m_nb_all_reduce = 0;
231 Int64 m_nb_reduce = 0;
232 bool m_is_trace = false;
233 RequestSet* m_request_set = nullptr;
235 MPI_Request m_empty_request1;
236 MPI_Request m_empty_request2;
237 int m_recv_buffer_for_empty_request[1];
238 int m_send_buffer_for_empty_request2[1];
239 int m_recv_buffer_for_empty_request2[1];
240
241 // If true, allows using the null rank (A_NULL_RANK) to specify MPI_ANY_SOURCE
242 // This is the default in Arccore versions before July 2024.
243 // Starting from 2025, it will have to be prohibited.
244 // The environment variable ARCCORE_ALLOW_NULL_RANK_FOR_MPI_ANY_SOURCE will allow
245 // temporarily maintaining a compatible mode.
246 bool m_is_allow_null_rank_for_any_source = true;
247
249
250 private:
251
252 void _trace(const char* function);
253 void _addRequest(MPI_Request request);
254 void _removeRequest(MPI_Request request);
255 void _checkFatalInRequest();
256 MessageId _probeMessage(MessageRank source, MessageTag tag, bool is_blocking);
257 MessageSourceInfo _legacyProbeMessage(MessageRank source, MessageTag tag, bool is_blocking);
258 bool _handleEndRequests(ArrayView<Request> requests, ArrayView<bool> done_indexes,
259 ArrayView<MPI_Status> status);
260 void _checkHasNoRequests();
261 MessageSourceInfo _buildSourceInfoFromStatus(const MPI_Status& status);
262};
263
264/*---------------------------------------------------------------------------*/
265/*---------------------------------------------------------------------------*/
266
267} // namespace Arcane::MessagePassing::Mpi
268
269/*---------------------------------------------------------------------------*/
270/*---------------------------------------------------------------------------*/
271
272#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 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:191
int commRank() const
Rank of this instance in the communicator.
Definition MpiAdapter.h:148
int commSize() const
Number of ranks in the communicator.
Definition MpiAdapter.h:151
void destroy()
Destroys the instance. It should no longer be used afterward.
MPI_Comm m_communicator
MPI Communicator.
Definition MpiAdapter.h:227
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:235
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)
MPI implementation of the message exchange manager.
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.