1 /* Copyright (c) 2009-2017. The SimGrid Team. All rights reserved. */
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. */
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_op, smpi, "Logging specific to SMPI (op)");
11 #define MAX_OP(a, b) (b) = (a) < (b) ? (b) : (a)
12 #define MIN_OP(a, b) (b) = (a) < (b) ? (a) : (b)
13 #define SUM_OP(a, b) (b) += (a)
14 #define PROD_OP(a, b) (b) *= (a)
15 #define LAND_OP(a, b) (b) = (a) && (b)
16 #define LOR_OP(a, b) (b) = (a) || (b)
17 #define LXOR_OP(a, b) (b) = (!(a) && (b)) || ((a) && !(b))
18 #define BAND_OP(a, b) (b) &= (a)
19 #define BOR_OP(a, b) (b) |= (a)
20 #define BXOR_OP(a, b) (b) ^= (a)
21 #define MAXLOC_OP(a, b) (b) = (a.value) < (b.value) ? (b) : (a)
22 #define MINLOC_OP(a, b) (b) = (a.value) < (b.value) ? (a) : (b)
24 #define APPLY_FUNC(a, b, length, type, func) \
27 type* x = (type*)(a); \
28 type* y = (type*)(b); \
29 for(i = 0; i < *(length); i++) { \
34 #define APPLY_OP_LOOP(dtype, type, op) \
35 if (*datatype == dtype) {\
36 APPLY_FUNC(a, b, length, type, op)\
40 #define APPLY_BASIC_OP_LOOP(op)\
41 APPLY_OP_LOOP(MPI_CHAR, char,op)\
42 APPLY_OP_LOOP(MPI_SHORT, short,op)\
43 APPLY_OP_LOOP(MPI_INT, int,op)\
44 APPLY_OP_LOOP(MPI_LONG, long,op)\
45 APPLY_OP_LOOP(MPI_LONG_LONG, long long,op)\
46 APPLY_OP_LOOP(MPI_SIGNED_CHAR, signed char,op)\
47 APPLY_OP_LOOP(MPI_UNSIGNED_CHAR, unsigned char,op)\
48 APPLY_OP_LOOP(MPI_UNSIGNED_SHORT, unsigned short,op)\
49 APPLY_OP_LOOP(MPI_UNSIGNED, unsigned int,op)\
50 APPLY_OP_LOOP(MPI_UNSIGNED_LONG, unsigned long,op)\
51 APPLY_OP_LOOP(MPI_UNSIGNED_LONG_LONG, unsigned long long,op)\
52 APPLY_OP_LOOP(MPI_WCHAR, wchar_t,op)\
53 APPLY_OP_LOOP(MPI_BYTE, int8_t,op)\
54 APPLY_OP_LOOP(MPI_INT8_T, int8_t,op)\
55 APPLY_OP_LOOP(MPI_INT16_T, int16_t,op)\
56 APPLY_OP_LOOP(MPI_INT32_T, int32_t,op)\
57 APPLY_OP_LOOP(MPI_INT64_T, int64_t,op)\
58 APPLY_OP_LOOP(MPI_UINT8_T, uint8_t,op)\
59 APPLY_OP_LOOP(MPI_UINT16_T, uint16_t,op)\
60 APPLY_OP_LOOP(MPI_UINT32_T, uint32_t,op)\
61 APPLY_OP_LOOP(MPI_UINT64_T, uint64_t,op)\
62 APPLY_OP_LOOP(MPI_AINT, MPI_Aint,op)\
63 APPLY_OP_LOOP(MPI_OFFSET, MPI_Offset,op)\
64 APPLY_OP_LOOP(MPI_INTEGER1, int,op)\
65 APPLY_OP_LOOP(MPI_INTEGER2, int16_t,op)\
66 APPLY_OP_LOOP(MPI_INTEGER4, int32_t,op)\
67 APPLY_OP_LOOP(MPI_INTEGER8, int64_t,op)
69 #define APPLY_BOOL_OP_LOOP(op)\
70 APPLY_OP_LOOP(MPI_C_BOOL, bool,op)
72 #define APPLY_FLOAT_OP_LOOP(op)\
73 APPLY_OP_LOOP(MPI_FLOAT, float,op)\
74 APPLY_OP_LOOP(MPI_DOUBLE, double,op)\
75 APPLY_OP_LOOP(MPI_LONG_DOUBLE, long double,op)\
76 APPLY_OP_LOOP(MPI_REAL, float,op)\
77 APPLY_OP_LOOP(MPI_REAL4, float,op)\
78 APPLY_OP_LOOP(MPI_REAL8, float,op)\
79 APPLY_OP_LOOP(MPI_REAL16, double,op)
81 #define APPLY_COMPLEX_OP_LOOP(op)\
82 APPLY_OP_LOOP(MPI_C_FLOAT_COMPLEX, float _Complex,op)\
83 APPLY_OP_LOOP(MPI_C_DOUBLE_COMPLEX, double _Complex,op)\
84 APPLY_OP_LOOP(MPI_C_LONG_DOUBLE_COMPLEX, long double _Complex,op)
86 #define APPLY_PAIR_OP_LOOP(op)\
87 APPLY_OP_LOOP(MPI_FLOAT_INT, float_int,op)\
88 APPLY_OP_LOOP(MPI_LONG_INT, long_int,op)\
89 APPLY_OP_LOOP(MPI_DOUBLE_INT, double_int,op)\
90 APPLY_OP_LOOP(MPI_SHORT_INT, short_int,op)\
91 APPLY_OP_LOOP(MPI_2INT, int_int,op)\
92 APPLY_OP_LOOP(MPI_2FLOAT, float_float,op)\
93 APPLY_OP_LOOP(MPI_2DOUBLE, double_double,op)\
94 APPLY_OP_LOOP(MPI_LONG_DOUBLE_INT, long_double_int,op)\
95 APPLY_OP_LOOP(MPI_2LONG, long_long,op)
97 #define APPLY_END_OP_LOOP(op)\
99 xbt_die("Failed to apply " #op " to type %s", (*datatype)->name());\
102 static void max_func(void *a, void *b, int *length, MPI_Datatype * datatype)
104 APPLY_BASIC_OP_LOOP(MAX_OP)
105 APPLY_FLOAT_OP_LOOP(MAX_OP)
106 APPLY_END_OP_LOOP(MAX_OP)
109 static void min_func(void *a, void *b, int *length, MPI_Datatype * datatype)
111 APPLY_BASIC_OP_LOOP(MIN_OP)
112 APPLY_FLOAT_OP_LOOP(MIN_OP)
113 APPLY_END_OP_LOOP(MIN_OP)
116 static void sum_func(void *a, void *b, int *length, MPI_Datatype * datatype)
118 APPLY_BASIC_OP_LOOP(SUM_OP)
119 APPLY_FLOAT_OP_LOOP(SUM_OP)
120 APPLY_COMPLEX_OP_LOOP(SUM_OP)
121 APPLY_END_OP_LOOP(SUM_OP)
124 static void prod_func(void *a, void *b, int *length, MPI_Datatype * datatype)
126 APPLY_BASIC_OP_LOOP(PROD_OP)
127 APPLY_FLOAT_OP_LOOP(PROD_OP)
128 APPLY_COMPLEX_OP_LOOP(PROD_OP)
129 APPLY_END_OP_LOOP(PROD_OP)
132 static void land_func(void *a, void *b, int *length, MPI_Datatype * datatype)
134 APPLY_BASIC_OP_LOOP(LAND_OP)
135 APPLY_BOOL_OP_LOOP(LAND_OP)
136 APPLY_END_OP_LOOP(LAND_OP)
139 static void lor_func(void *a, void *b, int *length, MPI_Datatype * datatype)
141 APPLY_BASIC_OP_LOOP(LOR_OP)
142 APPLY_BOOL_OP_LOOP(LOR_OP)
143 APPLY_END_OP_LOOP(LOR_OP)
146 static void lxor_func(void *a, void *b, int *length, MPI_Datatype * datatype)
148 APPLY_BASIC_OP_LOOP(LXOR_OP)
149 APPLY_BOOL_OP_LOOP(LXOR_OP)
150 APPLY_END_OP_LOOP(LXOR_OP)
153 static void band_func(void *a, void *b, int *length, MPI_Datatype * datatype)
155 APPLY_BASIC_OP_LOOP(BAND_OP)
156 APPLY_BOOL_OP_LOOP(BAND_OP)
157 APPLY_END_OP_LOOP(BAND_OP)
160 static void bor_func(void *a, void *b, int *length, MPI_Datatype * datatype)
162 APPLY_BASIC_OP_LOOP(BOR_OP)
163 APPLY_BOOL_OP_LOOP(BOR_OP)
164 APPLY_END_OP_LOOP(BOR_OP)
167 static void bxor_func(void *a, void *b, int *length, MPI_Datatype * datatype)
169 APPLY_BASIC_OP_LOOP(BXOR_OP)
170 APPLY_BOOL_OP_LOOP(BXOR_OP)
171 APPLY_END_OP_LOOP(BXOR_OP)
174 static void minloc_func(void *a, void *b, int *length, MPI_Datatype * datatype)
176 APPLY_PAIR_OP_LOOP(MINLOC_OP)
177 APPLY_END_OP_LOOP(MINLOC_OP)
180 static void maxloc_func(void *a, void *b, int *length, MPI_Datatype * datatype)
182 APPLY_PAIR_OP_LOOP(MAXLOC_OP)
183 APPLY_END_OP_LOOP(MAXLOC_OP)
186 static void replace_func(void *a, void *b, int *length, MPI_Datatype * datatype)
188 memcpy(b, a, *length * (*datatype)->size());
191 static void no_func(void *a, void *b, int *length, MPI_Datatype * datatype)
193 /* obviously a no-op */
196 #define CREATE_MPI_OP(name, func) \
197 static SMPI_Op mpi_##name (&(func) /* func */, true ); \
198 MPI_Op name = &mpi_##name;
200 CREATE_MPI_OP(MPI_MAX, max_func);
201 CREATE_MPI_OP(MPI_MIN, min_func);
202 CREATE_MPI_OP(MPI_SUM, sum_func);
203 CREATE_MPI_OP(MPI_PROD, prod_func);
204 CREATE_MPI_OP(MPI_LAND, land_func);
205 CREATE_MPI_OP(MPI_LOR, lor_func);
206 CREATE_MPI_OP(MPI_LXOR, lxor_func);
207 CREATE_MPI_OP(MPI_BAND, band_func);
208 CREATE_MPI_OP(MPI_BOR, bor_func);
209 CREATE_MPI_OP(MPI_BXOR, bxor_func);
210 CREATE_MPI_OP(MPI_MAXLOC, maxloc_func);
211 CREATE_MPI_OP(MPI_MINLOC, minloc_func);
212 CREATE_MPI_OP(MPI_REPLACE, replace_func);
213 CREATE_MPI_OP(MPI_NO_OP, no_func);
218 Op::Op(MPI_User_function * function, bool commutative) : func_(function), is_commutative_(commutative)
220 is_fortran_op_ = false;
223 bool Op::is_commutative()
225 return is_commutative_;
228 bool Op::is_fortran_op()
230 return is_fortran_op_;
233 void Op::set_fortran_op()
235 //tell that we were created from fortran, so we need to translate the type to fortran when called
236 is_fortran_op_ = true;
239 void Op::apply(void *invec, void *inoutvec, int *len, MPI_Datatype datatype)
241 if(smpi_privatize_global_variables){//we need to switch as the called function may silently touch global variables
242 XBT_DEBUG("Applying operation, switch to the right data frame ");
243 smpi_switch_data_segment(smpi_process()->index());
246 if(!smpi_process()->replaying()){
248 this->func_(invec, inoutvec, len, &datatype);
250 XBT_DEBUG("Applying operation of length %d from %p and from/to %p", *len, invec, inoutvec);
251 int tmp = datatype->c2f();
252 /* Unfortunately, the C and Fortran version of the MPI standard do not agree on the type here,
253 thus the reinterpret_cast. */
254 this->func_(invec, inoutvec, len, reinterpret_cast<MPI_Datatype*>(&tmp) );
260 return static_cast<Op*>(F2C::f2c(id));