Logo AND Algorithmique Numérique Distribuée

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