Arcane  v3.15.3.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
MpiParallelDispatch.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2025 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/* MpiParallelDispatch.cc (C) 2000-2025 */
9/* */
10/* Gestionnaire de parallélisme utilisant MPI. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/ArcanePrecomp.h"
15
16#include "arcane/utils/Array.h"
17#include "arcane/utils/PlatformUtils.h"
18#include "arcane/utils/String.h"
19#include "arcane/utils/ITraceMng.h"
20#include "arcane/utils/Real2.h"
21#include "arcane/utils/Real3.h"
22#include "arcane/utils/Real2x2.h"
23#include "arcane/utils/Real3x3.h"
24#include "arcane/utils/HPReal.h"
25#include "arcane/utils/APReal.h"
26
27#include "arcane/IParallelMng.h"
28
29#include "arcane/parallel/mpi/MpiDatatype.h"
30#include "arcane/parallel/mpi/MpiAdapter.h"
31#include "arcane/parallel/mpi/MpiParallelDispatch.h"
32#include "arcane/parallel/mpi/MpiLock.h"
33
35
36#include "arccore/message_passing_mpi/MpiTypeDispatcherImpl.h"
37
38#include <limits>
39
40/*---------------------------------------------------------------------------*/
41/*---------------------------------------------------------------------------*/
42
43namespace Arcane
44{
45
46namespace MP = ::Arccore::MessagePassing;
47
48/*---------------------------------------------------------------------------*/
49/*---------------------------------------------------------------------------*/
50
51template<class Type> MpiParallelDispatchT<Type>::
52MpiParallelDispatchT(ITraceMng* tm,IMessagePassingMng* parallel_mng,MpiAdapter* adapter,MpiDatatype* datatype)
53: TraceAccessor(tm)
54, m_mp_dispatcher(new MP::Mpi::MpiTypeDispatcher<Type>(parallel_mng,adapter,datatype))
55, m_min_max_sum_datatype(MPI_DATATYPE_NULL)
56, m_min_max_sum_operator(MPI_OP_NULL)
57{
58 _initialize();
59}
60
61/*---------------------------------------------------------------------------*/
62/*---------------------------------------------------------------------------*/
63
64template<class Type> MpiParallelDispatchT<Type>::
65~MpiParallelDispatchT()
66{
67 finalize();
68 delete m_mp_dispatcher;
69}
70
71/*---------------------------------------------------------------------------*/
72/*---------------------------------------------------------------------------*/
73
74template<class Type> void MpiParallelDispatchT<Type>::
75finalize()
76{
77 if (m_min_max_sum_datatype!=MPI_DATATYPE_NULL){
78 MPI_Type_free(&m_min_max_sum_datatype);
79 m_min_max_sum_datatype = MPI_DATATYPE_NULL;
80 }
81 if (m_min_max_sum_operator!=MPI_OP_NULL){
82 MPI_Op_free(&m_min_max_sum_operator);
83 m_min_max_sum_operator = MPI_OP_NULL;
84 }
85}
86
87/*---------------------------------------------------------------------------*/
88/*---------------------------------------------------------------------------*/
89
90template<class Type> void MpiParallelDispatchT<Type>::
91_initialize()
92{
93 MinMaxSumInfo mmsi;
94
95 int blen[2];
96 MPI_Aint indices[2];
97 MPI_Datatype oldtypes[2];
98
99 blen[0] = 2;
100 indices[0] = 0;
101 oldtypes[0] = MpiBuiltIn::datatype(Integer());
102
103 blen[1] = 3;
104 indices[1] = (char*)&mmsi.m_min_value - (char*)&mmsi;
105 oldtypes[1] = _mpiDatatype();
106
107 MPI_Type_create_struct(2,blen,indices,oldtypes,&m_min_max_sum_datatype);
108 MPI_Type_commit(&m_min_max_sum_datatype);
109
110 MPI_Op_create(_MinMaxSumOperator,1,&m_min_max_sum_operator);
111}
112
113/*---------------------------------------------------------------------------*/
114/*---------------------------------------------------------------------------*/
115
116template<class Type> void ARCANE_MPIOP_CALL MpiParallelDispatchT<Type>::
117_MinMaxSumOperator(void* a,void* b, int* len,MPI_Datatype* type)
118{
119 ARCANE_UNUSED(type);
120
121 Integer n = *len;
122 MinMaxSumInfo * va = static_cast<MinMaxSumInfo*>(a);
123 MinMaxSumInfo * vb = static_cast<MinMaxSumInfo*>(b);
124 for(Integer i=0;i<n;++i) {
125 MinMaxSumInfo& ma = va[i];
126 MinMaxSumInfo& mb = vb[i];
127 // Il faut bien etre certain qu'en cas de valeurs egales
128 // le rang retourne est le meme pour tout le monde
129 if (ma.m_min_value==mb.m_min_value){
130 mb.m_min_rank = math::min(mb.m_min_rank,ma.m_min_rank);
131 }
132 else if (ma.m_min_value<mb.m_min_value){
133 mb.m_min_value = ma.m_min_value;
134 mb.m_min_rank = ma.m_min_rank;
135 }
136 if (mb.m_max_value==ma.m_max_value){
137 mb.m_max_rank = math::min(mb.m_max_rank,ma.m_max_rank);
138 }
139 else if (mb.m_max_value<ma.m_max_value){
140 mb.m_max_value = ma.m_max_value;
141 mb.m_max_rank = ma.m_max_rank;
142 }
143 mb.m_sum_value = (Type)(ma.m_sum_value + mb.m_sum_value);
144 }
145}
146
147/*---------------------------------------------------------------------------*/
148/*---------------------------------------------------------------------------*/
149
150template<class Type> void MpiParallelDispatchT<Type>::
151computeMinMaxSumNoInit(Type& min_val,Type& max_val,Type& sum_val,
152 Int32& min_rank,Int32& max_rank)
153{
154 MinMaxSumInfo mmsi;
155 mmsi.m_min_rank = min_rank;
156 mmsi.m_max_rank = max_rank;
157 mmsi.m_min_value = min_val;
158 mmsi.m_max_value = max_val;
159 mmsi.m_sum_value = sum_val;
160 MinMaxSumInfo mmsi_ret;
161 _adapter()->allReduce(&mmsi,&mmsi_ret,1,m_min_max_sum_datatype,
162 m_min_max_sum_operator);
163 min_val = mmsi_ret.m_min_value;
164 max_val = mmsi_ret.m_max_value;
165 sum_val = mmsi_ret.m_sum_value;
166 min_rank = mmsi_ret.m_min_rank;
167 max_rank = mmsi_ret.m_max_rank;
168}
169
170/*---------------------------------------------------------------------------*/
171/*---------------------------------------------------------------------------*/
172
173template<class Type> void MpiParallelDispatchT<Type>::
174computeMinMaxSum(Type val,Type& min_val,Type& max_val,Type& sum_val,
175 Int32& min_rank,Int32& max_rank)
176{
177 min_rank = _adapter()->commRank();
178 max_rank = _adapter()->commRank();
179 min_val = val;
180 max_val = val;
181 sum_val = val;
182 computeMinMaxSumNoInit(min_val,max_val,sum_val,min_rank,max_rank);
183}
184
185/*---------------------------------------------------------------------------*/
186/*---------------------------------------------------------------------------*/
187
188template<class Type> void MpiParallelDispatchT<Type>::
189computeMinMaxSum(ConstArrayView<Type> values,
190 ArrayView<Type> min_values,
191 ArrayView<Type> max_values,
192 ArrayView<Type> sum_values,
193 ArrayView<Int32> min_ranks,
194 ArrayView<Int32> max_ranks)
195{
196 const Integer n = values.size();
197 UniqueArray<MinMaxSumInfo> mmsi(n);
198 const Integer comm_rank = m_mp_dispatcher->adapter()->commRank();
199 for(Integer i=0;i<n;++i) {
200 mmsi[i].m_min_rank = comm_rank;
201 mmsi[i].m_max_rank = comm_rank;
202 mmsi[i].m_min_value = values[i];
203 mmsi[i].m_max_value = values[i];
204 mmsi[i].m_sum_value = values[i];
205 }
206 UniqueArray<MinMaxSumInfo> mmsi_ret(n);
207 _adapter()->allReduce(mmsi.data(),mmsi_ret.data(),n,m_min_max_sum_datatype,
208 m_min_max_sum_operator);
209 for(Integer i=0;i<n;++i) {
210 min_values[i] = mmsi_ret[i].m_min_value;
211 max_values[i] = mmsi_ret[i].m_max_value;
212 sum_values[i] = mmsi_ret[i].m_sum_value;
213 min_ranks[i] = mmsi_ret[i].m_min_rank;
214 max_ranks[i] = mmsi_ret[i].m_max_rank;
215 }
216}
217
218/*---------------------------------------------------------------------------*/
219/*---------------------------------------------------------------------------*/
220
221template<class Type> void MpiParallelDispatchT<Type>::
222sendRecv(ConstArrayView<Type> send_buffer,ArrayView<Type> recv_buffer,Int32 rank)
223{
224 MPI_Datatype type = _mpiDatatype();
225 _adapter()->directSendRecv(send_buffer.data(),send_buffer.size(),
226 recv_buffer.data(),recv_buffer.size(),
227 rank,sizeof(Type),type);
228}
229
230/*---------------------------------------------------------------------------*/
231/*---------------------------------------------------------------------------*/
232
233template<class Type> Type MpiParallelDispatchT<Type>::
234scan(eReduceType op,Type send_buf)
235{
236 MPI_Datatype type = _mpiDatatype();
237 Type recv_buf = send_buf;
238 _adapter()->scan(&send_buf,&recv_buf,1,type,_mpiReduceOperator(op));
239 return recv_buf;
240}
241
242/*---------------------------------------------------------------------------*/
243/*---------------------------------------------------------------------------*/
244
245template<class Type> void MpiParallelDispatchT<Type>::
246scan(eReduceType op,ArrayView<Type> send_buf)
247{
248 MPI_Datatype type = _mpiDatatype();
249 Integer s = send_buf.size();
250 UniqueArray<Type> recv_buf(s);
251 _adapter()->scan(send_buf.data(),recv_buf.data(),s,type,_mpiReduceOperator(op));
252 send_buf.copy(recv_buf);
253}
254
255/*---------------------------------------------------------------------------*/
256/*---------------------------------------------------------------------------*/
257
258template<class Type> MPI_Datatype MpiParallelDispatchT<Type>::
259_mpiDatatype()
260{
261 return m_mp_dispatcher->datatype()->datatype();
262}
263
264/*---------------------------------------------------------------------------*/
265/*---------------------------------------------------------------------------*/
266
267template<class Type> MPI_Op MpiParallelDispatchT<Type>::
268_mpiReduceOperator(eReduceType rt)
269{
270 return m_mp_dispatcher->datatype()->reduceOperator(rt);
271}
272
273/*---------------------------------------------------------------------------*/
274/*---------------------------------------------------------------------------*/
275
276template<class Type> MpiAdapter* MpiParallelDispatchT<Type>::
277_adapter()
278{
279 return m_mp_dispatcher->adapter();
280}
281
282/*---------------------------------------------------------------------------*/
283/*---------------------------------------------------------------------------*/
284
285template<class Type> MpiDatatype* MpiParallelDispatchT<Type>::
286datatype() const
287{
288 return m_mp_dispatcher->datatype();
289}
290
291template<class Type> ITypeDispatcher<Type>* MpiParallelDispatchT<Type>::
292toArccoreDispatcher()
293{
294 return m_mp_dispatcher;
295}
296
297/*---------------------------------------------------------------------------*/
298/*---------------------------------------------------------------------------*/
299
300template class MpiParallelDispatchT<char>;
301template class MpiParallelDispatchT<signed char>;
302template class MpiParallelDispatchT<unsigned char>;
303template class MpiParallelDispatchT<short>;
304template class MpiParallelDispatchT<unsigned short>;
305template class MpiParallelDispatchT<int>;
306template class MpiParallelDispatchT<unsigned int>;
307template class MpiParallelDispatchT<long>;
308template class MpiParallelDispatchT<unsigned long>;
309template class MpiParallelDispatchT<long long>;
310template class MpiParallelDispatchT<unsigned long long>;
311template class MpiParallelDispatchT<float>;
312template class MpiParallelDispatchT<double>;
313template class MpiParallelDispatchT<long double>;
314template class MpiParallelDispatchT<APReal>;
315template class MpiParallelDispatchT<Real2>;
316template class MpiParallelDispatchT<Real3>;
317template class MpiParallelDispatchT<Real2x2>;
318template class MpiParallelDispatchT<Real3x3>;
319template class MpiParallelDispatchT<HPReal>;
320
321/*---------------------------------------------------------------------------*/
322/*---------------------------------------------------------------------------*/
323
324}
325
326/*---------------------------------------------------------------------------*/
327/*---------------------------------------------------------------------------*/
328
329namespace Arcane::MessagePassing::Mpi
330{
331using namespace Arcane;
332template class MpiTypeDispatcher<APReal>;
333template class MpiTypeDispatcher<Real2>;
334template class MpiTypeDispatcher<Real3>;
335template class MpiTypeDispatcher<Real2x2>;
336template class MpiTypeDispatcher<Real3x3>;
337template class MpiTypeDispatcher<HPReal>;
338}
339
340/*---------------------------------------------------------------------------*/
341/*---------------------------------------------------------------------------*/
Liste des fonctions d'échange de message.
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:149
Integer len(const char *s)
Retourne la longueur de la chaîne s.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
Espace de nommage contenant les types et déclarations qui gèrent le mécanisme de parallélisme par éch...
Int32 Integer
Type représentant un entier.
Type
Type of JSON value.
Definition rapidjson.h:665