Logo AND Algorithmique Numérique Distribuée

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