Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove extraneous parentheses, and silent clang.
[simgrid.git] / src / smpi / colls / star-reduction.c
1 #include "colls.h"
2
3 /*
4  * created by Pitch Patarasuk
5  * Modified by Xin Yuan
6  * 
7  * realize a subset of MPI predefine operators:
8  * MPI_LAND, MPI_BAND: C integer, Fortran integer, Byte
9  * MPI_LOR, MPI_BOR: C integer, Fortran integer, Byte
10  * MPI_LXOR, MPI_BXOR: C integer, Fortran integer, Byte
11  * MPI_SUM, MPI_PROD: C integer, Fortran integer, Floating point
12  * MPI_MIN, MPI_MAX: C integer, Fortran integer, Floating point, Byte
13  *
14  * Types not implemented: MPI_LONG_DOUBLE, MPI_LOGICAL, MPI_COMPLEX
15  */
16
17 #ifndef STAR_REDUCTION
18 #define STAR_REDUCTION
19
20
21 #ifdef MPICH2_REDUCTION
22 extern MPI_User_function * MPIR_Op_table[];
23 #elif defined MVAPICH_REDUCETION
24 extern void *MPIR_ToPointer();
25 struct MPIR_OP
26 {
27   MPI_User_function *op;
28   int               commute;
29   int               permanent;
30 };
31 #endif
32
33 static void star_generic_reduction(MPI_Op op, void *src, void *target, int *count, MPI_Datatype *dtype){
34   int i;
35   if ((op == MPI_BOR) || (op == MPI_LOR)) {
36     if ((*dtype == MPI_BYTE) || (*dtype == MPI_CHAR)) {
37       for (i=0;i<*count;i++) {
38           ((char *)target)[i] |= ((char *)src)[i];
39       }
40     }
41     else if ((*dtype == MPI_INT) 
42              || (*dtype == MPI_LONG) 
43              || (*dtype == MPI_INT) 
44              || (*dtype == MPI_UNSIGNED) 
45              || (*dtype == MPI_UNSIGNED_LONG)) {
46       for (i=0;i<*count;i++) {
47           ((int *)target)[i] |= ((int *)src)[i];
48       }
49     } 
50     else if ((*dtype == MPI_SHORT) 
51              || (*dtype == MPI_UNSIGNED_SHORT)) { 
52       for (i=0;i<*count;i++) {
53           ((short *)target)[i] |= ((short *)src)[i];
54       }
55     } 
56     else {
57       printf("reduction operation not supported\n");
58     }
59   }
60
61   else if ((op == MPI_BAND) || (op == MPI_LAND)) {
62     if ((*dtype == MPI_BYTE) || (*dtype == MPI_CHAR)) {
63       for (i=0;i<*count;i++) {
64           ((char *)target)[i] &= ((char *)src)[i];
65       }
66     }
67     else if ((*dtype == MPI_INT) 
68              || (*dtype == MPI_LONG) 
69              || (*dtype == MPI_UNSIGNED) 
70              || (*dtype == MPI_UNSIGNED_LONG)) {
71       for (i=0;i<*count;i++) {
72           ((int *)target)[i] &= ((int *)src)[i];
73       }
74     } 
75     else if ((*dtype == MPI_SHORT) 
76              || (*dtype == MPI_UNSIGNED_SHORT)) { 
77       for (i=0;i<*count;i++) {
78           ((short *)target)[i] &= ((short *)src)[i];
79       }
80     } 
81     else {
82       printf("reduction operation not supported\n");
83     }
84   }
85
86
87   else if ((op == MPI_BXOR) || (op == MPI_LXOR)) {
88     if ((*dtype == MPI_BYTE) || (*dtype == MPI_CHAR)) {
89       for (i=0;i<*count;i++) {
90           ((char *)target)[i] ^= ((char *)src)[i];
91       }
92     }
93     else if ((*dtype == MPI_INT) 
94              || (*dtype == MPI_LONG) 
95              || (*dtype == MPI_UNSIGNED) 
96              || (*dtype == MPI_UNSIGNED_LONG)) {
97       for (i=0;i<*count;i++) {
98           ((int *)target)[i] ^= ((int *)src)[i];
99       }
100     } 
101     else if ((*dtype == MPI_SHORT) 
102              || (*dtype == MPI_UNSIGNED_SHORT)) { 
103       for (i=0;i<*count;i++) {
104           ((short *)target)[i] ^= ((short *)src)[i];
105       }
106     } 
107     else {
108       printf("reduction operation not supported\n");
109     }
110   }
111
112   else if (op == MPI_MAX) {
113     if ((*dtype == MPI_INT) 
114         || (*dtype == MPI_LONG)) { 
115       for (i=0;i<*count;i++) {
116         if (((int *)src)[i] > ((int *)target)[i]) {
117           ((int *)target)[i] = ((int *)src)[i];
118         }
119       }
120     }
121     else if ((*dtype == MPI_UNSIGNED) 
122         || (*dtype == MPI_UNSIGNED_LONG)) {
123       for (i=0;i<*count;i++) {
124         if (((unsigned int *)src)[i] > ((unsigned int *)target)[i]) {
125           ((unsigned int *)target)[i] = ((unsigned int *)src)[i];
126         }
127       }
128     }
129     else if (*dtype == MPI_SHORT) {
130       for (i=0;i<*count;i++) {
131         if (((short *)src)[i] > ((short *)target)[i]) {
132           ((short *)target)[i] = ((short *)src)[i];
133         }
134       }
135     }
136     else if (*dtype == MPI_UNSIGNED_SHORT) { 
137       for (i=0;i<*count;i++) {
138         if (((unsigned short *)src)[i] > ((unsigned short *)target)[i]) {
139           ((unsigned short *)target)[i] = ((unsigned short *)src)[i];
140         }
141       }
142     }
143
144     else if (*dtype == MPI_DOUBLE) {
145       for (i=0;i<*count;i++) {
146         if (((double *)src)[i] > ((double *)target)[i]) {
147           ((double *)target)[i] = ((double *)src)[i];
148         }
149       }
150     }
151     else if (*dtype == MPI_FLOAT) {
152       for (i=0;i<*count;i++) {
153         if (((float *)src)[i] > ((float *)target)[i]) {
154           ((float *)target)[i] = ((float *)src)[i];
155         }
156       }
157     }
158     else if ((*dtype == MPI_CHAR) || (*dtype == MPI_BYTE)) {
159       for (i=0;i<*count;i++) {
160         if (((char *)src)[i] > ((char *)target)[i]) {
161           ((char *)target)[i] = ((char *)src)[i];
162         }
163       }
164     }
165     else {
166       printf("reduction operation not supported\n");
167     }
168   }
169
170
171
172   else if (op == MPI_MIN) {
173     if ((*dtype == MPI_INT) 
174         || (*dtype == MPI_LONG)) { 
175       for (i=0;i<*count;i++) {
176         if (((int *)src)[i] < ((int *)target)[i]) {
177           ((int *)target)[i] = ((int *)src)[i];
178         }
179       }
180     }
181     else if ((*dtype == MPI_UNSIGNED) 
182         || (*dtype == MPI_UNSIGNED_LONG)) {
183       for (i=0;i<*count;i++) {
184         if (((unsigned int *)src)[i] < ((unsigned int *)target)[i]) {
185           ((unsigned int *)target)[i] = ((unsigned int *)src)[i];
186         }
187       }
188     }
189     else if (*dtype == MPI_SHORT) {
190       for (i=0;i<*count;i++) {
191         if (((short *)src)[i] < ((short *)target)[i]) {
192           ((short *)target)[i] = ((short *)src)[i];
193         }
194       }
195     }
196     else if (*dtype == MPI_UNSIGNED_SHORT) { 
197       for (i=0;i<*count;i++) {
198         if (((unsigned short *)src)[i] < ((unsigned short *)target)[i]) {
199           ((unsigned short *)target)[i] = ((unsigned short *)src)[i];
200         }
201       }
202     }
203
204     else if (*dtype == MPI_DOUBLE) {
205       for (i=0;i<*count;i++) {
206         if (((double *)src)[i] < ((double *)target)[i]) {
207           ((double *)target)[i] = ((double *)src)[i];
208         }
209       }
210     }
211     else if (*dtype == MPI_FLOAT) {
212       for (i=0;i<*count;i++) {
213         if (((float *)src)[i] < ((float *)target)[i]) {
214           ((float *)target)[i] = ((float *)src)[i];
215         }
216       }
217     }
218     else if ((*dtype == MPI_CHAR) || (*dtype == MPI_BYTE)) {
219       for (i=0;i<*count;i++) {
220         if (((char *)src)[i] < ((char *)target)[i]) {
221           ((char *)target)[i] = ((char *)src)[i];
222         }
223       }
224     }
225     else {
226       printf("reduction operation not supported\n");
227     }
228   }
229
230
231   else if (op == MPI_SUM) {
232     if ((*dtype == MPI_INT) 
233         || (*dtype == MPI_LONG)) { 
234       for (i=0;i<*count;i++) {
235           ((int *)target)[i] += ((int *)src)[i];
236       }
237     }
238     else if ((*dtype == MPI_UNSIGNED) 
239         || (*dtype == MPI_UNSIGNED_LONG)) {
240       for (i=0;i<*count;i++) {
241           ((unsigned int *)target)[i] += ((unsigned int *)src)[i];
242       }
243     }
244     else if (*dtype == MPI_SHORT) {
245       for (i=0;i<*count;i++) {
246           ((short *)target)[i] += ((short *)src)[i];
247       }
248     }
249     else if (*dtype == MPI_UNSIGNED_SHORT) { 
250       for (i=0;i<*count;i++) {
251           ((unsigned short *)target)[i] += ((unsigned short *)src)[i];
252       }
253     }
254
255     else if (*dtype == MPI_DOUBLE) {
256       for (i=0;i<*count;i++) {
257           ((double *)target)[i] += ((double *)src)[i];
258       }
259     }
260     else if (*dtype == MPI_FLOAT) {
261       for (i=0;i<*count;i++) {
262           ((float *)target)[i] += ((float *)src)[i];
263       }
264     }
265     else {
266       printf("reduction operation not supported\n");
267     }
268   }
269
270   else if (op == MPI_PROD) {
271     if ((*dtype == MPI_INT) 
272         || (*dtype == MPI_LONG)) { 
273       for (i=0;i<*count;i++) {
274           ((int *)target)[i] *= ((int *)src)[i];
275       }
276     }
277     else if ((*dtype == MPI_UNSIGNED) 
278         || (*dtype == MPI_UNSIGNED_LONG)) {
279       for (i=0;i<*count;i++) {
280           ((unsigned int *)target)[i] *= ((unsigned int *)src)[i];
281       }
282     }
283     else if (*dtype == MPI_SHORT) {
284       for (i=0;i<*count;i++) {
285           ((short *)target)[i] *= ((short *)src)[i];
286       }
287     }
288     else if (*dtype == MPI_UNSIGNED_SHORT) { 
289       for (i=0;i<*count;i++) {
290           ((unsigned short *)target)[i] *= ((unsigned short *)src)[i];
291       }
292     }
293
294     else if (*dtype == MPI_DOUBLE) {
295       for (i=0;i<*count;i++) {
296           ((double *)target)[i] *= ((double *)src)[i];
297       }
298     }
299     else if (*dtype == MPI_FLOAT) {
300       for (i=0;i<*count;i++) {
301           ((float *)target)[i] *= ((float *)src)[i];
302       }
303     }
304     else {
305       printf("reduction operation not supported\n");
306     }
307   }
308
309   else {
310     printf("reduction operation not supported\n");
311   }
312 }
313
314 void star_reduction(MPI_Op op, void *src, void *target, int *count, MPI_Datatype *dtype){
315
316 #ifdef MPICH2_REDUCTION
317 MPI_User_function * uop = MPIR_Op_table[op % 16 - 1];
318  return (*uop) (src,target,count,dtype);
319 #elif defined MVAPICH_REDUCTION
320 MPI_User_function *uop;
321 struct MPIR_OP *op_ptr;
322 op_ptr = MPIR_ToPointer(op);
323 uop  = op_ptr->op;
324  return (*uop) (src,target,count,dtype);
325 #else
326  return star_generic_reduction(op,src,target,count,dtype);
327 #endif
328
329
330
331 }
332
333
334 #endif