Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
GatherMessageInfo.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/* GatherMessageInfo.h (C) 2000-2025 */
9/* */
10/* Information for 'gather' messages. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_MESSAGEPASSING_GATHERMESSAGEINFO_H
13#define ARCCORE_MESSAGEPASSING_GATHERMESSAGEINFO_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arccore/collections/Array.h"
18#include "arccore/message_passing/MessageRank.h"
19
20/*---------------------------------------------------------------------------*/
21/*---------------------------------------------------------------------------*/
22
24{
25
26/*---------------------------------------------------------------------------*/
27/*---------------------------------------------------------------------------*/
28
29/*!
30 * \brief Brief information for a 'gather' message.
31 *
32 * It is better to use the GatherMessageInfo class rather than this class.
33 * This class allows using 'Gather', 'GatherVariable',
34 * 'AllGather', and 'AllGatherVariable' messages generically.
35 */
36class ARCCORE_MESSAGEPASSING_EXPORT GatherMessageInfoBase
37{
38 public:
39
40 //! Message mode
41 enum class Mode
42 {
43 Gather,
44 GatherVariable,
45 GatherVariableNeedComputeInfo,
46 Null
47 };
48
49 public:
50
51 //! Message for everyone and blocking
53
54 //! Blocking message having destination \a rank
56 : m_destination_rank(dest_rank)
57 {}
58
59 //! Message having destination \a dest_rank and blocking mode \a blocking_type
61 : m_destination_rank(dest_rank)
62 , m_is_blocking(blocking_type == Blocking)
63 {}
64
65 public:
66
67 void setBlocking(bool is_blocking)
68 {
69 m_is_blocking = is_blocking;
70 }
71 //! Indicates if the message is blocking.
72 bool isBlocking() const { return m_is_blocking; }
73
74 //! Rank of the message destination
75 MessageRank destinationRank() const { return m_destination_rank; }
76
77 //! Sets the rank of the message destination
79 {
80 m_destination_rank = rank;
81 }
82
83 //! Message mode
84 Mode mode() const { return m_mode; }
85
86 //! Prints the message
87 void print(std::ostream& o) const;
88
89 friend std::ostream& operator<<(std::ostream& o, const GatherMessageInfoBase& pmessage)
90 {
91 pmessage.print(o);
92 return o;
93 }
94
95 public:
96
97 // Indicates if the message is valid (i.e.: it has been initialized
98 // with a valid message)
99 bool isValid() const
100 {
101 if (m_mode == Mode::Null)
102 return false;
103 return true;
104 }
105
106 protected:
107
108 void _setType(Mode t)
109 {
110 m_mode = t;
111 }
112
113 private:
114
115 MessageRank m_destination_rank;
116 bool m_is_blocking = true;
117 Mode m_mode = Mode::Null;
118};
119
120/*---------------------------------------------------------------------------*/
121/*---------------------------------------------------------------------------*/
122
123/*!
124 * \brief Brief information for a 'gather' message for data type \a DataType.
125 *
126 * One of the setGather() or setGatherVariable() methods must be called before
127 * sending the corresponding message. The instances passed as arguments
128 * to these two methods must remain alive until the message is complete.
129 */
130template <typename DataType>
133{
134 public:
135
136 using BaseClass = GatherMessageInfoBase;
137
138 public:
139
140 //! Message for everyone and blocking
141 GatherMessageInfo() = default;
142
143 //! Blocking message having destination \a rank
144 explicit GatherMessageInfo(MessageRank dest_rank)
145 : BaseClass(dest_rank)
146 {}
147
148 //! Message having destination \a dest_rank and blocking mode \a blocking_type
150 : BaseClass(dest_rank, blocking_type)
151 {}
152
153 public:
154
155 /*!
156 * \brief Brief message equivalent to MPI_Gather or MPI_Allgather.
157 *
158 * All ranks must provide a valid value for \a send_buf.
159 * Only the destination rank must provide \a receive_buf. \a receive_buf
160 * must be able to accommodate the size send_buf.size() * nb_rank.
161 */
163 {
164 _setType(Mode::Gather);
165 m_receive_buf = receive_buf;
166 m_send_buffer = send_buf;
167 }
168
169 /*!
170 * \brief Brief message equivalent to MPI_Gatherv or MPI_Allgatherv.
171 *
172 * This prototype is used when it is unknown what each rank will send. If this
173 * information is known, it is preferable to use
174 * the setGatherVariable() method containing the displacements and message size
175 * of each participant.
176 *
177 * Calling this method triggers a call to mpGather() to determine what
178 * each participant must send. For this reason, it cannot be used
179 * in blocking mode.
180 *
181 * Only the destination rank must provide \a receive_array. For others, it
182 * is possible to use \a nullptr.
183 */
185 {
186 _setType(Mode::GatherVariableNeedComputeInfo);
187 m_local_reception_buffer = receive_array;
188 m_send_buffer = send_buf;
189 }
190
191 /*!
192 * \brief Brief message equivalent to MPI_Gatherv or MPI_Allgatherv.
193 *
194 * All ranks must provide a valid value for \a send_buf,
195 * \a receive_counts and \a receive_displacements.
196 * Only the destination rank must provide \a receive_buf. \a receive_buf
197 * must be able to accommodate the size send_buf.size() * nb_rank.
198 */
200 Span<const Int32> receive_counts, Span<const Int32> receive_displacements)
201 {
202 _setType(Mode::GatherVariable);
203 m_receive_buf = receive_buf;
204 m_send_buffer = send_buf;
205 m_receive_displacements = receive_displacements;
206 m_receive_counts = receive_counts;
207 }
208
209 /*!
210 * \brief Receive buffer for the T_GatherVariableNeedComputeInfo type.
211 *
212 * May be null for ranks not involved in reception.
213 */
214 Array<DataType>* localReceptionBuffer() const { return m_local_reception_buffer; }
215
216 //! Send buffer. It is used in all modes.
217 Span<const DataType> sendBuffer() const { return m_send_buffer; }
218
219 //! Receive buffer. Used in Gather and GatherVariable mode by ranks that receive
220 Span<DataType> receiveBuffer() const { return m_receive_buf; }
221
222 //! Displacement array. Used in GatherVariable mode.
223 Span<const Int32> receiveDisplacement() { return m_receive_displacements; }
224
225 //! Counts array. Used in GatherVariable mode.
226 Span<const Int32> receiveCounts() const { return m_receive_counts; }
227
228 private:
229
230 Array<DataType>* m_local_reception_buffer = nullptr;
231 Span<const DataType> m_send_buffer;
232 Span<DataType> m_receive_buf;
233 Span<const Int32> m_receive_displacements;
234 Span<const Int32> m_receive_counts;
235};
236
237/*---------------------------------------------------------------------------*/
238/*---------------------------------------------------------------------------*/
239
240} // namespace Arcane::MessagePassing
241
242/*---------------------------------------------------------------------------*/
243/*---------------------------------------------------------------------------*/
244
245#endif
Base class for 1D data vectors.
Brief information for a 'gather' message.
GatherMessageInfoBase()=default
Message for everyone and blocking.
bool isBlocking() const
Indicates if the message is blocking.
void print(std::ostream &o) const
Prints the message.
GatherMessageInfoBase(MessageRank dest_rank)
Blocking message having destination rank.
void setDestinationRank(MessageRank rank)
Sets the rank of the message destination.
GatherMessageInfoBase(MessageRank dest_rank, eBlockingType blocking_type)
Message having destination dest_rank and blocking mode blocking_type.
MessageRank destinationRank() const
Rank of the message destination.
Span< const DataType > sendBuffer() const
Send buffer. It is used in all modes.
GatherMessageInfo()=default
Message for everyone and blocking.
Array< DataType > * localReceptionBuffer() const
Receive buffer for the T_GatherVariableNeedComputeInfo type.
GatherMessageInfo(MessageRank dest_rank, eBlockingType blocking_type)
Message having destination dest_rank and blocking mode blocking_type.
Span< const Int32 > receiveCounts() const
Counts array. Used in GatherVariable mode.
Span< DataType > receiveBuffer() const
Receive buffer. Used in Gather and GatherVariable mode by ranks that receive.
void setGatherVariable(Span< const DataType > send_buf, Array< DataType > *receive_array)
Brief message equivalent to MPI_Gatherv or MPI_Allgatherv.
GatherMessageInfo(MessageRank dest_rank)
Blocking message having destination rank.
void setGather(Span< const DataType > send_buf, Span< DataType > receive_buf)
Brief message equivalent to MPI_Gather or MPI_Allgather.
Span< const Int32 > receiveDisplacement()
Displacement array. Used in GatherVariable mode.
void setGatherVariable(Span< const DataType > send_buf, Span< DataType > receive_buf, Span< const Int32 > receive_counts, Span< const Int32 > receive_displacements)
Brief message equivalent to MPI_Gatherv or MPI_Allgatherv.
View of an array of elements of type T.
Definition Span.h:635
Declarations of types and methods used by message exchange mechanisms.
eBlockingType
Type indicating whether a message is blocking or not.