Logo AND Algorithmique Numérique Distribuée

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