Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
SharedMemoryParallelDispatch.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/* SharedMemoryParallelDispatch.h (C) 2000-2024 */
9/* */
10/* Implementation of messages in shared memory. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_PARALLEL_THREAD_SHAREDMEMORYPARALLELDISPATCH_H
13#define ARCANE_PARALLEL_THREAD_SHAREDMEMORYPARALLELDISPATCH_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/utils/TraceAccessor.h"
18#include "arcane/utils/Array.h"
19#include "arcane/utils/IThreadBarrier.h"
20#include "arcane/utils/HPReal.h"
21#include "arcane/utils/DataTypeContainer.h"
22#include "arcane/utils/MemoryView.h"
23
24#include "arcane/core/IParallelDispatch.h"
25#include "arcane/core/ISerializer.h"
26
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
33{
34
35/*---------------------------------------------------------------------------*/
36/*---------------------------------------------------------------------------*/
37
41template <class Type>
43
44namespace impl
45{
46 template <typename DataType>
48 {
49 public:
50
51 void resize(Int64 n)
52 {
53 all_dispatchs.resize(n);
54 all_dispatchs_base.resize(n);
55 }
56
57 public:
58
61 };
62} // namespace impl
63
64/*---------------------------------------------------------------------------*/
65/*---------------------------------------------------------------------------*/
66
67template <typename DataType>
69{
70 public:
71
73};
74
75/*---------------------------------------------------------------------------*/
76/*---------------------------------------------------------------------------*/
77
79: public ArcaneDataTypeContainer<ThreadDispatcherContainerTraits>
80{
81 public:
82
85 {
86 public:
87
89 };
90
91 public:
92
93 void resize(Integer n)
94 {
95 m_char.resize(n);
96 m_signed_char.resize(n);
97 m_unsigned_char.resize(n);
98 m_short.resize(n);
99 m_unsigned_short.resize(n);
100 m_int.resize(n);
101 m_unsigned_int.resize(n);
102 m_long.resize(n);
103 m_unsigned_long.resize(n);
104 m_long_long.resize(n);
105 m_unsigned_long_long.resize(n);
106 m_float.resize(n);
107 m_double.resize(n);
108 m_long_double.resize(n);
109 m_apreal.resize(n);
110 m_real2.resize(n);
111 m_real3.resize(n);
112 m_real2x2.resize(n);
113 m_real3x3.resize(n);
114 m_hpreal.resize(n);
115 }
116
117 public:
118
119 CreateSubParallelMngInfo m_create_sub_parallel_mng_info;
120};
121
122/*---------------------------------------------------------------------------*/
123/*---------------------------------------------------------------------------*/
124
128class SharedMemoryParallelDispatchBase
129: public TraceAccessor
131{
133
134 protected:
135
136 template <typename DataType> friend class SharedMemoryParallelDispatch;
137 using Request = Parallel::Request;
138 using PointToPointMessageInfo = Parallel::PointToPointMessageInfo;
139 using MessageRank = Parallel::MessageRank;
140 using MessageTag = Parallel::MessageTag;
141
142 protected:
143
145 {
146 public:
147
148 virtual ~IResizableArray() = default;
149
150 public:
151
152 virtual void resize(Int64 new_size) = 0;
153 virtual MutableMemoryView memoryView() const = 0;
154 };
155
157 {
158 public:
159
160 ConstMemoryView send_buf;
161 Span<const Int32> send_count;
162 Span<const Int32> send_index;
163 MutableMemoryView recv_buf;
164 Span<const Int32> recv_count;
165 Span<const Int32> recv_index;
166 };
167
168 protected:
169
170 SharedMemoryParallelDispatchBase(ITraceMng* tm, SharedMemoryParallelMng* parallel_mng,
171 ISharedMemoryMessageQueue* message_queue,
173
174 protected:
175
176 void _genericAllToAll(ConstMemoryView send_buf, MutableMemoryView recv_buf, Int32 count);
177 void _genericAllGather(ConstMemoryView send_buf, MutableMemoryView recv_buf);
178 void _genericAllGatherVariable(ConstMemoryView send_buf, IResizableArray* recv_buf);
179 void _genericAllToAllVariable(ConstMemoryView send_buf,
180 Span<const Int32> send_count, Span<const Int32> send_index,
181 MutableMemoryView recv_buf,
182 Span<const Int32> recv_count, Span<const Int32> recv_index);
183 void _genericScatterVariable(ConstMemoryView send_buf, MutableMemoryView recv_buf, Int32 root);
184 Request _genericSend(ConstMemoryView send_buffer, const PointToPointMessageInfo& message2);
185 Request _genericReceive(MutableMemoryView recv_buffer, const PointToPointMessageInfo& message2);
186 void _genericBroadcast(MutableMemoryView send_buf, Int32 rank);
187
188 protected:
189
190 // Accessible par 'SharedMemoryParallelDispatch'
191 ConstMemoryView m_const_view;
192 MutableMemoryView m_recv_view;
193 ConstMemoryView m_send_view;
194
195 private:
196
197 MutableMemoryView m_broadcast_view;
198
199 protected:
200
201 SharedMemoryParallelMng* m_parallel_mng = nullptr;
202 Int32 m_rank = -1;
203 Int32 m_nb_rank = 0;
204 ISharedMemoryMessageQueue* m_message_queue = nullptr;
205
206 protected:
207
208 void _collectiveBarrier();
209
210 private:
211
212 AllToAllVariableInfo m_alltoallv_infos;
214};
215
216/*---------------------------------------------------------------------------*/
217/*---------------------------------------------------------------------------*/
218
222template <class Type>
223class SharedMemoryParallelDispatch
224: public SharedMemoryParallelDispatchBase
225, public IParallelDispatchT<Type>
226, public ITypeDispatcher<Type>
227{
228 using BaseClass = SharedMemoryParallelDispatchBase;
229
230 class ResizableArrayRef : public IResizableArray
231 {
232 public:
233
234 ResizableArrayRef(Array<Type>& v)
235 : m_array_ref(v)
236 {}
237
238 public:
239
240 virtual void resize(Int64 new_size) { m_array_ref.resize(new_size); }
241 virtual MutableMemoryView memoryView() const { return MutableMemoryView(m_array_ref.span()); }
242
243 private:
244
245 Array<Type>& m_array_ref;
246 };
247
249 {
250 public:
251
252 Span<Type> reduce_buf;
253 Type reduce_value;
254 int m_index;
255 };
256
257 private:
258
260 {
261 public:
262
263 Int32 m_min_rank;
264 Int32 m_max_rank;
265 Type m_min_value;
266 Type m_max_value;
267 Type m_sum_value;
268 };
269
270 public:
271
272 using Request = Parallel::Request;
273 using PointToPointMessageInfo = Parallel::PointToPointMessageInfo;
274 using MessageRank = Parallel::MessageRank;
275 using MessageTag = Parallel::MessageTag;
276 typedef Parallel::eReduceType eReduceType;
277
278 public:
279
280 SharedMemoryParallelDispatch(ITraceMng* tm, SharedMemoryParallelMng* parallel_mng,
281 ISharedMemoryMessageQueue* message_queue,
283 ~SharedMemoryParallelDispatch() override;
284 void finalize() override;
285
286 public:
287
289 void broadcast(Span<Type> send_buf, Int32 sub_domain) override;
290 void allGather(Span<const Type> send_buf, Span<Type> recv_buf) override;
291 void allGatherVariable(Span<const Type> send_buf, Array<Type>& recv_buf) override;
292 void gather(Span<const Type> send_buf, Span<Type> recv_buf, Int32 rank) override;
293 void gatherVariable(Span<const Type> send_buf, Array<Type>& recv_buf, Int32 rank) override;
294 void scatterVariable(Span<const Type> send_buf, Span<Type> recv_buf, Int32 root) override;
295 void allReduce(eReduceType op, Span<Type> send_buf) override;
296 void allToAll(Span<const Type> send_buf, Span<Type> recv_buf, Int32 count) override;
297 void allToAllVariable(Span<const Type> send_buf, ConstArrayView<Int32> send_count,
298 ConstArrayView<Int32> send_index, Span<Type> recv_buf,
299 ConstArrayView<Int32> recv_count, ConstArrayView<Int32> recv_index) override;
300 Request send(Span<const Type> send_buffer, Int32 proc, bool is_blocked) override;
301 Request send(Span<const Type> send_buffer, const PointToPointMessageInfo& message) override;
302 Request receive(Span<Type> recv_buffer, Int32 rank, bool is_blocked) override;
303 Request receive(Span<Type> recv_buffer, const PointToPointMessageInfo& message) override;
304 Request nonBlockingAllReduce(eReduceType op, Span<const Type> send_buf, Span<Type> recv_buf) override;
305 Request nonBlockingAllGather(Span<const Type> send_buf, Span<Type> recv_buf) override;
306 Request nonBlockingBroadcast(Span<Type> send_buf, Int32 rank) override;
307 Request nonBlockingGather(Span<const Type> send_buf, Span<Type> recv_buf, Int32 rank) override;
308 Request nonBlockingAllToAll(Span<const Type> send_buf, Span<Type> recv_buf, Int32 count) override;
309 Request nonBlockingAllToAllVariable(Span<const Type> send_buf, ConstArrayView<Int32> send_count,
310 ConstArrayView<Int32> send_index, Span<Type> recv_buf,
311 ConstArrayView<Int32> recv_count, ConstArrayView<Int32> recv_index) override;
312 Request gather(Arccore::MessagePassing::GatherMessageInfo<Type>&) override;
314
315 void broadcast(ArrayView<Type> send_buf, Integer sub_domain) override
316 {
317 this->broadcast(Span<Type>(send_buf), sub_domain);
318 }
319 void allGather(ConstArrayView<Type> send_buf, ArrayView<Type> recv_buf) override
320 {
321 this->allGather(Span<const Type>(send_buf), Span<Type>(recv_buf));
322 }
323 void allGatherVariable(ConstArrayView<Type> send_buf, Array<Type>& recv_buf) override
324 {
325 this->allGatherVariable(Span<const Type>(send_buf), recv_buf);
326 }
327 void gather(ConstArrayView<Type> send_buf, ArrayView<Type> recv_buf, Integer rank) override
328 {
329 this->gather(Span<const Type>(send_buf), Span<Type>(recv_buf), rank);
330 }
331 void gatherVariable(ConstArrayView<Type> send_buf, Array<Type>& recv_buf, Integer rank) override
332 {
333 this->gatherVariable(Span<const Type>(send_buf), recv_buf, rank);
334 }
335 void scatterVariable(ConstArrayView<Type> send_buf, ArrayView<Type> recv_buf, Integer root) override
336 {
337 this->scatterVariable(Span<const Type>(send_buf), Span<Type>(recv_buf), root);
338 }
339 void allToAll(ConstArrayView<Type> send_buf, ArrayView<Type> recv_buf, Integer count) override
340 {
341 this->allToAll(Span<const Type>(send_buf), Span<Type>(recv_buf), count);
342 }
343 void allToAllVariable(ConstArrayView<Type> send_buf, Int32ConstArrayView send_count,
344 Int32ConstArrayView send_index, ArrayView<Type> recv_buf,
345 Int32ConstArrayView recv_count, Int32ConstArrayView recv_index) override
346 {
347 this->allToAllVariable(Span<const Type>(send_buf), send_count, send_index,
348 Span<Type>(recv_buf), recv_count, recv_index);
349 }
350 Request send(ConstArrayView<Type> send_buffer, Integer proc, bool is_blocked) override
351 {
352 return this->send(Span<const Type>(send_buffer), proc, is_blocked);
353 }
354 Request recv(ArrayView<Type> recv_buffer, Int32 rank, bool is_blocked) override
355 {
356 return this->receive(Span<Type>(recv_buffer), rank, is_blocked);
357 }
358 void allReduce(eReduceType op, ArrayView<Type> send_buf) override
359 {
360 return this->allReduce(op, Span<Type>(send_buf));
361 }
362
363 void send(ConstArrayView<Type> send_buffer, Integer proc) override;
364 void recv(ArrayView<Type> recv_buffer, Integer proc) override;
365 void sendRecv(ConstArrayView<Type> send_buffer, ArrayView<Type> recv_buffer, Integer proc) override;
366 Type allReduce(eReduceType op, Type send_buf) override;
367 Type scan(eReduceType op, Type send_buf) override;
368 void scan(eReduceType op, ArrayView<Type> send_buf) override;
369 void computeMinMaxSum(Type val, Type& min_val, Type& max_val, Type& sum_val,
370 Int32& min_rank,
371 Int32& max_rank) override;
372 void computeMinMaxSum(ConstArrayView<Type> values,
373 ArrayView<Type> min_values,
374 ArrayView<Type> max_values,
375 ArrayView<Type> sum_values,
376 ArrayView<Int32> min_ranks,
377 ArrayView<Int32> max_ranks) override;
378 ITypeDispatcher<Type>* toArccoreDispatcher() override { return this; }
379
380 void waitAll();
381
382 public:
383
384 ArrayView<SharedMemoryParallelDispatch<Type>*> m_all_dispatchs;
385
386 private:
387 public:
388
389 ReduceInfo m_reduce_infos;
390
391 private:
392
393 void _allReduceOrScan(eReduceType op, Span<Type> send_buf, bool is_scan);
394};
395
396/*---------------------------------------------------------------------------*/
397/*---------------------------------------------------------------------------*/
398
399} // End namespace Arcane::MessagePassing
400
401/*---------------------------------------------------------------------------*/
402/*---------------------------------------------------------------------------*/
403
404#endif
#define ARCCORE_INTERNAL_DEFINE_REFERENCE_COUNTED_INCLASS_METHODS(OPTIONAL_OVERRIDE)
Container containing an instance of a class per data type Arcane.
Modifiable view of an array of type T.
Base class for 1D data vectors.
Constant view of an array of type T.
Constant view on a contiguous memory region containing fixed-size elements.
Parallel message handling for the type Type.
Brief information for a 'gather' message for data type DataType.
Parallel message handling for the type Type.
Information for sending/receiving a point-to-point message.
Base class common to SharedMemoryParallelDispatch.
Mutable view on a contiguous memory region containing fixed-size elements.
Reference to an instance.
Thread-safe implementation of a reference counter.
View of an array of elements of type T.
Definition Span.h:635
TraceAccessor(ITraceMng *m)
Constructs an accessor via the trace manager m.
1D data vector with value semantics (STL style).
Declarations of types and methods used by message exchange mechanisms.
eReduceType
Supported reduction types.
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.
ConstArrayView< Int32 > Int32ConstArrayView
C equivalent of a 1D array of 32-bit integers.
Definition UtilsTypes.h:482
std::int32_t Int32
Signed integer type of 32 bits.
Type
Type of JSON value.
Definition rapidjson.h:730