Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[smpi] add a gestion of non-contignous data
[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     contiguous_vector += type_c->block_length*type_c->size_oldtype;
197     noncontiguous_vector += type_c->block_stride*type_c->size_oldtype;
198   }
199 }
200 /*
201  *  Copies contiguous data into noncontiguous memory.
202  *  @param noncontiguous_vector - output vector
203  *  @param contiguous_vector - input vector
204  *  @param type - pointer contening :
205  *      - stride - stride of between noncontiguous data
206  *      - block_length - the width or height of blocked matrix
207  *      - count - the number of rows of matrix
208  */
209 void unserialize_vector( const void *contiguous_vector,
210                          void *noncontiguous_vector,
211                          size_t count,
212                          void *type)
213 {
214   s_smpi_mpi_vector_t* type_c = (s_smpi_mpi_vector_t*)type;
215   int i;
216   for (i = 0; i < type_c->block_count * count; i++) {
217     memcpy(noncontiguous_vector,
218            contiguous_vector, type_c->block_length * type_c->size_oldtype);
219     contiguous_vector += type_c->block_length*type_c->size_oldtype;
220     noncontiguous_vector += type_c->block_stride*type_c->size_oldtype;
221   }
222 }
223
224 /*
225  * Create a Sub type vector to be able to serialize and unserialize it
226  * the structre s_smpi_mpi_vector_t is derived from s_smpi_subtype which
227  * required the functions unserialize and serialize
228  *
229  */
230 s_smpi_mpi_vector_t* smpi_datatype_vector_create( int block_stride,
231                                                   int block_length,
232                                                   int block_count,
233                                                   MPI_Datatype old_type,
234                                                   int size_oldtype){
235   s_smpi_mpi_vector_t *new_t= xbt_new(s_smpi_mpi_vector_t,1);
236   new_t->base.serialize = &serialize_vector;
237   new_t->base.unserialize = &unserialize_vector;
238   new_t->block_stride = block_stride;
239   new_t->block_length = block_length;
240   new_t->block_count = block_count;
241   new_t->old_type = old_type;
242   new_t->size_oldtype = size_oldtype;
243   return new_t;
244 }
245
246 void smpi_datatype_create(MPI_Datatype* new_type, int size, int has_subtype,
247                           void *struct_type, int flags){
248   MPI_Datatype new_t= xbt_new(s_smpi_mpi_datatype_t,1);
249   new_t->size=size;
250   new_t->has_subtype=has_subtype;
251   new_t->lb=0;
252   new_t->ub=size;
253   new_t->flags=flags;
254   new_t->substruct=struct_type;
255   *new_type = new_t;
256 }
257
258 void smpi_datatype_free(MPI_Datatype* type){
259   xbt_free(*type);
260 }
261
262 int smpi_datatype_contiguous(int count, MPI_Datatype old_type, MPI_Datatype* new_type)
263 {
264   int retval;
265   if ((old_type->flags & DT_FLAG_COMMITED) != DT_FLAG_COMMITED) {
266     retval = MPI_ERR_TYPE;
267   } else {
268     smpi_datatype_create(new_type, count *
269                          smpi_datatype_size(old_type),1,NULL, DT_FLAG_CONTIGUOUS);
270     retval=MPI_SUCCESS;
271   }
272   return retval;
273 }
274
275 int smpi_datatype_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type)
276 {
277   int retval;
278   if (blocklen<=0) return MPI_ERR_ARG;
279   if ((old_type->flags & DT_FLAG_COMMITED) != DT_FLAG_COMMITED) {
280     retval = MPI_ERR_TYPE;
281   } else {
282     if(stride != blocklen){
283       s_smpi_mpi_vector_t* subtype = smpi_datatype_vector_create( stride,
284                                                                   blocklen,
285                                                                   count,
286                                                                   old_type,
287                                                                   smpi_datatype_size(old_type));
288
289       smpi_datatype_create(new_type, count * (blocklen) *
290                            smpi_datatype_size(old_type),
291                            1,
292                            subtype,
293                            DT_FLAG_VECTOR);
294       retval=MPI_SUCCESS;
295     }else{
296       /* in this situation the data are contignous thus it's not
297        * required to serialize and unserialize it*/
298       smpi_datatype_create(new_type, count * (blocklen) *
299                            smpi_datatype_size(old_type),
300                            0,
301                            NULL,
302                            DT_FLAG_VECTOR);
303       retval=MPI_SUCCESS;
304     }
305   }
306   return retval;
307 }
308
309 int smpi_datatype_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type)
310 {
311   int retval;
312   if (blocklen<=0) return MPI_ERR_ARG;
313   if ((old_type->flags & DT_FLAG_COMMITED) != DT_FLAG_COMMITED) {
314     retval = MPI_ERR_TYPE;
315   } else {
316     /*FIXME: as for the vector the data should be serialized and
317      * unserialized moreover a structure derived from s_smpi_subtype should
318      * be created*/
319     smpi_datatype_create(new_type, count * ((blocklen *
320                                              smpi_datatype_size(old_type))+stride),
321                          0,
322                          NULL,
323                          DT_FLAG_VECTOR);
324     retval=MPI_SUCCESS;
325   }
326   return retval;
327 }
328
329
330 int smpi_datatype_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
331 {
332   int i;
333   int retval;
334   int size = 0;
335   for(i=0; i< count; i++){
336     if   (blocklens[i]<=0)
337       return MPI_ERR_ARG;
338     size += blocklens[i];
339   }
340   if ((old_type->flags & DT_FLAG_COMMITED) != DT_FLAG_COMMITED) {
341     retval = MPI_ERR_TYPE;
342   } else {
343     /*FIXME: as for the vector the data should be serialized and
344      * unserialized moreover a structure derived from s_smpi_subtype should
345      * be created*/
346     smpi_datatype_create(new_type,  (size) *
347                          smpi_datatype_size(old_type),0, NULL, DT_FLAG_DATA);
348     retval=MPI_SUCCESS;
349   }
350   return retval;
351 }
352
353 int smpi_datatype_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
354 {
355   int i;
356   int retval;
357   int size = 0;
358   for(i=0; i< count; i++){
359     if   (blocklens[i]<=0)
360       return MPI_ERR_ARG;
361     size += blocklens[i];
362   }
363   if ((old_type->flags & DT_FLAG_COMMITED) != DT_FLAG_COMMITED) {
364     retval = MPI_ERR_TYPE;
365   } else {
366     /*FIXME: as for the vector the data should be serialized and
367      * unserialized moreover a structure derived from s_smpi_subtype should
368      * be created*/
369     smpi_datatype_create(new_type,(size * smpi_datatype_size(old_type)), 0,NULL, DT_FLAG_DATA);
370     retval=MPI_SUCCESS;
371   }
372   return retval;
373 }
374
375 int smpi_datatype_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type)
376 {
377   int i;
378   size_t size; //Khalid added this
379
380   for(i=0; i< count; i++){
381     if (blocklens[i]<=0)
382       return MPI_ERR_ARG;
383     if ((old_types[i]->flags & DT_FLAG_COMMITED) != DT_FLAG_COMMITED)
384       return MPI_ERR_TYPE;
385     size += blocklens[i]*smpi_datatype_size(old_types[i]);
386   }
387   /*FIXME: as for the vector the data should be serialized and
388    * unserialized moreover a structure derived from s_smpi_subtype should
389    * be created*/
390   smpi_datatype_create(new_type, size,
391                        0, NULL,
392                        DT_FLAG_DATA);
393   return MPI_SUCCESS;
394 }
395
396 void smpi_datatype_commit(MPI_Datatype *datatype)
397 {
398   (*datatype)->flags=  ((*datatype)->flags | DT_FLAG_COMMITED);
399 }
400
401 typedef struct s_smpi_mpi_op {
402   MPI_User_function *func;
403 } s_smpi_mpi_op_t;
404
405 #define MAX_OP(a, b)  (b) = (a) < (b) ? (b) : (a)
406 #define MIN_OP(a, b)  (b) = (a) < (b) ? (a) : (b)
407 #define SUM_OP(a, b)  (b) += (a)
408 #define PROD_OP(a, b) (b) *= (a)
409 #define LAND_OP(a, b) (b) = (a) && (b)
410 #define LOR_OP(a, b)  (b) = (a) || (b)
411 #define LXOR_OP(a, b) (b) = (!(a) && (b)) || ((a) && !(b))
412 #define BAND_OP(a, b) (b) &= (a)
413 #define BOR_OP(a, b)  (b) |= (a)
414 #define BXOR_OP(a, b) (b) ^= (a)
415 #define MAXLOC_OP(a, b)  (b) = (a.value) < (b.value) ? (b) : (a)
416 #define MINLOC_OP(a, b)  (b) = (a.value) < (b.value) ? (a) : (b)
417 //TODO : MINLOC & MAXLOC
418
419 #define APPLY_FUNC(a, b, length, type, func) \
420 {                                          \
421   int i;                                   \
422   type* x = (type*)(a);                    \
423   type* y = (type*)(b);                    \
424   for(i = 0; i < *(length); i++) {         \
425     func(x[i], y[i]);                      \
426   }                                        \
427 }
428
429 static void max_func(void *a, void *b, int *length,
430                      MPI_Datatype * datatype)
431 {
432   if (*datatype == MPI_CHAR) {
433     APPLY_FUNC(a, b, length, char, MAX_OP);
434   } else if (*datatype == MPI_SHORT) {
435     APPLY_FUNC(a, b, length, short, MAX_OP);
436   } else if (*datatype == MPI_INT) {
437     APPLY_FUNC(a, b, length, int, MAX_OP);
438   } else if (*datatype == MPI_LONG) {
439     APPLY_FUNC(a, b, length, long, MAX_OP);
440   } else if (*datatype == MPI_UNSIGNED_SHORT) {
441     APPLY_FUNC(a, b, length, unsigned short, MAX_OP);
442   } else if (*datatype == MPI_UNSIGNED) {
443     APPLY_FUNC(a, b, length, unsigned int, MAX_OP);
444   } else if (*datatype == MPI_UNSIGNED_LONG) {
445     APPLY_FUNC(a, b, length, unsigned long, MAX_OP);
446   } else if (*datatype == MPI_FLOAT) {
447     APPLY_FUNC(a, b, length, float, MAX_OP);
448   } else if (*datatype == MPI_DOUBLE) {
449     APPLY_FUNC(a, b, length, double, MAX_OP);
450   } else if (*datatype == MPI_LONG_DOUBLE) {
451     APPLY_FUNC(a, b, length, long double, MAX_OP);
452   }
453 }
454
455 static void min_func(void *a, void *b, int *length,
456                      MPI_Datatype * datatype)
457 {
458   if (*datatype == MPI_CHAR) {
459     APPLY_FUNC(a, b, length, char, MIN_OP);
460   } else if (*datatype == MPI_SHORT) {
461     APPLY_FUNC(a, b, length, short, MIN_OP);
462   } else if (*datatype == MPI_INT) {
463     APPLY_FUNC(a, b, length, int, MIN_OP);
464   } else if (*datatype == MPI_LONG) {
465     APPLY_FUNC(a, b, length, long, MIN_OP);
466   } else if (*datatype == MPI_UNSIGNED_SHORT) {
467     APPLY_FUNC(a, b, length, unsigned short, MIN_OP);
468   } else if (*datatype == MPI_UNSIGNED) {
469     APPLY_FUNC(a, b, length, unsigned int, MIN_OP);
470   } else if (*datatype == MPI_UNSIGNED_LONG) {
471     APPLY_FUNC(a, b, length, unsigned long, MIN_OP);
472   } else if (*datatype == MPI_FLOAT) {
473     APPLY_FUNC(a, b, length, float, MIN_OP);
474   } else if (*datatype == MPI_DOUBLE) {
475     APPLY_FUNC(a, b, length, double, MIN_OP);
476   } else if (*datatype == MPI_LONG_DOUBLE) {
477     APPLY_FUNC(a, b, length, long double, MIN_OP);
478   }
479 }
480
481 static void sum_func(void *a, void *b, int *length,
482                      MPI_Datatype * datatype)
483 {
484   if (*datatype == MPI_CHAR) {
485     APPLY_FUNC(a, b, length, char, SUM_OP);
486   } else if (*datatype == MPI_SHORT) {
487     APPLY_FUNC(a, b, length, short, SUM_OP);
488   } else if (*datatype == MPI_INT) {
489     APPLY_FUNC(a, b, length, int, SUM_OP);
490   } else if (*datatype == MPI_LONG) {
491     APPLY_FUNC(a, b, length, long, SUM_OP);
492   } else if (*datatype == MPI_UNSIGNED_SHORT) {
493     APPLY_FUNC(a, b, length, unsigned short, SUM_OP);
494   } else if (*datatype == MPI_UNSIGNED) {
495     APPLY_FUNC(a, b, length, unsigned int, SUM_OP);
496   } else if (*datatype == MPI_UNSIGNED_LONG) {
497     APPLY_FUNC(a, b, length, unsigned long, SUM_OP);
498   } else if (*datatype == MPI_FLOAT) {
499     APPLY_FUNC(a, b, length, float, SUM_OP);
500   } else if (*datatype == MPI_DOUBLE) {
501     APPLY_FUNC(a, b, length, double, SUM_OP);
502   } else if (*datatype == MPI_LONG_DOUBLE) {
503     APPLY_FUNC(a, b, length, long double, SUM_OP);
504   } else if (*datatype == MPI_C_FLOAT_COMPLEX) {
505     APPLY_FUNC(a, b, length, float _Complex, SUM_OP);
506   } else if (*datatype == MPI_C_DOUBLE_COMPLEX) {
507     APPLY_FUNC(a, b, length, double _Complex, SUM_OP);
508   } else if (*datatype == MPI_C_LONG_DOUBLE_COMPLEX) {
509     APPLY_FUNC(a, b, length, long double _Complex, SUM_OP);
510   }
511 }
512
513 static void prod_func(void *a, void *b, int *length,
514                       MPI_Datatype * datatype)
515 {
516   if (*datatype == MPI_CHAR) {
517     APPLY_FUNC(a, b, length, char, PROD_OP);
518   } else if (*datatype == MPI_SHORT) {
519     APPLY_FUNC(a, b, length, short, PROD_OP);
520   } else if (*datatype == MPI_INT) {
521     APPLY_FUNC(a, b, length, int, PROD_OP);
522   } else if (*datatype == MPI_LONG) {
523     APPLY_FUNC(a, b, length, long, PROD_OP);
524   } else if (*datatype == MPI_UNSIGNED_SHORT) {
525     APPLY_FUNC(a, b, length, unsigned short, PROD_OP);
526   } else if (*datatype == MPI_UNSIGNED) {
527     APPLY_FUNC(a, b, length, unsigned int, PROD_OP);
528   } else if (*datatype == MPI_UNSIGNED_LONG) {
529     APPLY_FUNC(a, b, length, unsigned long, PROD_OP);
530   } else if (*datatype == MPI_FLOAT) {
531     APPLY_FUNC(a, b, length, float, PROD_OP);
532   } else if (*datatype == MPI_DOUBLE) {
533     APPLY_FUNC(a, b, length, double, PROD_OP);
534   } else if (*datatype == MPI_LONG_DOUBLE) {
535     APPLY_FUNC(a, b, length, long double, PROD_OP);
536   } else if (*datatype == MPI_C_FLOAT_COMPLEX) {
537     APPLY_FUNC(a, b, length, float _Complex, PROD_OP);
538   } else if (*datatype == MPI_C_DOUBLE_COMPLEX) {
539     APPLY_FUNC(a, b, length, double _Complex, PROD_OP);
540   } else if (*datatype == MPI_C_LONG_DOUBLE_COMPLEX) {
541     APPLY_FUNC(a, b, length, long double _Complex, PROD_OP);
542   }
543 }
544
545 static void land_func(void *a, void *b, int *length,
546                       MPI_Datatype * datatype)
547 {
548   if (*datatype == MPI_CHAR) {
549     APPLY_FUNC(a, b, length, char, LAND_OP);
550   } else if (*datatype == MPI_SHORT) {
551     APPLY_FUNC(a, b, length, short, LAND_OP);
552   } else if (*datatype == MPI_INT) {
553     APPLY_FUNC(a, b, length, int, LAND_OP);
554   } else if (*datatype == MPI_LONG) {
555     APPLY_FUNC(a, b, length, long, LAND_OP);
556   } else if (*datatype == MPI_UNSIGNED_SHORT) {
557     APPLY_FUNC(a, b, length, unsigned short, LAND_OP);
558   } else if (*datatype == MPI_UNSIGNED) {
559     APPLY_FUNC(a, b, length, unsigned int, LAND_OP);
560   } else if (*datatype == MPI_UNSIGNED_LONG) {
561     APPLY_FUNC(a, b, length, unsigned long, LAND_OP);
562   } else if (*datatype == MPI_C_BOOL) {
563     APPLY_FUNC(a, b, length, _Bool, LAND_OP);
564   }
565 }
566
567 static void lor_func(void *a, void *b, int *length,
568                      MPI_Datatype * datatype)
569 {
570   if (*datatype == MPI_CHAR) {
571     APPLY_FUNC(a, b, length, char, LOR_OP);
572   } else if (*datatype == MPI_SHORT) {
573     APPLY_FUNC(a, b, length, short, LOR_OP);
574   } else if (*datatype == MPI_INT) {
575     APPLY_FUNC(a, b, length, int, LOR_OP);
576   } else if (*datatype == MPI_LONG) {
577     APPLY_FUNC(a, b, length, long, LOR_OP);
578   } else if (*datatype == MPI_UNSIGNED_SHORT) {
579     APPLY_FUNC(a, b, length, unsigned short, LOR_OP);
580   } else if (*datatype == MPI_UNSIGNED) {
581     APPLY_FUNC(a, b, length, unsigned int, LOR_OP);
582   } else if (*datatype == MPI_UNSIGNED_LONG) {
583     APPLY_FUNC(a, b, length, unsigned long, LOR_OP);
584   } else if (*datatype == MPI_C_BOOL) {
585     APPLY_FUNC(a, b, length, _Bool, LOR_OP);
586   }
587 }
588
589 static void lxor_func(void *a, void *b, int *length,
590                       MPI_Datatype * datatype)
591 {
592   if (*datatype == MPI_CHAR) {
593     APPLY_FUNC(a, b, length, char, LXOR_OP);
594   } else if (*datatype == MPI_SHORT) {
595     APPLY_FUNC(a, b, length, short, LXOR_OP);
596   } else if (*datatype == MPI_INT) {
597     APPLY_FUNC(a, b, length, int, LXOR_OP);
598   } else if (*datatype == MPI_LONG) {
599     APPLY_FUNC(a, b, length, long, LXOR_OP);
600   } else if (*datatype == MPI_UNSIGNED_SHORT) {
601     APPLY_FUNC(a, b, length, unsigned short, LXOR_OP);
602   } else if (*datatype == MPI_UNSIGNED) {
603     APPLY_FUNC(a, b, length, unsigned int, LXOR_OP);
604   } else if (*datatype == MPI_UNSIGNED_LONG) {
605     APPLY_FUNC(a, b, length, unsigned long, LXOR_OP);
606   } else if (*datatype == MPI_C_BOOL) {
607     APPLY_FUNC(a, b, length, _Bool, LXOR_OP);
608   }
609 }
610
611 static void band_func(void *a, void *b, int *length,
612                       MPI_Datatype * datatype)
613 {
614   if (*datatype == MPI_CHAR) {
615     APPLY_FUNC(a, b, length, char, BAND_OP);
616   }
617   if (*datatype == MPI_SHORT) {
618     APPLY_FUNC(a, b, length, short, BAND_OP);
619   } else if (*datatype == MPI_INT) {
620     APPLY_FUNC(a, b, length, int, BAND_OP);
621   } else if (*datatype == MPI_LONG) {
622     APPLY_FUNC(a, b, length, long, BAND_OP);
623   } else if (*datatype == MPI_UNSIGNED_SHORT) {
624     APPLY_FUNC(a, b, length, unsigned short, BAND_OP);
625   } else if (*datatype == MPI_UNSIGNED) {
626     APPLY_FUNC(a, b, length, unsigned int, BAND_OP);
627   } else if (*datatype == MPI_UNSIGNED_LONG) {
628     APPLY_FUNC(a, b, length, unsigned long, BAND_OP);
629   } else if (*datatype == MPI_BYTE) {
630     APPLY_FUNC(a, b, length, uint8_t, BAND_OP);
631   }
632 }
633
634 static void bor_func(void *a, void *b, int *length,
635                      MPI_Datatype * datatype)
636 {
637   if (*datatype == MPI_CHAR) {
638     APPLY_FUNC(a, b, length, char, BOR_OP);
639   } else if (*datatype == MPI_SHORT) {
640     APPLY_FUNC(a, b, length, short, BOR_OP);
641   } else if (*datatype == MPI_INT) {
642     APPLY_FUNC(a, b, length, int, BOR_OP);
643   } else if (*datatype == MPI_LONG) {
644     APPLY_FUNC(a, b, length, long, BOR_OP);
645   } else if (*datatype == MPI_UNSIGNED_SHORT) {
646     APPLY_FUNC(a, b, length, unsigned short, BOR_OP);
647   } else if (*datatype == MPI_UNSIGNED) {
648     APPLY_FUNC(a, b, length, unsigned int, BOR_OP);
649   } else if (*datatype == MPI_UNSIGNED_LONG) {
650     APPLY_FUNC(a, b, length, unsigned long, BOR_OP);
651   } else if (*datatype == MPI_BYTE) {
652     APPLY_FUNC(a, b, length, uint8_t, BOR_OP);
653   }
654 }
655
656 static void bxor_func(void *a, void *b, int *length,
657                       MPI_Datatype * datatype)
658 {
659   if (*datatype == MPI_CHAR) {
660     APPLY_FUNC(a, b, length, char, BXOR_OP);
661   } else if (*datatype == MPI_SHORT) {
662     APPLY_FUNC(a, b, length, short, BXOR_OP);
663   } else if (*datatype == MPI_INT) {
664     APPLY_FUNC(a, b, length, int, BXOR_OP);
665   } else if (*datatype == MPI_LONG) {
666     APPLY_FUNC(a, b, length, long, BXOR_OP);
667   } else if (*datatype == MPI_UNSIGNED_SHORT) {
668     APPLY_FUNC(a, b, length, unsigned short, BXOR_OP);
669   } else if (*datatype == MPI_UNSIGNED) {
670     APPLY_FUNC(a, b, length, unsigned int, BXOR_OP);
671   } else if (*datatype == MPI_UNSIGNED_LONG) {
672     APPLY_FUNC(a, b, length, unsigned long, BXOR_OP);
673   } else if (*datatype == MPI_BYTE) {
674     APPLY_FUNC(a, b, length, uint8_t, BXOR_OP);
675   }
676 }
677
678 static void minloc_func(void *a, void *b, int *length,
679                         MPI_Datatype * datatype)
680 {
681   if (*datatype == MPI_FLOAT_INT) {
682     APPLY_FUNC(a, b, length, float_int, MINLOC_OP);
683   } else if (*datatype == MPI_LONG_INT) {
684     APPLY_FUNC(a, b, length, long_int, MINLOC_OP);
685   } else if (*datatype == MPI_DOUBLE_INT) {
686     APPLY_FUNC(a, b, length, double_int, MINLOC_OP);
687   } else if (*datatype == MPI_SHORT_INT) {
688     APPLY_FUNC(a, b, length, short_int, MINLOC_OP);
689   } else if (*datatype == MPI_2INT) {
690     APPLY_FUNC(a, b, length, int_int, MINLOC_OP);
691   } else if (*datatype == MPI_LONG_DOUBLE_INT) {
692     APPLY_FUNC(a, b, length, long_double_int, MINLOC_OP);
693   }
694 }
695
696 static void maxloc_func(void *a, void *b, int *length,
697                         MPI_Datatype * datatype)
698 {
699   if (*datatype == MPI_FLOAT_INT) {
700     APPLY_FUNC(a, b, length, float_int, MAXLOC_OP);
701   } else if (*datatype == MPI_LONG_INT) {
702     APPLY_FUNC(a, b, length, long_int, MAXLOC_OP);
703   } else if (*datatype == MPI_DOUBLE_INT) {
704     APPLY_FUNC(a, b, length, double_int, MAXLOC_OP);
705   } else if (*datatype == MPI_SHORT_INT) {
706     APPLY_FUNC(a, b, length, short_int, MAXLOC_OP);
707   } else if (*datatype == MPI_2INT) {
708     APPLY_FUNC(a, b, length, int_int, MAXLOC_OP);
709   } else if (*datatype == MPI_LONG_DOUBLE_INT) {
710     APPLY_FUNC(a, b, length, long_double_int, MAXLOC_OP);
711   }
712 }
713
714
715 #define CREATE_MPI_OP(name, func)                             \
716   static s_smpi_mpi_op_t mpi_##name = { &(func) /* func */ }; \
717 MPI_Op name = &mpi_##name;
718
719 CREATE_MPI_OP(MPI_MAX, max_func);
720 CREATE_MPI_OP(MPI_MIN, min_func);
721 CREATE_MPI_OP(MPI_SUM, sum_func);
722 CREATE_MPI_OP(MPI_PROD, prod_func);
723 CREATE_MPI_OP(MPI_LAND, land_func);
724 CREATE_MPI_OP(MPI_LOR, lor_func);
725 CREATE_MPI_OP(MPI_LXOR, lxor_func);
726 CREATE_MPI_OP(MPI_BAND, band_func);
727 CREATE_MPI_OP(MPI_BOR, bor_func);
728 CREATE_MPI_OP(MPI_BXOR, bxor_func);
729 CREATE_MPI_OP(MPI_MAXLOC, maxloc_func);
730 CREATE_MPI_OP(MPI_MINLOC, minloc_func);
731
732 MPI_Op smpi_op_new(MPI_User_function * function, int commute)
733 {
734   MPI_Op op;
735
736   //FIXME: add commute param
737   op = xbt_new(s_smpi_mpi_op_t, 1);
738   op->func = function;
739   return op;
740 }
741
742 void smpi_op_destroy(MPI_Op op)
743 {
744   xbt_free(op);
745 }
746
747 void smpi_op_apply(MPI_Op op, void *invec, void *inoutvec, int *len,
748                    MPI_Datatype * datatype)
749 {
750   op->func(invec, inoutvec, len, datatype);
751 }