Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / src / smpi / smpi_mpi_dt.cpp
1 /* smpi_mpi_dt.c -- MPI primitives to handle datatypes                      */
2 /* Copyright (c) 2009-2017. The SimGrid Team. All rights reserved.          */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <limits.h>
11 #include "private.h"
12 #include "smpi_mpi_dt_private.h"
13 #include "mc/mc.h"
14 #include "xbt/replay.h"
15 #include <xbt/ex.hpp>
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_homogeneous(nullptr);
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 = static_cast<smpi_type_key_elem>(
196           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 containing :
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( 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   char* noncontiguous_vector_char = static_cast<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( 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   char* contiguous_vector_char = static_cast<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     return;
428   type->in_use++;
429
430   if(type->sizeof_substruct!=0){
431     static_cast<s_smpi_subtype_t *>((type)->substruct)->subtype_use(&type);  
432   }
433 #if HAVE_MC
434   if(MC_is_active())
435     MC_ignore(&(type->in_use), sizeof(type->in_use));
436 #endif
437 }
438
439 void smpi_datatype_unuse(MPI_Datatype type)
440 {
441   if (type == MPI_DATATYPE_NULL)
442     return;
443
444   if (type->in_use > 0)
445     type->in_use--;
446
447   if(type->sizeof_substruct!=0){
448     static_cast<s_smpi_subtype_t *>((type)->substruct)->subtype_free(&type);  
449   }
450
451   if (type->in_use == 0)
452     smpi_datatype_free(&type);
453
454 #if HAVE_MC
455   if(MC_is_active())
456     MC_ignore(&(type->in_use), sizeof(type->in_use));
457 #endif
458 }
459
460 /*Contiguous Implementation*/
461
462 /* Copies noncontiguous data into contiguous memory.
463  *  @param contiguous_hvector - output hvector
464  *  @param noncontiguous_hvector - input hvector
465  *  @param type - pointer contening :
466  *      - stride - stride of between noncontiguous data, in bytes
467  *      - block_length - the width or height of blocked matrix
468  *      - count - the number of rows of matrix
469  */
470 void serialize_contiguous( void* noncontiguous_hvector, void *contiguous_hvector, int count, void *type)
471 {
472   s_smpi_mpi_contiguous_t* type_c = reinterpret_cast<s_smpi_mpi_contiguous_t*>(type);
473   char* contiguous_vector_char = static_cast<char*>(contiguous_hvector);
474   char* noncontiguous_vector_char = static_cast<char*>(noncontiguous_hvector)+type_c->lb;
475   memcpy(contiguous_vector_char, noncontiguous_vector_char, count* type_c->block_count * type_c->size_oldtype);
476 }
477 /* Copies contiguous data into noncontiguous memory.
478  *  @param noncontiguous_vector - output hvector
479  *  @param contiguous_vector - input hvector
480  *  @param type - pointer contening :
481  *      - stride - stride of between noncontiguous data, in bytes
482  *      - block_length - the width or height of blocked matrix
483  *      - count - the number of rows of matrix
484  */
485 void unserialize_contiguous(void* contiguous_vector, void *noncontiguous_vector, int count, void *type, MPI_Op op)
486 {
487   s_smpi_mpi_contiguous_t* type_c = reinterpret_cast<s_smpi_mpi_contiguous_t*>(type);
488   char* contiguous_vector_char = static_cast<char*>(contiguous_vector);
489   char* noncontiguous_vector_char = static_cast<char*>(noncontiguous_vector)+type_c->lb;
490   int n= count* type_c->block_count;
491   smpi_op_apply(op, contiguous_vector_char, noncontiguous_vector_char, &n, &type_c->old_type);
492 }
493
494 void free_contiguous(MPI_Datatype* d){
495   smpi_datatype_unuse(reinterpret_cast<s_smpi_mpi_contiguous_t*>((*d)->substruct)->old_type);
496 }
497
498 void use_contiguous(MPI_Datatype* d){
499   smpi_datatype_use(reinterpret_cast<s_smpi_mpi_contiguous_t*>((*d)->substruct)->old_type);
500 }
501
502 /* Create a Sub type contiguous to be able to serialize and unserialize it the structure s_smpi_mpi_contiguous_t is
503  * derived from s_smpi_subtype which required the functions unserialize and serialize */
504 s_smpi_mpi_contiguous_t* smpi_datatype_contiguous_create( MPI_Aint lb, int block_count, MPI_Datatype old_type,
505                                                   int size_oldtype){
506   if(block_count==0)
507     return nullptr;
508   s_smpi_mpi_contiguous_t *new_t= xbt_new(s_smpi_mpi_contiguous_t,1);
509   new_t->base.serialize = &serialize_contiguous;
510   new_t->base.unserialize = &unserialize_contiguous;
511   new_t->base.subtype_free = &free_contiguous;
512   new_t->base.subtype_use = &use_contiguous;
513   new_t->lb = lb;
514   new_t->block_count = block_count;
515   new_t->old_type = old_type;
516   smpi_datatype_use(old_type);
517   new_t->size_oldtype = size_oldtype;
518   smpi_datatype_use(old_type);
519   return new_t;
520 }
521
522 int smpi_datatype_contiguous(int count, MPI_Datatype old_type, MPI_Datatype* new_type, MPI_Aint lb)
523 {
524   int retval;
525   if(old_type->sizeof_substruct){
526     //handle this case as a hvector with stride equals to the extent of the datatype
527     return smpi_datatype_hvector(count, 1, smpi_datatype_get_extent(old_type), old_type, new_type);
528   }
529   
530   s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb, count, old_type,smpi_datatype_size(old_type));
531
532   smpi_datatype_create(new_type, count * smpi_datatype_size(old_type),lb,lb + count * smpi_datatype_size(old_type),
533             sizeof(s_smpi_mpi_contiguous_t),subtype, DT_FLAG_CONTIGUOUS);
534   retval=MPI_SUCCESS;
535   return retval;
536 }
537
538 int smpi_datatype_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type)
539 {
540   int retval;
541   if (blocklen<0) 
542     return MPI_ERR_ARG;
543   MPI_Aint lb = 0;
544   MPI_Aint ub = 0;
545   if(count>0){
546     lb=smpi_datatype_lb(old_type);
547     ub=((count-1)*stride+blocklen-1)*smpi_datatype_get_extent(old_type)+smpi_datatype_ub(old_type);
548   }
549   if(old_type->sizeof_substruct != 0 || stride != blocklen){
550
551     s_smpi_mpi_vector_t* subtype = smpi_datatype_vector_create(stride, blocklen, count, old_type,
552                                                                 smpi_datatype_size(old_type));
553     smpi_datatype_create(new_type, count * (blocklen) * smpi_datatype_size(old_type), lb, ub, sizeof(s_smpi_mpi_vector_t), subtype,
554                          DT_FLAG_VECTOR);
555     retval=MPI_SUCCESS;
556   }else{
557     /* in this situation the data are contiguous thus it's not required to serialize and unserialize it*/
558     smpi_datatype_create(new_type, count * blocklen * smpi_datatype_size(old_type), 0, ((count -1) * stride + blocklen)*
559                          smpi_datatype_size(old_type), 0, nullptr, DT_FLAG_VECTOR|DT_FLAG_CONTIGUOUS);
560     retval=MPI_SUCCESS;
561   }
562   return retval;
563 }
564
565 void free_vector(MPI_Datatype* d){
566   smpi_datatype_unuse(reinterpret_cast<s_smpi_mpi_indexed_t*>((*d)->substruct)->old_type);
567 }
568
569 void use_vector(MPI_Datatype* d){
570   smpi_datatype_use(reinterpret_cast<s_smpi_mpi_indexed_t*>((*d)->substruct)->old_type);
571 }
572
573 /* Hvector Implementation - Vector with stride in bytes */
574
575 /* Copies noncontiguous data into contiguous memory.
576  *  @param contiguous_hvector - output hvector
577  *  @param noncontiguous_hvector - input hvector
578  *  @param type - pointer contening :
579  *      - stride - stride of between noncontiguous data, in bytes
580  *      - block_length - the width or height of blocked matrix
581  *      - count - the number of rows of matrix
582  */
583 void serialize_hvector( void* noncontiguous_hvector, void *contiguous_hvector, int count, void *type)
584 {
585   s_smpi_mpi_hvector_t* type_c = reinterpret_cast<s_smpi_mpi_hvector_t*>(type);
586   int i;
587   char* contiguous_vector_char = static_cast<char*>(contiguous_hvector);
588   char* noncontiguous_vector_char = static_cast<char*>(noncontiguous_hvector);
589
590   for (i = 0; i < type_c->block_count * count; i++) {
591     if (type_c->old_type->sizeof_substruct == 0)
592       memcpy(contiguous_vector_char, noncontiguous_vector_char, type_c->block_length * type_c->size_oldtype);
593     else
594       static_cast<s_smpi_subtype_t*>(type_c->old_type->substruct)->serialize( noncontiguous_vector_char,
595                                                                    contiguous_vector_char,
596                                                                    type_c->block_length, type_c->old_type->substruct);
597
598     contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
599     if((i+1)%type_c->block_count ==0)
600       noncontiguous_vector_char += type_c->block_length*type_c->size_oldtype;
601     else
602       noncontiguous_vector_char += type_c->block_stride;
603   }
604 }
605 /* Copies contiguous data into noncontiguous memory.
606  *  @param noncontiguous_vector - output hvector
607  *  @param contiguous_vector - input hvector
608  *  @param type - pointer contening :
609  *      - stride - stride of between noncontiguous data, in bytes
610  *      - block_length - the width or height of blocked matrix
611  *      - count - the number of rows of matrix
612  */
613 void unserialize_hvector( void* contiguous_vector, void *noncontiguous_vector, int count, void *type, MPI_Op op)
614 {
615   s_smpi_mpi_hvector_t* type_c = reinterpret_cast<s_smpi_mpi_hvector_t*>(type);
616   int i;
617
618   char* contiguous_vector_char = static_cast<char*>(contiguous_vector);
619   char* noncontiguous_vector_char = static_cast<char*>(noncontiguous_vector);
620
621   for (i = 0; i < type_c->block_count * count; i++) {
622     if (type_c->old_type->sizeof_substruct == 0)
623       smpi_op_apply(op, contiguous_vector_char, noncontiguous_vector_char, &type_c->block_length, &type_c->old_type);
624     else
625       static_cast<s_smpi_subtype_t*>(type_c->old_type->substruct)->unserialize( contiguous_vector_char, noncontiguous_vector_char,
626                                                                      type_c->block_length, type_c->old_type->substruct,
627                                                                      op);
628     contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
629     if((i+1)%type_c->block_count ==0)
630       noncontiguous_vector_char += type_c->block_length*type_c->size_oldtype;
631     else
632       noncontiguous_vector_char += type_c->block_stride;
633   }
634 }
635
636 /* Create a Sub type vector to be able to serialize and unserialize it the structure s_smpi_mpi_vector_t is derived
637  * from s_smpi_subtype which required the functions unserialize and serialize
638  *
639  */
640 s_smpi_mpi_hvector_t* smpi_datatype_hvector_create( MPI_Aint block_stride, int block_length, int block_count,
641                                                   MPI_Datatype old_type, int size_oldtype){
642   s_smpi_mpi_hvector_t *new_t= xbt_new(s_smpi_mpi_hvector_t,1);
643   new_t->base.serialize = &serialize_hvector;
644   new_t->base.unserialize = &unserialize_hvector;
645   new_t->base.subtype_free = &free_hvector;
646   new_t->base.subtype_use = &use_hvector;
647   new_t->block_stride = block_stride;
648   new_t->block_length = block_length;
649   new_t->block_count = block_count;
650   new_t->old_type = old_type;
651   new_t->size_oldtype = size_oldtype;
652   smpi_datatype_use(old_type);
653   return new_t;
654 }
655
656 //do nothing for vector types
657 void free_hvector(MPI_Datatype* d){
658   smpi_datatype_unuse(reinterpret_cast<s_smpi_mpi_hvector_t*>((*d)->substruct)->old_type);
659 }
660
661 void use_hvector(MPI_Datatype* d){
662   smpi_datatype_use(reinterpret_cast<s_smpi_mpi_hvector_t*>((*d)->substruct)->old_type);
663 }
664
665 int smpi_datatype_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type)
666 {
667   int retval;
668   if (blocklen<0) 
669     return MPI_ERR_ARG;
670   MPI_Aint lb = 0;
671   MPI_Aint ub = 0;
672   if(count>0){
673     lb=smpi_datatype_lb(old_type);
674     ub=((count-1)*stride)+(blocklen-1)*smpi_datatype_get_extent(old_type)+smpi_datatype_ub(old_type);
675   }
676   if(old_type->sizeof_substruct != 0 || stride != blocklen*smpi_datatype_get_extent(old_type)){
677     s_smpi_mpi_hvector_t* subtype = smpi_datatype_hvector_create( stride, blocklen, count, old_type,
678                                                                   smpi_datatype_size(old_type));
679
680     smpi_datatype_create(new_type, count * blocklen * smpi_datatype_size(old_type), lb,ub, sizeof(s_smpi_mpi_hvector_t), subtype, DT_FLAG_VECTOR);
681     retval=MPI_SUCCESS;
682   }else{
683     smpi_datatype_create(new_type, count * blocklen * smpi_datatype_size(old_type),0,count * blocklen *
684                                              smpi_datatype_size(old_type), 0, nullptr, DT_FLAG_VECTOR|DT_FLAG_CONTIGUOUS);
685     retval=MPI_SUCCESS;
686   }
687   return retval;
688 }
689
690 /* Indexed Implementation */
691
692 /* Copies noncontiguous data into contiguous memory.
693  *  @param contiguous_indexed - output indexed
694  *  @param noncontiguous_indexed - input indexed
695  *  @param type - pointer contening :
696  *      - block_lengths - the width or height of blocked matrix
697  *      - block_indices - indices of each data, in element
698  *      - count - the number of rows of matrix
699  */
700 void serialize_indexed( void* noncontiguous_indexed, void *contiguous_indexed, int count, void *type)
701 {
702   s_smpi_mpi_indexed_t* type_c = reinterpret_cast<s_smpi_mpi_indexed_t*>(type);
703   int i,j;
704   char* contiguous_indexed_char = static_cast<char*>(contiguous_indexed);
705   char* noncontiguous_indexed_char = static_cast<char*>(noncontiguous_indexed)+type_c->block_indices[0] * type_c->size_oldtype;
706   for(j=0; j<count;j++){
707     for (i = 0; i < type_c->block_count; i++) {
708       if (type_c->old_type->sizeof_substruct == 0)
709         memcpy(contiguous_indexed_char, noncontiguous_indexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
710       else
711         static_cast<s_smpi_subtype_t*>(type_c->old_type->substruct)->serialize( noncontiguous_indexed_char,
712                                                                      contiguous_indexed_char,
713                                                                      type_c->block_lengths[i],
714                                                                      type_c->old_type->substruct);
715
716       contiguous_indexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
717       if (i<type_c->block_count-1)
718         noncontiguous_indexed_char =
719           static_cast<char*>(noncontiguous_indexed) + type_c->block_indices[i+1]*smpi_datatype_get_extent(type_c->old_type);
720       else
721         noncontiguous_indexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
722     }
723     noncontiguous_indexed=static_cast< void*>(noncontiguous_indexed_char);
724   }
725 }
726 /* Copies contiguous data into noncontiguous memory.
727  *  @param noncontiguous_indexed - output indexed
728  *  @param contiguous_indexed - input indexed
729  *  @param type - pointer contening :
730  *      - block_lengths - the width or height of blocked matrix
731  *      - block_indices - indices of each data, in element
732  *      - count - the number of rows of matrix
733  */
734 void unserialize_indexed( void* contiguous_indexed, void *noncontiguous_indexed, int count, void *type, MPI_Op op)
735 {
736   s_smpi_mpi_indexed_t* type_c = reinterpret_cast<s_smpi_mpi_indexed_t*>(type);
737   int i,j;
738   char* contiguous_indexed_char = static_cast<char*>(contiguous_indexed);
739   char* noncontiguous_indexed_char =
740     static_cast<char*>(noncontiguous_indexed)+type_c->block_indices[0]*smpi_datatype_get_extent(type_c->old_type);
741   for(j=0; j<count;j++){
742     for (i = 0; i < type_c->block_count; i++) {
743       if (type_c->old_type->sizeof_substruct == 0)
744         smpi_op_apply(op, contiguous_indexed_char, noncontiguous_indexed_char, &type_c->block_lengths[i],
745                     &type_c->old_type);
746       else
747         static_cast<s_smpi_subtype_t*>(type_c->old_type->substruct)->unserialize( contiguous_indexed_char,
748                                                                        noncontiguous_indexed_char,
749                                                                        type_c->block_lengths[i],
750                                                                        type_c->old_type->substruct, op);
751
752       contiguous_indexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
753       if (i<type_c->block_count-1)
754         noncontiguous_indexed_char =
755           static_cast<char*>(noncontiguous_indexed) + type_c->block_indices[i+1]*smpi_datatype_get_extent(type_c->old_type);
756       else
757         noncontiguous_indexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
758     }
759     noncontiguous_indexed=static_cast<void*>(noncontiguous_indexed_char);
760   }
761 }
762
763 void free_indexed(MPI_Datatype* type){
764   if((*type)->in_use==0){
765     xbt_free(reinterpret_cast<s_smpi_mpi_indexed_t*>((*type)->substruct)->block_lengths);
766     xbt_free(reinterpret_cast<s_smpi_mpi_indexed_t*>((*type)->substruct)->block_indices);
767   }
768   smpi_datatype_unuse(reinterpret_cast<s_smpi_mpi_indexed_t*>((*type)->substruct)->old_type);
769 }
770
771 void use_indexed(MPI_Datatype* type){
772   smpi_datatype_use(reinterpret_cast<s_smpi_mpi_indexed_t*>((*type)->substruct)->old_type);
773 }
774
775
776 /* Create a Sub type indexed to be able to serialize and unserialize it the structure s_smpi_mpi_indexed_t is derived
777  * from s_smpi_subtype which required the functions unserialize and serialize */
778 s_smpi_mpi_indexed_t* smpi_datatype_indexed_create( int* block_lengths, int* block_indices, int block_count,
779                                                   MPI_Datatype old_type, int size_oldtype){
780   s_smpi_mpi_indexed_t *new_t= xbt_new(s_smpi_mpi_indexed_t,1);
781   new_t->base.serialize = &serialize_indexed;
782   new_t->base.unserialize = &unserialize_indexed;
783   new_t->base.subtype_free = &free_indexed;
784   new_t->base.subtype_use = &use_indexed;
785   new_t->block_lengths= xbt_new(int, block_count);
786   new_t->block_indices= xbt_new(int, block_count);
787   int i;
788   for(i=0;i<block_count;i++){
789     new_t->block_lengths[i]=block_lengths[i];
790     new_t->block_indices[i]=block_indices[i];
791   }
792   new_t->block_count = block_count;
793   smpi_datatype_use(old_type);
794   new_t->old_type = old_type;
795   new_t->size_oldtype = size_oldtype;
796   return new_t;
797 }
798
799 int smpi_datatype_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
800 {
801   int i;
802   int retval;
803   int size = 0;
804   bool contiguous=true;
805   MPI_Aint lb = 0;
806   MPI_Aint ub = 0;
807   if(count>0){
808     lb=indices[0]*smpi_datatype_get_extent(old_type);
809     ub=indices[0]*smpi_datatype_get_extent(old_type) + blocklens[0]*smpi_datatype_ub(old_type);
810   }
811
812   for(i=0; i< count; i++){
813     if   (blocklens[i]<0)
814       return MPI_ERR_ARG;
815     size += blocklens[i];
816
817     if(indices[i]*smpi_datatype_get_extent(old_type)+smpi_datatype_lb(old_type)<lb)
818       lb = indices[i]*smpi_datatype_get_extent(old_type)+smpi_datatype_lb(old_type);
819     if(indices[i]*smpi_datatype_get_extent(old_type)+blocklens[i]*smpi_datatype_ub(old_type)>ub)
820       ub = indices[i]*smpi_datatype_get_extent(old_type)+blocklens[i]*smpi_datatype_ub(old_type);
821
822     if ( (i< count -1) && (indices[i]+blocklens[i] != indices[i+1]) )
823       contiguous=false;
824   }
825   if (old_type->sizeof_substruct != 0)
826     contiguous=false;
827
828   if(!contiguous){
829     s_smpi_mpi_indexed_t* subtype = smpi_datatype_indexed_create( blocklens, indices, count, old_type,
830                                                                   smpi_datatype_size(old_type));
831      smpi_datatype_create(new_type,  size * smpi_datatype_size(old_type),lb,ub,sizeof(s_smpi_mpi_indexed_t), subtype, DT_FLAG_DATA);
832   }else{
833     s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb, size, old_type,
834                                                                   smpi_datatype_size(old_type));
835     smpi_datatype_create(new_type, size * smpi_datatype_size(old_type), lb, ub, sizeof(s_smpi_mpi_contiguous_t), subtype,
836                          DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
837   }
838   retval=MPI_SUCCESS;
839   return retval;
840 }
841 /* Hindexed Implementation - Indexed with indices in bytes */
842
843 /* Copies noncontiguous data into contiguous memory.
844  *  @param contiguous_hindexed - output hindexed
845  *  @param noncontiguous_hindexed - input hindexed
846  *  @param type - pointer contening :
847  *      - block_lengths - the width or height of blocked matrix
848  *      - block_indices - indices of each data, in bytes
849  *      - count - the number of rows of matrix
850  */
851 void serialize_hindexed( void* noncontiguous_hindexed, void *contiguous_hindexed, int count, void *type)
852 {
853   s_smpi_mpi_hindexed_t* type_c = reinterpret_cast<s_smpi_mpi_hindexed_t*>(type);
854   int i,j;
855   char* contiguous_hindexed_char = static_cast<char*>(contiguous_hindexed);
856   char* noncontiguous_hindexed_char = static_cast<char*>(noncontiguous_hindexed)+ type_c->block_indices[0];
857   for(j=0; j<count;j++){
858     for (i = 0; i < type_c->block_count; i++) {
859       if (type_c->old_type->sizeof_substruct == 0)
860         memcpy(contiguous_hindexed_char, noncontiguous_hindexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
861       else
862         static_cast<s_smpi_subtype_t*>(type_c->old_type->substruct)->serialize( noncontiguous_hindexed_char,
863                                                                      contiguous_hindexed_char,
864                                                                      type_c->block_lengths[i],
865                                                                      type_c->old_type->substruct);
866
867       contiguous_hindexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
868       if (i<type_c->block_count-1)
869         noncontiguous_hindexed_char = static_cast<char*>(noncontiguous_hindexed) + type_c->block_indices[i+1];
870       else
871         noncontiguous_hindexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
872     }
873     noncontiguous_hindexed=static_cast<void*>(noncontiguous_hindexed_char);
874   }
875 }
876 /* Copies contiguous data into noncontiguous memory.
877  *  @param noncontiguous_hindexed - output hindexed
878  *  @param contiguous_hindexed - input hindexed
879  *  @param type - pointer contening :
880  *      - block_lengths - the width or height of blocked matrix
881  *      - block_indices - indices of each data, in bytes
882  *      - count - the number of rows of matrix
883  */
884 void unserialize_hindexed( void* contiguous_hindexed, void *noncontiguous_hindexed, int count, void *type,
885                          MPI_Op op)
886 {
887   s_smpi_mpi_hindexed_t* type_c = reinterpret_cast<s_smpi_mpi_hindexed_t*>(type);
888   int i,j;
889
890   char* contiguous_hindexed_char = static_cast<char*>(contiguous_hindexed);
891   char* noncontiguous_hindexed_char = static_cast<char*>(noncontiguous_hindexed)+ type_c->block_indices[0];
892   for(j=0; j<count;j++){
893     for (i = 0; i < type_c->block_count; i++) {
894       if (type_c->old_type->sizeof_substruct == 0)
895         smpi_op_apply(op, contiguous_hindexed_char, noncontiguous_hindexed_char, &type_c->block_lengths[i],
896                             &type_c->old_type);
897       else
898         static_cast<s_smpi_subtype_t*>(type_c->old_type->substruct)->unserialize( contiguous_hindexed_char,
899                                                                        noncontiguous_hindexed_char,
900                                                                        type_c->block_lengths[i],
901                                                                        type_c->old_type->substruct, op);
902
903       contiguous_hindexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
904       if (i<type_c->block_count-1)
905         noncontiguous_hindexed_char = static_cast<char*>(noncontiguous_hindexed) + type_c->block_indices[i+1];
906       else
907         noncontiguous_hindexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
908     }
909     noncontiguous_hindexed=static_cast<void*>(noncontiguous_hindexed_char);
910   }
911 }
912
913 void free_hindexed(MPI_Datatype* type){
914   if((*type)->in_use==0){
915     xbt_free(reinterpret_cast<s_smpi_mpi_hindexed_t*>((*type)->substruct)->block_lengths);
916     xbt_free(reinterpret_cast<s_smpi_mpi_hindexed_t*>((*type)->substruct)->block_indices);
917   }
918   smpi_datatype_unuse(reinterpret_cast<s_smpi_mpi_hindexed_t*>((*type)->substruct)->old_type);
919 }
920
921 void use_hindexed(MPI_Datatype* type){
922   smpi_datatype_use(reinterpret_cast<s_smpi_mpi_hindexed_t*>((*type)->substruct)->old_type);
923 }
924
925 /* Create a Sub type hindexed to be able to serialize and unserialize it the structure s_smpi_mpi_hindexed_t is derived
926  * from s_smpi_subtype which required the functions unserialize and serialize
927  */
928 s_smpi_mpi_hindexed_t* smpi_datatype_hindexed_create( int* block_lengths, MPI_Aint* block_indices, int block_count,
929                                                   MPI_Datatype old_type, int size_oldtype){
930   s_smpi_mpi_hindexed_t *new_t= xbt_new(s_smpi_mpi_hindexed_t,1);
931   new_t->base.serialize = &serialize_hindexed;
932   new_t->base.unserialize = &unserialize_hindexed;
933   new_t->base.subtype_free = &free_hindexed;
934   new_t->base.subtype_use = &use_hindexed;
935   new_t->block_lengths= xbt_new(int, block_count);
936   new_t->block_indices= xbt_new(MPI_Aint, block_count);
937   for(int i=0;i<block_count;i++){
938     new_t->block_lengths[i]=block_lengths[i];
939     new_t->block_indices[i]=block_indices[i];
940   }
941   new_t->block_count = block_count;
942   new_t->old_type = old_type;
943   smpi_datatype_use(old_type);
944   new_t->size_oldtype = size_oldtype;
945   return new_t;
946 }
947
948 int smpi_datatype_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
949 {
950   int i;
951   int retval;
952   int size = 0;
953   bool contiguous=true;
954   MPI_Aint lb = 0;
955   MPI_Aint ub = 0;
956   if(count>0){
957     lb=indices[0] + smpi_datatype_lb(old_type);
958     ub=indices[0] + blocklens[0]*smpi_datatype_ub(old_type);
959   }
960   for(i=0; i< count; i++){
961     if   (blocklens[i]<0)
962       return MPI_ERR_ARG;
963     size += blocklens[i];
964
965     if(indices[i]+smpi_datatype_lb(old_type)<lb) 
966       lb = indices[i]+smpi_datatype_lb(old_type);
967     if(indices[i]+blocklens[i]*smpi_datatype_ub(old_type)>ub) 
968       ub = indices[i]+blocklens[i]*smpi_datatype_ub(old_type);
969
970     if ( (i< count -1) && (indices[i]+blocklens[i]*(static_cast<int>(smpi_datatype_size(old_type))) != indices[i+1]) )
971       contiguous=false;
972   }
973   if (old_type->sizeof_substruct != 0 || lb!=0)
974     contiguous=false;
975
976   if(!contiguous){
977     s_smpi_mpi_hindexed_t* subtype = smpi_datatype_hindexed_create( blocklens, indices, count, old_type,
978                                                                   smpi_datatype_size(old_type));
979     smpi_datatype_create(new_type,  size * smpi_datatype_size(old_type), lb, ub ,sizeof(s_smpi_mpi_hindexed_t), subtype, DT_FLAG_DATA);
980   }else{
981     s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create(lb,size, old_type, smpi_datatype_size(old_type));
982     smpi_datatype_create(new_type,  size * smpi_datatype_size(old_type), 0,size * smpi_datatype_size(old_type),
983                1, subtype, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
984   }
985   retval=MPI_SUCCESS;
986   return retval;
987 }
988
989 /* struct Implementation - Indexed with indices in bytes */
990
991 /* Copies noncontiguous data into contiguous memory.
992  *  @param contiguous_struct - output struct
993  *  @param noncontiguous_struct - input struct
994  *  @param type - pointer contening :
995  *      - stride - stride of between noncontiguous data
996  *      - block_length - the width or height of blocked matrix
997  *      - count - the number of rows of matrix
998  */
999 void serialize_struct( void* noncontiguous_struct, void *contiguous_struct, int count, void *type)
1000 {
1001   s_smpi_mpi_struct_t* type_c = reinterpret_cast<s_smpi_mpi_struct_t*>(type);
1002   int i,j;
1003   char* contiguous_struct_char = static_cast<char*>(contiguous_struct);
1004   char* noncontiguous_struct_char = static_cast<char*>(noncontiguous_struct)+ type_c->block_indices[0];
1005   for(j=0; j<count;j++){
1006     for (i = 0; i < type_c->block_count; i++) {
1007       if (type_c->old_types[i]->sizeof_substruct == 0)
1008         memcpy(contiguous_struct_char, noncontiguous_struct_char,
1009                type_c->block_lengths[i] * smpi_datatype_size(type_c->old_types[i]));
1010       else
1011         static_cast<s_smpi_subtype_t*>(type_c->old_types[i]->substruct)->serialize( noncontiguous_struct_char,
1012                                                                          contiguous_struct_char,
1013                                                                          type_c->block_lengths[i],
1014                                                                          type_c->old_types[i]->substruct);
1015
1016
1017       contiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_size(type_c->old_types[i]);
1018       if (i<type_c->block_count-1)
1019         noncontiguous_struct_char = static_cast<char*>(noncontiguous_struct) + type_c->block_indices[i+1];
1020       else //let's hope this is MPI_UB ?
1021         noncontiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_types[i]);
1022     }
1023     noncontiguous_struct=static_cast<void*>(noncontiguous_struct_char);
1024   }
1025 }
1026
1027 /* Copies contiguous data into noncontiguous memory.
1028  *  @param noncontiguous_struct - output struct
1029  *  @param contiguous_struct - input struct
1030  *  @param type - pointer contening :
1031  *      - stride - stride of between noncontiguous data
1032  *      - block_length - the width or height of blocked matrix
1033  *      - count - the number of rows of matrix
1034  */
1035 void unserialize_struct( void* contiguous_struct, void *noncontiguous_struct, int count, void *type, MPI_Op op)
1036 {
1037   s_smpi_mpi_struct_t* type_c = reinterpret_cast<s_smpi_mpi_struct_t*>(type);
1038   int i,j;
1039
1040   char* contiguous_struct_char = static_cast<char*>(contiguous_struct);
1041   char* noncontiguous_struct_char = static_cast<char*>(noncontiguous_struct)+ type_c->block_indices[0];
1042   for(j=0; j<count;j++){
1043     for (i = 0; i < type_c->block_count; i++) {
1044       if (type_c->old_types[i]->sizeof_substruct == 0)
1045         smpi_op_apply(op, contiguous_struct_char, noncontiguous_struct_char, &type_c->block_lengths[i],
1046            & type_c->old_types[i]);
1047       else
1048         static_cast<s_smpi_subtype_t*>(type_c->old_types[i]->substruct)->unserialize( contiguous_struct_char,
1049                                                                            noncontiguous_struct_char,
1050                                                                            type_c->block_lengths[i],
1051                                                                            type_c->old_types[i]->substruct, op);
1052
1053       contiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_size(type_c->old_types[i]);
1054       if (i<type_c->block_count-1)
1055         noncontiguous_struct_char =  static_cast<char*>(noncontiguous_struct) + type_c->block_indices[i+1];
1056       else
1057         noncontiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_types[i]);
1058     }
1059     noncontiguous_struct=reinterpret_cast<void*>(noncontiguous_struct_char);
1060   }
1061 }
1062
1063 void free_struct(MPI_Datatype* type){
1064   int i=0;
1065   for (i = 0; i < reinterpret_cast<s_smpi_mpi_struct_t*>((*type)->substruct)->block_count; i++)
1066     smpi_datatype_unuse(reinterpret_cast<s_smpi_mpi_struct_t*>((*type)->substruct)->old_types[i]);
1067   if((*type)->in_use==0){
1068     xbt_free(reinterpret_cast<s_smpi_mpi_struct_t*>((*type)->substruct)->block_lengths);
1069     xbt_free(reinterpret_cast<s_smpi_mpi_struct_t*>((*type)->substruct)->block_indices);
1070     xbt_free(reinterpret_cast<s_smpi_mpi_struct_t*>((*type)->substruct)->old_types);
1071   }
1072 }
1073
1074 void use_struct(MPI_Datatype* type){
1075   int i=0;
1076   for (i = 0; i < reinterpret_cast<s_smpi_mpi_struct_t*>((*type)->substruct)->block_count; i++)
1077     smpi_datatype_use(reinterpret_cast<s_smpi_mpi_struct_t*>((*type)->substruct)->old_types[i]);
1078 }
1079
1080 /* Create a Sub type struct to be able to serialize and unserialize it the structure s_smpi_mpi_struct_t is derived
1081  * from s_smpi_subtype which required the functions unserialize and serialize
1082  */
1083 s_smpi_mpi_struct_t* smpi_datatype_struct_create( int* block_lengths, MPI_Aint* block_indices, int block_count,
1084                                                   MPI_Datatype* old_types){
1085   s_smpi_mpi_struct_t *new_t= xbt_new(s_smpi_mpi_struct_t,1);
1086   new_t->base.serialize = &serialize_struct;
1087   new_t->base.unserialize = &unserialize_struct;
1088   new_t->base.subtype_free = &free_struct;
1089   new_t->base.subtype_use = &use_struct;
1090   new_t->block_lengths= xbt_new(int, block_count);
1091   new_t->block_indices= xbt_new(MPI_Aint, block_count);
1092   new_t->old_types=  xbt_new(MPI_Datatype, block_count);
1093   int i;
1094   for(i=0;i<block_count;i++){
1095     new_t->block_lengths[i]=block_lengths[i];
1096     new_t->block_indices[i]=block_indices[i];
1097     new_t->old_types[i]=old_types[i];
1098     smpi_datatype_use(new_t->old_types[i]);
1099   }
1100   new_t->block_count = block_count;
1101   return new_t;
1102 }
1103
1104 int smpi_datatype_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type)
1105 {
1106   int i;
1107   size_t size = 0;
1108   bool contiguous=true;
1109   size = 0;
1110   MPI_Aint lb = 0;
1111   MPI_Aint ub = 0;
1112   if(count>0){
1113     lb=indices[0] + smpi_datatype_lb(old_types[0]);
1114     ub=indices[0] + blocklens[0]*smpi_datatype_ub(old_types[0]);
1115   }
1116   bool forced_lb=false;
1117   bool forced_ub=false;
1118   for(i=0; i< count; i++){
1119     if (blocklens[i]<0)
1120       return MPI_ERR_ARG;
1121     if (old_types[i]->sizeof_substruct != 0)
1122       contiguous=false;
1123
1124     size += blocklens[i]*smpi_datatype_size(old_types[i]);
1125     if (old_types[i]==MPI_LB){
1126       lb=indices[i];
1127       forced_lb=true;
1128     }
1129     if (old_types[i]==MPI_UB){
1130       ub=indices[i];
1131       forced_ub=true;
1132     }
1133
1134     if(!forced_lb && indices[i]+smpi_datatype_lb(old_types[i])<lb) 
1135       lb = indices[i];
1136     if(!forced_ub &&  indices[i]+blocklens[i]*smpi_datatype_ub(old_types[i])>ub)
1137       ub = indices[i]+blocklens[i]*smpi_datatype_ub(old_types[i]);
1138
1139     if ( (i< count -1) && (indices[i]+blocklens[i]*static_cast<int>(smpi_datatype_size(old_types[i])) != indices[i+1]) )
1140       contiguous=false;
1141   }
1142
1143   if(!contiguous){
1144     s_smpi_mpi_struct_t* subtype = smpi_datatype_struct_create( blocklens, indices, count, old_types);
1145
1146     smpi_datatype_create(new_type,  size, lb, ub,sizeof(s_smpi_mpi_struct_t), subtype, DT_FLAG_DATA);
1147   }else{
1148     s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb, size, MPI_CHAR, 1);
1149     smpi_datatype_create(new_type,  size, lb, ub,1, subtype, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
1150   }
1151   return MPI_SUCCESS;
1152 }
1153
1154 void smpi_datatype_commit(MPI_Datatype *datatype)
1155 {
1156   (*datatype)->flags=  ((*datatype)->flags | DT_FLAG_COMMITED);
1157 }
1158
1159 typedef struct s_smpi_mpi_op {
1160   MPI_User_function *func;
1161   bool is_commute;
1162   bool is_fortran_op;
1163 } s_smpi_mpi_op_t;
1164
1165 #define MAX_OP(a, b)  (b) = (a) < (b) ? (b) : (a)
1166 #define MIN_OP(a, b)  (b) = (a) < (b) ? (a) : (b)
1167 #define SUM_OP(a, b)  (b) += (a)
1168 #define PROD_OP(a, b) (b) *= (a)
1169 #define LAND_OP(a, b) (b) = (a) && (b)
1170 #define LOR_OP(a, b)  (b) = (a) || (b)
1171 #define LXOR_OP(a, b) (b) = (!(a) && (b)) || ((a) && !(b))
1172 #define BAND_OP(a, b) (b) &= (a)
1173 #define BOR_OP(a, b)  (b) |= (a)
1174 #define BXOR_OP(a, b) (b) ^= (a)
1175 #define MAXLOC_OP(a, b)  (b) = (a.value) < (b.value) ? (b) : (a)
1176 #define MINLOC_OP(a, b)  (b) = (a.value) < (b.value) ? (a) : (b)
1177
1178 #define APPLY_FUNC(a, b, length, type, func) \
1179 {                                          \
1180   int i;                                   \
1181   type* x = (type*)(a);                    \
1182   type* y = (type*)(b);                    \
1183   for(i = 0; i < *(length); i++) {         \
1184     func(x[i], y[i]);                      \
1185   }                                        \
1186 }
1187
1188 #define APPLY_OP_LOOP(dtype, type, op) \
1189   if (*datatype == dtype) {\
1190     APPLY_FUNC(a, b, length, type, op)\
1191   } else \
1192
1193
1194 #define APPLY_BASIC_OP_LOOP(op)\
1195 APPLY_OP_LOOP(MPI_CHAR, char,op)\
1196 APPLY_OP_LOOP(MPI_SHORT, short,op)\
1197 APPLY_OP_LOOP(MPI_INT, int,op)\
1198 APPLY_OP_LOOP(MPI_LONG, long,op)\
1199 APPLY_OP_LOOP(MPI_LONG_LONG, long long,op)\
1200 APPLY_OP_LOOP(MPI_SIGNED_CHAR, signed char,op)\
1201 APPLY_OP_LOOP(MPI_UNSIGNED_CHAR, unsigned char,op)\
1202 APPLY_OP_LOOP(MPI_UNSIGNED_SHORT, unsigned short,op)\
1203 APPLY_OP_LOOP(MPI_UNSIGNED, unsigned int,op)\
1204 APPLY_OP_LOOP(MPI_UNSIGNED_LONG, unsigned long,op)\
1205 APPLY_OP_LOOP(MPI_UNSIGNED_LONG_LONG, unsigned long long,op)\
1206 APPLY_OP_LOOP(MPI_WCHAR, wchar_t,op)\
1207 APPLY_OP_LOOP(MPI_BYTE, int8_t,op)\
1208 APPLY_OP_LOOP(MPI_INT8_T, int8_t,op)\
1209 APPLY_OP_LOOP(MPI_INT16_T, int16_t,op)\
1210 APPLY_OP_LOOP(MPI_INT32_T, int32_t,op)\
1211 APPLY_OP_LOOP(MPI_INT64_T, int64_t,op)\
1212 APPLY_OP_LOOP(MPI_UINT8_T, uint8_t,op)\
1213 APPLY_OP_LOOP(MPI_UINT16_T, uint16_t,op)\
1214 APPLY_OP_LOOP(MPI_UINT32_T, uint32_t,op)\
1215 APPLY_OP_LOOP(MPI_UINT64_T, uint64_t,op)\
1216 APPLY_OP_LOOP(MPI_AINT, MPI_Aint,op)\
1217 APPLY_OP_LOOP(MPI_OFFSET, MPI_Offset,op)\
1218 APPLY_OP_LOOP(MPI_INTEGER1, int,op)\
1219 APPLY_OP_LOOP(MPI_INTEGER2, int16_t,op)\
1220 APPLY_OP_LOOP(MPI_INTEGER4, int32_t,op)\
1221 APPLY_OP_LOOP(MPI_INTEGER8, int64_t,op)
1222
1223 #define APPLY_BOOL_OP_LOOP(op)\
1224 APPLY_OP_LOOP(MPI_C_BOOL, bool,op)
1225
1226 #define APPLY_FLOAT_OP_LOOP(op)\
1227 APPLY_OP_LOOP(MPI_FLOAT, float,op)\
1228 APPLY_OP_LOOP(MPI_DOUBLE, double,op)\
1229 APPLY_OP_LOOP(MPI_LONG_DOUBLE, long double,op)\
1230 APPLY_OP_LOOP(MPI_REAL, float,op)\
1231 APPLY_OP_LOOP(MPI_REAL4, float,op)\
1232 APPLY_OP_LOOP(MPI_REAL8, float,op)\
1233 APPLY_OP_LOOP(MPI_REAL16, double,op)
1234
1235 #define APPLY_COMPLEX_OP_LOOP(op)\
1236 APPLY_OP_LOOP(MPI_C_FLOAT_COMPLEX, float _Complex,op)\
1237 APPLY_OP_LOOP(MPI_C_DOUBLE_COMPLEX, double _Complex,op)\
1238 APPLY_OP_LOOP(MPI_C_LONG_DOUBLE_COMPLEX, long double _Complex,op)
1239
1240 #define APPLY_PAIR_OP_LOOP(op)\
1241 APPLY_OP_LOOP(MPI_FLOAT_INT, float_int,op)\
1242 APPLY_OP_LOOP(MPI_LONG_INT, long_int,op)\
1243 APPLY_OP_LOOP(MPI_DOUBLE_INT, double_int,op)\
1244 APPLY_OP_LOOP(MPI_SHORT_INT, short_int,op)\
1245 APPLY_OP_LOOP(MPI_2INT, int_int,op)\
1246 APPLY_OP_LOOP(MPI_2FLOAT, float_float,op)\
1247 APPLY_OP_LOOP(MPI_2DOUBLE, double_double,op)\
1248 APPLY_OP_LOOP(MPI_LONG_DOUBLE_INT, long_double_int,op)\
1249 APPLY_OP_LOOP(MPI_2LONG, long_long,op)
1250
1251 #define APPLY_END_OP_LOOP(op)\
1252   {\
1253     xbt_die("Failed to apply " #op " to type %s", (*datatype)->name);\
1254   }
1255
1256
1257 static void max_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1258 {
1259   APPLY_BASIC_OP_LOOP(MAX_OP)
1260   APPLY_FLOAT_OP_LOOP(MAX_OP)
1261   APPLY_END_OP_LOOP(MAX_OP)
1262 }
1263
1264 static void min_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1265 {
1266   APPLY_BASIC_OP_LOOP(MIN_OP)
1267   APPLY_FLOAT_OP_LOOP(MIN_OP)
1268   APPLY_END_OP_LOOP(MIN_OP)
1269 }
1270
1271 static void sum_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1272 {
1273   APPLY_BASIC_OP_LOOP(SUM_OP)
1274   APPLY_FLOAT_OP_LOOP(SUM_OP)
1275   APPLY_COMPLEX_OP_LOOP(SUM_OP)
1276   APPLY_END_OP_LOOP(SUM_OP)
1277 }
1278
1279 static void prod_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1280 {
1281   APPLY_BASIC_OP_LOOP(PROD_OP)
1282   APPLY_FLOAT_OP_LOOP(PROD_OP)
1283   APPLY_COMPLEX_OP_LOOP(PROD_OP)
1284   APPLY_END_OP_LOOP(PROD_OP)
1285 }
1286
1287 static void land_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1288 {
1289   APPLY_BASIC_OP_LOOP(LAND_OP)
1290   APPLY_BOOL_OP_LOOP(LAND_OP)
1291   APPLY_END_OP_LOOP(LAND_OP)
1292 }
1293
1294 static void lor_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1295 {
1296   APPLY_BASIC_OP_LOOP(LOR_OP)
1297   APPLY_BOOL_OP_LOOP(LOR_OP)
1298   APPLY_END_OP_LOOP(LOR_OP)
1299 }
1300
1301 static void lxor_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1302 {
1303   APPLY_BASIC_OP_LOOP(LXOR_OP)
1304   APPLY_BOOL_OP_LOOP(LXOR_OP)
1305   APPLY_END_OP_LOOP(LXOR_OP)
1306 }
1307
1308 static void band_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1309 {
1310   APPLY_BASIC_OP_LOOP(BAND_OP)
1311   APPLY_BOOL_OP_LOOP(BAND_OP)
1312   APPLY_END_OP_LOOP(BAND_OP)
1313 }
1314
1315 static void bor_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1316 {
1317   APPLY_BASIC_OP_LOOP(BOR_OP)
1318   APPLY_BOOL_OP_LOOP(BOR_OP)
1319   APPLY_END_OP_LOOP(BOR_OP)
1320 }
1321
1322 static void bxor_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1323 {
1324   APPLY_BASIC_OP_LOOP(BXOR_OP)
1325   APPLY_BOOL_OP_LOOP(BXOR_OP)
1326   APPLY_END_OP_LOOP(BXOR_OP)
1327 }
1328
1329 static void minloc_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1330 {
1331   APPLY_PAIR_OP_LOOP(MINLOC_OP)
1332   APPLY_END_OP_LOOP(MINLOC_OP)
1333 }
1334
1335 static void maxloc_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1336 {
1337   APPLY_PAIR_OP_LOOP(MAXLOC_OP)
1338   APPLY_END_OP_LOOP(MAXLOC_OP)
1339 }
1340
1341 static void replace_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1342 {
1343   memcpy(b, a, *length * smpi_datatype_size(*datatype));
1344 }
1345
1346 #define CREATE_MPI_OP(name, func)                             \
1347   static s_smpi_mpi_op_t mpi_##name = { &(func) /* func */, true, false }; \
1348 MPI_Op name = &mpi_##name;
1349
1350 CREATE_MPI_OP(MPI_MAX, max_func);
1351 CREATE_MPI_OP(MPI_MIN, min_func);
1352 CREATE_MPI_OP(MPI_SUM, sum_func);
1353 CREATE_MPI_OP(MPI_PROD, prod_func);
1354 CREATE_MPI_OP(MPI_LAND, land_func);
1355 CREATE_MPI_OP(MPI_LOR, lor_func);
1356 CREATE_MPI_OP(MPI_LXOR, lxor_func);
1357 CREATE_MPI_OP(MPI_BAND, band_func);
1358 CREATE_MPI_OP(MPI_BOR, bor_func);
1359 CREATE_MPI_OP(MPI_BXOR, bxor_func);
1360 CREATE_MPI_OP(MPI_MAXLOC, maxloc_func);
1361 CREATE_MPI_OP(MPI_MINLOC, minloc_func);
1362 CREATE_MPI_OP(MPI_REPLACE, replace_func);
1363
1364 MPI_Op smpi_op_new(MPI_User_function * function, bool commute)
1365 {
1366   MPI_Op op;
1367   op = xbt_new(s_smpi_mpi_op_t, 1);
1368   op->func = function;
1369   op-> is_commute = commute;
1370   op-> is_fortran_op = false;
1371   return op;
1372 }
1373
1374 bool smpi_op_is_commute(MPI_Op op)
1375 {
1376   return (op==MPI_OP_NULL) ? true : op-> is_commute;
1377 }
1378
1379 void smpi_op_destroy(MPI_Op op)
1380 {
1381   xbt_free(op);
1382 }
1383
1384 void smpi_op_set_fortran(MPI_Op op)
1385 {
1386   //tell that we were created from fortran, so we need to translate the type to fortran when called
1387   op->is_fortran_op = true;
1388 }
1389
1390 void smpi_op_apply(MPI_Op op, void *invec, void *inoutvec, int *len, MPI_Datatype * datatype)
1391 {
1392   if(op==MPI_OP_NULL)
1393     return;
1394
1395   if(smpi_privatize_global_variables){//we need to switch as the called function may silently touch global variables
1396     XBT_DEBUG("Applying operation, switch to the right data frame ");
1397     smpi_switch_data_segment(smpi_process_index());
1398   }
1399
1400   if(!smpi_process_get_replaying()){
1401     if(! op->is_fortran_op)
1402       op->func(invec, inoutvec, len, datatype);
1403     else{
1404       int tmp = smpi_type_c2f(*datatype);
1405       /* Unfortunately, the C and Fortran version of the MPI standard do not agree on the type here,
1406          thus the reinterpret_cast. */
1407       op->func(invec, inoutvec, len, reinterpret_cast<MPI_Datatype*>(&tmp) );
1408     }
1409   }
1410 }
1411
1412 int smpi_type_attr_delete(MPI_Datatype type, int keyval){
1413   smpi_type_key_elem elem =
1414     static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
1415   if(elem==nullptr)
1416     return MPI_ERR_ARG;
1417   if(elem->delete_fn!=MPI_NULL_DELETE_FN){
1418     void * value = nullptr;
1419     int flag;
1420     if(smpi_type_attr_get(type, keyval, &value, &flag)==MPI_SUCCESS){
1421       int ret = elem->delete_fn(type, keyval, value, &flag);
1422       if(ret!=MPI_SUCCESS) 
1423         return ret;
1424     }
1425   }  
1426   if(type->attributes==nullptr)
1427     return MPI_ERR_ARG;
1428
1429   xbt_dict_remove_ext(type->attributes, reinterpret_cast<const char*>(&keyval), sizeof(int));
1430   return MPI_SUCCESS;
1431 }
1432
1433 int smpi_type_attr_get(MPI_Datatype type, int keyval, void* attr_value, int* flag){
1434   smpi_type_key_elem elem =
1435     static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
1436   if(elem==nullptr)
1437     return MPI_ERR_ARG;
1438   if(type->attributes==nullptr){
1439     *flag=0;
1440     return MPI_SUCCESS;
1441   }
1442   try {
1443     *static_cast<void**>(attr_value) = xbt_dict_get_ext(type->attributes, reinterpret_cast<const char*>(&keyval), sizeof(int));
1444     *flag=1;
1445   }
1446   catch (xbt_ex& ex) {
1447     *flag=0;
1448   }
1449   return MPI_SUCCESS;
1450 }
1451
1452 int smpi_type_attr_put(MPI_Datatype type, int keyval, void* attr_value){
1453   if(smpi_type_keyvals==nullptr)
1454     smpi_type_keyvals = xbt_dict_new_homogeneous(nullptr);
1455   smpi_type_key_elem elem =
1456      static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
1457   if(elem==nullptr)
1458     return MPI_ERR_ARG;
1459   int flag;
1460   void* value = nullptr;
1461   smpi_type_attr_get(type, keyval, &value, &flag);
1462   if(flag!=0 && elem->delete_fn!=MPI_NULL_DELETE_FN){
1463     int ret = elem->delete_fn(type, keyval, value, &flag);
1464     if(ret!=MPI_SUCCESS) 
1465       return ret;
1466   }
1467   if(type->attributes==nullptr)
1468     type->attributes = xbt_dict_new_homogeneous(nullptr);
1469
1470   xbt_dict_set_ext(type->attributes, reinterpret_cast<const char*>(&keyval), sizeof(int), attr_value, nullptr);
1471   return MPI_SUCCESS;
1472 }
1473
1474 int smpi_type_keyval_create(MPI_Type_copy_attr_function* copy_fn, MPI_Type_delete_attr_function* delete_fn, int* keyval,
1475                             void* extra_state){
1476   if(smpi_type_keyvals==nullptr)
1477     smpi_type_keyvals = xbt_dict_new_homogeneous(nullptr);
1478
1479   smpi_type_key_elem value = (smpi_type_key_elem) xbt_new0(s_smpi_mpi_type_key_elem_t,1);
1480
1481   value->copy_fn=copy_fn;
1482   value->delete_fn=delete_fn;
1483
1484   *keyval = type_keyval_id;
1485   xbt_dict_set_ext(smpi_type_keyvals,reinterpret_cast<const char*>(keyval), sizeof(int),reinterpret_cast<void*>(value), nullptr);
1486   type_keyval_id++;
1487   return MPI_SUCCESS;
1488 }
1489
1490 int smpi_type_keyval_free(int* keyval){
1491   smpi_type_key_elem elem =
1492     static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int)));
1493   if(elem==0){
1494     return MPI_ERR_ARG;
1495   }
1496   xbt_dict_remove_ext(smpi_type_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int));
1497   xbt_free(elem);
1498   return MPI_SUCCESS;
1499 }
1500
1501 int smpi_mpi_pack(void* inbuf, int incount, MPI_Datatype type, void* outbuf, int outcount, int* position,MPI_Comm comm){
1502   size_t size = smpi_datatype_size(type);
1503   if (outcount - *position < incount*static_cast<int>(size))
1504     return MPI_ERR_BUFFER;
1505   smpi_datatype_copy(inbuf, incount, type, static_cast<char*>(outbuf) + *position, outcount, MPI_CHAR);
1506   *position += incount * size;
1507   return MPI_SUCCESS;
1508 }
1509
1510 int smpi_mpi_unpack(void* inbuf, int insize, int* position, void* outbuf, int outcount, MPI_Datatype type,MPI_Comm comm){
1511   int size = static_cast<int>(smpi_datatype_size(type));
1512   if (outcount*size> insize)
1513     return MPI_ERR_BUFFER;
1514   smpi_datatype_copy(static_cast<char*>(inbuf) + *position, insize, MPI_CHAR, outbuf, outcount, type);
1515   *position += outcount * size;
1516   return MPI_SUCCESS;
1517 }