Logo AND Algorithmique Numérique Distribuée

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