Logo AND Algorithmique Numérique Distribuée

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