Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
adapt two collectives of starmpi to avoid timing issues, by using only smpi calls...
[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 #define CREATE_MPI_DATATYPE_NULL(name)       \
32   static s_smpi_mpi_datatype_t mpi_##name = { \
33     0,  /* size */                 \
34     0,             /*was 1 has_subtype*/             \
35     0,             /* lb */                   \
36     0,  /* ub = lb + size */       \
37     DT_FLAG_BASIC,  /* flags */              \
38     NULL           /* pointer on extended struct*/ \
39   };                                          \
40 MPI_Datatype name = &mpi_##name;
41
42 //The following are datatypes for the MPI functions MPI_MAXLOC and MPI_MINLOC.
43 typedef struct {
44   float value;
45   int index;
46 } float_int;
47 typedef struct {
48   float value;
49   float index;
50 } float_float;
51 typedef struct {
52   double value;
53   double index;
54 } double_double;
55 typedef struct {
56   long value;
57   int index;
58 } long_int;
59 typedef struct {
60   double value;
61   int index;
62 } double_int;
63 typedef struct {
64   short value;
65   int index;
66 } short_int;
67 typedef struct {
68   int value;
69   int index;
70 } int_int;
71 typedef struct {
72   long double value;
73   int index;
74 } long_double_int;
75
76 // Predefined data types
77 CREATE_MPI_DATATYPE(MPI_CHAR, char);
78 CREATE_MPI_DATATYPE(MPI_SHORT, short);
79 CREATE_MPI_DATATYPE(MPI_INT, int);
80 CREATE_MPI_DATATYPE(MPI_LONG, long);
81 CREATE_MPI_DATATYPE(MPI_LONG_LONG, long long);
82 CREATE_MPI_DATATYPE(MPI_SIGNED_CHAR, signed char);
83 CREATE_MPI_DATATYPE(MPI_UNSIGNED_CHAR, unsigned char);
84 CREATE_MPI_DATATYPE(MPI_UNSIGNED_SHORT, unsigned short);
85 CREATE_MPI_DATATYPE(MPI_UNSIGNED, unsigned int);
86 CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG, unsigned long);
87 CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG_LONG, unsigned long long);
88 CREATE_MPI_DATATYPE(MPI_FLOAT, float);
89 CREATE_MPI_DATATYPE(MPI_DOUBLE, double);
90 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE, long double);
91 CREATE_MPI_DATATYPE(MPI_WCHAR, wchar_t);
92 CREATE_MPI_DATATYPE(MPI_C_BOOL, _Bool);
93 CREATE_MPI_DATATYPE(MPI_INT8_T, int8_t);
94 CREATE_MPI_DATATYPE(MPI_INT16_T, int16_t);
95 CREATE_MPI_DATATYPE(MPI_INT32_T, int32_t);
96 CREATE_MPI_DATATYPE(MPI_INT64_T, int64_t);
97 CREATE_MPI_DATATYPE(MPI_UINT8_T, uint8_t);
98 CREATE_MPI_DATATYPE(MPI_UINT16_T, uint16_t);
99 CREATE_MPI_DATATYPE(MPI_UINT32_T, uint32_t);
100 CREATE_MPI_DATATYPE(MPI_UINT64_T, uint64_t);
101 CREATE_MPI_DATATYPE(MPI_C_FLOAT_COMPLEX, float _Complex);
102 CREATE_MPI_DATATYPE(MPI_C_DOUBLE_COMPLEX, double _Complex);
103 CREATE_MPI_DATATYPE(MPI_C_LONG_DOUBLE_COMPLEX, long double _Complex);
104 CREATE_MPI_DATATYPE(MPI_AINT, MPI_Aint);
105 CREATE_MPI_DATATYPE(MPI_OFFSET, MPI_Offset);
106
107 CREATE_MPI_DATATYPE(MPI_FLOAT_INT, float_int);
108 CREATE_MPI_DATATYPE(MPI_LONG_INT, long_int);
109 CREATE_MPI_DATATYPE(MPI_DOUBLE_INT, double_int);
110 CREATE_MPI_DATATYPE(MPI_SHORT_INT, short_int);
111 CREATE_MPI_DATATYPE(MPI_2INT, int_int);
112 CREATE_MPI_DATATYPE(MPI_2FLOAT, float_float);
113 CREATE_MPI_DATATYPE(MPI_2DOUBLE, double_double);
114
115 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE_INT, long_double_int);
116
117 CREATE_MPI_DATATYPE_NULL(MPI_UB);
118 CREATE_MPI_DATATYPE_NULL(MPI_LB);
119 CREATE_MPI_DATATYPE_NULL(MPI_PACKED);
120 // Internal use only
121 CREATE_MPI_DATATYPE(MPI_PTR, void*);
122
123
124 size_t smpi_datatype_size(MPI_Datatype datatype)
125 {
126   return datatype->size;
127 }
128
129
130
131 MPI_Aint smpi_datatype_lb(MPI_Datatype datatype)
132 {
133   return datatype->lb;
134 }
135
136 MPI_Aint smpi_datatype_ub(MPI_Datatype datatype)
137 {
138   return datatype->ub;
139 }
140
141 int smpi_datatype_extent(MPI_Datatype datatype, MPI_Aint * lb,
142                          MPI_Aint * extent)
143 {
144   *lb = datatype->lb;
145   *extent = datatype->ub - datatype->lb;
146   return MPI_SUCCESS;
147 }
148
149 MPI_Aint smpi_datatype_get_extent(MPI_Datatype datatype){
150   return datatype->ub - datatype->lb;
151 }
152
153 int smpi_datatype_copy(void *sendbuf, int sendcount, MPI_Datatype sendtype,
154                        void *recvbuf, int recvcount, MPI_Datatype recvtype)
155 {
156   int count;
157
158   /* First check if we really have something to do */
159   if (recvcount > 0 && recvbuf != sendbuf) {
160     /* FIXME: treat packed cases */
161     sendcount *= smpi_datatype_size(sendtype);
162     recvcount *= smpi_datatype_size(recvtype);
163     count = sendcount < recvcount ? sendcount : recvcount;
164
165     if(sendtype->has_subtype == 0 && recvtype->has_subtype == 0) {
166       memcpy(recvbuf, sendbuf, count);
167     }
168     else if (sendtype->has_subtype == 0)
169     {
170       s_smpi_subtype_t *subtype =  recvtype->substruct;
171       subtype->unserialize( sendbuf, recvbuf,1, subtype);
172     }
173     else if (recvtype->has_subtype == 0)
174     {
175       s_smpi_subtype_t *subtype =  sendtype->substruct;
176       subtype->serialize(sendbuf, recvbuf,1, subtype);
177     }else{
178       s_smpi_subtype_t *subtype =  sendtype->substruct;
179
180       s_smpi_mpi_vector_t* type_c = (s_smpi_mpi_vector_t*)sendtype;
181
182       void * buf_tmp = xbt_malloc(count * type_c->size_oldtype);
183
184       subtype->serialize( sendbuf, buf_tmp,1, subtype);
185       subtype =  recvtype->substruct;
186       subtype->unserialize(recvbuf, buf_tmp,1, subtype);
187
188       free(buf_tmp);
189     }
190   }
191
192   return sendcount > recvcount ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
193 }
194
195 /*
196  *  Copies noncontiguous data into contiguous memory.
197  *  @param contiguous_vector - output vector
198  *  @param noncontiguous_vector - input vector
199  *  @param type - pointer contening :
200  *      - stride - stride of between noncontiguous data
201  *      - block_length - the width or height of blocked matrix
202  *      - count - the number of rows of matrix
203  */
204 void serialize_vector( const void *noncontiguous_vector,
205                        void *contiguous_vector,
206                        size_t count,
207                        void *type)
208 {
209   s_smpi_mpi_vector_t* type_c = (s_smpi_mpi_vector_t*)type;
210   int i;
211   char* contiguous_vector_char = (char*)contiguous_vector;
212   char* noncontiguous_vector_char = (char*)noncontiguous_vector;
213
214   for (i = 0; i < type_c->block_count * count; i++) {
215       if (type_c->old_type->has_subtype == 0)
216         memcpy(contiguous_vector_char,
217                noncontiguous_vector_char, type_c->block_length * type_c->size_oldtype);
218       else
219         ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_vector_char,
220                                                                      contiguous_vector_char,
221                                                                      type_c->block_length,
222                                                                      type_c->old_type->substruct);
223
224     contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
225     noncontiguous_vector_char += type_c->block_stride*smpi_datatype_get_extent(type_c->old_type);
226   }
227 }
228
229 /*
230  *  Copies contiguous data into noncontiguous memory.
231  *  @param noncontiguous_vector - output vector
232  *  @param contiguous_vector - input vector
233  *  @param type - pointer contening :
234  *      - stride - stride of between noncontiguous data
235  *      - block_length - the width or height of blocked matrix
236  *      - count - the number of rows of matrix
237  */
238 void unserialize_vector( const void *contiguous_vector,
239                          void *noncontiguous_vector,
240                          size_t count,
241                          void *type)
242 {
243   s_smpi_mpi_vector_t* type_c = (s_smpi_mpi_vector_t*)type;
244   int i;
245
246   char* contiguous_vector_char = (char*)contiguous_vector;
247   char* noncontiguous_vector_char = (char*)noncontiguous_vector;
248
249   for (i = 0; i < type_c->block_count * count; i++) {
250     if (type_c->old_type->has_subtype == 0)
251       memcpy(noncontiguous_vector_char,
252              contiguous_vector_char, type_c->block_length * type_c->size_oldtype);
253     else
254       ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_vector_char,
255                                                                      noncontiguous_vector_char,
256                                                                      type_c->block_length,
257                                                                      type_c->old_type->substruct);
258     contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
259     noncontiguous_vector_char += type_c->block_stride*smpi_datatype_get_extent(type_c->old_type);
260   }
261 }
262
263 /*
264  * Create a Sub type vector to be able to serialize and unserialize it
265  * the structure s_smpi_mpi_vector_t is derived from s_smpi_subtype which
266  * required the functions unserialize and serialize
267  *
268  */
269 s_smpi_mpi_vector_t* smpi_datatype_vector_create( int block_stride,
270                                                   int block_length,
271                                                   int block_count,
272                                                   MPI_Datatype old_type,
273                                                   int size_oldtype){
274   s_smpi_mpi_vector_t *new_t= xbt_new(s_smpi_mpi_vector_t,1);
275   new_t->base.serialize = &serialize_vector;
276   new_t->base.unserialize = &unserialize_vector;
277   new_t->base.subtype_free = &free_vector;
278   new_t->block_stride = block_stride;
279   new_t->block_length = block_length;
280   new_t->block_count = block_count;
281   new_t->old_type = old_type;
282   new_t->size_oldtype = size_oldtype;
283   return new_t;
284 }
285
286 void smpi_datatype_create(MPI_Datatype* new_type, int size,int lb, int ub, int has_subtype,
287                           void *struct_type, int flags){
288   MPI_Datatype new_t= xbt_new(s_smpi_mpi_datatype_t,1);
289   new_t->size = size;
290   new_t->has_subtype = has_subtype;
291   new_t->lb = lb;
292   new_t->ub = ub;
293   new_t->flags = flags;
294   new_t->substruct = struct_type;
295   new_t->in_use=0;
296   *new_type = new_t;
297 }
298
299 void smpi_datatype_free(MPI_Datatype* type){
300
301   if((*type)->flags & DT_FLAG_PREDEFINED)return;
302
303   //if still used, mark for deletion
304   if((*type)->in_use!=0){
305       (*type)->flags |=DT_FLAG_DESTROYED;
306       return;
307   }
308
309   if ((*type)->has_subtype == 1){
310     ((s_smpi_subtype_t *)(*type)->substruct)->subtype_free(type);  
311     xbt_free((*type)->substruct);
312   }
313   xbt_free(*type);
314
315 }
316
317 void smpi_datatype_use(MPI_Datatype type){
318   if(type)type->in_use++;
319 }
320
321
322 void smpi_datatype_unuse(MPI_Datatype type){
323   if(type && type->in_use-- == 0 && (type->flags & DT_FLAG_DESTROYED))
324     smpi_datatype_free(&type);
325 }
326
327 int smpi_datatype_contiguous(int count, MPI_Datatype old_type, MPI_Datatype* new_type)
328 {
329   int retval;
330   if(old_type->has_subtype){
331           //handle this case as a hvector with stride equals to the extent of the datatype
332           return smpi_datatype_hvector(count, 1, smpi_datatype_get_extent(old_type), old_type, new_type);
333   }
334   smpi_datatype_create(new_type,
335                                           count * smpi_datatype_size(old_type),
336                                           0,count * smpi_datatype_size(old_type),
337                                           0,NULL, DT_FLAG_CONTIGUOUS);
338   retval=MPI_SUCCESS;
339   return retval;
340 }
341
342 int smpi_datatype_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type)
343 {
344   int retval;
345   if (blocklen<=0) return MPI_ERR_ARG;
346   MPI_Aint lb = 0;
347   MPI_Aint ub = 0;
348   if(count>0){
349     lb=smpi_datatype_lb(old_type);
350     ub=((count-1)*stride+blocklen-1)*smpi_datatype_get_extent(old_type)+smpi_datatype_ub(old_type);
351   }
352   if(old_type->has_subtype || stride != blocklen){
353
354
355     s_smpi_mpi_vector_t* subtype = smpi_datatype_vector_create( stride,
356                                                                 blocklen,
357                                                                 count,
358                                                                 old_type,
359                                                                 smpi_datatype_size(old_type));
360     smpi_datatype_create(new_type,
361                          count * (blocklen) * smpi_datatype_size(old_type), lb,
362                          ub,
363                          1,
364                          subtype,
365                          DT_FLAG_VECTOR);
366     retval=MPI_SUCCESS;
367   }else{
368     /* in this situation the data are contignous thus it's not
369      * required to serialize and unserialize it*/
370     smpi_datatype_create(new_type, count * blocklen *
371                          smpi_datatype_size(old_type), 0, ((count -1) * stride + blocklen)*
372                          smpi_datatype_size(old_type),
373                          0,
374                          NULL,
375                          DT_FLAG_VECTOR|DT_FLAG_CONTIGUOUS);
376     retval=MPI_SUCCESS;
377   }
378   return retval;
379 }
380
381 void free_vector(MPI_Datatype* d){
382 }
383
384 /*
385 Hvector Implementation - Vector with stride in bytes
386 */
387
388
389 /*
390  *  Copies noncontiguous data into contiguous memory.
391  *  @param contiguous_hvector - output hvector
392  *  @param noncontiguous_hvector - input hvector
393  *  @param type - pointer contening :
394  *      - stride - stride of between noncontiguous data, in bytes
395  *      - block_length - the width or height of blocked matrix
396  *      - count - the number of rows of matrix
397  */
398 void serialize_hvector( const void *noncontiguous_hvector,
399                        void *contiguous_hvector,
400                        size_t count,
401                        void *type)
402 {
403   s_smpi_mpi_hvector_t* type_c = (s_smpi_mpi_hvector_t*)type;
404   int i;
405   char* contiguous_vector_char = (char*)contiguous_hvector;
406   char* noncontiguous_vector_char = (char*)noncontiguous_hvector;
407
408   for (i = 0; i < type_c->block_count * count; i++) {
409     if (type_c->old_type->has_subtype == 0)
410       memcpy(contiguous_vector_char,
411            noncontiguous_vector_char, type_c->block_length * type_c->size_oldtype);
412     else
413       ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_vector_char,
414                                                                    contiguous_vector_char,
415                                                                    type_c->block_length,
416                                                                    type_c->old_type->substruct);
417
418     contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
419     noncontiguous_vector_char += type_c->block_stride;
420   }
421 }
422 /*
423  *  Copies contiguous data into noncontiguous memory.
424  *  @param noncontiguous_vector - output hvector
425  *  @param contiguous_vector - input hvector
426  *  @param type - pointer contening :
427  *      - stride - stride of between noncontiguous data, in bytes
428  *      - block_length - the width or height of blocked matrix
429  *      - count - the number of rows of matrix
430  */
431 void unserialize_hvector( const void *contiguous_vector,
432                          void *noncontiguous_vector,
433                          size_t count,
434                          void *type)
435 {
436   s_smpi_mpi_hvector_t* type_c = (s_smpi_mpi_hvector_t*)type;
437   int i;
438
439   char* contiguous_vector_char = (char*)contiguous_vector;
440   char* noncontiguous_vector_char = (char*)noncontiguous_vector;
441
442   for (i = 0; i < type_c->block_count * count; i++) {
443     if (type_c->old_type->has_subtype == 0)
444       memcpy(noncontiguous_vector_char,
445            contiguous_vector_char, type_c->block_length * type_c->size_oldtype);
446     else
447       ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_vector_char,
448                                                                      noncontiguous_vector_char,
449                                                                      type_c->block_length,
450                                                                      type_c->old_type->substruct);
451     contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
452     noncontiguous_vector_char += type_c->block_stride;
453   }
454 }
455
456 /*
457  * Create a Sub type vector to be able to serialize and unserialize it
458  * the structure s_smpi_mpi_vector_t is derived from s_smpi_subtype which
459  * required the functions unserialize and serialize
460  *
461  */
462 s_smpi_mpi_hvector_t* smpi_datatype_hvector_create( MPI_Aint block_stride,
463                                                   int block_length,
464                                                   int block_count,
465                                                   MPI_Datatype old_type,
466                                                   int size_oldtype){
467   s_smpi_mpi_hvector_t *new_t= xbt_new(s_smpi_mpi_hvector_t,1);
468   new_t->base.serialize = &serialize_hvector;
469   new_t->base.unserialize = &unserialize_hvector;
470   new_t->base.subtype_free = &free_hvector;
471   new_t->block_stride = block_stride;
472   new_t->block_length = block_length;
473   new_t->block_count = block_count;
474   new_t->old_type = old_type;
475   new_t->size_oldtype = size_oldtype;
476   return new_t;
477 }
478
479 //do nothing for vector types
480 void free_hvector(MPI_Datatype* d){
481 }
482
483 int smpi_datatype_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type)
484 {
485   int retval;
486   if (blocklen<=0) return MPI_ERR_ARG;
487   MPI_Aint lb = 0;
488   MPI_Aint ub = 0;
489   if(count>0){
490     lb=smpi_datatype_lb(old_type);
491     ub=((count-1)*stride)+(blocklen-1)*smpi_datatype_get_extent(old_type)+smpi_datatype_ub(old_type);
492   }
493   if(old_type->has_subtype || stride != blocklen*smpi_datatype_get_extent(old_type)){
494     s_smpi_mpi_hvector_t* subtype = smpi_datatype_hvector_create( stride,
495                                                                   blocklen,
496                                                                   count,
497                                                                   old_type,
498                                                                   smpi_datatype_size(old_type));
499
500     smpi_datatype_create(new_type, count * blocklen * smpi_datatype_size(old_type),
501                                                  lb,ub,
502                          1,
503                          subtype,
504                          DT_FLAG_VECTOR);
505     retval=MPI_SUCCESS;
506   }else{
507     smpi_datatype_create(new_type, count * blocklen *
508                                              smpi_datatype_size(old_type),0,count * blocklen *
509                                              smpi_datatype_size(old_type),
510                                             0,
511                                             NULL,
512                                             DT_FLAG_VECTOR|DT_FLAG_CONTIGUOUS);
513     retval=MPI_SUCCESS;
514   }
515   return retval;
516 }
517
518
519 /*
520 Indexed Implementation
521 */
522
523 /*
524  *  Copies noncontiguous data into contiguous memory.
525  *  @param contiguous_indexed - output indexed
526  *  @param noncontiguous_indexed - input indexed
527  *  @param type - pointer contening :
528  *      - block_lengths - the width or height of blocked matrix
529  *      - block_indices - indices of each data, in element
530  *      - count - the number of rows of matrix
531  */
532 void serialize_indexed( const void *noncontiguous_indexed,
533                        void *contiguous_indexed,
534                        size_t count,
535                        void *type)
536 {
537   s_smpi_mpi_indexed_t* type_c = (s_smpi_mpi_indexed_t*)type;
538   int i,j;
539   char* contiguous_indexed_char = (char*)contiguous_indexed;
540   char* noncontiguous_indexed_char = (char*)noncontiguous_indexed;
541   for(j=0; j<count;j++){
542     for (i = 0; i < type_c->block_count; i++) {
543       if (type_c->old_type->has_subtype == 0)
544         memcpy(contiguous_indexed_char,
545                      noncontiguous_indexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
546       else
547         ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_indexed_char,
548                                                                      contiguous_indexed_char,
549                                                                      type_c->block_lengths[i],
550                                                                      type_c->old_type->substruct);
551
552
553       contiguous_indexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
554       if (i<type_c->block_count-1)noncontiguous_indexed_char = (char*)noncontiguous_indexed + type_c->block_indices[i+1]*smpi_datatype_get_extent(type_c->old_type);
555       else noncontiguous_indexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
556     }
557     noncontiguous_indexed=(void*)noncontiguous_indexed_char;
558   }
559 }
560 /*
561  *  Copies contiguous data into noncontiguous memory.
562  *  @param noncontiguous_indexed - output indexed
563  *  @param contiguous_indexed - input indexed
564  *  @param type - pointer contening :
565  *      - block_lengths - the width or height of blocked matrix
566  *      - block_indices - indices of each data, in element
567  *      - count - the number of rows of matrix
568  */
569 void unserialize_indexed( const void *contiguous_indexed,
570                          void *noncontiguous_indexed,
571                          size_t count,
572                          void *type)
573 {
574   s_smpi_mpi_indexed_t* type_c = (s_smpi_mpi_indexed_t*)type;
575   int i,j;
576
577   char* contiguous_indexed_char = (char*)contiguous_indexed;
578   char* noncontiguous_indexed_char = (char*)noncontiguous_indexed;
579   for(j=0; j<count;j++){
580     for (i = 0; i < type_c->block_count; i++) {
581       if (type_c->old_type->has_subtype == 0)
582         memcpy(noncontiguous_indexed_char,
583              contiguous_indexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
584       else
585         ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_indexed_char,
586                                                                        noncontiguous_indexed_char,
587                                                                        type_c->block_lengths[i],
588                                                                        type_c->old_type->substruct);
589
590       contiguous_indexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
591       if (i<type_c->block_count-1)
592         noncontiguous_indexed_char = (char*)noncontiguous_indexed + type_c->block_indices[i+1]*smpi_datatype_get_extent(type_c->old_type);
593       else noncontiguous_indexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
594     }
595     noncontiguous_indexed=(void*)noncontiguous_indexed_char;
596   }
597 }
598
599 void free_indexed(MPI_Datatype* type){
600   xbt_free(((s_smpi_mpi_indexed_t *)(*type)->substruct)->block_lengths);
601   xbt_free(((s_smpi_mpi_indexed_t *)(*type)->substruct)->block_indices);
602 }
603
604 /*
605  * Create a Sub type indexed to be able to serialize and unserialize it
606  * the structure s_smpi_mpi_indexed_t is derived from s_smpi_subtype which
607  * required the functions unserialize and serialize
608  */
609 s_smpi_mpi_indexed_t* smpi_datatype_indexed_create( int* block_lengths,
610                                                   int* block_indices,
611                                                   int block_count,
612                                                   MPI_Datatype old_type,
613                                                   int size_oldtype){
614   s_smpi_mpi_indexed_t *new_t= xbt_new(s_smpi_mpi_indexed_t,1);
615   new_t->base.serialize = &serialize_indexed;
616   new_t->base.unserialize = &unserialize_indexed;
617   new_t->base.subtype_free = &free_indexed;
618  //TODO : add a custom function for each time to clean these 
619   new_t->block_lengths= xbt_new(int, block_count);
620   new_t->block_indices= xbt_new(int, block_count);
621   int i;
622   for(i=0;i<block_count;i++){
623     new_t->block_lengths[i]=block_lengths[i];
624     new_t->block_indices[i]=block_indices[i];
625   }
626   new_t->block_count = block_count;
627   new_t->old_type = old_type;
628   new_t->size_oldtype = size_oldtype;
629   return new_t;
630 }
631
632
633 int smpi_datatype_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
634 {
635   int i;
636   int retval;
637   int size = 0;
638   int contiguous=1;
639   MPI_Aint lb = 0;
640   MPI_Aint ub = 0;
641   if(count>0){
642     lb=indices[0]*smpi_datatype_get_extent(old_type);
643     ub=indices[0]*smpi_datatype_get_extent(old_type) + blocklens[0]*smpi_datatype_ub(old_type);
644   }
645
646   for(i=0; i< count; i++){
647     if   (blocklens[i]<=0)
648       return MPI_ERR_ARG;
649     size += blocklens[i];
650
651     if(indices[i]*smpi_datatype_get_extent(old_type)+smpi_datatype_lb(old_type)<lb)
652         lb = indices[i]*smpi_datatype_get_extent(old_type)+smpi_datatype_lb(old_type);
653     if(indices[i]*smpi_datatype_get_extent(old_type)+blocklens[i]*smpi_datatype_ub(old_type)>ub)
654         ub = indices[i]*smpi_datatype_get_extent(old_type)+blocklens[i]*smpi_datatype_ub(old_type);
655
656     if ( (i< count -1) && (indices[i]+blocklens[i] != indices[i+1]) )contiguous=0;
657   }
658   if (old_type->has_subtype == 1)
659     contiguous=0;
660
661   if(!contiguous){
662     s_smpi_mpi_indexed_t* subtype = smpi_datatype_indexed_create( blocklens,
663                                                                   indices,
664                                                                   count,
665                                                                   old_type,
666                                                                   smpi_datatype_size(old_type));
667      smpi_datatype_create(new_type,  size *
668                          smpi_datatype_size(old_type),lb,ub,1, subtype, DT_FLAG_DATA);
669   }else{
670     smpi_datatype_create(new_type,  size *
671                          smpi_datatype_size(old_type),0,size *
672                          smpi_datatype_size(old_type),0, NULL, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
673   }
674   retval=MPI_SUCCESS;
675   return retval;
676 }
677
678
679 /*
680 Hindexed Implementation - Indexed with indices in bytes 
681 */
682
683 /*
684  *  Copies noncontiguous data into contiguous memory.
685  *  @param contiguous_hindexed - output hindexed
686  *  @param noncontiguous_hindexed - input hindexed
687  *  @param type - pointer contening :
688  *      - block_lengths - the width or height of blocked matrix
689  *      - block_indices - indices of each data, in bytes
690  *      - count - the number of rows of matrix
691  */
692 void serialize_hindexed( const void *noncontiguous_hindexed,
693                        void *contiguous_hindexed,
694                        size_t count,
695                        void *type)
696 {
697   s_smpi_mpi_hindexed_t* type_c = (s_smpi_mpi_hindexed_t*)type;
698   int i,j;
699   char* contiguous_hindexed_char = (char*)contiguous_hindexed;
700   char* noncontiguous_hindexed_char = (char*)noncontiguous_hindexed;
701   for(j=0; j<count;j++){
702     for (i = 0; i < type_c->block_count; i++) {
703       if (type_c->old_type->has_subtype == 0)
704         memcpy(contiguous_hindexed_char,
705                      noncontiguous_hindexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
706       else
707         ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_hindexed_char,
708                                                                      contiguous_hindexed_char,
709                                                                      type_c->block_lengths[i],
710                                                                      type_c->old_type->substruct);
711
712       contiguous_hindexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
713       if (i<type_c->block_count-1)noncontiguous_hindexed_char = (char*)noncontiguous_hindexed + type_c->block_indices[i+1];
714       else noncontiguous_hindexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
715     }
716     noncontiguous_hindexed=(void*)noncontiguous_hindexed_char;
717   }
718 }
719 /*
720  *  Copies contiguous data into noncontiguous memory.
721  *  @param noncontiguous_hindexed - output hindexed
722  *  @param contiguous_hindexed - input hindexed
723  *  @param type - pointer contening :
724  *      - block_lengths - the width or height of blocked matrix
725  *      - block_indices - indices of each data, in bytes
726  *      - count - the number of rows of matrix
727  */
728 void unserialize_hindexed( const void *contiguous_hindexed,
729                          void *noncontiguous_hindexed,
730                          size_t count,
731                          void *type)
732 {
733   s_smpi_mpi_hindexed_t* type_c = (s_smpi_mpi_hindexed_t*)type;
734   int i,j;
735
736   char* contiguous_hindexed_char = (char*)contiguous_hindexed;
737   char* noncontiguous_hindexed_char = (char*)noncontiguous_hindexed;
738   for(j=0; j<count;j++){
739     for (i = 0; i < type_c->block_count; i++) {
740       if (type_c->old_type->has_subtype == 0)
741         memcpy(noncontiguous_hindexed_char,
742                contiguous_hindexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
743       else
744         ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_hindexed_char,
745                                                                        noncontiguous_hindexed_char,
746                                                                        type_c->block_lengths[i],
747                                                                        type_c->old_type->substruct);
748
749       contiguous_hindexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
750       if (i<type_c->block_count-1)noncontiguous_hindexed_char = (char*)noncontiguous_hindexed + type_c->block_indices[i+1];
751       else noncontiguous_hindexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
752     }
753     noncontiguous_hindexed=(void*)noncontiguous_hindexed_char;
754   }
755 }
756
757 void free_hindexed(MPI_Datatype* type){
758   xbt_free(((s_smpi_mpi_hindexed_t *)(*type)->substruct)->block_lengths);
759   xbt_free(((s_smpi_mpi_hindexed_t *)(*type)->substruct)->block_indices);
760 }
761
762 /*
763  * Create a Sub type hindexed to be able to serialize and unserialize it
764  * the structure s_smpi_mpi_hindexed_t is derived from s_smpi_subtype which
765  * required the functions unserialize and serialize
766  */
767 s_smpi_mpi_hindexed_t* smpi_datatype_hindexed_create( int* block_lengths,
768                                                   MPI_Aint* block_indices,
769                                                   int block_count,
770                                                   MPI_Datatype old_type,
771                                                   int size_oldtype){
772   s_smpi_mpi_hindexed_t *new_t= xbt_new(s_smpi_mpi_hindexed_t,1);
773   new_t->base.serialize = &serialize_hindexed;
774   new_t->base.unserialize = &unserialize_hindexed;
775   new_t->base.subtype_free = &free_hindexed;
776  //TODO : add a custom function for each time to clean these 
777   new_t->block_lengths= xbt_new(int, block_count);
778   new_t->block_indices= xbt_new(MPI_Aint, block_count);
779   int i;
780   for(i=0;i<block_count;i++){
781     new_t->block_lengths[i]=block_lengths[i];
782     new_t->block_indices[i]=block_indices[i];
783   }
784   new_t->block_count = block_count;
785   new_t->old_type = old_type;
786   new_t->size_oldtype = size_oldtype;
787   return new_t;
788 }
789
790
791 int smpi_datatype_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
792 {
793   int i;
794   int retval;
795   int size = 0;
796   int contiguous=1;
797   MPI_Aint lb = 0;
798   MPI_Aint ub = 0;
799   if(count>0){
800     lb=indices[0] + smpi_datatype_lb(old_type);
801     ub=indices[0] + blocklens[0]*smpi_datatype_ub(old_type);
802   }
803   for(i=0; i< count; i++){
804     if   (blocklens[i]<=0)
805       return MPI_ERR_ARG;
806     size += blocklens[i];
807
808     if(indices[i]+smpi_datatype_lb(old_type)<lb) lb = indices[i]+smpi_datatype_lb(old_type);
809     if(indices[i]+blocklens[i]*smpi_datatype_ub(old_type)>ub) ub = indices[i]+blocklens[i]*smpi_datatype_ub(old_type);
810
811     if ( (i< count -1) && (indices[i]+blocklens[i]*smpi_datatype_size(old_type) != indices[i+1]) )contiguous=0;
812   }
813   if (old_type->has_subtype == 1 || lb!=0)
814     contiguous=0;
815
816   if(!contiguous){
817     s_smpi_mpi_hindexed_t* subtype = smpi_datatype_hindexed_create( blocklens,
818                                                                   indices,
819                                                                   count,
820                                                                   old_type,
821                                                                   smpi_datatype_size(old_type));
822     smpi_datatype_create(new_type,  size * smpi_datatype_size(old_type),
823                                                  lb,
824                          ub
825                          ,1, subtype, DT_FLAG_DATA);
826   }else{
827     smpi_datatype_create(new_type,  size * smpi_datatype_size(old_type),
828                                              0,size * smpi_datatype_size(old_type),
829                                              0, NULL, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
830   }
831   retval=MPI_SUCCESS;
832   return retval;
833 }
834
835
836 /*
837 struct Implementation - Indexed with indices in bytes 
838 */
839
840 /*
841  *  Copies noncontiguous data into contiguous memory.
842  *  @param contiguous_struct - output struct
843  *  @param noncontiguous_struct - input struct
844  *  @param type - pointer contening :
845  *      - stride - stride of between noncontiguous data
846  *      - block_length - the width or height of blocked matrix
847  *      - count - the number of rows of matrix
848  */
849 void serialize_struct( const void *noncontiguous_struct,
850                        void *contiguous_struct,
851                        size_t count,
852                        void *type)
853 {
854   s_smpi_mpi_struct_t* type_c = (s_smpi_mpi_struct_t*)type;
855   int i,j;
856   char* contiguous_struct_char = (char*)contiguous_struct;
857   char* noncontiguous_struct_char = (char*)noncontiguous_struct;
858   for(j=0; j<count;j++){
859     for (i = 0; i < type_c->block_count; i++) {
860       if (type_c->old_types[i]->has_subtype == 0)
861         memcpy(contiguous_struct_char,
862              noncontiguous_struct_char, type_c->block_lengths[i] * smpi_datatype_size(type_c->old_types[i]));
863       else
864         ((s_smpi_subtype_t*)type_c->old_types[i]->substruct)->serialize( noncontiguous_struct_char,
865                                                                          contiguous_struct_char,
866                                                                          type_c->block_lengths[i],
867                                                                          type_c->old_types[i]->substruct);
868
869
870       contiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_size(type_c->old_types[i]);
871       if (i<type_c->block_count-1)noncontiguous_struct_char = (char*)noncontiguous_struct + type_c->block_indices[i+1];
872       else noncontiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_types[i]);//let's hope this is MPI_UB ?
873     }
874     noncontiguous_struct=(void*)noncontiguous_struct_char;
875   }
876 }
877 /*
878  *  Copies contiguous data into noncontiguous memory.
879  *  @param noncontiguous_struct - output struct
880  *  @param contiguous_struct - input struct
881  *  @param type - pointer contening :
882  *      - stride - stride of between noncontiguous data
883  *      - block_length - the width or height of blocked matrix
884  *      - count - the number of rows of matrix
885  */
886 void unserialize_struct( const void *contiguous_struct,
887                          void *noncontiguous_struct,
888                          size_t count,
889                          void *type)
890 {
891   s_smpi_mpi_struct_t* type_c = (s_smpi_mpi_struct_t*)type;
892   int i,j;
893
894   char* contiguous_struct_char = (char*)contiguous_struct;
895   char* noncontiguous_struct_char = (char*)noncontiguous_struct;
896   for(j=0; j<count;j++){
897     for (i = 0; i < type_c->block_count; i++) {
898       if (type_c->old_types[i]->has_subtype == 0)
899         memcpy(noncontiguous_struct_char,
900              contiguous_struct_char, type_c->block_lengths[i] * smpi_datatype_size(type_c->old_types[i]));
901       else
902         ((s_smpi_subtype_t*)type_c->old_types[i]->substruct)->unserialize( contiguous_struct_char,
903                                                                            noncontiguous_struct_char,
904                                                                            type_c->block_lengths[i],
905                                                                            type_c->old_types[i]->substruct);
906
907       contiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_size(type_c->old_types[i]);
908       if (i<type_c->block_count-1)noncontiguous_struct_char =  (char*)noncontiguous_struct + type_c->block_indices[i+1];
909       else noncontiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_types[i]);
910     }
911     noncontiguous_struct=(void*)noncontiguous_struct_char;
912     
913   }
914 }
915
916 void free_struct(MPI_Datatype* type){
917   xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->block_lengths);
918   xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->block_indices);
919   xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->old_types);
920 }
921
922 /*
923  * Create a Sub type struct to be able to serialize and unserialize it
924  * the structure s_smpi_mpi_struct_t is derived from s_smpi_subtype which
925  * required the functions unserialize and serialize
926  */
927 s_smpi_mpi_struct_t* smpi_datatype_struct_create( int* block_lengths,
928                                                   MPI_Aint* block_indices,
929                                                   int block_count,
930                                                   MPI_Datatype* old_types){
931   s_smpi_mpi_struct_t *new_t= xbt_new(s_smpi_mpi_struct_t,1);
932   new_t->base.serialize = &serialize_struct;
933   new_t->base.unserialize = &unserialize_struct;
934   new_t->base.subtype_free = &free_struct;
935  //TODO : add a custom function for each time to clean these 
936   new_t->block_lengths= xbt_new(int, block_count);
937   new_t->block_indices= xbt_new(MPI_Aint, block_count);
938   new_t->old_types=  xbt_new(MPI_Datatype, block_count);
939   int i;
940   for(i=0;i<block_count;i++){
941     new_t->block_lengths[i]=block_lengths[i];
942     new_t->block_indices[i]=block_indices[i];
943     new_t->old_types[i]=old_types[i];
944   }
945   //new_t->block_lengths = block_lengths;
946   //new_t->block_indices = block_indices;
947   new_t->block_count = block_count;
948   //new_t->old_types = old_types;
949   return new_t;
950 }
951
952
953 int smpi_datatype_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type)
954 {
955   int i;
956   size_t size = 0;
957   int contiguous=1;
958   size = 0;
959   MPI_Aint lb = 0;
960   MPI_Aint ub = 0;
961   if(count>0){
962     lb=indices[0] + smpi_datatype_lb(old_types[0]);
963     ub=indices[0] + blocklens[0]*smpi_datatype_ub(old_types[0]);
964   }
965   int forced_lb=0;
966   int forced_ub=0;
967   for(i=0; i< count; i++){
968     if (blocklens[i]<=0)
969       return MPI_ERR_ARG;
970     if (old_types[i]->has_subtype == 1)
971       contiguous=0;
972
973     size += blocklens[i]*smpi_datatype_size(old_types[i]);
974     if (old_types[i]==MPI_LB){
975       lb=indices[i];
976       forced_lb=1;
977     }
978     if (old_types[i]==MPI_UB){
979       ub=indices[i];
980       forced_ub=1;
981     }
982
983     if(!forced_lb && indices[i]+smpi_datatype_lb(old_types[i])<lb) lb = indices[i];
984     if(!forced_ub && indices[i]+blocklens[i]*smpi_datatype_ub(old_types[i])>ub) ub = indices[i]+blocklens[i]*smpi_datatype_ub(old_types[i]);
985
986     if ( (i< count -1) && (indices[i]+blocklens[i]*smpi_datatype_size(old_types[i]) != indices[i+1]) )contiguous=0;
987   }
988
989   if(!contiguous){
990     s_smpi_mpi_struct_t* subtype = smpi_datatype_struct_create( blocklens,
991                                                               indices,
992                                                               count,
993                                                               old_types);
994
995     smpi_datatype_create(new_type,  size, lb, ub,1, subtype, DT_FLAG_DATA);
996   }else{
997     smpi_datatype_create(new_type,  size, lb, ub,0, NULL, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
998   }
999   return MPI_SUCCESS;
1000 }
1001
1002 void smpi_datatype_commit(MPI_Datatype *datatype)
1003 {
1004   (*datatype)->flags=  ((*datatype)->flags | DT_FLAG_COMMITED);
1005 }
1006
1007 typedef struct s_smpi_mpi_op {
1008   MPI_User_function *func;
1009 } s_smpi_mpi_op_t;
1010
1011 #define MAX_OP(a, b)  (b) = (a) < (b) ? (b) : (a)
1012 #define MIN_OP(a, b)  (b) = (a) < (b) ? (a) : (b)
1013 #define SUM_OP(a, b)  (b) += (a)
1014 #define PROD_OP(a, b) (b) *= (a)
1015 #define LAND_OP(a, b) (b) = (a) && (b)
1016 #define LOR_OP(a, b)  (b) = (a) || (b)
1017 #define LXOR_OP(a, b) (b) = (!(a) && (b)) || ((a) && !(b))
1018 #define BAND_OP(a, b) (b) &= (a)
1019 #define BOR_OP(a, b)  (b) |= (a)
1020 #define BXOR_OP(a, b) (b) ^= (a)
1021 #define MAXLOC_OP(a, b)  (b) = (a.value) < (b.value) ? (b) : (a)
1022 #define MINLOC_OP(a, b)  (b) = (a.value) < (b.value) ? (a) : (b)
1023 //TODO : MINLOC & MAXLOC
1024
1025 #define APPLY_FUNC(a, b, length, type, func) \
1026 {                                          \
1027   int i;                                   \
1028   type* x = (type*)(a);                    \
1029   type* y = (type*)(b);                    \
1030   for(i = 0; i < *(length); i++) {         \
1031     func(x[i], y[i]);                      \
1032   }                                        \
1033 }
1034
1035 static void max_func(void *a, void *b, int *length,
1036                      MPI_Datatype * datatype)
1037 {
1038   if (*datatype == MPI_CHAR) {
1039     APPLY_FUNC(a, b, length, char, MAX_OP);
1040   } else if (*datatype == MPI_SHORT) {
1041     APPLY_FUNC(a, b, length, short, MAX_OP);
1042   } else if (*datatype == MPI_INT) {
1043     APPLY_FUNC(a, b, length, int, MAX_OP);
1044   } else if (*datatype == MPI_LONG) {
1045     APPLY_FUNC(a, b, length, long, MAX_OP);
1046   } else if (*datatype == MPI_UNSIGNED_SHORT) {
1047     APPLY_FUNC(a, b, length, unsigned short, MAX_OP);
1048   } else if (*datatype == MPI_UNSIGNED) {
1049     APPLY_FUNC(a, b, length, unsigned int, MAX_OP);
1050   } else if (*datatype == MPI_UNSIGNED_LONG) {
1051     APPLY_FUNC(a, b, length, unsigned long, MAX_OP);
1052   } else if (*datatype == MPI_FLOAT) {
1053     APPLY_FUNC(a, b, length, float, MAX_OP);
1054   } else if (*datatype == MPI_DOUBLE) {
1055     APPLY_FUNC(a, b, length, double, MAX_OP);
1056   } else if (*datatype == MPI_LONG_DOUBLE) {
1057     APPLY_FUNC(a, b, length, long double, MAX_OP);
1058   }
1059 }
1060
1061 static void min_func(void *a, void *b, int *length,
1062                      MPI_Datatype * datatype)
1063 {
1064   if (*datatype == MPI_CHAR) {
1065     APPLY_FUNC(a, b, length, char, MIN_OP);
1066   } else if (*datatype == MPI_SHORT) {
1067     APPLY_FUNC(a, b, length, short, MIN_OP);
1068   } else if (*datatype == MPI_INT) {
1069     APPLY_FUNC(a, b, length, int, MIN_OP);
1070   } else if (*datatype == MPI_LONG) {
1071     APPLY_FUNC(a, b, length, long, MIN_OP);
1072   } else if (*datatype == MPI_UNSIGNED_SHORT) {
1073     APPLY_FUNC(a, b, length, unsigned short, MIN_OP);
1074   } else if (*datatype == MPI_UNSIGNED) {
1075     APPLY_FUNC(a, b, length, unsigned int, MIN_OP);
1076   } else if (*datatype == MPI_UNSIGNED_LONG) {
1077     APPLY_FUNC(a, b, length, unsigned long, MIN_OP);
1078   } else if (*datatype == MPI_FLOAT) {
1079     APPLY_FUNC(a, b, length, float, MIN_OP);
1080   } else if (*datatype == MPI_DOUBLE) {
1081     APPLY_FUNC(a, b, length, double, MIN_OP);
1082   } else if (*datatype == MPI_LONG_DOUBLE) {
1083     APPLY_FUNC(a, b, length, long double, MIN_OP);
1084   }
1085 }
1086
1087 static void sum_func(void *a, void *b, int *length,
1088                      MPI_Datatype * datatype)
1089 {
1090   if (*datatype == MPI_CHAR) {
1091     APPLY_FUNC(a, b, length, char, SUM_OP);
1092   } else if (*datatype == MPI_SHORT) {
1093     APPLY_FUNC(a, b, length, short, SUM_OP);
1094   } else if (*datatype == MPI_INT) {
1095     APPLY_FUNC(a, b, length, int, SUM_OP);
1096   } else if (*datatype == MPI_LONG) {
1097     APPLY_FUNC(a, b, length, long, SUM_OP);
1098   } else if (*datatype == MPI_UNSIGNED_SHORT) {
1099     APPLY_FUNC(a, b, length, unsigned short, SUM_OP);
1100   } else if (*datatype == MPI_UNSIGNED) {
1101     APPLY_FUNC(a, b, length, unsigned int, SUM_OP);
1102   } else if (*datatype == MPI_UNSIGNED_LONG) {
1103     APPLY_FUNC(a, b, length, unsigned long, SUM_OP);
1104   } else if (*datatype == MPI_FLOAT) {
1105     APPLY_FUNC(a, b, length, float, SUM_OP);
1106   } else if (*datatype == MPI_DOUBLE) {
1107     APPLY_FUNC(a, b, length, double, SUM_OP);
1108   } else if (*datatype == MPI_LONG_DOUBLE) {
1109     APPLY_FUNC(a, b, length, long double, SUM_OP);
1110   } else if (*datatype == MPI_C_FLOAT_COMPLEX) {
1111     APPLY_FUNC(a, b, length, float _Complex, SUM_OP);
1112   } else if (*datatype == MPI_C_DOUBLE_COMPLEX) {
1113     APPLY_FUNC(a, b, length, double _Complex, SUM_OP);
1114   } else if (*datatype == MPI_C_LONG_DOUBLE_COMPLEX) {
1115     APPLY_FUNC(a, b, length, long double _Complex, SUM_OP);
1116   }
1117 }
1118
1119 static void prod_func(void *a, void *b, int *length,
1120                       MPI_Datatype * datatype)
1121 {
1122   if (*datatype == MPI_CHAR) {
1123     APPLY_FUNC(a, b, length, char, PROD_OP);
1124   } else if (*datatype == MPI_SHORT) {
1125     APPLY_FUNC(a, b, length, short, PROD_OP);
1126   } else if (*datatype == MPI_INT) {
1127     APPLY_FUNC(a, b, length, int, PROD_OP);
1128   } else if (*datatype == MPI_LONG) {
1129     APPLY_FUNC(a, b, length, long, PROD_OP);
1130   } else if (*datatype == MPI_UNSIGNED_SHORT) {
1131     APPLY_FUNC(a, b, length, unsigned short, PROD_OP);
1132   } else if (*datatype == MPI_UNSIGNED) {
1133     APPLY_FUNC(a, b, length, unsigned int, PROD_OP);
1134   } else if (*datatype == MPI_UNSIGNED_LONG) {
1135     APPLY_FUNC(a, b, length, unsigned long, PROD_OP);
1136   } else if (*datatype == MPI_FLOAT) {
1137     APPLY_FUNC(a, b, length, float, PROD_OP);
1138   } else if (*datatype == MPI_DOUBLE) {
1139     APPLY_FUNC(a, b, length, double, PROD_OP);
1140   } else if (*datatype == MPI_LONG_DOUBLE) {
1141     APPLY_FUNC(a, b, length, long double, PROD_OP);
1142   } else if (*datatype == MPI_C_FLOAT_COMPLEX) {
1143     APPLY_FUNC(a, b, length, float _Complex, PROD_OP);
1144   } else if (*datatype == MPI_C_DOUBLE_COMPLEX) {
1145     APPLY_FUNC(a, b, length, double _Complex, PROD_OP);
1146   } else if (*datatype == MPI_C_LONG_DOUBLE_COMPLEX) {
1147     APPLY_FUNC(a, b, length, long double _Complex, PROD_OP);
1148   }
1149 }
1150
1151 static void land_func(void *a, void *b, int *length,
1152                       MPI_Datatype * datatype)
1153 {
1154   if (*datatype == MPI_CHAR) {
1155     APPLY_FUNC(a, b, length, char, LAND_OP);
1156   } else if (*datatype == MPI_SHORT) {
1157     APPLY_FUNC(a, b, length, short, LAND_OP);
1158   } else if (*datatype == MPI_INT) {
1159     APPLY_FUNC(a, b, length, int, LAND_OP);
1160   } else if (*datatype == MPI_LONG) {
1161     APPLY_FUNC(a, b, length, long, LAND_OP);
1162   } else if (*datatype == MPI_UNSIGNED_SHORT) {
1163     APPLY_FUNC(a, b, length, unsigned short, LAND_OP);
1164   } else if (*datatype == MPI_UNSIGNED) {
1165     APPLY_FUNC(a, b, length, unsigned int, LAND_OP);
1166   } else if (*datatype == MPI_UNSIGNED_LONG) {
1167     APPLY_FUNC(a, b, length, unsigned long, LAND_OP);
1168   } else if (*datatype == MPI_C_BOOL) {
1169     APPLY_FUNC(a, b, length, _Bool, LAND_OP);
1170   }
1171 }
1172
1173 static void lor_func(void *a, void *b, int *length,
1174                      MPI_Datatype * datatype)
1175 {
1176   if (*datatype == MPI_CHAR) {
1177     APPLY_FUNC(a, b, length, char, LOR_OP);
1178   } else if (*datatype == MPI_SHORT) {
1179     APPLY_FUNC(a, b, length, short, LOR_OP);
1180   } else if (*datatype == MPI_INT) {
1181     APPLY_FUNC(a, b, length, int, LOR_OP);
1182   } else if (*datatype == MPI_LONG) {
1183     APPLY_FUNC(a, b, length, long, LOR_OP);
1184   } else if (*datatype == MPI_UNSIGNED_SHORT) {
1185     APPLY_FUNC(a, b, length, unsigned short, LOR_OP);
1186   } else if (*datatype == MPI_UNSIGNED) {
1187     APPLY_FUNC(a, b, length, unsigned int, LOR_OP);
1188   } else if (*datatype == MPI_UNSIGNED_LONG) {
1189     APPLY_FUNC(a, b, length, unsigned long, LOR_OP);
1190   } else if (*datatype == MPI_C_BOOL) {
1191     APPLY_FUNC(a, b, length, _Bool, LOR_OP);
1192   }
1193 }
1194
1195 static void lxor_func(void *a, void *b, int *length,
1196                       MPI_Datatype * datatype)
1197 {
1198   if (*datatype == MPI_CHAR) {
1199     APPLY_FUNC(a, b, length, char, LXOR_OP);
1200   } else if (*datatype == MPI_SHORT) {
1201     APPLY_FUNC(a, b, length, short, LXOR_OP);
1202   } else if (*datatype == MPI_INT) {
1203     APPLY_FUNC(a, b, length, int, LXOR_OP);
1204   } else if (*datatype == MPI_LONG) {
1205     APPLY_FUNC(a, b, length, long, LXOR_OP);
1206   } else if (*datatype == MPI_UNSIGNED_SHORT) {
1207     APPLY_FUNC(a, b, length, unsigned short, LXOR_OP);
1208   } else if (*datatype == MPI_UNSIGNED) {
1209     APPLY_FUNC(a, b, length, unsigned int, LXOR_OP);
1210   } else if (*datatype == MPI_UNSIGNED_LONG) {
1211     APPLY_FUNC(a, b, length, unsigned long, LXOR_OP);
1212   } else if (*datatype == MPI_C_BOOL) {
1213     APPLY_FUNC(a, b, length, _Bool, LXOR_OP);
1214   }
1215 }
1216
1217 static void band_func(void *a, void *b, int *length,
1218                       MPI_Datatype * datatype)
1219 {
1220   if (*datatype == MPI_CHAR) {
1221     APPLY_FUNC(a, b, length, char, BAND_OP);
1222   }
1223   if (*datatype == MPI_SHORT) {
1224     APPLY_FUNC(a, b, length, short, BAND_OP);
1225   } else if (*datatype == MPI_INT) {
1226     APPLY_FUNC(a, b, length, int, BAND_OP);
1227   } else if (*datatype == MPI_LONG) {
1228     APPLY_FUNC(a, b, length, long, BAND_OP);
1229   } else if (*datatype == MPI_UNSIGNED_SHORT) {
1230     APPLY_FUNC(a, b, length, unsigned short, BAND_OP);
1231   } else if (*datatype == MPI_UNSIGNED) {
1232     APPLY_FUNC(a, b, length, unsigned int, BAND_OP);
1233   } else if (*datatype == MPI_UNSIGNED_LONG) {
1234     APPLY_FUNC(a, b, length, unsigned long, BAND_OP);
1235   } else if (*datatype == MPI_BYTE) {
1236     APPLY_FUNC(a, b, length, uint8_t, BAND_OP);
1237   }
1238 }
1239
1240 static void bor_func(void *a, void *b, int *length,
1241                      MPI_Datatype * datatype)
1242 {
1243   if (*datatype == MPI_CHAR) {
1244     APPLY_FUNC(a, b, length, char, BOR_OP);
1245   } else if (*datatype == MPI_SHORT) {
1246     APPLY_FUNC(a, b, length, short, BOR_OP);
1247   } else if (*datatype == MPI_INT) {
1248     APPLY_FUNC(a, b, length, int, BOR_OP);
1249   } else if (*datatype == MPI_LONG) {
1250     APPLY_FUNC(a, b, length, long, BOR_OP);
1251   } else if (*datatype == MPI_UNSIGNED_SHORT) {
1252     APPLY_FUNC(a, b, length, unsigned short, BOR_OP);
1253   } else if (*datatype == MPI_UNSIGNED) {
1254     APPLY_FUNC(a, b, length, unsigned int, BOR_OP);
1255   } else if (*datatype == MPI_UNSIGNED_LONG) {
1256     APPLY_FUNC(a, b, length, unsigned long, BOR_OP);
1257   } else if (*datatype == MPI_BYTE) {
1258     APPLY_FUNC(a, b, length, uint8_t, BOR_OP);
1259   }
1260 }
1261
1262 static void bxor_func(void *a, void *b, int *length,
1263                       MPI_Datatype * datatype)
1264 {
1265   if (*datatype == MPI_CHAR) {
1266     APPLY_FUNC(a, b, length, char, BXOR_OP);
1267   } else if (*datatype == MPI_SHORT) {
1268     APPLY_FUNC(a, b, length, short, BXOR_OP);
1269   } else if (*datatype == MPI_INT) {
1270     APPLY_FUNC(a, b, length, int, BXOR_OP);
1271   } else if (*datatype == MPI_LONG) {
1272     APPLY_FUNC(a, b, length, long, BXOR_OP);
1273   } else if (*datatype == MPI_UNSIGNED_SHORT) {
1274     APPLY_FUNC(a, b, length, unsigned short, BXOR_OP);
1275   } else if (*datatype == MPI_UNSIGNED) {
1276     APPLY_FUNC(a, b, length, unsigned int, BXOR_OP);
1277   } else if (*datatype == MPI_UNSIGNED_LONG) {
1278     APPLY_FUNC(a, b, length, unsigned long, BXOR_OP);
1279   } else if (*datatype == MPI_BYTE) {
1280     APPLY_FUNC(a, b, length, uint8_t, BXOR_OP);
1281   }
1282 }
1283
1284 static void minloc_func(void *a, void *b, int *length,
1285                         MPI_Datatype * datatype)
1286 {
1287   if (*datatype == MPI_FLOAT_INT) {
1288     APPLY_FUNC(a, b, length, float_int, MINLOC_OP);
1289   } else if (*datatype == MPI_LONG_INT) {
1290     APPLY_FUNC(a, b, length, long_int, MINLOC_OP);
1291   } else if (*datatype == MPI_DOUBLE_INT) {
1292     APPLY_FUNC(a, b, length, double_int, MINLOC_OP);
1293   } else if (*datatype == MPI_SHORT_INT) {
1294     APPLY_FUNC(a, b, length, short_int, MINLOC_OP);
1295   } else if (*datatype == MPI_2INT) {
1296     APPLY_FUNC(a, b, length, int_int, MINLOC_OP);
1297   } else if (*datatype == MPI_LONG_DOUBLE_INT) {
1298     APPLY_FUNC(a, b, length, long_double_int, MINLOC_OP);
1299   } else if (*datatype == MPI_2FLOAT) {
1300     APPLY_FUNC(a, b, length, float_float, MINLOC_OP);
1301   } else if (*datatype == MPI_2DOUBLE) {
1302     APPLY_FUNC(a, b, length, double_double, MINLOC_OP);
1303   }
1304 }
1305
1306 static void maxloc_func(void *a, void *b, int *length,
1307                         MPI_Datatype * datatype)
1308 {
1309   if (*datatype == MPI_FLOAT_INT) {
1310     APPLY_FUNC(a, b, length, float_int, MAXLOC_OP);
1311   } else if (*datatype == MPI_LONG_INT) {
1312     APPLY_FUNC(a, b, length, long_int, MAXLOC_OP);
1313   } else if (*datatype == MPI_DOUBLE_INT) {
1314     APPLY_FUNC(a, b, length, double_int, MAXLOC_OP);
1315   } else if (*datatype == MPI_SHORT_INT) {
1316     APPLY_FUNC(a, b, length, short_int, MAXLOC_OP);
1317   } else if (*datatype == MPI_2INT) {
1318     APPLY_FUNC(a, b, length, int_int, MAXLOC_OP);
1319   } else if (*datatype == MPI_LONG_DOUBLE_INT) {
1320     APPLY_FUNC(a, b, length, long_double_int, MAXLOC_OP);
1321   } else if (*datatype == MPI_2FLOAT) {
1322     APPLY_FUNC(a, b, length, float_float, MAXLOC_OP);
1323   } else if (*datatype == MPI_2DOUBLE) {
1324     APPLY_FUNC(a, b, length, double_double, MAXLOC_OP);
1325   }
1326 }
1327
1328
1329 #define CREATE_MPI_OP(name, func)                             \
1330   static s_smpi_mpi_op_t mpi_##name = { &(func) /* func */ }; \
1331 MPI_Op name = &mpi_##name;
1332
1333 CREATE_MPI_OP(MPI_MAX, max_func);
1334 CREATE_MPI_OP(MPI_MIN, min_func);
1335 CREATE_MPI_OP(MPI_SUM, sum_func);
1336 CREATE_MPI_OP(MPI_PROD, prod_func);
1337 CREATE_MPI_OP(MPI_LAND, land_func);
1338 CREATE_MPI_OP(MPI_LOR, lor_func);
1339 CREATE_MPI_OP(MPI_LXOR, lxor_func);
1340 CREATE_MPI_OP(MPI_BAND, band_func);
1341 CREATE_MPI_OP(MPI_BOR, bor_func);
1342 CREATE_MPI_OP(MPI_BXOR, bxor_func);
1343 CREATE_MPI_OP(MPI_MAXLOC, maxloc_func);
1344 CREATE_MPI_OP(MPI_MINLOC, minloc_func);
1345
1346 MPI_Op smpi_op_new(MPI_User_function * function, int commute)
1347 {
1348   MPI_Op op;
1349
1350   //FIXME: add commute param
1351   op = xbt_new(s_smpi_mpi_op_t, 1);
1352   op->func = function;
1353   return op;
1354 }
1355
1356 void smpi_op_destroy(MPI_Op op)
1357 {
1358   xbt_free(op);
1359 }
1360
1361 void smpi_op_apply(MPI_Op op, void *invec, void *inoutvec, int *len,
1362                    MPI_Datatype * datatype)
1363 {
1364   op->func(invec, inoutvec, len, datatype);
1365 }