Arcane  v3.14.10.0
Documentation utilisateur
Chargement...
Recherche...
Aucune correspondance
MpiDatatype.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2022 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/* MpiDatatype.h (C) 2000-2020 */
9/* */
10/* Encapsulation d'un MPI_Datatype. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_MESSAGEPASSINGMPI_MPIDATATYPE_H
13#define ARCCORE_MESSAGEPASSINGMPI_MPIDATATYPE_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arccore/message_passing_mpi/MessagePassingMpiGlobal.h"
18
19// TODO: a supprimer
20#include "arccore/base/FatalErrorException.h"
21
22#include <algorithm>
23
24/*---------------------------------------------------------------------------*/
25/*---------------------------------------------------------------------------*/
26
27namespace Arccore::MessagePassing::Mpi
28{
29
30/*---------------------------------------------------------------------------*/
31/*---------------------------------------------------------------------------*/
32
33//! Opérateurs de réduction pour les types complexes (Real2, Real3, Real2x2 et Real3x3)
35{
36 public:
37 virtual ~IMpiReduceOperator(){}
38 virtual MPI_Op reduceOperator(eReduceType rt) =0;
39};
40
41/*---------------------------------------------------------------------------*/
42/*---------------------------------------------------------------------------*/
43//! Opérateur de réduction interne MPI (MPI_MAX, MPI_MIN, MPI_SUM)
45: public IMpiReduceOperator
46{
47 public:
48 MPI_Op reduceOperator(eReduceType rt) override;
49};
50
51/*---------------------------------------------------------------------------*/
52/*---------------------------------------------------------------------------*/
53
54//! Opérateurs de réduction pour les types classiques
55template<typename RealType>
57: public IMpiReduceOperator
58{
59 public:
60 StdMpiReduceOperator(bool is_commutative);
61 void destroy();
62 MPI_Op reduceOperator(eReduceType rt) override;
63 private:
64 MPI_Op m_min_operator;
65 MPI_Op m_max_operator;
66 MPI_Op m_sum_operator;
67 private:
68 static void ARCCORE_MPIOP_CALL _MinOperator(void* a,void* b,int* len,MPI_Datatype* type);
69 static void ARCCORE_MPIOP_CALL _MaxOperator(void* a,void* b,int* len,MPI_Datatype* type);
70 static void ARCCORE_MPIOP_CALL _SumOperator(void* a,void* b,int* len,MPI_Datatype* type);
71 void _create(bool is_commutative);
72};
73
74
75template<typename RealType> inline
77StdMpiReduceOperator(bool is_commutative)
78{
79 m_min_operator = MPI_OP_NULL;
80 m_max_operator = MPI_OP_NULL;
81 m_sum_operator = MPI_OP_NULL;
82 _create(is_commutative);
83}
84
85template<typename RealType> inline
86void ARCCORE_MPIOP_CALL StdMpiReduceOperator<RealType>::
87_MinOperator(void* a,void* b,int* len,MPI_Datatype* type)
88{
89 ARCCORE_UNUSED(type);
90 RealType* ra = (RealType*)a;
91 RealType* rb = (RealType*)b;
92 Integer s = *len;
93 for( Integer i=0; i<s; ++i ){
94 RealType vb = rb[i];
95 RealType va = ra[i];
96 rb[i] = std::min(va,vb);
97 }
98}
99
100template<typename RealType> inline
101void ARCCORE_MPIOP_CALL StdMpiReduceOperator<RealType>::
102_MaxOperator(void* a,void* b,int* len,MPI_Datatype* type)
103{
104 ARCCORE_UNUSED(type);
105 RealType* ra = (RealType*)a;
106 RealType* rb = (RealType*)b;
107 Integer s = *len;
108 for( Integer i=0; i<s; ++i ){
109 RealType vb = rb[i];
110 RealType va = ra[i];
111 rb[i] = std::max(va,vb);
112 }
113}
114
115template<typename RealType> inline
116void ARCCORE_MPIOP_CALL StdMpiReduceOperator<RealType>::
117_SumOperator(void* a,void* b,int* len,MPI_Datatype* type)
118{
119 ARCCORE_UNUSED(type);
120 RealType* ra = (RealType*)a;
121 RealType* rb = (RealType*)b;
122 Integer s = *len;
123 for( Integer i=0; i<s; ++i ){
124 RealType vb = rb[i];
125 RealType va = ra[i];
126 rb[i] = va + vb;
127 }
128}
129
130template<typename RealType> inline
131void StdMpiReduceOperator<RealType>::
132_create(bool is_commutative)
133{
134 int commutative = (is_commutative) ? 1 : 0;
135 MPI_Op_create(_MinOperator,commutative,&m_min_operator);
136 MPI_Op_create(_MaxOperator,commutative,&m_max_operator);
137 MPI_Op_create(_SumOperator,commutative,&m_sum_operator);
138}
139
140template<typename RealType> inline
141void StdMpiReduceOperator<RealType>::
142destroy()
143{
144 if (m_min_operator!=MPI_OP_NULL){
145 MPI_Op_free(&m_min_operator);
146 m_min_operator = MPI_OP_NULL;
147 }
148 if (m_max_operator!=MPI_OP_NULL){
149 MPI_Op_free(&m_max_operator);
150 m_max_operator = MPI_OP_NULL;
151 }
152 if (m_sum_operator!=MPI_OP_NULL){
153 MPI_Op_free(&m_sum_operator);
154 m_sum_operator = MPI_OP_NULL;
155 }
156}
157
158/*---------------------------------------------------------------------------*/
159/*---------------------------------------------------------------------------*/
160
161template<typename RealType> inline
162MPI_Op StdMpiReduceOperator<RealType>::
163reduceOperator(eReduceType rt)
164{
165 MPI_Op op = MPI_OP_NULL;
166 switch(rt){
167 case ReduceMax: op = m_max_operator; break;
168 case ReduceMin: op = m_min_operator; break;
169 case ReduceSum: op = m_sum_operator; break;
170 }
171 if (op==MPI_OP_NULL)
172 ARCCORE_FATAL("Reduce operation unknown or not implemented");
173 return op;
174}
175
176/*---------------------------------------------------------------------------*/
177/*---------------------------------------------------------------------------*/
178/*!
179 * \internal
180 * \brief Encapsulation d'un MPI_Datatype.
181 */
182class ARCCORE_MESSAGEPASSINGMPI_EXPORT MpiDatatype
183{
184 public:
185
186 MpiDatatype(MPI_Datatype datatype);
187 MpiDatatype(MPI_Datatype datatype,bool is_built_in,IMpiReduceOperator* reduce_operator);
188 ~MpiDatatype();
189
190 public:
191
192 MPI_Op reduceOperator(eReduceType reduce_type)
193 {
194 return m_reduce_operator->reduceOperator(reduce_type);
195 }
196 MPI_Datatype datatype() const { return m_datatype; }
197
198 public:
199
200 private:
201
202 private:
203
204 MPI_Datatype m_datatype;
205 IMpiReduceOperator* m_reduce_operator;
206 bool m_is_built_in;
207
208 private:
209};
210
211/*---------------------------------------------------------------------------*/
212/*---------------------------------------------------------------------------*/
213
214} // End namespace Arccore::MessagePassing::Mpi
215
216/*---------------------------------------------------------------------------*/
217/*---------------------------------------------------------------------------*/
218
219#endif
Opérateur de réduction interne MPI (MPI_MAX, MPI_MIN, MPI_SUM)
Definition MpiDatatype.h:46
Opérateurs de réduction pour les types complexes (Real2, Real3, Real2x2 et Real3x3)
Definition MpiDatatype.h:35
Opérateurs de réduction pour les types classiques.
Definition MpiDatatype.h:58
Integer len(const char *s)
Retourne la longueur de la chaîne s.
eReduceType
Types des réductions supportées.
@ ReduceMin
Minimum des valeurs.
@ ReduceSum
Somme des valeurs.
@ ReduceMax
Maximum des valeurs.
Int32 Integer
Type représentant un entier.