Logo AND Algorithmique Numérique Distribuée

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