Logo AND Algorithmique Numérique Distribuée

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