Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : ignore in_use in s_smpi_mpi_datatype
[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 #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   if(MC_is_active())
306     MC_ignore(&(new_t->in_use), sizeof(new_t->in_use));
307 }
308
309 void smpi_datatype_free(MPI_Datatype* type){
310
311   if((*type)->flags & DT_FLAG_PREDEFINED)return;
312
313   //if still used, mark for deletion
314   if((*type)->in_use!=0){
315       (*type)->flags |=DT_FLAG_DESTROYED;
316       return;
317   }
318
319   if ((*type)->has_subtype == 1){
320     ((s_smpi_subtype_t *)(*type)->substruct)->subtype_free(type);  
321     xbt_free((*type)->substruct);
322   }
323   xbt_free(*type);
324
325 }
326
327 void smpi_datatype_use(MPI_Datatype type){
328   if(type)type->in_use++;
329   
330   if(MC_is_active())
331     MC_ignore(&(type->in_use), sizeof(type->in_use));
332 }
333
334
335 void smpi_datatype_unuse(MPI_Datatype type){
336   if(type && type->in_use-- == 0 && (type->flags & DT_FLAG_DESTROYED))
337     smpi_datatype_free(&type);
338
339   if(MC_is_active())
340     MC_ignore(&(type->in_use), sizeof(type->in_use));
341 }
342
343
344
345
346 /*
347 Contiguous Implementation
348 */
349
350
351 /*
352  *  Copies noncontiguous data into contiguous memory.
353  *  @param contiguous_hvector - output hvector
354  *  @param noncontiguous_hvector - input hvector
355  *  @param type - pointer contening :
356  *      - stride - stride of between noncontiguous data, in bytes
357  *      - block_length - the width or height of blocked matrix
358  *      - count - the number of rows of matrix
359  */
360 void serialize_contiguous( const void *noncontiguous_hvector,
361                        void *contiguous_hvector,
362                        size_t count,
363                        void *type)
364 {
365   s_smpi_mpi_contiguous_t* type_c = (s_smpi_mpi_contiguous_t*)type;
366   char* contiguous_vector_char = (char*)contiguous_hvector;
367   char* noncontiguous_vector_char = (char*)noncontiguous_hvector+type_c->lb;
368   memcpy(contiguous_vector_char,
369            noncontiguous_vector_char, count* type_c->block_count * type_c->size_oldtype);
370 }
371 /*
372  *  Copies contiguous data into noncontiguous memory.
373  *  @param noncontiguous_vector - output hvector
374  *  @param contiguous_vector - input hvector
375  *  @param type - pointer contening :
376  *      - stride - stride of between noncontiguous data, in bytes
377  *      - block_length - the width or height of blocked matrix
378  *      - count - the number of rows of matrix
379  */
380 void unserialize_contiguous( const void *contiguous_vector,
381                          void *noncontiguous_vector,
382                          size_t count,
383                          void *type)
384 {
385   s_smpi_mpi_contiguous_t* type_c = (s_smpi_mpi_contiguous_t*)type;
386   char* contiguous_vector_char = (char*)contiguous_vector;
387   char* noncontiguous_vector_char = (char*)noncontiguous_vector+type_c->lb;
388
389   memcpy(noncontiguous_vector_char,
390            contiguous_vector_char, count*  type_c->block_count * type_c->size_oldtype);
391 }
392
393 void free_contiguous(MPI_Datatype* d){
394 }
395
396 /*
397  * Create a Sub type contiguous to be able to serialize and unserialize it
398  * the structure s_smpi_mpi_contiguous_t is derived from s_smpi_subtype which
399  * required the functions unserialize and serialize
400  *
401  */
402 s_smpi_mpi_contiguous_t* smpi_datatype_contiguous_create( MPI_Aint lb,
403                                                   int block_count,
404                                                   MPI_Datatype old_type,
405                                                   int size_oldtype){
406   s_smpi_mpi_contiguous_t *new_t= xbt_new(s_smpi_mpi_contiguous_t,1);
407   new_t->base.serialize = &serialize_contiguous;
408   new_t->base.unserialize = &unserialize_contiguous;
409   new_t->base.subtype_free = &free_contiguous;
410   new_t->lb = lb;
411   new_t->block_count = block_count;
412   new_t->old_type = old_type;
413   new_t->size_oldtype = size_oldtype;
414   return new_t;
415 }
416
417
418
419
420 int smpi_datatype_contiguous(int count, MPI_Datatype old_type, MPI_Datatype* new_type, MPI_Aint lb)
421 {
422   int retval;
423   if(old_type->has_subtype){
424           //handle this case as a hvector with stride equals to the extent of the datatype
425           return smpi_datatype_hvector(count, 1, smpi_datatype_get_extent(old_type), old_type, new_type);
426   }
427   
428   s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb,
429                                                                 count,
430                                                                 old_type,
431                                                                 smpi_datatype_size(old_type));
432                                                                 
433   smpi_datatype_create(new_type,
434                                           count * smpi_datatype_size(old_type),
435                                           lb,lb + count * smpi_datatype_size(old_type),
436                                           1,subtype, DT_FLAG_CONTIGUOUS);
437   retval=MPI_SUCCESS;
438   return retval;
439 }
440
441 int smpi_datatype_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type)
442 {
443   int retval;
444   if (blocklen<0) return MPI_ERR_ARG;
445   MPI_Aint lb = 0;
446   MPI_Aint ub = 0;
447   if(count>0){
448     lb=smpi_datatype_lb(old_type);
449     ub=((count-1)*stride+blocklen-1)*smpi_datatype_get_extent(old_type)+smpi_datatype_ub(old_type);
450   }
451   if(old_type->has_subtype || stride != blocklen){
452
453
454     s_smpi_mpi_vector_t* subtype = smpi_datatype_vector_create( stride,
455                                                                 blocklen,
456                                                                 count,
457                                                                 old_type,
458                                                                 smpi_datatype_size(old_type));
459     smpi_datatype_create(new_type,
460                          count * (blocklen) * smpi_datatype_size(old_type), lb,
461                          ub,
462                          1,
463                          subtype,
464                          DT_FLAG_VECTOR);
465     retval=MPI_SUCCESS;
466   }else{
467     /* in this situation the data are contignous thus it's not
468      * required to serialize and unserialize it*/
469     smpi_datatype_create(new_type, count * blocklen *
470                          smpi_datatype_size(old_type), 0, ((count -1) * stride + blocklen)*
471                          smpi_datatype_size(old_type),
472                          0,
473                          NULL,
474                          DT_FLAG_VECTOR|DT_FLAG_CONTIGUOUS);
475     retval=MPI_SUCCESS;
476   }
477   return retval;
478 }
479
480 void free_vector(MPI_Datatype* d){
481 }
482
483 /*
484 Hvector Implementation - Vector with stride in bytes
485 */
486
487
488 /*
489  *  Copies noncontiguous data into contiguous memory.
490  *  @param contiguous_hvector - output hvector
491  *  @param noncontiguous_hvector - input hvector
492  *  @param type - pointer contening :
493  *      - stride - stride of between noncontiguous data, in bytes
494  *      - block_length - the width or height of blocked matrix
495  *      - count - the number of rows of matrix
496  */
497 void serialize_hvector( const void *noncontiguous_hvector,
498                        void *contiguous_hvector,
499                        size_t count,
500                        void *type)
501 {
502   s_smpi_mpi_hvector_t* type_c = (s_smpi_mpi_hvector_t*)type;
503   int i;
504   char* contiguous_vector_char = (char*)contiguous_hvector;
505   char* noncontiguous_vector_char = (char*)noncontiguous_hvector;
506
507   for (i = 0; i < type_c->block_count * count; i++) {
508     if (type_c->old_type->has_subtype == 0)
509       memcpy(contiguous_vector_char,
510            noncontiguous_vector_char, type_c->block_length * type_c->size_oldtype);
511     else
512       ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_vector_char,
513                                                                    contiguous_vector_char,
514                                                                    type_c->block_length,
515                                                                    type_c->old_type->substruct);
516
517     contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
518     if((i+1)%type_c->block_count ==0)
519     noncontiguous_vector_char += type_c->block_length*type_c->size_oldtype;
520     else
521     noncontiguous_vector_char += type_c->block_stride;
522   }
523 }
524 /*
525  *  Copies contiguous data into noncontiguous memory.
526  *  @param noncontiguous_vector - output hvector
527  *  @param contiguous_vector - input hvector
528  *  @param type - pointer contening :
529  *      - stride - stride of between noncontiguous data, in bytes
530  *      - block_length - the width or height of blocked matrix
531  *      - count - the number of rows of matrix
532  */
533 void unserialize_hvector( const void *contiguous_vector,
534                          void *noncontiguous_vector,
535                          size_t count,
536                          void *type)
537 {
538   s_smpi_mpi_hvector_t* type_c = (s_smpi_mpi_hvector_t*)type;
539   int i;
540
541   char* contiguous_vector_char = (char*)contiguous_vector;
542   char* noncontiguous_vector_char = (char*)noncontiguous_vector;
543
544   for (i = 0; i < type_c->block_count * count; i++) {
545     if (type_c->old_type->has_subtype == 0)
546       memcpy(noncontiguous_vector_char,
547            contiguous_vector_char, type_c->block_length * type_c->size_oldtype);
548     else
549       ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_vector_char,
550                                                                      noncontiguous_vector_char,
551                                                                      type_c->block_length,
552                                                                      type_c->old_type->substruct);
553     contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
554     if((i+1)%type_c->block_count ==0)
555     noncontiguous_vector_char += type_c->block_length*type_c->size_oldtype;
556     else
557     noncontiguous_vector_char += type_c->block_stride;
558   }
559 }
560
561 /*
562  * Create a Sub type vector to be able to serialize and unserialize it
563  * the structure s_smpi_mpi_vector_t is derived from s_smpi_subtype which
564  * required the functions unserialize and serialize
565  *
566  */
567 s_smpi_mpi_hvector_t* smpi_datatype_hvector_create( MPI_Aint block_stride,
568                                                   int block_length,
569                                                   int block_count,
570                                                   MPI_Datatype old_type,
571                                                   int size_oldtype){
572   s_smpi_mpi_hvector_t *new_t= xbt_new(s_smpi_mpi_hvector_t,1);
573   new_t->base.serialize = &serialize_hvector;
574   new_t->base.unserialize = &unserialize_hvector;
575   new_t->base.subtype_free = &free_hvector;
576   new_t->block_stride = block_stride;
577   new_t->block_length = block_length;
578   new_t->block_count = block_count;
579   new_t->old_type = old_type;
580   new_t->size_oldtype = size_oldtype;
581   return new_t;
582 }
583
584 //do nothing for vector types
585 void free_hvector(MPI_Datatype* d){
586 }
587
588 int smpi_datatype_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type)
589 {
590   int retval;
591   if (blocklen<0) return MPI_ERR_ARG;
592   MPI_Aint lb = 0;
593   MPI_Aint ub = 0;
594   if(count>0){
595     lb=smpi_datatype_lb(old_type);
596     ub=((count-1)*stride)+(blocklen-1)*smpi_datatype_get_extent(old_type)+smpi_datatype_ub(old_type);
597   }
598   if(old_type->has_subtype || stride != blocklen*smpi_datatype_get_extent(old_type)){
599     s_smpi_mpi_hvector_t* subtype = smpi_datatype_hvector_create( stride,
600                                                                   blocklen,
601                                                                   count,
602                                                                   old_type,
603                                                                   smpi_datatype_size(old_type));
604
605     smpi_datatype_create(new_type, count * blocklen * smpi_datatype_size(old_type),
606                                                  lb,ub,
607                          1,
608                          subtype,
609                          DT_FLAG_VECTOR);
610     retval=MPI_SUCCESS;
611   }else{
612     smpi_datatype_create(new_type, count * blocklen *
613                                              smpi_datatype_size(old_type),0,count * blocklen *
614                                              smpi_datatype_size(old_type),
615                                             0,
616                                             NULL,
617                                             DT_FLAG_VECTOR|DT_FLAG_CONTIGUOUS);
618     retval=MPI_SUCCESS;
619   }
620   return retval;
621 }
622
623
624 /*
625 Indexed Implementation
626 */
627
628 /*
629  *  Copies noncontiguous data into contiguous memory.
630  *  @param contiguous_indexed - output indexed
631  *  @param noncontiguous_indexed - input indexed
632  *  @param type - pointer contening :
633  *      - block_lengths - the width or height of blocked matrix
634  *      - block_indices - indices of each data, in element
635  *      - count - the number of rows of matrix
636  */
637 void serialize_indexed( const void *noncontiguous_indexed,
638                        void *contiguous_indexed,
639                        size_t count,
640                        void *type)
641 {
642   s_smpi_mpi_indexed_t* type_c = (s_smpi_mpi_indexed_t*)type;
643   int i,j;
644   char* contiguous_indexed_char = (char*)contiguous_indexed;
645   char* noncontiguous_indexed_char = (char*)noncontiguous_indexed+type_c->block_indices[0] * type_c->size_oldtype;
646   for(j=0; j<count;j++){
647     for (i = 0; i < type_c->block_count; i++) {
648       if (type_c->old_type->has_subtype == 0)
649         memcpy(contiguous_indexed_char,
650                      noncontiguous_indexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
651       else
652         ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_indexed_char,
653                                                                      contiguous_indexed_char,
654                                                                      type_c->block_lengths[i],
655                                                                      type_c->old_type->substruct);
656
657
658       contiguous_indexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
659       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);
660       else noncontiguous_indexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
661     }
662     noncontiguous_indexed=(void*)noncontiguous_indexed_char;
663   }
664 }
665 /*
666  *  Copies contiguous data into noncontiguous memory.
667  *  @param noncontiguous_indexed - output indexed
668  *  @param contiguous_indexed - input indexed
669  *  @param type - pointer contening :
670  *      - block_lengths - the width or height of blocked matrix
671  *      - block_indices - indices of each data, in element
672  *      - count - the number of rows of matrix
673  */
674 void unserialize_indexed( const void *contiguous_indexed,
675                          void *noncontiguous_indexed,
676                          size_t count,
677                          void *type)
678 {
679
680   s_smpi_mpi_indexed_t* type_c = (s_smpi_mpi_indexed_t*)type;
681   int i,j;
682   char* contiguous_indexed_char = (char*)contiguous_indexed;
683   char* noncontiguous_indexed_char = (char*)noncontiguous_indexed+type_c->block_indices[0]*smpi_datatype_get_extent(type_c->old_type);
684   for(j=0; j<count;j++){
685     for (i = 0; i < type_c->block_count; i++) {
686       if (type_c->old_type->has_subtype == 0)
687         memcpy(noncontiguous_indexed_char ,
688              contiguous_indexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
689       else
690         ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_indexed_char,
691                                                                        noncontiguous_indexed_char,
692                                                                        type_c->block_lengths[i],
693                                                                        type_c->old_type->substruct);
694
695       contiguous_indexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
696       if (i<type_c->block_count-1)
697         noncontiguous_indexed_char = (char*)noncontiguous_indexed + type_c->block_indices[i+1]*smpi_datatype_get_extent(type_c->old_type);
698       else noncontiguous_indexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
699     }
700     noncontiguous_indexed=(void*)noncontiguous_indexed_char;
701   }
702 }
703
704 void free_indexed(MPI_Datatype* type){
705   xbt_free(((s_smpi_mpi_indexed_t *)(*type)->substruct)->block_lengths);
706   xbt_free(((s_smpi_mpi_indexed_t *)(*type)->substruct)->block_indices);
707 }
708
709 /*
710  * Create a Sub type indexed to be able to serialize and unserialize it
711  * the structure s_smpi_mpi_indexed_t is derived from s_smpi_subtype which
712  * required the functions unserialize and serialize
713  */
714 s_smpi_mpi_indexed_t* smpi_datatype_indexed_create( int* block_lengths,
715                                                   int* block_indices,
716                                                   int block_count,
717                                                   MPI_Datatype old_type,
718                                                   int size_oldtype){
719   s_smpi_mpi_indexed_t *new_t= xbt_new(s_smpi_mpi_indexed_t,1);
720   new_t->base.serialize = &serialize_indexed;
721   new_t->base.unserialize = &unserialize_indexed;
722   new_t->base.subtype_free = &free_indexed;
723  //TODO : add a custom function for each time to clean these 
724   new_t->block_lengths= xbt_new(int, block_count);
725   new_t->block_indices= xbt_new(int, block_count);
726   int i;
727   for(i=0;i<block_count;i++){
728     new_t->block_lengths[i]=block_lengths[i];
729     new_t->block_indices[i]=block_indices[i];
730   }
731   new_t->block_count = block_count;
732   new_t->old_type = old_type;
733   new_t->size_oldtype = size_oldtype;
734   return new_t;
735 }
736
737
738 int smpi_datatype_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
739 {
740   int i;
741   int retval;
742   int size = 0;
743   int contiguous=1;
744   MPI_Aint lb = 0;
745   MPI_Aint ub = 0;
746   if(count>0){
747     lb=indices[0]*smpi_datatype_get_extent(old_type);
748     ub=indices[0]*smpi_datatype_get_extent(old_type) + blocklens[0]*smpi_datatype_ub(old_type);
749   }
750
751   for(i=0; i< count; i++){
752     if   (blocklens[i]<0)
753       return MPI_ERR_ARG;
754     size += blocklens[i];
755
756     if(indices[i]*smpi_datatype_get_extent(old_type)+smpi_datatype_lb(old_type)<lb)
757         lb = indices[i]*smpi_datatype_get_extent(old_type)+smpi_datatype_lb(old_type);
758     if(indices[i]*smpi_datatype_get_extent(old_type)+blocklens[i]*smpi_datatype_ub(old_type)>ub)
759         ub = indices[i]*smpi_datatype_get_extent(old_type)+blocklens[i]*smpi_datatype_ub(old_type);
760
761     if ( (i< count -1) && (indices[i]+blocklens[i] != indices[i+1]) )contiguous=0;
762   }
763   if (old_type->has_subtype == 1)
764     contiguous=0;
765
766   if(!contiguous){
767     s_smpi_mpi_indexed_t* subtype = smpi_datatype_indexed_create( blocklens,
768                                                                   indices,
769                                                                   count,
770                                                                   old_type,
771                                                                   smpi_datatype_size(old_type));
772      smpi_datatype_create(new_type,  size *
773                          smpi_datatype_size(old_type),lb,ub,1, subtype, DT_FLAG_DATA);
774   }else{
775     s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb,
776                                                                   size,
777                                                                   old_type,
778                                                                   smpi_datatype_size(old_type));
779     smpi_datatype_create(new_type,  size *
780                          smpi_datatype_size(old_type),lb,ub,1, subtype, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
781   }
782   retval=MPI_SUCCESS;
783   return retval;
784 }
785
786
787 /*
788 Hindexed Implementation - Indexed with indices in bytes 
789 */
790
791 /*
792  *  Copies noncontiguous data into contiguous memory.
793  *  @param contiguous_hindexed - output hindexed
794  *  @param noncontiguous_hindexed - input hindexed
795  *  @param type - pointer contening :
796  *      - block_lengths - the width or height of blocked matrix
797  *      - block_indices - indices of each data, in bytes
798  *      - count - the number of rows of matrix
799  */
800 void serialize_hindexed( const void *noncontiguous_hindexed,
801                        void *contiguous_hindexed,
802                        size_t count,
803                        void *type)
804 {
805   s_smpi_mpi_hindexed_t* type_c = (s_smpi_mpi_hindexed_t*)type;
806   int i,j;
807   char* contiguous_hindexed_char = (char*)contiguous_hindexed;
808   char* noncontiguous_hindexed_char = (char*)noncontiguous_hindexed+ type_c->block_indices[0];
809   for(j=0; j<count;j++){
810     for (i = 0; i < type_c->block_count; i++) {
811       if (type_c->old_type->has_subtype == 0)
812         memcpy(contiguous_hindexed_char,
813                      noncontiguous_hindexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
814       else
815         ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_hindexed_char,
816                                                                      contiguous_hindexed_char,
817                                                                      type_c->block_lengths[i],
818                                                                      type_c->old_type->substruct);
819
820       contiguous_hindexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
821       if (i<type_c->block_count-1)noncontiguous_hindexed_char = (char*)noncontiguous_hindexed + type_c->block_indices[i+1];
822       else noncontiguous_hindexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
823     }
824     noncontiguous_hindexed=(void*)noncontiguous_hindexed_char;
825   }
826 }
827 /*
828  *  Copies contiguous data into noncontiguous memory.
829  *  @param noncontiguous_hindexed - output hindexed
830  *  @param contiguous_hindexed - input hindexed
831  *  @param type - pointer contening :
832  *      - block_lengths - the width or height of blocked matrix
833  *      - block_indices - indices of each data, in bytes
834  *      - count - the number of rows of matrix
835  */
836 void unserialize_hindexed( const void *contiguous_hindexed,
837                          void *noncontiguous_hindexed,
838                          size_t count,
839                          void *type)
840 {
841   s_smpi_mpi_hindexed_t* type_c = (s_smpi_mpi_hindexed_t*)type;
842   int i,j;
843
844   char* contiguous_hindexed_char = (char*)contiguous_hindexed;
845   char* noncontiguous_hindexed_char = (char*)noncontiguous_hindexed+ type_c->block_indices[0];
846   for(j=0; j<count;j++){
847     for (i = 0; i < type_c->block_count; i++) {
848       if (type_c->old_type->has_subtype == 0)
849         memcpy(noncontiguous_hindexed_char,
850                contiguous_hindexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
851       else
852         ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_hindexed_char,
853                                                                        noncontiguous_hindexed_char,
854                                                                        type_c->block_lengths[i],
855                                                                        type_c->old_type->substruct);
856
857       contiguous_hindexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
858       if (i<type_c->block_count-1)noncontiguous_hindexed_char = (char*)noncontiguous_hindexed + type_c->block_indices[i+1];
859       else noncontiguous_hindexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
860     }
861     noncontiguous_hindexed=(void*)noncontiguous_hindexed_char;
862   }
863 }
864
865 void free_hindexed(MPI_Datatype* type){
866   xbt_free(((s_smpi_mpi_hindexed_t *)(*type)->substruct)->block_lengths);
867   xbt_free(((s_smpi_mpi_hindexed_t *)(*type)->substruct)->block_indices);
868 }
869
870 /*
871  * Create a Sub type hindexed to be able to serialize and unserialize it
872  * the structure s_smpi_mpi_hindexed_t is derived from s_smpi_subtype which
873  * required the functions unserialize and serialize
874  */
875 s_smpi_mpi_hindexed_t* smpi_datatype_hindexed_create( int* block_lengths,
876                                                   MPI_Aint* block_indices,
877                                                   int block_count,
878                                                   MPI_Datatype old_type,
879                                                   int size_oldtype){
880   s_smpi_mpi_hindexed_t *new_t= xbt_new(s_smpi_mpi_hindexed_t,1);
881   new_t->base.serialize = &serialize_hindexed;
882   new_t->base.unserialize = &unserialize_hindexed;
883   new_t->base.subtype_free = &free_hindexed;
884  //TODO : add a custom function for each time to clean these 
885   new_t->block_lengths= xbt_new(int, block_count);
886   new_t->block_indices= xbt_new(MPI_Aint, block_count);
887   int i;
888   for(i=0;i<block_count;i++){
889     new_t->block_lengths[i]=block_lengths[i];
890     new_t->block_indices[i]=block_indices[i];
891   }
892   new_t->block_count = block_count;
893   new_t->old_type = old_type;
894   new_t->size_oldtype = size_oldtype;
895   return new_t;
896 }
897
898
899 int smpi_datatype_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
900 {
901   int i;
902   int retval;
903   int size = 0;
904   int contiguous=1;
905   MPI_Aint lb = 0;
906   MPI_Aint ub = 0;
907   if(count>0){
908     lb=indices[0] + smpi_datatype_lb(old_type);
909     ub=indices[0] + blocklens[0]*smpi_datatype_ub(old_type);
910   }
911   for(i=0; i< count; i++){
912     if   (blocklens[i]<0)
913       return MPI_ERR_ARG;
914     size += blocklens[i];
915
916     if(indices[i]+smpi_datatype_lb(old_type)<lb) lb = indices[i]+smpi_datatype_lb(old_type);
917     if(indices[i]+blocklens[i]*smpi_datatype_ub(old_type)>ub) ub = indices[i]+blocklens[i]*smpi_datatype_ub(old_type);
918
919     if ( (i< count -1) && (indices[i]+blocklens[i]*smpi_datatype_size(old_type) != indices[i+1]) )contiguous=0;
920   }
921   if (old_type->has_subtype == 1 || lb!=0)
922     contiguous=0;
923
924   if(!contiguous){
925     s_smpi_mpi_hindexed_t* subtype = smpi_datatype_hindexed_create( blocklens,
926                                                                   indices,
927                                                                   count,
928                                                                   old_type,
929                                                                   smpi_datatype_size(old_type));
930     smpi_datatype_create(new_type,  size * smpi_datatype_size(old_type),
931                                                  lb,
932                          ub
933                          ,1, subtype, DT_FLAG_DATA);
934   }else{
935     s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb,
936                                                                   size,
937                                                                   old_type,
938                                                                   smpi_datatype_size(old_type));
939     smpi_datatype_create(new_type,  size * smpi_datatype_size(old_type),
940                                              0,size * smpi_datatype_size(old_type),
941                                              1, subtype, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
942   }
943   retval=MPI_SUCCESS;
944   return retval;
945 }
946
947
948 /*
949 struct Implementation - Indexed with indices in bytes 
950 */
951
952 /*
953  *  Copies noncontiguous data into contiguous memory.
954  *  @param contiguous_struct - output struct
955  *  @param noncontiguous_struct - input struct
956  *  @param type - pointer contening :
957  *      - stride - stride of between noncontiguous data
958  *      - block_length - the width or height of blocked matrix
959  *      - count - the number of rows of matrix
960  */
961 void serialize_struct( const void *noncontiguous_struct,
962                        void *contiguous_struct,
963                        size_t count,
964                        void *type)
965 {
966   s_smpi_mpi_struct_t* type_c = (s_smpi_mpi_struct_t*)type;
967   int i,j;
968   char* contiguous_struct_char = (char*)contiguous_struct;
969   char* noncontiguous_struct_char = (char*)noncontiguous_struct+ type_c->block_indices[0];
970   for(j=0; j<count;j++){
971     for (i = 0; i < type_c->block_count; i++) {
972       if (type_c->old_types[i]->has_subtype == 0)
973         memcpy(contiguous_struct_char,
974              noncontiguous_struct_char, type_c->block_lengths[i] * smpi_datatype_size(type_c->old_types[i]));
975       else
976         ((s_smpi_subtype_t*)type_c->old_types[i]->substruct)->serialize( noncontiguous_struct_char,
977                                                                          contiguous_struct_char,
978                                                                          type_c->block_lengths[i],
979                                                                          type_c->old_types[i]->substruct);
980
981
982       contiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_size(type_c->old_types[i]);
983       if (i<type_c->block_count-1)noncontiguous_struct_char = (char*)noncontiguous_struct + type_c->block_indices[i+1];
984       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 ?
985     }
986     noncontiguous_struct=(void*)noncontiguous_struct_char;
987   }
988 }
989 /*
990  *  Copies contiguous data into noncontiguous memory.
991  *  @param noncontiguous_struct - output struct
992  *  @param contiguous_struct - input struct
993  *  @param type - pointer contening :
994  *      - stride - stride of between noncontiguous data
995  *      - block_length - the width or height of blocked matrix
996  *      - count - the number of rows of matrix
997  */
998 void unserialize_struct( const void *contiguous_struct,
999                          void *noncontiguous_struct,
1000                          size_t count,
1001                          void *type)
1002 {
1003   s_smpi_mpi_struct_t* type_c = (s_smpi_mpi_struct_t*)type;
1004   int i,j;
1005
1006   char* contiguous_struct_char = (char*)contiguous_struct;
1007   char* noncontiguous_struct_char = (char*)noncontiguous_struct+ type_c->block_indices[0];
1008   for(j=0; j<count;j++){
1009     for (i = 0; i < type_c->block_count; i++) {
1010       if (type_c->old_types[i]->has_subtype == 0)
1011         memcpy(noncontiguous_struct_char,
1012              contiguous_struct_char, type_c->block_lengths[i] * smpi_datatype_size(type_c->old_types[i]));
1013       else
1014         ((s_smpi_subtype_t*)type_c->old_types[i]->substruct)->unserialize( contiguous_struct_char,
1015                                                                            noncontiguous_struct_char,
1016                                                                            type_c->block_lengths[i],
1017                                                                            type_c->old_types[i]->substruct);
1018
1019       contiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_size(type_c->old_types[i]);
1020       if (i<type_c->block_count-1)noncontiguous_struct_char =  (char*)noncontiguous_struct + type_c->block_indices[i+1];
1021       else noncontiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_types[i]);
1022     }
1023     noncontiguous_struct=(void*)noncontiguous_struct_char;
1024     
1025   }
1026 }
1027
1028 void free_struct(MPI_Datatype* type){
1029   xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->block_lengths);
1030   xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->block_indices);
1031   xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->old_types);
1032 }
1033
1034 /*
1035  * Create a Sub type struct to be able to serialize and unserialize it
1036  * the structure s_smpi_mpi_struct_t is derived from s_smpi_subtype which
1037  * required the functions unserialize and serialize
1038  */
1039 s_smpi_mpi_struct_t* smpi_datatype_struct_create( int* block_lengths,
1040                                                   MPI_Aint* block_indices,
1041                                                   int block_count,
1042                                                   MPI_Datatype* old_types){
1043   s_smpi_mpi_struct_t *new_t= xbt_new(s_smpi_mpi_struct_t,1);
1044   new_t->base.serialize = &serialize_struct;
1045   new_t->base.unserialize = &unserialize_struct;
1046   new_t->base.subtype_free = &free_struct;
1047  //TODO : add a custom function for each time to clean these 
1048   new_t->block_lengths= xbt_new(int, block_count);
1049   new_t->block_indices= xbt_new(MPI_Aint, block_count);
1050   new_t->old_types=  xbt_new(MPI_Datatype, block_count);
1051   int i;
1052   for(i=0;i<block_count;i++){
1053     new_t->block_lengths[i]=block_lengths[i];
1054     new_t->block_indices[i]=block_indices[i];
1055     new_t->old_types[i]=old_types[i];
1056   }
1057   //new_t->block_lengths = block_lengths;
1058   //new_t->block_indices = block_indices;
1059   new_t->block_count = block_count;
1060   //new_t->old_types = old_types;
1061   return new_t;
1062 }
1063
1064
1065 int smpi_datatype_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type)
1066 {
1067   int i;
1068   size_t size = 0;
1069   int contiguous=1;
1070   size = 0;
1071   MPI_Aint lb = 0;
1072   MPI_Aint ub = 0;
1073   if(count>0){
1074     lb=indices[0] + smpi_datatype_lb(old_types[0]);
1075     ub=indices[0] + blocklens[0]*smpi_datatype_ub(old_types[0]);
1076   }
1077   int forced_lb=0;
1078   int forced_ub=0;
1079   for(i=0; i< count; i++){
1080     if (blocklens[i]<0)
1081       return MPI_ERR_ARG;
1082     if (old_types[i]->has_subtype == 1)
1083       contiguous=0;
1084
1085     size += blocklens[i]*smpi_datatype_size(old_types[i]);
1086     if (old_types[i]==MPI_LB){
1087       lb=indices[i];
1088       forced_lb=1;
1089     }
1090     if (old_types[i]==MPI_UB){
1091       ub=indices[i];
1092       forced_ub=1;
1093     }
1094
1095     if(!forced_lb && indices[i]+smpi_datatype_lb(old_types[i])<lb) lb = indices[i];
1096     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]);
1097
1098     if ( (i< count -1) && (indices[i]+blocklens[i]*smpi_datatype_size(old_types[i]) != indices[i+1]) )contiguous=0;
1099   }
1100
1101   if(!contiguous){
1102     s_smpi_mpi_struct_t* subtype = smpi_datatype_struct_create( blocklens,
1103                                                               indices,
1104                                                               count,
1105                                                               old_types);
1106
1107     smpi_datatype_create(new_type,  size, lb, ub,1, subtype, DT_FLAG_DATA);
1108   }else{
1109     s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb,
1110                                                                   size,
1111                                                                   MPI_CHAR,
1112                                                                   1);
1113     smpi_datatype_create(new_type,  size, lb, ub,1, subtype, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
1114   }
1115   return MPI_SUCCESS;
1116 }
1117
1118 void smpi_datatype_commit(MPI_Datatype *datatype)
1119 {
1120   (*datatype)->flags=  ((*datatype)->flags | DT_FLAG_COMMITED);
1121 }
1122
1123 typedef struct s_smpi_mpi_op {
1124   MPI_User_function *func;
1125   int is_commute;
1126 } s_smpi_mpi_op_t;
1127
1128 #define MAX_OP(a, b)  (b) = (a) < (b) ? (b) : (a)
1129 #define MIN_OP(a, b)  (b) = (a) < (b) ? (a) : (b)
1130 #define SUM_OP(a, b)  (b) += (a)
1131 #define PROD_OP(a, b) (b) *= (a)
1132 #define LAND_OP(a, b) (b) = (a) && (b)
1133 #define LOR_OP(a, b)  (b) = (a) || (b)
1134 #define LXOR_OP(a, b) (b) = (!(a) && (b)) || ((a) && !(b))
1135 #define BAND_OP(a, b) (b) &= (a)
1136 #define BOR_OP(a, b)  (b) |= (a)
1137 #define BXOR_OP(a, b) (b) ^= (a)
1138 #define MAXLOC_OP(a, b)  (b) = (a.value) < (b.value) ? (b) : (a)
1139 #define MINLOC_OP(a, b)  (b) = (a.value) < (b.value) ? (a) : (b)
1140 //TODO : MINLOC & MAXLOC
1141
1142 #define APPLY_FUNC(a, b, length, type, func) \
1143 {                                          \
1144   int i;                                   \
1145   type* x = (type*)(a);                    \
1146   type* y = (type*)(b);                    \
1147   for(i = 0; i < *(length); i++) {         \
1148     func(x[i], y[i]);                      \
1149   }                                        \
1150 }
1151
1152 static void max_func(void *a, void *b, int *length,
1153                      MPI_Datatype * datatype)
1154 {
1155   if (*datatype == MPI_CHAR) {
1156     APPLY_FUNC(a, b, length, char, MAX_OP);
1157   } else if (*datatype == MPI_SHORT) {
1158     APPLY_FUNC(a, b, length, short, MAX_OP);
1159   } else if (*datatype == MPI_INT) {
1160     APPLY_FUNC(a, b, length, int, MAX_OP);
1161   } else if (*datatype == MPI_LONG) {
1162     APPLY_FUNC(a, b, length, long, MAX_OP);
1163   } else if (*datatype == MPI_UNSIGNED_SHORT) {
1164     APPLY_FUNC(a, b, length, unsigned short, MAX_OP);
1165   } else if (*datatype == MPI_UNSIGNED) {
1166     APPLY_FUNC(a, b, length, unsigned int, MAX_OP);
1167   } else if (*datatype == MPI_UNSIGNED_LONG) {
1168     APPLY_FUNC(a, b, length, unsigned long, MAX_OP);
1169   } else if (*datatype == MPI_FLOAT) {
1170     APPLY_FUNC(a, b, length, float, MAX_OP);
1171   } else if (*datatype == MPI_DOUBLE) {
1172     APPLY_FUNC(a, b, length, double, MAX_OP);
1173   } else if (*datatype == MPI_LONG_DOUBLE) {
1174     APPLY_FUNC(a, b, length, long double, MAX_OP);
1175   }
1176 }
1177
1178 static void min_func(void *a, void *b, int *length,
1179                      MPI_Datatype * datatype)
1180 {
1181   if (*datatype == MPI_CHAR) {
1182     APPLY_FUNC(a, b, length, char, MIN_OP);
1183   } else if (*datatype == MPI_SHORT) {
1184     APPLY_FUNC(a, b, length, short, MIN_OP);
1185   } else if (*datatype == MPI_INT) {
1186     APPLY_FUNC(a, b, length, int, MIN_OP);
1187   } else if (*datatype == MPI_LONG) {
1188     APPLY_FUNC(a, b, length, long, MIN_OP);
1189   } else if (*datatype == MPI_UNSIGNED_SHORT) {
1190     APPLY_FUNC(a, b, length, unsigned short, MIN_OP);
1191   } else if (*datatype == MPI_UNSIGNED) {
1192     APPLY_FUNC(a, b, length, unsigned int, MIN_OP);
1193   } else if (*datatype == MPI_UNSIGNED_LONG) {
1194     APPLY_FUNC(a, b, length, unsigned long, MIN_OP);
1195   } else if (*datatype == MPI_FLOAT) {
1196     APPLY_FUNC(a, b, length, float, MIN_OP);
1197   } else if (*datatype == MPI_DOUBLE) {
1198     APPLY_FUNC(a, b, length, double, MIN_OP);
1199   } else if (*datatype == MPI_LONG_DOUBLE) {
1200     APPLY_FUNC(a, b, length, long double, MIN_OP);
1201   }
1202 }
1203
1204 static void sum_func(void *a, void *b, int *length,
1205                      MPI_Datatype * datatype)
1206 {
1207   if (*datatype == MPI_CHAR) {
1208     APPLY_FUNC(a, b, length, char, SUM_OP);
1209   } else if (*datatype == MPI_SHORT) {
1210     APPLY_FUNC(a, b, length, short, SUM_OP);
1211   } else if (*datatype == MPI_INT) {
1212     APPLY_FUNC(a, b, length, int, SUM_OP);
1213   } else if (*datatype == MPI_LONG) {
1214     APPLY_FUNC(a, b, length, long, SUM_OP);
1215   } else if (*datatype == MPI_UNSIGNED_SHORT) {
1216     APPLY_FUNC(a, b, length, unsigned short, SUM_OP);
1217   } else if (*datatype == MPI_UNSIGNED) {
1218     APPLY_FUNC(a, b, length, unsigned int, SUM_OP);
1219   } else if (*datatype == MPI_UNSIGNED_LONG) {
1220     APPLY_FUNC(a, b, length, unsigned long, SUM_OP);
1221   } else if (*datatype == MPI_FLOAT) {
1222     APPLY_FUNC(a, b, length, float, SUM_OP);
1223   } else if (*datatype == MPI_DOUBLE) {
1224     APPLY_FUNC(a, b, length, double, SUM_OP);
1225   } else if (*datatype == MPI_LONG_DOUBLE) {
1226     APPLY_FUNC(a, b, length, long double, SUM_OP);
1227   } else if (*datatype == MPI_C_FLOAT_COMPLEX) {
1228     APPLY_FUNC(a, b, length, float _Complex, SUM_OP);
1229   } else if (*datatype == MPI_C_DOUBLE_COMPLEX) {
1230     APPLY_FUNC(a, b, length, double _Complex, SUM_OP);
1231   } else if (*datatype == MPI_C_LONG_DOUBLE_COMPLEX) {
1232     APPLY_FUNC(a, b, length, long double _Complex, SUM_OP);
1233   }
1234 }
1235
1236 static void prod_func(void *a, void *b, int *length,
1237                       MPI_Datatype * datatype)
1238 {
1239   if (*datatype == MPI_CHAR) {
1240     APPLY_FUNC(a, b, length, char, PROD_OP);
1241   } else if (*datatype == MPI_SHORT) {
1242     APPLY_FUNC(a, b, length, short, PROD_OP);
1243   } else if (*datatype == MPI_INT) {
1244     APPLY_FUNC(a, b, length, int, PROD_OP);
1245   } else if (*datatype == MPI_LONG) {
1246     APPLY_FUNC(a, b, length, long, PROD_OP);
1247   } else if (*datatype == MPI_UNSIGNED_SHORT) {
1248     APPLY_FUNC(a, b, length, unsigned short, PROD_OP);
1249   } else if (*datatype == MPI_UNSIGNED) {
1250     APPLY_FUNC(a, b, length, unsigned int, PROD_OP);
1251   } else if (*datatype == MPI_UNSIGNED_LONG) {
1252     APPLY_FUNC(a, b, length, unsigned long, PROD_OP);
1253   } else if (*datatype == MPI_FLOAT) {
1254     APPLY_FUNC(a, b, length, float, PROD_OP);
1255   } else if (*datatype == MPI_DOUBLE) {
1256     APPLY_FUNC(a, b, length, double, PROD_OP);
1257   } else if (*datatype == MPI_LONG_DOUBLE) {
1258     APPLY_FUNC(a, b, length, long double, PROD_OP);
1259   } else if (*datatype == MPI_C_FLOAT_COMPLEX) {
1260     APPLY_FUNC(a, b, length, float _Complex, PROD_OP);
1261   } else if (*datatype == MPI_C_DOUBLE_COMPLEX) {
1262     APPLY_FUNC(a, b, length, double _Complex, PROD_OP);
1263   } else if (*datatype == MPI_C_LONG_DOUBLE_COMPLEX) {
1264     APPLY_FUNC(a, b, length, long double _Complex, PROD_OP);
1265   }
1266 }
1267
1268 static void land_func(void *a, void *b, int *length,
1269                       MPI_Datatype * datatype)
1270 {
1271   if (*datatype == MPI_CHAR) {
1272     APPLY_FUNC(a, b, length, char, LAND_OP);
1273   } else if (*datatype == MPI_SHORT) {
1274     APPLY_FUNC(a, b, length, short, LAND_OP);
1275   } else if (*datatype == MPI_INT) {
1276     APPLY_FUNC(a, b, length, int, LAND_OP);
1277   } else if (*datatype == MPI_LONG) {
1278     APPLY_FUNC(a, b, length, long, LAND_OP);
1279   } else if (*datatype == MPI_UNSIGNED_SHORT) {
1280     APPLY_FUNC(a, b, length, unsigned short, LAND_OP);
1281   } else if (*datatype == MPI_UNSIGNED) {
1282     APPLY_FUNC(a, b, length, unsigned int, LAND_OP);
1283   } else if (*datatype == MPI_UNSIGNED_LONG) {
1284     APPLY_FUNC(a, b, length, unsigned long, LAND_OP);
1285   } else if (*datatype == MPI_C_BOOL) {
1286     APPLY_FUNC(a, b, length, _Bool, LAND_OP);
1287   }
1288 }
1289
1290 static void lor_func(void *a, void *b, int *length,
1291                      MPI_Datatype * datatype)
1292 {
1293   if (*datatype == MPI_CHAR) {
1294     APPLY_FUNC(a, b, length, char, LOR_OP);
1295   } else if (*datatype == MPI_SHORT) {
1296     APPLY_FUNC(a, b, length, short, LOR_OP);
1297   } else if (*datatype == MPI_INT) {
1298     APPLY_FUNC(a, b, length, int, LOR_OP);
1299   } else if (*datatype == MPI_LONG) {
1300     APPLY_FUNC(a, b, length, long, LOR_OP);
1301   } else if (*datatype == MPI_UNSIGNED_SHORT) {
1302     APPLY_FUNC(a, b, length, unsigned short, LOR_OP);
1303   } else if (*datatype == MPI_UNSIGNED) {
1304     APPLY_FUNC(a, b, length, unsigned int, LOR_OP);
1305   } else if (*datatype == MPI_UNSIGNED_LONG) {
1306     APPLY_FUNC(a, b, length, unsigned long, LOR_OP);
1307   } else if (*datatype == MPI_C_BOOL) {
1308     APPLY_FUNC(a, b, length, _Bool, LOR_OP);
1309   }
1310 }
1311
1312 static void lxor_func(void *a, void *b, int *length,
1313                       MPI_Datatype * datatype)
1314 {
1315   if (*datatype == MPI_CHAR) {
1316     APPLY_FUNC(a, b, length, char, LXOR_OP);
1317   } else if (*datatype == MPI_SHORT) {
1318     APPLY_FUNC(a, b, length, short, LXOR_OP);
1319   } else if (*datatype == MPI_INT) {
1320     APPLY_FUNC(a, b, length, int, LXOR_OP);
1321   } else if (*datatype == MPI_LONG) {
1322     APPLY_FUNC(a, b, length, long, LXOR_OP);
1323   } else if (*datatype == MPI_UNSIGNED_SHORT) {
1324     APPLY_FUNC(a, b, length, unsigned short, LXOR_OP);
1325   } else if (*datatype == MPI_UNSIGNED) {
1326     APPLY_FUNC(a, b, length, unsigned int, LXOR_OP);
1327   } else if (*datatype == MPI_UNSIGNED_LONG) {
1328     APPLY_FUNC(a, b, length, unsigned long, LXOR_OP);
1329   } else if (*datatype == MPI_C_BOOL) {
1330     APPLY_FUNC(a, b, length, _Bool, LXOR_OP);
1331   }
1332 }
1333
1334 static void band_func(void *a, void *b, int *length,
1335                       MPI_Datatype * datatype)
1336 {
1337   if (*datatype == MPI_CHAR) {
1338     APPLY_FUNC(a, b, length, char, BAND_OP);
1339   }
1340   if (*datatype == MPI_SHORT) {
1341     APPLY_FUNC(a, b, length, short, BAND_OP);
1342   } else if (*datatype == MPI_INT) {
1343     APPLY_FUNC(a, b, length, int, BAND_OP);
1344   } else if (*datatype == MPI_LONG) {
1345     APPLY_FUNC(a, b, length, long, BAND_OP);
1346   } else if (*datatype == MPI_UNSIGNED_SHORT) {
1347     APPLY_FUNC(a, b, length, unsigned short, BAND_OP);
1348   } else if (*datatype == MPI_UNSIGNED) {
1349     APPLY_FUNC(a, b, length, unsigned int, BAND_OP);
1350   } else if (*datatype == MPI_UNSIGNED_LONG) {
1351     APPLY_FUNC(a, b, length, unsigned long, BAND_OP);
1352   } else if (*datatype == MPI_BYTE) {
1353     APPLY_FUNC(a, b, length, uint8_t, BAND_OP);
1354   }
1355 }
1356
1357 static void bor_func(void *a, void *b, int *length,
1358                      MPI_Datatype * datatype)
1359 {
1360   if (*datatype == MPI_CHAR) {
1361     APPLY_FUNC(a, b, length, char, BOR_OP);
1362   } else if (*datatype == MPI_SHORT) {
1363     APPLY_FUNC(a, b, length, short, BOR_OP);
1364   } else if (*datatype == MPI_INT) {
1365     APPLY_FUNC(a, b, length, int, BOR_OP);
1366   } else if (*datatype == MPI_LONG) {
1367     APPLY_FUNC(a, b, length, long, BOR_OP);
1368   } else if (*datatype == MPI_UNSIGNED_SHORT) {
1369     APPLY_FUNC(a, b, length, unsigned short, BOR_OP);
1370   } else if (*datatype == MPI_UNSIGNED) {
1371     APPLY_FUNC(a, b, length, unsigned int, BOR_OP);
1372   } else if (*datatype == MPI_UNSIGNED_LONG) {
1373     APPLY_FUNC(a, b, length, unsigned long, BOR_OP);
1374   } else if (*datatype == MPI_BYTE) {
1375     APPLY_FUNC(a, b, length, uint8_t, BOR_OP);
1376   }
1377 }
1378
1379 static void bxor_func(void *a, void *b, int *length,
1380                       MPI_Datatype * datatype)
1381 {
1382   if (*datatype == MPI_CHAR) {
1383     APPLY_FUNC(a, b, length, char, BXOR_OP);
1384   } else if (*datatype == MPI_SHORT) {
1385     APPLY_FUNC(a, b, length, short, BXOR_OP);
1386   } else if (*datatype == MPI_INT) {
1387     APPLY_FUNC(a, b, length, int, BXOR_OP);
1388   } else if (*datatype == MPI_LONG) {
1389     APPLY_FUNC(a, b, length, long, BXOR_OP);
1390   } else if (*datatype == MPI_UNSIGNED_SHORT) {
1391     APPLY_FUNC(a, b, length, unsigned short, BXOR_OP);
1392   } else if (*datatype == MPI_UNSIGNED) {
1393     APPLY_FUNC(a, b, length, unsigned int, BXOR_OP);
1394   } else if (*datatype == MPI_UNSIGNED_LONG) {
1395     APPLY_FUNC(a, b, length, unsigned long, BXOR_OP);
1396   } else if (*datatype == MPI_BYTE) {
1397     APPLY_FUNC(a, b, length, uint8_t, BXOR_OP);
1398   }
1399 }
1400
1401 static void minloc_func(void *a, void *b, int *length,
1402                         MPI_Datatype * datatype)
1403 {
1404   if (*datatype == MPI_FLOAT_INT) {
1405     APPLY_FUNC(a, b, length, float_int, MINLOC_OP);
1406   } else if (*datatype == MPI_LONG_INT) {
1407     APPLY_FUNC(a, b, length, long_int, MINLOC_OP);
1408   } else if (*datatype == MPI_DOUBLE_INT) {
1409     APPLY_FUNC(a, b, length, double_int, MINLOC_OP);
1410   } else if (*datatype == MPI_SHORT_INT) {
1411     APPLY_FUNC(a, b, length, short_int, MINLOC_OP);
1412   } else if (*datatype == MPI_2INT) {
1413     APPLY_FUNC(a, b, length, int_int, MINLOC_OP);
1414   } else if (*datatype == MPI_LONG_DOUBLE_INT) {
1415     APPLY_FUNC(a, b, length, long_double_int, MINLOC_OP);
1416   } else if (*datatype == MPI_2FLOAT) {
1417     APPLY_FUNC(a, b, length, float_float, MINLOC_OP);
1418   } else if (*datatype == MPI_2DOUBLE) {
1419     APPLY_FUNC(a, b, length, double_double, MINLOC_OP);
1420   }
1421 }
1422
1423 static void maxloc_func(void *a, void *b, int *length,
1424                         MPI_Datatype * datatype)
1425 {
1426   if (*datatype == MPI_FLOAT_INT) {
1427     APPLY_FUNC(a, b, length, float_int, MAXLOC_OP);
1428   } else if (*datatype == MPI_LONG_INT) {
1429     APPLY_FUNC(a, b, length, long_int, MAXLOC_OP);
1430   } else if (*datatype == MPI_DOUBLE_INT) {
1431     APPLY_FUNC(a, b, length, double_int, MAXLOC_OP);
1432   } else if (*datatype == MPI_SHORT_INT) {
1433     APPLY_FUNC(a, b, length, short_int, MAXLOC_OP);
1434   } else if (*datatype == MPI_2INT) {
1435     APPLY_FUNC(a, b, length, int_int, MAXLOC_OP);
1436   } else if (*datatype == MPI_LONG_DOUBLE_INT) {
1437     APPLY_FUNC(a, b, length, long_double_int, MAXLOC_OP);
1438   } else if (*datatype == MPI_2FLOAT) {
1439     APPLY_FUNC(a, b, length, float_float, MAXLOC_OP);
1440   } else if (*datatype == MPI_2DOUBLE) {
1441     APPLY_FUNC(a, b, length, double_double, MAXLOC_OP);
1442   }
1443 }
1444
1445
1446 #define CREATE_MPI_OP(name, func)                             \
1447   static s_smpi_mpi_op_t mpi_##name = { &(func) /* func */, TRUE }; \
1448 MPI_Op name = &mpi_##name;
1449
1450 CREATE_MPI_OP(MPI_MAX, max_func);
1451 CREATE_MPI_OP(MPI_MIN, min_func);
1452 CREATE_MPI_OP(MPI_SUM, sum_func);
1453 CREATE_MPI_OP(MPI_PROD, prod_func);
1454 CREATE_MPI_OP(MPI_LAND, land_func);
1455 CREATE_MPI_OP(MPI_LOR, lor_func);
1456 CREATE_MPI_OP(MPI_LXOR, lxor_func);
1457 CREATE_MPI_OP(MPI_BAND, band_func);
1458 CREATE_MPI_OP(MPI_BOR, bor_func);
1459 CREATE_MPI_OP(MPI_BXOR, bxor_func);
1460 CREATE_MPI_OP(MPI_MAXLOC, maxloc_func);
1461 CREATE_MPI_OP(MPI_MINLOC, minloc_func);
1462
1463 MPI_Op smpi_op_new(MPI_User_function * function, int commute)
1464 {
1465   MPI_Op op;
1466   op = xbt_new(s_smpi_mpi_op_t, 1);
1467   op->func = function;
1468   op-> is_commute = commute;
1469   return op;
1470 }
1471
1472 int smpi_op_is_commute(MPI_Op op)
1473 {
1474   return (op==MPI_OP_NULL) ? 1 : op-> is_commute;
1475 }
1476
1477 void smpi_op_destroy(MPI_Op op)
1478 {
1479   xbt_free(op);
1480 }
1481
1482 void smpi_op_apply(MPI_Op op, void *invec, void *inoutvec, int *len,
1483                    MPI_Datatype * datatype)
1484 {
1485   op->func(invec, inoutvec, len, datatype);
1486 }