Logo AND Algorithmique Numérique Distribuée

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