Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix use after delete.
[simgrid.git] / src / smpi / mpi / smpi_op.cpp
1 /* Copyright (c) 2009-2019. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "smpi_op.hpp"
7 #include "private.hpp"
8 #include "smpi_datatype.hpp"
9 #include "src/smpi/include/smpi_actor.hpp"
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_op, smpi, "Logging specific to SMPI (op)");
12
13 #define MAX_OP(a, b)  (b) = (a) < (b) ? (b) : (a)
14 #define MIN_OP(a, b)  (b) = (a) < (b) ? (a) : (b)
15 #define SUM_OP(a, b)  (b) += (a)
16 #define SUM_OP_COMPLEX(a, b)                                                                                           \
17   {                                                                                                                    \
18     ((b).value) += ((a).value);                                                                                        \
19     ((b).index) += ((a).index);                                                                                        \
20   }
21 #define PROD_OP(a, b) (b) *= (a)
22 #define PROD_OP_COMPLEX(a, b)                                                                                          \
23   {                                                                                                                    \
24     ((b).value) *= ((a).value);                                                                                        \
25     ((b).index) *= ((a).index);                                                                                        \
26   }
27 #define LAND_OP(a, b) (b) = (a) && (b)
28 #define LOR_OP(a, b)  (b) = (a) || (b)
29 #define LXOR_OP(a, b) (b) = (not(a) && (b)) || ((a) && not(b))
30 #define BAND_OP(a, b) (b) &= (a)
31 #define BOR_OP(a, b)  (b) |= (a)
32 #define BXOR_OP(a, b) (b) ^= (a)
33 #define MAXLOC_OP(a, b)                                                                                                \
34   (b) = ((a).value) < ((b).value) ? (b) : (((a).value) == ((b).value) ? (((a).index) < ((b).index) ? (a) : (b)) : (a))
35 #define MINLOC_OP(a, b)                                                                                                \
36   (b) = ((a).value) < ((b).value) ? (a) : (((a).value) == ((b).value) ? (((a).index) < ((b).index) ? (a) : (b)) : (b))
37
38 #define APPLY_FUNC(a, b, length, type, func) \
39 {                                          \
40   int i;                                   \
41   type* x = (type*)(a);                    \
42   type* y = (type*)(b);                    \
43   for(i = 0; i < *(length); i++) {         \
44     func(x[i], y[i]);                      \
45   }                                        \
46 }
47
48 #define APPLY_OP_LOOP(dtype, type, op)                                                                                 \
49   if (*datatype == (dtype)) {                                                                                          \
50     APPLY_FUNC(a, b, length, type, op)                                                                                 \
51   } else
52
53 #define APPLY_BASIC_OP_LOOP(op)\
54 APPLY_OP_LOOP(MPI_CHAR, char,op)\
55 APPLY_OP_LOOP(MPI_SHORT, short,op)\
56 APPLY_OP_LOOP(MPI_INT, int,op)\
57 APPLY_OP_LOOP(MPI_LONG, long,op)\
58 APPLY_OP_LOOP(MPI_LONG_LONG, long long,op)\
59 APPLY_OP_LOOP(MPI_SIGNED_CHAR, signed char,op)\
60 APPLY_OP_LOOP(MPI_UNSIGNED_CHAR, unsigned char,op)\
61 APPLY_OP_LOOP(MPI_UNSIGNED_SHORT, unsigned short,op)\
62 APPLY_OP_LOOP(MPI_UNSIGNED, unsigned int,op)\
63 APPLY_OP_LOOP(MPI_UNSIGNED_LONG, unsigned long,op)\
64 APPLY_OP_LOOP(MPI_UNSIGNED_LONG_LONG, unsigned long long,op)\
65 APPLY_OP_LOOP(MPI_WCHAR, wchar_t,op)\
66 APPLY_OP_LOOP(MPI_BYTE, int8_t,op)\
67 APPLY_OP_LOOP(MPI_INT8_T, int8_t,op)\
68 APPLY_OP_LOOP(MPI_INT16_T, int16_t,op)\
69 APPLY_OP_LOOP(MPI_INT32_T, int32_t,op)\
70 APPLY_OP_LOOP(MPI_INT64_T, int64_t,op)\
71 APPLY_OP_LOOP(MPI_UINT8_T, uint8_t,op)\
72 APPLY_OP_LOOP(MPI_UINT16_T, uint16_t,op)\
73 APPLY_OP_LOOP(MPI_UINT32_T, uint32_t,op)\
74 APPLY_OP_LOOP(MPI_UINT64_T, uint64_t,op)\
75 APPLY_OP_LOOP(MPI_AINT, MPI_Aint,op)\
76 APPLY_OP_LOOP(MPI_OFFSET, MPI_Offset,op)\
77 APPLY_OP_LOOP(MPI_INTEGER1, int,op)\
78 APPLY_OP_LOOP(MPI_INTEGER2, int16_t,op)\
79 APPLY_OP_LOOP(MPI_INTEGER4, int32_t,op)\
80 APPLY_OP_LOOP(MPI_INTEGER8, int64_t,op)\
81 APPLY_OP_LOOP(MPI_COUNT, long long,op)
82
83
84 #define APPLY_BOOL_OP_LOOP(op)\
85 APPLY_OP_LOOP(MPI_C_BOOL, bool,op)
86
87 #define APPLY_FLOAT_OP_LOOP(op)\
88 APPLY_OP_LOOP(MPI_FLOAT, float,op)\
89 APPLY_OP_LOOP(MPI_DOUBLE, double,op)\
90 APPLY_OP_LOOP(MPI_LONG_DOUBLE, long double,op)\
91 APPLY_OP_LOOP(MPI_REAL, float,op)\
92 APPLY_OP_LOOP(MPI_REAL4, float,op)\
93 APPLY_OP_LOOP(MPI_REAL8, double,op)\
94 APPLY_OP_LOOP(MPI_REAL16, long double,op)
95
96 #define APPLY_COMPLEX_OP_LOOP(op)\
97 APPLY_OP_LOOP(MPI_C_FLOAT_COMPLEX, float _Complex,op)\
98 APPLY_OP_LOOP(MPI_C_DOUBLE_COMPLEX, double _Complex,op)\
99 APPLY_OP_LOOP(MPI_C_LONG_DOUBLE_COMPLEX, long double _Complex,op)
100
101 #define APPLY_PAIR_OP_LOOP(op)\
102 APPLY_OP_LOOP(MPI_FLOAT_INT, float_int,op)\
103 APPLY_OP_LOOP(MPI_LONG_INT, long_int,op)\
104 APPLY_OP_LOOP(MPI_DOUBLE_INT, double_int,op)\
105 APPLY_OP_LOOP(MPI_SHORT_INT, short_int,op)\
106 APPLY_OP_LOOP(MPI_2INT, int_int,op)\
107 APPLY_OP_LOOP(MPI_2FLOAT, float_float,op)\
108 APPLY_OP_LOOP(MPI_2DOUBLE, double_double,op)\
109 APPLY_OP_LOOP(MPI_LONG_DOUBLE_INT, long_double_int,op)\
110 APPLY_OP_LOOP(MPI_2LONG, long_long,op)\
111 APPLY_OP_LOOP(MPI_COMPLEX8, float_float,op)\
112 APPLY_OP_LOOP(MPI_COMPLEX16, double_double,op)\
113 APPLY_OP_LOOP(MPI_COMPLEX32, double_double,op)
114
115 #define APPLY_END_OP_LOOP(op)                                                                                          \
116   {                                                                                                                    \
117     xbt_die("Failed to apply " _XBT_STRINGIFY(op) " to type %s", (*datatype)->name());                                 \
118   }
119
120 static void max_func(void *a, void *b, int *length, MPI_Datatype * datatype)
121 {
122   APPLY_BASIC_OP_LOOP(MAX_OP)
123   APPLY_FLOAT_OP_LOOP(MAX_OP)
124   APPLY_END_OP_LOOP(MAX_OP)
125 }
126
127 static void min_func(void *a, void *b, int *length, MPI_Datatype * datatype)
128 {
129   APPLY_BASIC_OP_LOOP(MIN_OP)
130   APPLY_FLOAT_OP_LOOP(MIN_OP)
131   APPLY_END_OP_LOOP(MIN_OP)
132 }
133
134 static void sum_func(void *a, void *b, int *length, MPI_Datatype * datatype)
135 {
136   APPLY_BASIC_OP_LOOP(SUM_OP)
137   APPLY_FLOAT_OP_LOOP(SUM_OP)
138   APPLY_COMPLEX_OP_LOOP(SUM_OP)
139   APPLY_PAIR_OP_LOOP(SUM_OP_COMPLEX)
140   APPLY_END_OP_LOOP(SUM_OP)
141 }
142
143 static void prod_func(void *a, void *b, int *length, MPI_Datatype * datatype)
144 {
145   APPLY_BASIC_OP_LOOP(PROD_OP)
146   APPLY_FLOAT_OP_LOOP(PROD_OP)
147   APPLY_COMPLEX_OP_LOOP(PROD_OP)
148   APPLY_PAIR_OP_LOOP(PROD_OP_COMPLEX)
149   APPLY_END_OP_LOOP(PROD_OP)
150 }
151
152 static void land_func(void *a, void *b, int *length, MPI_Datatype * datatype)
153 {
154   APPLY_BASIC_OP_LOOP(LAND_OP)
155   APPLY_FLOAT_OP_LOOP(LAND_OP)
156   APPLY_BOOL_OP_LOOP(LAND_OP)
157   APPLY_END_OP_LOOP(LAND_OP)
158 }
159
160 static void lor_func(void *a, void *b, int *length, MPI_Datatype * datatype)
161 {
162   APPLY_BASIC_OP_LOOP(LOR_OP)
163   APPLY_FLOAT_OP_LOOP(LOR_OP)
164   APPLY_BOOL_OP_LOOP(LOR_OP)
165   APPLY_END_OP_LOOP(LOR_OP)
166 }
167
168 static void lxor_func(void *a, void *b, int *length, MPI_Datatype * datatype)
169 {
170   APPLY_BASIC_OP_LOOP(LXOR_OP)
171   APPLY_FLOAT_OP_LOOP(LXOR_OP)
172   APPLY_BOOL_OP_LOOP(LXOR_OP)
173   APPLY_END_OP_LOOP(LXOR_OP)
174 }
175
176 static void band_func(void *a, void *b, int *length, MPI_Datatype * datatype)
177 {
178   APPLY_BASIC_OP_LOOP(BAND_OP)
179   APPLY_BOOL_OP_LOOP(BAND_OP)
180   APPLY_END_OP_LOOP(BAND_OP)
181 }
182
183 static void bor_func(void *a, void *b, int *length, MPI_Datatype * datatype)
184 {
185   APPLY_BASIC_OP_LOOP(BOR_OP)
186   APPLY_BOOL_OP_LOOP(BOR_OP)
187   APPLY_END_OP_LOOP(BOR_OP)
188 }
189
190 static void bxor_func(void *a, void *b, int *length, MPI_Datatype * datatype)
191 {
192   APPLY_BASIC_OP_LOOP(BXOR_OP)
193   APPLY_BOOL_OP_LOOP(BXOR_OP)
194   APPLY_END_OP_LOOP(BXOR_OP)
195 }
196
197 static void minloc_func(void *a, void *b, int *length, MPI_Datatype * datatype)
198 {
199   APPLY_PAIR_OP_LOOP(MINLOC_OP)
200   APPLY_END_OP_LOOP(MINLOC_OP)
201 }
202
203 static void maxloc_func(void *a, void *b, int *length, MPI_Datatype * datatype)
204 {
205   APPLY_PAIR_OP_LOOP(MAXLOC_OP)
206   APPLY_END_OP_LOOP(MAXLOC_OP)
207 }
208
209 static void replace_func(void *a, void *b, int *length, MPI_Datatype * datatype)
210 {
211   memcpy(b, a, *length * (*datatype)->size());
212 }
213
214 static void no_func(void*, void*, int*, MPI_Datatype*)
215 {
216   /* obviously a no-op */
217 }
218
219 #define CREATE_MPI_OP(name, func)                                                                                      \
220   static SMPI_Op _XBT_CONCAT(mpi_, name)(&(func) /* func */, true, true);                                              \
221   MPI_Op name = &_XBT_CONCAT(mpi_, name);
222
223 CREATE_MPI_OP(MPI_MAX, max_func);
224 CREATE_MPI_OP(MPI_MIN, min_func);
225 CREATE_MPI_OP(MPI_SUM, sum_func);
226 CREATE_MPI_OP(MPI_PROD, prod_func);
227 CREATE_MPI_OP(MPI_LAND, land_func);
228 CREATE_MPI_OP(MPI_LOR, lor_func);
229 CREATE_MPI_OP(MPI_LXOR, lxor_func);
230 CREATE_MPI_OP(MPI_BAND, band_func);
231 CREATE_MPI_OP(MPI_BOR, bor_func);
232 CREATE_MPI_OP(MPI_BXOR, bxor_func);
233 CREATE_MPI_OP(MPI_MAXLOC, maxloc_func);
234 CREATE_MPI_OP(MPI_MINLOC, minloc_func);
235 CREATE_MPI_OP(MPI_REPLACE, replace_func);
236 CREATE_MPI_OP(MPI_NO_OP, no_func);
237
238 namespace simgrid{
239 namespace smpi{
240
241 void Op::apply(const void* invec, void* inoutvec, const int* len, MPI_Datatype datatype)
242 {
243   if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
244     // we need to switch as the called function may silently touch global variables
245     XBT_DEBUG("Applying operation, switch to the right data frame ");
246     smpi_switch_data_segment(simgrid::s4u::Actor::self());
247   }
248
249   if (not smpi_process()->replaying() && *len > 0) {
250     if (not is_fortran_op_)
251       this->func_(const_cast<void*>(invec), inoutvec, const_cast<int*>(len), &datatype);
252     else{
253       XBT_DEBUG("Applying operation of length %d from %p and from/to %p", *len, invec, inoutvec);
254       int tmp = datatype->c2f();
255       /* Unfortunately, the C and Fortran version of the MPI standard do not agree on the type here,
256          thus the reinterpret_cast. */
257       this->func_(const_cast<void*>(invec), inoutvec, const_cast<int*>(len), reinterpret_cast<MPI_Datatype*>(&tmp));
258     }
259   }
260 }
261
262 Op* Op::f2c(int id){
263   return static_cast<Op*>(F2C::f2c(id));
264 }
265
266 void Op::ref(){
267   refcount_++;
268 }
269
270 void Op::unref(MPI_Op* op){
271   if((*op)!=MPI_OP_NULL){
272     (*op)->refcount_--;
273     if((*op)->refcount_==0 && (*op)->predefined_==false)
274       delete(*op);
275   }
276 }
277
278 }
279 }