Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5493e5c16342ed2c600a9f865cddbd010b90f7b7
[simgrid.git] / src / smpi / smpi_mpi_dt.c
1 /* smpi_mpi_dt.c -- MPI primitives to handle datatypes                        */
2 /* FIXME: a very incomplete implementation                                    */
3
4 /* Copyright (c) 2009, 2010. The SimGrid Team.
5  * All rights reserved.                                                     */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include "private.h"
15 #include "smpi_mpi_dt_private.h"
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_mpi_dt, smpi,
18                                 "Logging specific to SMPI (datatype)");
19
20 #define CREATE_MPI_DATATYPE(name, type)       \
21   static s_smpi_mpi_datatype_t mpi_##name = { \
22     sizeof(type),  /* size */                 \
23     0,             /*was 1 has_subtype*/             \
24     0,             /* lb */                   \
25     sizeof(type),  /* ub = lb + size */       \
26     DT_FLAG_BASIC,  /* flags */              \
27     NULL           /* pointer on extended struct*/ \
28   };                                          \
29 MPI_Datatype name = &mpi_##name;
30
31
32 //The following are datatypes for the MPI functions MPI_MAXLOC and MPI_MINLOC.
33 typedef struct {
34   float value;
35   int index;
36 } float_int;
37 typedef struct {
38   long value;
39   int index;
40 } long_int;
41 typedef struct {
42   double value;
43   int index;
44 } double_int;
45 typedef struct {
46   short value;
47   int index;
48 } short_int;
49 typedef struct {
50   int value;
51   int index;
52 } int_int;
53 typedef struct {
54   long double value;
55   int index;
56 } long_double_int;
57
58 // Predefined data types
59 CREATE_MPI_DATATYPE(MPI_CHAR, char);
60 CREATE_MPI_DATATYPE(MPI_SHORT, short);
61 CREATE_MPI_DATATYPE(MPI_INT, int);
62 CREATE_MPI_DATATYPE(MPI_LONG, long);
63 CREATE_MPI_DATATYPE(MPI_LONG_LONG, long long);
64 CREATE_MPI_DATATYPE(MPI_SIGNED_CHAR, signed char);
65 CREATE_MPI_DATATYPE(MPI_UNSIGNED_CHAR, unsigned char);
66 CREATE_MPI_DATATYPE(MPI_UNSIGNED_SHORT, unsigned short);
67 CREATE_MPI_DATATYPE(MPI_UNSIGNED, unsigned int);
68 CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG, unsigned long);
69 CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG_LONG, unsigned long long);
70 CREATE_MPI_DATATYPE(MPI_FLOAT, float);
71 CREATE_MPI_DATATYPE(MPI_DOUBLE, double);
72 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE, long double);
73 CREATE_MPI_DATATYPE(MPI_WCHAR, wchar_t);
74 CREATE_MPI_DATATYPE(MPI_C_BOOL, _Bool);
75 CREATE_MPI_DATATYPE(MPI_INT8_T, int8_t);
76 CREATE_MPI_DATATYPE(MPI_INT16_T, int16_t);
77 CREATE_MPI_DATATYPE(MPI_INT32_T, int32_t);
78 CREATE_MPI_DATATYPE(MPI_INT64_T, int64_t);
79 CREATE_MPI_DATATYPE(MPI_UINT8_T, uint8_t);
80 CREATE_MPI_DATATYPE(MPI_UINT16_T, uint16_t);
81 CREATE_MPI_DATATYPE(MPI_UINT32_T, uint32_t);
82 CREATE_MPI_DATATYPE(MPI_UINT64_T, uint64_t);
83 CREATE_MPI_DATATYPE(MPI_C_FLOAT_COMPLEX, float _Complex);
84 CREATE_MPI_DATATYPE(MPI_C_DOUBLE_COMPLEX, double _Complex);
85 CREATE_MPI_DATATYPE(MPI_C_LONG_DOUBLE_COMPLEX, long double _Complex);
86 CREATE_MPI_DATATYPE(MPI_AINT, MPI_Aint);
87 CREATE_MPI_DATATYPE(MPI_OFFSET, MPI_Offset);
88
89 CREATE_MPI_DATATYPE(MPI_FLOAT_INT, float_int);
90 CREATE_MPI_DATATYPE(MPI_LONG_INT, long_int);
91 CREATE_MPI_DATATYPE(MPI_DOUBLE_INT, double_int);
92 CREATE_MPI_DATATYPE(MPI_SHORT_INT, short_int);
93 CREATE_MPI_DATATYPE(MPI_2INT, int_int);
94 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE_INT, long_double_int);
95
96 // Internal use only
97 CREATE_MPI_DATATYPE(MPI_PTR, void*);
98
99
100 size_t smpi_datatype_size(MPI_Datatype datatype)
101 {
102   return datatype->size;
103 }
104
105
106
107 MPI_Aint smpi_datatype_lb(MPI_Datatype datatype)
108 {
109   return datatype->lb;
110 }
111
112 MPI_Aint smpi_datatype_ub(MPI_Datatype datatype)
113 {
114   return datatype->ub;
115 }
116
117 int smpi_datatype_extent(MPI_Datatype datatype, MPI_Aint * lb,
118                          MPI_Aint * extent)
119 {
120   int retval;
121
122   if ((datatype->flags & DT_FLAG_COMMITED) != DT_FLAG_COMMITED) {
123     retval = MPI_ERR_TYPE;
124   } else {
125     *lb = datatype->lb;
126     *extent = datatype->ub - datatype->lb;
127     retval = MPI_SUCCESS;
128   }
129   return retval;
130 }
131
132 int smpi_datatype_copy(void *sendbuf, int sendcount, MPI_Datatype sendtype,
133                        void *recvbuf, int recvcount, MPI_Datatype recvtype)
134 {
135   int retval, count;
136
137   /* First check if we really have something to do */
138   if (recvcount == 0) {
139     retval = sendcount == 0 ? MPI_SUCCESS : MPI_ERR_TRUNCATE;
140   } else {
141     /* FIXME: treat packed cases */
142     sendcount *= smpi_datatype_size(sendtype);
143     recvcount *= smpi_datatype_size(recvtype);
144     count = sendcount < recvcount ? sendcount : recvcount;
145
146     if(sendtype->has_subtype == 0 && recvtype->has_subtype == 0) {
147       memcpy(recvbuf, sendbuf, count);
148     }
149     else if (sendtype->has_subtype == 0)
150     {
151       s_smpi_subtype_t *subtype =  recvtype->substruct;
152       subtype->unserialize( sendbuf, recvbuf,1, subtype);
153     }
154     else if (recvtype->has_subtype == 0)
155     {
156       s_smpi_subtype_t *subtype =  sendtype->substruct;
157       subtype->serialize(sendbuf, recvbuf,1, subtype);
158     }else{
159       s_smpi_subtype_t *subtype =  sendtype->substruct;
160
161       s_smpi_mpi_vector_t* type_c = (s_smpi_mpi_vector_t*)sendtype;
162
163       void * buf_tmp = malloc(count * type_c->size_oldtype);
164
165       subtype->serialize( sendbuf, buf_tmp,1, subtype);
166       subtype =  recvtype->substruct;
167       subtype->unserialize(recvbuf, buf_tmp,1, subtype);
168
169       free(buf_tmp);
170     }
171     retval = sendcount > recvcount ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
172   }
173
174   return retval;
175 }
176
177 /*
178  *  Copies noncontiguous data into contiguous memory.
179  *  @param contiguous_vector - output vector
180  *  @param noncontiguous_vector - input vector
181  *  @param type - pointer contening :
182  *      - stride - stride of between noncontiguous data
183  *      - block_length - the width or height of blocked matrix
184  *      - count - the number of rows of matrix
185  */
186 void serialize_vector( const void *noncontiguous_vector,
187                        void *contiguous_vector,
188                        size_t count,
189                        void *type)
190 {
191   s_smpi_mpi_vector_t* type_c = (s_smpi_mpi_vector_t*)type;
192   int i;
193   for (i = 0; i < type_c->block_count * count; i++) {
194     memcpy(contiguous_vector,
195            noncontiguous_vector, type_c->block_length * type_c->size_oldtype);
196
197     char* contiguous_vector_char = (char*)contiguous_vector;
198     char* noncontiguous_vector_char = (char*)noncontiguous_vector;
199     contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
200     noncontiguous_vector_char += type_c->block_stride*type_c->size_oldtype;
201   }
202 }
203 /*
204  *  Copies contiguous data into noncontiguous memory.
205  *  @param noncontiguous_vector - output vector
206  *  @param contiguous_vector - input vector
207  *  @param type - pointer contening :
208  *      - stride - stride of between noncontiguous data
209  *      - block_length - the width or height of blocked matrix
210  *      - count - the number of rows of matrix
211  */
212 void unserialize_vector( const void *contiguous_vector,
213                          void *noncontiguous_vector,
214                          size_t count,
215                          void *type)
216 {
217   s_smpi_mpi_vector_t* type_c = (s_smpi_mpi_vector_t*)type;
218   int i;
219   for (i = 0; i < type_c->block_count * count; i++) {
220     memcpy(noncontiguous_vector,
221            contiguous_vector, type_c->block_length * type_c->size_oldtype);
222
223     char* contiguous_vector_char = (char*)contiguous_vector;
224     char* noncontiguous_vector_char = (char*)noncontiguous_vector;
225
226     contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
227     noncontiguous_vector_char += type_c->block_stride*type_c->size_oldtype;
228   }
229 }
230
231 /*
232  * Create a Sub type vector to be able to serialize and unserialize it
233  * the structre s_smpi_mpi_vector_t is derived from s_smpi_subtype which
234  * required the functions unserialize and serialize
235  *
236  */
237 s_smpi_mpi_vector_t* smpi_datatype_vector_create( int block_stride,
238                                                   int block_length,
239                                                   int block_count,
240                                                   MPI_Datatype old_type,
241                                                   int size_oldtype){
242   s_smpi_mpi_vector_t *new_t= xbt_new(s_smpi_mpi_vector_t,1);
243   new_t->base.serialize = &serialize_vector;
244   new_t->base.unserialize = &unserialize_vector;
245   new_t->block_stride = block_stride;
246   new_t->block_length = block_length;
247   new_t->block_count = block_count;
248   new_t->old_type = old_type;
249   new_t->size_oldtype = size_oldtype;
250   return new_t;
251 }
252
253 void smpi_datatype_create(MPI_Datatype* new_type, int size, int has_subtype,
254                           void *struct_type, int flags){
255   MPI_Datatype new_t= xbt_new(s_smpi_mpi_datatype_t,1);
256   new_t->size=size;
257   new_t->has_subtype=has_subtype;
258   new_t->lb=0;
259   new_t->ub=size;
260   new_t->flags=flags;
261   new_t->substruct=struct_type;
262   *new_type = new_t;
263 }
264
265 void smpi_datatype_free(MPI_Datatype* type){
266   xbt_free(*type);
267 }
268
269 int smpi_datatype_contiguous(int count, MPI_Datatype old_type, MPI_Datatype* new_type)
270 {
271   int retval;
272   if ((old_type->flags & DT_FLAG_COMMITED) != DT_FLAG_COMMITED) {
273     retval = MPI_ERR_TYPE;
274   } else {
275     smpi_datatype_create(new_type, count *
276                          smpi_datatype_size(old_type),1,NULL, DT_FLAG_CONTIGUOUS);
277     retval=MPI_SUCCESS;
278   }
279   return retval;
280 }
281
282 int smpi_datatype_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type)
283 {
284   int retval;
285   if (blocklen<=0) return MPI_ERR_ARG;
286   if ((old_type->flags & DT_FLAG_COMMITED) != DT_FLAG_COMMITED) {
287     retval = MPI_ERR_TYPE;
288   } else {
289     if(stride != blocklen){
290       s_smpi_mpi_vector_t* subtype = smpi_datatype_vector_create( stride,
291                                                                   blocklen,
292                                                                   count,
293                                                                   old_type,
294                                                                   smpi_datatype_size(old_type));
295
296       smpi_datatype_create(new_type, count * (blocklen) *
297                            smpi_datatype_size(old_type),
298                            1,
299                            subtype,
300                            DT_FLAG_VECTOR);
301       retval=MPI_SUCCESS;
302     }else{
303       /* in this situation the data are contignous thus it's not
304        * required to serialize and unserialize it*/
305       smpi_datatype_create(new_type, count * (blocklen) *
306                            smpi_datatype_size(old_type),
307                            0,
308                            NULL,
309                            DT_FLAG_VECTOR);
310       retval=MPI_SUCCESS;
311     }
312   }
313   return retval;
314 }
315
316 int smpi_datatype_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type)
317 {
318   int retval;
319   if (blocklen<=0) return MPI_ERR_ARG;
320   if ((old_type->flags & DT_FLAG_COMMITED) != DT_FLAG_COMMITED) {
321     retval = MPI_ERR_TYPE;
322   } else {
323     /*FIXME: as for the vector the data should be serialized and
324      * unserialized moreover a structure derived from s_smpi_subtype should
325      * be created*/
326     smpi_datatype_create(new_type, count * ((blocklen *
327                                              smpi_datatype_size(old_type))+stride),
328                          0,
329                          NULL,
330                          DT_FLAG_VECTOR);
331     retval=MPI_SUCCESS;
332   }
333   return retval;
334 }
335
336
337 int smpi_datatype_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
338 {
339   int i;
340   int retval;
341   int size = 0;
342   for(i=0; i< count; i++){
343     if   (blocklens[i]<=0)
344       return MPI_ERR_ARG;
345     size += blocklens[i];
346   }
347   if ((old_type->flags & DT_FLAG_COMMITED) != DT_FLAG_COMMITED) {
348     retval = MPI_ERR_TYPE;
349   } else {
350     /*FIXME: as for the vector the data should be serialized and
351      * unserialized moreover a structure derived from s_smpi_subtype should
352      * be created*/
353     smpi_datatype_create(new_type,  (size) *
354                          smpi_datatype_size(old_type),0, NULL, DT_FLAG_DATA);
355     retval=MPI_SUCCESS;
356   }
357   return retval;
358 }
359
360 int smpi_datatype_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
361 {
362   int i;
363   int retval;
364   int size = 0;
365   for(i=0; i< count; i++){
366     if   (blocklens[i]<=0)
367       return MPI_ERR_ARG;
368     size += blocklens[i];
369   }
370   if ((old_type->flags & DT_FLAG_COMMITED) != DT_FLAG_COMMITED) {
371     retval = MPI_ERR_TYPE;
372   } else {
373     /*FIXME: as for the vector the data should be serialized and
374      * unserialized moreover a structure derived from s_smpi_subtype should
375      * be created*/
376     smpi_datatype_create(new_type,(size * smpi_datatype_size(old_type)), 0,NULL, DT_FLAG_DATA);
377     retval=MPI_SUCCESS;
378   }
379   return retval;
380 }
381
382 int smpi_datatype_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type)
383 {
384   int i;
385   size_t size; //Khalid added this
386
387   for(i=0; i< count; i++){
388     if (blocklens[i]<=0)
389       return MPI_ERR_ARG;
390     if ((old_types[i]->flags & DT_FLAG_COMMITED) != DT_FLAG_COMMITED)
391       return MPI_ERR_TYPE;
392     size += blocklens[i]*smpi_datatype_size(old_types[i]);
393   }
394   /*FIXME: as for the vector the data should be serialized and
395    * unserialized moreover a structure derived from s_smpi_subtype should
396    * be created*/
397   smpi_datatype_create(new_type, size,
398                        0, NULL,
399                        DT_FLAG_DATA);
400   return MPI_SUCCESS;
401 }
402
403 void smpi_datatype_commit(MPI_Datatype *datatype)
404 {
405   (*datatype)->flags=  ((*datatype)->flags | DT_FLAG_COMMITED);
406 }
407
408 typedef struct s_smpi_mpi_op {
409   MPI_User_function *func;
410 } s_smpi_mpi_op_t;
411
412 #define MAX_OP(a, b)  (b) = (a) < (b) ? (b) : (a)
413 #define MIN_OP(a, b)  (b) = (a) < (b) ? (a) : (b)
414 #define SUM_OP(a, b)  (b) += (a)
415 #define PROD_OP(a, b) (b) *= (a)
416 #define LAND_OP(a, b) (b) = (a) && (b)
417 #define LOR_OP(a, b)  (b) = (a) || (b)
418 #define LXOR_OP(a, b) (b) = (!(a) && (b)) || ((a) && !(b))
419 #define BAND_OP(a, b) (b) &= (a)
420 #define BOR_OP(a, b)  (b) |= (a)
421 #define BXOR_OP(a, b) (b) ^= (a)
422 #define MAXLOC_OP(a, b)  (b) = (a.value) < (b.value) ? (b) : (a)
423 #define MINLOC_OP(a, b)  (b) = (a.value) < (b.value) ? (a) : (b)
424 //TODO : MINLOC & MAXLOC
425
426 #define APPLY_FUNC(a, b, length, type, func) \
427 {                                          \
428   int i;                                   \
429   type* x = (type*)(a);                    \
430   type* y = (type*)(b);                    \
431   for(i = 0; i < *(length); i++) {         \
432     func(x[i], y[i]);                      \
433   }                                        \
434 }
435
436 static void max_func(void *a, void *b, int *length,
437                      MPI_Datatype * datatype)
438 {
439   if (*datatype == MPI_CHAR) {
440     APPLY_FUNC(a, b, length, char, MAX_OP);
441   } else if (*datatype == MPI_SHORT) {
442     APPLY_FUNC(a, b, length, short, MAX_OP);
443   } else if (*datatype == MPI_INT) {
444     APPLY_FUNC(a, b, length, int, MAX_OP);
445   } else if (*datatype == MPI_LONG) {
446     APPLY_FUNC(a, b, length, long, MAX_OP);
447   } else if (*datatype == MPI_UNSIGNED_SHORT) {
448     APPLY_FUNC(a, b, length, unsigned short, MAX_OP);
449   } else if (*datatype == MPI_UNSIGNED) {
450     APPLY_FUNC(a, b, length, unsigned int, MAX_OP);
451   } else if (*datatype == MPI_UNSIGNED_LONG) {
452     APPLY_FUNC(a, b, length, unsigned long, MAX_OP);
453   } else if (*datatype == MPI_FLOAT) {
454     APPLY_FUNC(a, b, length, float, MAX_OP);
455   } else if (*datatype == MPI_DOUBLE) {
456     APPLY_FUNC(a, b, length, double, MAX_OP);
457   } else if (*datatype == MPI_LONG_DOUBLE) {
458     APPLY_FUNC(a, b, length, long double, MAX_OP);
459   }
460 }
461
462 static void min_func(void *a, void *b, int *length,
463                      MPI_Datatype * datatype)
464 {
465   if (*datatype == MPI_CHAR) {
466     APPLY_FUNC(a, b, length, char, MIN_OP);
467   } else if (*datatype == MPI_SHORT) {
468     APPLY_FUNC(a, b, length, short, MIN_OP);
469   } else if (*datatype == MPI_INT) {
470     APPLY_FUNC(a, b, length, int, MIN_OP);
471   } else if (*datatype == MPI_LONG) {
472     APPLY_FUNC(a, b, length, long, MIN_OP);
473   } else if (*datatype == MPI_UNSIGNED_SHORT) {
474     APPLY_FUNC(a, b, length, unsigned short, MIN_OP);
475   } else if (*datatype == MPI_UNSIGNED) {
476     APPLY_FUNC(a, b, length, unsigned int, MIN_OP);
477   } else if (*datatype == MPI_UNSIGNED_LONG) {
478     APPLY_FUNC(a, b, length, unsigned long, MIN_OP);
479   } else if (*datatype == MPI_FLOAT) {
480     APPLY_FUNC(a, b, length, float, MIN_OP);
481   } else if (*datatype == MPI_DOUBLE) {
482     APPLY_FUNC(a, b, length, double, MIN_OP);
483   } else if (*datatype == MPI_LONG_DOUBLE) {
484     APPLY_FUNC(a, b, length, long double, MIN_OP);
485   }
486 }
487
488 static void sum_func(void *a, void *b, int *length,
489                      MPI_Datatype * datatype)
490 {
491   if (*datatype == MPI_CHAR) {
492     APPLY_FUNC(a, b, length, char, SUM_OP);
493   } else if (*datatype == MPI_SHORT) {
494     APPLY_FUNC(a, b, length, short, SUM_OP);
495   } else if (*datatype == MPI_INT) {
496     APPLY_FUNC(a, b, length, int, SUM_OP);
497   } else if (*datatype == MPI_LONG) {
498     APPLY_FUNC(a, b, length, long, SUM_OP);
499   } else if (*datatype == MPI_UNSIGNED_SHORT) {
500     APPLY_FUNC(a, b, length, unsigned short, SUM_OP);
501   } else if (*datatype == MPI_UNSIGNED) {
502     APPLY_FUNC(a, b, length, unsigned int, SUM_OP);
503   } else if (*datatype == MPI_UNSIGNED_LONG) {
504     APPLY_FUNC(a, b, length, unsigned long, SUM_OP);
505   } else if (*datatype == MPI_FLOAT) {
506     APPLY_FUNC(a, b, length, float, SUM_OP);
507   } else if (*datatype == MPI_DOUBLE) {
508     APPLY_FUNC(a, b, length, double, SUM_OP);
509   } else if (*datatype == MPI_LONG_DOUBLE) {
510     APPLY_FUNC(a, b, length, long double, SUM_OP);
511   } else if (*datatype == MPI_C_FLOAT_COMPLEX) {
512     APPLY_FUNC(a, b, length, float _Complex, SUM_OP);
513   } else if (*datatype == MPI_C_DOUBLE_COMPLEX) {
514     APPLY_FUNC(a, b, length, double _Complex, SUM_OP);
515   } else if (*datatype == MPI_C_LONG_DOUBLE_COMPLEX) {
516     APPLY_FUNC(a, b, length, long double _Complex, SUM_OP);
517   }
518 }
519
520 static void prod_func(void *a, void *b, int *length,
521                       MPI_Datatype * datatype)
522 {
523   if (*datatype == MPI_CHAR) {
524     APPLY_FUNC(a, b, length, char, PROD_OP);
525   } else if (*datatype == MPI_SHORT) {
526     APPLY_FUNC(a, b, length, short, PROD_OP);
527   } else if (*datatype == MPI_INT) {
528     APPLY_FUNC(a, b, length, int, PROD_OP);
529   } else if (*datatype == MPI_LONG) {
530     APPLY_FUNC(a, b, length, long, PROD_OP);
531   } else if (*datatype == MPI_UNSIGNED_SHORT) {
532     APPLY_FUNC(a, b, length, unsigned short, PROD_OP);
533   } else if (*datatype == MPI_UNSIGNED) {
534     APPLY_FUNC(a, b, length, unsigned int, PROD_OP);
535   } else if (*datatype == MPI_UNSIGNED_LONG) {
536     APPLY_FUNC(a, b, length, unsigned long, PROD_OP);
537   } else if (*datatype == MPI_FLOAT) {
538     APPLY_FUNC(a, b, length, float, PROD_OP);
539   } else if (*datatype == MPI_DOUBLE) {
540     APPLY_FUNC(a, b, length, double, PROD_OP);
541   } else if (*datatype == MPI_LONG_DOUBLE) {
542     APPLY_FUNC(a, b, length, long double, PROD_OP);
543   } else if (*datatype == MPI_C_FLOAT_COMPLEX) {
544     APPLY_FUNC(a, b, length, float _Complex, PROD_OP);
545   } else if (*datatype == MPI_C_DOUBLE_COMPLEX) {
546     APPLY_FUNC(a, b, length, double _Complex, PROD_OP);
547   } else if (*datatype == MPI_C_LONG_DOUBLE_COMPLEX) {
548     APPLY_FUNC(a, b, length, long double _Complex, PROD_OP);
549   }
550 }
551
552 static void land_func(void *a, void *b, int *length,
553                       MPI_Datatype * datatype)
554 {
555   if (*datatype == MPI_CHAR) {
556     APPLY_FUNC(a, b, length, char, LAND_OP);
557   } else if (*datatype == MPI_SHORT) {
558     APPLY_FUNC(a, b, length, short, LAND_OP);
559   } else if (*datatype == MPI_INT) {
560     APPLY_FUNC(a, b, length, int, LAND_OP);
561   } else if (*datatype == MPI_LONG) {
562     APPLY_FUNC(a, b, length, long, LAND_OP);
563   } else if (*datatype == MPI_UNSIGNED_SHORT) {
564     APPLY_FUNC(a, b, length, unsigned short, LAND_OP);
565   } else if (*datatype == MPI_UNSIGNED) {
566     APPLY_FUNC(a, b, length, unsigned int, LAND_OP);
567   } else if (*datatype == MPI_UNSIGNED_LONG) {
568     APPLY_FUNC(a, b, length, unsigned long, LAND_OP);
569   } else if (*datatype == MPI_C_BOOL) {
570     APPLY_FUNC(a, b, length, _Bool, LAND_OP);
571   }
572 }
573
574 static void lor_func(void *a, void *b, int *length,
575                      MPI_Datatype * datatype)
576 {
577   if (*datatype == MPI_CHAR) {
578     APPLY_FUNC(a, b, length, char, LOR_OP);
579   } else if (*datatype == MPI_SHORT) {
580     APPLY_FUNC(a, b, length, short, LOR_OP);
581   } else if (*datatype == MPI_INT) {
582     APPLY_FUNC(a, b, length, int, LOR_OP);
583   } else if (*datatype == MPI_LONG) {
584     APPLY_FUNC(a, b, length, long, LOR_OP);
585   } else if (*datatype == MPI_UNSIGNED_SHORT) {
586     APPLY_FUNC(a, b, length, unsigned short, LOR_OP);
587   } else if (*datatype == MPI_UNSIGNED) {
588     APPLY_FUNC(a, b, length, unsigned int, LOR_OP);
589   } else if (*datatype == MPI_UNSIGNED_LONG) {
590     APPLY_FUNC(a, b, length, unsigned long, LOR_OP);
591   } else if (*datatype == MPI_C_BOOL) {
592     APPLY_FUNC(a, b, length, _Bool, LOR_OP);
593   }
594 }
595
596 static void lxor_func(void *a, void *b, int *length,
597                       MPI_Datatype * datatype)
598 {
599   if (*datatype == MPI_CHAR) {
600     APPLY_FUNC(a, b, length, char, LXOR_OP);
601   } else if (*datatype == MPI_SHORT) {
602     APPLY_FUNC(a, b, length, short, LXOR_OP);
603   } else if (*datatype == MPI_INT) {
604     APPLY_FUNC(a, b, length, int, LXOR_OP);
605   } else if (*datatype == MPI_LONG) {
606     APPLY_FUNC(a, b, length, long, LXOR_OP);
607   } else if (*datatype == MPI_UNSIGNED_SHORT) {
608     APPLY_FUNC(a, b, length, unsigned short, LXOR_OP);
609   } else if (*datatype == MPI_UNSIGNED) {
610     APPLY_FUNC(a, b, length, unsigned int, LXOR_OP);
611   } else if (*datatype == MPI_UNSIGNED_LONG) {
612     APPLY_FUNC(a, b, length, unsigned long, LXOR_OP);
613   } else if (*datatype == MPI_C_BOOL) {
614     APPLY_FUNC(a, b, length, _Bool, LXOR_OP);
615   }
616 }
617
618 static void band_func(void *a, void *b, int *length,
619                       MPI_Datatype * datatype)
620 {
621   if (*datatype == MPI_CHAR) {
622     APPLY_FUNC(a, b, length, char, BAND_OP);
623   }
624   if (*datatype == MPI_SHORT) {
625     APPLY_FUNC(a, b, length, short, BAND_OP);
626   } else if (*datatype == MPI_INT) {
627     APPLY_FUNC(a, b, length, int, BAND_OP);
628   } else if (*datatype == MPI_LONG) {
629     APPLY_FUNC(a, b, length, long, BAND_OP);
630   } else if (*datatype == MPI_UNSIGNED_SHORT) {
631     APPLY_FUNC(a, b, length, unsigned short, BAND_OP);
632   } else if (*datatype == MPI_UNSIGNED) {
633     APPLY_FUNC(a, b, length, unsigned int, BAND_OP);
634   } else if (*datatype == MPI_UNSIGNED_LONG) {
635     APPLY_FUNC(a, b, length, unsigned long, BAND_OP);
636   } else if (*datatype == MPI_BYTE) {
637     APPLY_FUNC(a, b, length, uint8_t, BAND_OP);
638   }
639 }
640
641 static void bor_func(void *a, void *b, int *length,
642                      MPI_Datatype * datatype)
643 {
644   if (*datatype == MPI_CHAR) {
645     APPLY_FUNC(a, b, length, char, BOR_OP);
646   } else if (*datatype == MPI_SHORT) {
647     APPLY_FUNC(a, b, length, short, BOR_OP);
648   } else if (*datatype == MPI_INT) {
649     APPLY_FUNC(a, b, length, int, BOR_OP);
650   } else if (*datatype == MPI_LONG) {
651     APPLY_FUNC(a, b, length, long, BOR_OP);
652   } else if (*datatype == MPI_UNSIGNED_SHORT) {
653     APPLY_FUNC(a, b, length, unsigned short, BOR_OP);
654   } else if (*datatype == MPI_UNSIGNED) {
655     APPLY_FUNC(a, b, length, unsigned int, BOR_OP);
656   } else if (*datatype == MPI_UNSIGNED_LONG) {
657     APPLY_FUNC(a, b, length, unsigned long, BOR_OP);
658   } else if (*datatype == MPI_BYTE) {
659     APPLY_FUNC(a, b, length, uint8_t, BOR_OP);
660   }
661 }
662
663 static void bxor_func(void *a, void *b, int *length,
664                       MPI_Datatype * datatype)
665 {
666   if (*datatype == MPI_CHAR) {
667     APPLY_FUNC(a, b, length, char, BXOR_OP);
668   } else if (*datatype == MPI_SHORT) {
669     APPLY_FUNC(a, b, length, short, BXOR_OP);
670   } else if (*datatype == MPI_INT) {
671     APPLY_FUNC(a, b, length, int, BXOR_OP);
672   } else if (*datatype == MPI_LONG) {
673     APPLY_FUNC(a, b, length, long, BXOR_OP);
674   } else if (*datatype == MPI_UNSIGNED_SHORT) {
675     APPLY_FUNC(a, b, length, unsigned short, BXOR_OP);
676   } else if (*datatype == MPI_UNSIGNED) {
677     APPLY_FUNC(a, b, length, unsigned int, BXOR_OP);
678   } else if (*datatype == MPI_UNSIGNED_LONG) {
679     APPLY_FUNC(a, b, length, unsigned long, BXOR_OP);
680   } else if (*datatype == MPI_BYTE) {
681     APPLY_FUNC(a, b, length, uint8_t, BXOR_OP);
682   }
683 }
684
685 static void minloc_func(void *a, void *b, int *length,
686                         MPI_Datatype * datatype)
687 {
688   if (*datatype == MPI_FLOAT_INT) {
689     APPLY_FUNC(a, b, length, float_int, MINLOC_OP);
690   } else if (*datatype == MPI_LONG_INT) {
691     APPLY_FUNC(a, b, length, long_int, MINLOC_OP);
692   } else if (*datatype == MPI_DOUBLE_INT) {
693     APPLY_FUNC(a, b, length, double_int, MINLOC_OP);
694   } else if (*datatype == MPI_SHORT_INT) {
695     APPLY_FUNC(a, b, length, short_int, MINLOC_OP);
696   } else if (*datatype == MPI_2INT) {
697     APPLY_FUNC(a, b, length, int_int, MINLOC_OP);
698   } else if (*datatype == MPI_LONG_DOUBLE_INT) {
699     APPLY_FUNC(a, b, length, long_double_int, MINLOC_OP);
700   }
701 }
702
703 static void maxloc_func(void *a, void *b, int *length,
704                         MPI_Datatype * datatype)
705 {
706   if (*datatype == MPI_FLOAT_INT) {
707     APPLY_FUNC(a, b, length, float_int, MAXLOC_OP);
708   } else if (*datatype == MPI_LONG_INT) {
709     APPLY_FUNC(a, b, length, long_int, MAXLOC_OP);
710   } else if (*datatype == MPI_DOUBLE_INT) {
711     APPLY_FUNC(a, b, length, double_int, MAXLOC_OP);
712   } else if (*datatype == MPI_SHORT_INT) {
713     APPLY_FUNC(a, b, length, short_int, MAXLOC_OP);
714   } else if (*datatype == MPI_2INT) {
715     APPLY_FUNC(a, b, length, int_int, MAXLOC_OP);
716   } else if (*datatype == MPI_LONG_DOUBLE_INT) {
717     APPLY_FUNC(a, b, length, long_double_int, MAXLOC_OP);
718   }
719 }
720
721
722 #define CREATE_MPI_OP(name, func)                             \
723   static s_smpi_mpi_op_t mpi_##name = { &(func) /* func */ }; \
724 MPI_Op name = &mpi_##name;
725
726 CREATE_MPI_OP(MPI_MAX, max_func);
727 CREATE_MPI_OP(MPI_MIN, min_func);
728 CREATE_MPI_OP(MPI_SUM, sum_func);
729 CREATE_MPI_OP(MPI_PROD, prod_func);
730 CREATE_MPI_OP(MPI_LAND, land_func);
731 CREATE_MPI_OP(MPI_LOR, lor_func);
732 CREATE_MPI_OP(MPI_LXOR, lxor_func);
733 CREATE_MPI_OP(MPI_BAND, band_func);
734 CREATE_MPI_OP(MPI_BOR, bor_func);
735 CREATE_MPI_OP(MPI_BXOR, bxor_func);
736 CREATE_MPI_OP(MPI_MAXLOC, maxloc_func);
737 CREATE_MPI_OP(MPI_MINLOC, minloc_func);
738
739 MPI_Op smpi_op_new(MPI_User_function * function, int commute)
740 {
741   MPI_Op op;
742
743   //FIXME: add commute param
744   op = xbt_new(s_smpi_mpi_op_t, 1);
745   op->func = function;
746   return op;
747 }
748
749 void smpi_op_destroy(MPI_Op op)
750 {
751   xbt_free(op);
752 }
753
754 void smpi_op_apply(MPI_Op op, void *invec, void *inoutvec, int *len,
755                    MPI_Datatype * datatype)
756 {
757   op->func(invec, inoutvec, len, datatype);
758 }