Logo AND Algorithmique Numérique Distribuée

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