Logo AND Algorithmique Numérique Distribuée

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