Logo AND Algorithmique Numérique Distribuée

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