Logo AND Algorithmique Numérique Distribuée

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