Logo AND Algorithmique Numérique Distribuée

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