1 /* smpi_mpi_dt.c -- MPI primitives to handle datatypes */
2 /* FIXME: a very incomplete implementation */
4 /* Copyright (c) 2009, 2010. The SimGrid Team.
5 * All rights reserved. */
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. */
15 #include "smpi_mpi_dt_private.h"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_mpi_dt, smpi,
18 "Logging specific to SMPI (datatype)");
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*/ \
25 sizeof(type), /* ub = lb + size */ \
26 DT_FLAG_BASIC, /* flags */ \
27 NULL /* pointer on extended struct*/ \
29 MPI_Datatype name = &mpi_##name;
31 #define CREATE_MPI_DATATYPE_NULL(name) \
32 static s_smpi_mpi_datatype_t mpi_##name = { \
34 0, /*was 1 has_subtype*/ \
36 0, /* ub = lb + size */ \
37 DT_FLAG_BASIC, /* flags */ \
38 NULL /* pointer on extended struct*/ \
40 MPI_Datatype name = &mpi_##name;
42 //The following are datatypes for the MPI functions MPI_MAXLOC and MPI_MINLOC.
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);
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);
115 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE_INT, long_double_int);
117 CREATE_MPI_DATATYPE_NULL(MPI_UB);
118 CREATE_MPI_DATATYPE_NULL(MPI_LB);
119 CREATE_MPI_DATATYPE_NULL(MPI_PACKED);
121 CREATE_MPI_DATATYPE(MPI_PTR, void*);
124 size_t smpi_datatype_size(MPI_Datatype datatype)
126 return datatype->size;
131 MPI_Aint smpi_datatype_lb(MPI_Datatype datatype)
136 MPI_Aint smpi_datatype_ub(MPI_Datatype datatype)
141 int smpi_datatype_extent(MPI_Datatype datatype, MPI_Aint * lb,
145 *extent = datatype->ub - datatype->lb;
149 MPI_Aint smpi_datatype_get_extent(MPI_Datatype datatype){
150 return datatype->ub - datatype->lb;
153 int smpi_datatype_copy(void *sendbuf, int sendcount, MPI_Datatype sendtype,
154 void *recvbuf, int recvcount, MPI_Datatype recvtype)
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;
165 if(sendtype->has_subtype == 0 && recvtype->has_subtype == 0) {
166 memcpy(recvbuf, sendbuf, count);
168 else if (sendtype->has_subtype == 0)
170 s_smpi_subtype_t *subtype = recvtype->substruct;
171 subtype->unserialize( sendbuf, recvbuf,1, subtype);
173 else if (recvtype->has_subtype == 0)
175 s_smpi_subtype_t *subtype = sendtype->substruct;
176 subtype->serialize(sendbuf, recvbuf,1, subtype);
178 s_smpi_subtype_t *subtype = sendtype->substruct;
181 void * buf_tmp = xbt_malloc(count);
183 subtype->serialize( sendbuf, buf_tmp,1, subtype);
184 subtype = recvtype->substruct;
185 subtype->unserialize( buf_tmp, recvbuf,1, subtype);
191 return sendcount > recvcount ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
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
203 void serialize_vector( const void *noncontiguous_vector,
204 void *contiguous_vector,
208 s_smpi_mpi_vector_t* type_c = (s_smpi_mpi_vector_t*)type;
210 char* contiguous_vector_char = (char*)contiguous_vector;
211 char* noncontiguous_vector_char = (char*)noncontiguous_vector;
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);
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);
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);
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
237 void unserialize_vector( const void *contiguous_vector,
238 void *noncontiguous_vector,
242 s_smpi_mpi_vector_t* type_c = (s_smpi_mpi_vector_t*)type;
245 char* contiguous_vector_char = (char*)contiguous_vector;
246 char* noncontiguous_vector_char = (char*)noncontiguous_vector;
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);
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);
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
268 s_smpi_mpi_vector_t* smpi_datatype_vector_create( int block_stride,
271 MPI_Datatype old_type,
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;
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);
289 new_t->has_subtype = has_subtype;
292 new_t->flags = flags;
293 new_t->substruct = struct_type;
298 void smpi_datatype_free(MPI_Datatype* type){
300 if((*type)->flags & DT_FLAG_PREDEFINED)return;
302 //if still used, mark for deletion
303 if((*type)->in_use!=0){
304 (*type)->flags |=DT_FLAG_DESTROYED;
308 if ((*type)->has_subtype == 1){
309 ((s_smpi_subtype_t *)(*type)->substruct)->subtype_free(type);
310 xbt_free((*type)->substruct);
316 void smpi_datatype_use(MPI_Datatype type){
317 if(type)type->in_use++;
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);
330 Contiguous Implementation
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
343 void serialize_contiguous( const void *noncontiguous_hvector,
344 void *contiguous_hvector,
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);
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
363 void unserialize_contiguous( const void *contiguous_vector,
364 void *noncontiguous_vector,
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;
372 memcpy(noncontiguous_vector_char,
373 contiguous_vector_char, count* type_c->block_count * type_c->size_oldtype);
376 void free_contiguous(MPI_Datatype* d){
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
385 s_smpi_mpi_contiguous_t* smpi_datatype_contiguous_create( MPI_Aint lb,
387 MPI_Datatype old_type,
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;
394 new_t->block_count = block_count;
395 new_t->old_type = old_type;
396 new_t->size_oldtype = size_oldtype;
403 int smpi_datatype_contiguous(int count, MPI_Datatype old_type, MPI_Datatype* new_type, MPI_Aint lb)
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);
411 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb,
414 smpi_datatype_size(old_type));
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);
424 int smpi_datatype_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type)
427 if (blocklen<=0) return MPI_ERR_ARG;
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);
434 if(old_type->has_subtype || stride != blocklen){
437 s_smpi_mpi_vector_t* subtype = smpi_datatype_vector_create( stride,
441 smpi_datatype_size(old_type));
442 smpi_datatype_create(new_type,
443 count * (blocklen) * smpi_datatype_size(old_type), lb,
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),
457 DT_FLAG_VECTOR|DT_FLAG_CONTIGUOUS);
463 void free_vector(MPI_Datatype* d){
467 Hvector Implementation - Vector with stride in bytes
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
480 void serialize_hvector( const void *noncontiguous_hvector,
481 void *contiguous_hvector,
485 s_smpi_mpi_hvector_t* type_c = (s_smpi_mpi_hvector_t*)type;
487 char* contiguous_vector_char = (char*)contiguous_hvector;
488 char* noncontiguous_vector_char = (char*)noncontiguous_hvector;
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);
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);
500 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
501 noncontiguous_vector_char += type_c->block_stride;
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
513 void unserialize_hvector( const void *contiguous_vector,
514 void *noncontiguous_vector,
518 s_smpi_mpi_hvector_t* type_c = (s_smpi_mpi_hvector_t*)type;
521 char* contiguous_vector_char = (char*)contiguous_vector;
522 char* noncontiguous_vector_char = (char*)noncontiguous_vector;
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);
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;
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
544 s_smpi_mpi_hvector_t* smpi_datatype_hvector_create( MPI_Aint block_stride,
547 MPI_Datatype old_type,
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;
561 //do nothing for vector types
562 void free_hvector(MPI_Datatype* d){
565 int smpi_datatype_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type)
568 if (blocklen<=0) return MPI_ERR_ARG;
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);
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,
580 smpi_datatype_size(old_type));
582 smpi_datatype_create(new_type, count * blocklen * smpi_datatype_size(old_type),
589 smpi_datatype_create(new_type, count * blocklen *
590 smpi_datatype_size(old_type),0,count * blocklen *
591 smpi_datatype_size(old_type),
594 DT_FLAG_VECTOR|DT_FLAG_CONTIGUOUS);
602 Indexed Implementation
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
614 void serialize_indexed( const void *noncontiguous_indexed,
615 void *contiguous_indexed,
619 s_smpi_mpi_indexed_t* type_c = (s_smpi_mpi_indexed_t*)type;
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);
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);
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);
639 noncontiguous_indexed=(void*)noncontiguous_indexed_char;
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
651 void unserialize_indexed( const void *contiguous_indexed,
652 void *noncontiguous_indexed,
657 s_smpi_mpi_indexed_t* type_c = (s_smpi_mpi_indexed_t*)type;
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);
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);
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);
677 noncontiguous_indexed=(void*)noncontiguous_indexed_char;
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);
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
691 s_smpi_mpi_indexed_t* smpi_datatype_indexed_create( int* block_lengths,
694 MPI_Datatype old_type,
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);
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];
708 new_t->block_count = block_count;
709 new_t->old_type = old_type;
710 new_t->size_oldtype = size_oldtype;
715 int smpi_datatype_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
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);
728 for(i=0; i< count; i++){
731 size += blocklens[i];
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);
738 if ( (i< count -1) && (indices[i]+blocklens[i] != indices[i+1]) )contiguous=0;
740 if (old_type->has_subtype == 1)
744 s_smpi_mpi_indexed_t* subtype = smpi_datatype_indexed_create( blocklens,
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);
752 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb,
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);
765 Hindexed Implementation - Indexed with indices in bytes
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
777 void serialize_hindexed( const void *noncontiguous_hindexed,
778 void *contiguous_hindexed,
782 s_smpi_mpi_hindexed_t* type_c = (s_smpi_mpi_hindexed_t*)type;
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);
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);
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);
801 noncontiguous_hindexed=(void*)noncontiguous_hindexed_char;
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
813 void unserialize_hindexed( const void *contiguous_hindexed,
814 void *noncontiguous_hindexed,
818 s_smpi_mpi_hindexed_t* type_c = (s_smpi_mpi_hindexed_t*)type;
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);
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);
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);
838 noncontiguous_hindexed=(void*)noncontiguous_hindexed_char;
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);
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
852 s_smpi_mpi_hindexed_t* smpi_datatype_hindexed_create( int* block_lengths,
853 MPI_Aint* block_indices,
855 MPI_Datatype old_type,
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);
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];
869 new_t->block_count = block_count;
870 new_t->old_type = old_type;
871 new_t->size_oldtype = size_oldtype;
876 int smpi_datatype_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
885 lb=indices[0] + smpi_datatype_lb(old_type);
886 ub=indices[0] + blocklens[0]*smpi_datatype_ub(old_type);
888 for(i=0; i< count; i++){
891 size += blocklens[i];
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);
896 if ( (i< count -1) && (indices[i]+blocklens[i]*smpi_datatype_size(old_type) != indices[i+1]) )contiguous=0;
898 if (old_type->has_subtype == 1 || lb!=0)
902 s_smpi_mpi_hindexed_t* subtype = smpi_datatype_hindexed_create( blocklens,
906 smpi_datatype_size(old_type));
907 smpi_datatype_create(new_type, size * smpi_datatype_size(old_type),
910 ,1, subtype, DT_FLAG_DATA);
912 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb,
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);
926 struct Implementation - Indexed with indices in bytes
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
938 void serialize_struct( const void *noncontiguous_struct,
939 void *contiguous_struct,
943 s_smpi_mpi_struct_t* type_c = (s_smpi_mpi_struct_t*)type;
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]));
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);
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 ?
963 noncontiguous_struct=(void*)noncontiguous_struct_char;
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
975 void unserialize_struct( const void *contiguous_struct,
976 void *noncontiguous_struct,
980 s_smpi_mpi_struct_t* type_c = (s_smpi_mpi_struct_t*)type;
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]));
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);
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]);
1000 noncontiguous_struct=(void*)noncontiguous_struct_char;
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);
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
1016 s_smpi_mpi_struct_t* smpi_datatype_struct_create( int* block_lengths,
1017 MPI_Aint* block_indices,
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);
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];
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;
1042 int smpi_datatype_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type)
1051 lb=indices[0] + smpi_datatype_lb(old_types[0]);
1052 ub=indices[0] + blocklens[0]*smpi_datatype_ub(old_types[0]);
1056 for(i=0; i< count; i++){
1057 if (blocklens[i]<=0)
1059 if (old_types[i]->has_subtype == 1)
1062 size += blocklens[i]*smpi_datatype_size(old_types[i]);
1063 if (old_types[i]==MPI_LB){
1067 if (old_types[i]==MPI_UB){
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]);
1075 if ( (i< count -1) && (indices[i]+blocklens[i]*smpi_datatype_size(old_types[i]) != indices[i+1]) )contiguous=0;
1079 s_smpi_mpi_struct_t* subtype = smpi_datatype_struct_create( blocklens,
1084 smpi_datatype_create(new_type, size, lb, ub,1, subtype, DT_FLAG_DATA);
1086 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb,
1090 smpi_datatype_create(new_type, size, lb, ub,1, subtype, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
1095 void smpi_datatype_commit(MPI_Datatype *datatype)
1097 (*datatype)->flags= ((*datatype)->flags | DT_FLAG_COMMITED);
1100 typedef struct s_smpi_mpi_op {
1101 MPI_User_function *func;
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
1119 #define APPLY_FUNC(a, b, length, type, func) \
1122 type* x = (type*)(a); \
1123 type* y = (type*)(b); \
1124 for(i = 0; i < *(length); i++) { \
1129 static void max_func(void *a, void *b, int *length,
1130 MPI_Datatype * datatype)
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);
1155 static void min_func(void *a, void *b, int *length,
1156 MPI_Datatype * datatype)
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);
1181 static void sum_func(void *a, void *b, int *length,
1182 MPI_Datatype * datatype)
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);
1213 static void prod_func(void *a, void *b, int *length,
1214 MPI_Datatype * datatype)
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);
1245 static void land_func(void *a, void *b, int *length,
1246 MPI_Datatype * datatype)
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);
1267 static void lor_func(void *a, void *b, int *length,
1268 MPI_Datatype * datatype)
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);
1289 static void lxor_func(void *a, void *b, int *length,
1290 MPI_Datatype * datatype)
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);
1311 static void band_func(void *a, void *b, int *length,
1312 MPI_Datatype * datatype)
1314 if (*datatype == MPI_CHAR) {
1315 APPLY_FUNC(a, b, length, char, BAND_OP);
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);
1334 static void bor_func(void *a, void *b, int *length,
1335 MPI_Datatype * datatype)
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);
1356 static void bxor_func(void *a, void *b, int *length,
1357 MPI_Datatype * datatype)
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);
1378 static void minloc_func(void *a, void *b, int *length,
1379 MPI_Datatype * datatype)
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);
1400 static void maxloc_func(void *a, void *b, int *length,
1401 MPI_Datatype * datatype)
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);
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;
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);
1440 MPI_Op smpi_op_new(MPI_User_function * function, int commute)
1443 op = xbt_new(s_smpi_mpi_op_t, 1);
1444 op->func = function;
1445 op-> is_commute = commute;
1449 int smpi_op_is_commute(MPI_Op op)
1451 return op-> is_commute;
1454 void smpi_op_destroy(MPI_Op op)
1459 void smpi_op_apply(MPI_Op op, void *invec, void *inoutvec, int *len,
1460 MPI_Datatype * datatype)
1462 op->func(invec, inoutvec, len, datatype);