1 /* smpi_mpi_dt.c -- MPI primitives to handle datatypes */
2 /* FIXME: a very incomplete implementation */
4 /* Copyright (c) 2009-2013. 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 #include "simgrid/modelchecker.h"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_mpi_dt, smpi,
20 "Logging specific to SMPI (datatype)");
22 #define CREATE_MPI_DATATYPE(name, type) \
23 static s_smpi_mpi_datatype_t mpi_##name = { \
24 sizeof(type), /* size */ \
25 0, /*was 1 has_subtype*/ \
27 sizeof(type), /* ub = lb + size */ \
28 DT_FLAG_BASIC, /* flags */ \
29 NULL /* pointer on extended struct*/ \
31 MPI_Datatype name = &mpi_##name;
33 #define CREATE_MPI_DATATYPE_NULL(name) \
34 static s_smpi_mpi_datatype_t mpi_##name = { \
36 0, /*was 1 has_subtype*/ \
38 0, /* ub = lb + size */ \
39 DT_FLAG_BASIC, /* flags */ \
40 NULL /* pointer on extended struct*/ \
42 MPI_Datatype name = &mpi_##name;
44 //The following are datatypes for the MPI functions MPI_MAXLOC and MPI_MINLOC.
78 // Predefined data types
79 CREATE_MPI_DATATYPE(MPI_CHAR, char);
80 CREATE_MPI_DATATYPE(MPI_SHORT, short);
81 CREATE_MPI_DATATYPE(MPI_INT, int);
82 CREATE_MPI_DATATYPE(MPI_LONG, long);
83 CREATE_MPI_DATATYPE(MPI_LONG_LONG, long long);
84 CREATE_MPI_DATATYPE(MPI_SIGNED_CHAR, signed char);
85 CREATE_MPI_DATATYPE(MPI_UNSIGNED_CHAR, unsigned char);
86 CREATE_MPI_DATATYPE(MPI_UNSIGNED_SHORT, unsigned short);
87 CREATE_MPI_DATATYPE(MPI_UNSIGNED, unsigned int);
88 CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG, unsigned long);
89 CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG_LONG, unsigned long long);
90 CREATE_MPI_DATATYPE(MPI_FLOAT, float);
91 CREATE_MPI_DATATYPE(MPI_DOUBLE, double);
92 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE, long double);
93 CREATE_MPI_DATATYPE(MPI_WCHAR, wchar_t);
94 CREATE_MPI_DATATYPE(MPI_C_BOOL, _Bool);
95 CREATE_MPI_DATATYPE(MPI_INT8_T, int8_t);
96 CREATE_MPI_DATATYPE(MPI_INT16_T, int16_t);
97 CREATE_MPI_DATATYPE(MPI_INT32_T, int32_t);
98 CREATE_MPI_DATATYPE(MPI_INT64_T, int64_t);
99 CREATE_MPI_DATATYPE(MPI_UINT8_T, uint8_t);
100 CREATE_MPI_DATATYPE(MPI_UINT16_T, uint16_t);
101 CREATE_MPI_DATATYPE(MPI_UINT32_T, uint32_t);
102 CREATE_MPI_DATATYPE(MPI_UINT64_T, uint64_t);
103 CREATE_MPI_DATATYPE(MPI_C_FLOAT_COMPLEX, float _Complex);
104 CREATE_MPI_DATATYPE(MPI_C_DOUBLE_COMPLEX, double _Complex);
105 CREATE_MPI_DATATYPE(MPI_C_LONG_DOUBLE_COMPLEX, long double _Complex);
106 CREATE_MPI_DATATYPE(MPI_AINT, MPI_Aint);
107 CREATE_MPI_DATATYPE(MPI_OFFSET, MPI_Offset);
109 CREATE_MPI_DATATYPE(MPI_FLOAT_INT, float_int);
110 CREATE_MPI_DATATYPE(MPI_LONG_INT, long_int);
111 CREATE_MPI_DATATYPE(MPI_DOUBLE_INT, double_int);
112 CREATE_MPI_DATATYPE(MPI_SHORT_INT, short_int);
113 CREATE_MPI_DATATYPE(MPI_2INT, int_int);
114 CREATE_MPI_DATATYPE(MPI_2FLOAT, float_float);
115 CREATE_MPI_DATATYPE(MPI_2DOUBLE, double_double);
117 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE_INT, long_double_int);
119 CREATE_MPI_DATATYPE_NULL(MPI_UB);
120 CREATE_MPI_DATATYPE_NULL(MPI_LB);
121 CREATE_MPI_DATATYPE_NULL(MPI_PACKED);
123 CREATE_MPI_DATATYPE(MPI_PTR, void*);
126 size_t smpi_datatype_size(MPI_Datatype datatype)
128 return datatype->size;
133 MPI_Aint smpi_datatype_lb(MPI_Datatype datatype)
138 MPI_Aint smpi_datatype_ub(MPI_Datatype datatype)
143 int smpi_datatype_extent(MPI_Datatype datatype, MPI_Aint * lb,
147 *extent = datatype->ub - datatype->lb;
151 MPI_Aint smpi_datatype_get_extent(MPI_Datatype datatype){
152 return datatype->ub - datatype->lb;
155 int smpi_datatype_copy(void *sendbuf, int sendcount, MPI_Datatype sendtype,
156 void *recvbuf, int recvcount, MPI_Datatype recvtype)
160 /* First check if we really have something to do */
161 if (recvcount > 0 && recvbuf != sendbuf) {
162 /* FIXME: treat packed cases */
163 sendcount *= smpi_datatype_size(sendtype);
164 recvcount *= smpi_datatype_size(recvtype);
165 count = sendcount < recvcount ? sendcount : recvcount;
167 if(sendtype->has_subtype == 0 && recvtype->has_subtype == 0) {
168 memcpy(recvbuf, sendbuf, count);
170 else if (sendtype->has_subtype == 0)
172 s_smpi_subtype_t *subtype = recvtype->substruct;
173 subtype->unserialize( sendbuf, recvbuf,1, subtype);
175 else if (recvtype->has_subtype == 0)
177 s_smpi_subtype_t *subtype = sendtype->substruct;
178 subtype->serialize(sendbuf, recvbuf,1, subtype);
180 s_smpi_subtype_t *subtype = sendtype->substruct;
183 void * buf_tmp = xbt_malloc(count);
185 subtype->serialize( sendbuf, buf_tmp,count/smpi_datatype_size(sendtype), subtype);
186 subtype = recvtype->substruct;
187 subtype->unserialize( buf_tmp, recvbuf,count/smpi_datatype_size(recvtype), subtype);
193 return sendcount > recvcount ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
197 * Copies noncontiguous data into contiguous memory.
198 * @param contiguous_vector - output vector
199 * @param noncontiguous_vector - input vector
200 * @param type - pointer contening :
201 * - stride - stride of between noncontiguous data
202 * - block_length - the width or height of blocked matrix
203 * - count - the number of rows of matrix
205 void serialize_vector( const void *noncontiguous_vector,
206 void *contiguous_vector,
210 s_smpi_mpi_vector_t* type_c = (s_smpi_mpi_vector_t*)type;
212 char* contiguous_vector_char = (char*)contiguous_vector;
213 char* noncontiguous_vector_char = (char*)noncontiguous_vector;
215 for (i = 0; i < type_c->block_count * count; i++) {
216 if (type_c->old_type->has_subtype == 0)
217 memcpy(contiguous_vector_char,
218 noncontiguous_vector_char, type_c->block_length * type_c->size_oldtype);
220 ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_vector_char,
221 contiguous_vector_char,
222 type_c->block_length,
223 type_c->old_type->substruct);
225 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
226 if((i+1)%type_c->block_count ==0)
227 noncontiguous_vector_char += type_c->block_length*smpi_datatype_get_extent(type_c->old_type);
229 noncontiguous_vector_char += type_c->block_stride*smpi_datatype_get_extent(type_c->old_type);
234 * Copies contiguous data into noncontiguous memory.
235 * @param noncontiguous_vector - output vector
236 * @param contiguous_vector - input vector
237 * @param type - pointer contening :
238 * - stride - stride of between noncontiguous data
239 * - block_length - the width or height of blocked matrix
240 * - count - the number of rows of matrix
242 void unserialize_vector( const void *contiguous_vector,
243 void *noncontiguous_vector,
247 s_smpi_mpi_vector_t* type_c = (s_smpi_mpi_vector_t*)type;
250 char* contiguous_vector_char = (char*)contiguous_vector;
251 char* noncontiguous_vector_char = (char*)noncontiguous_vector;
253 for (i = 0; i < type_c->block_count * count; i++) {
254 if (type_c->old_type->has_subtype == 0)
255 memcpy(noncontiguous_vector_char,
256 contiguous_vector_char, type_c->block_length * type_c->size_oldtype);
258 ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_vector_char,
259 noncontiguous_vector_char,
260 type_c->block_length,
261 type_c->old_type->substruct);
262 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
263 if((i+1)%type_c->block_count ==0)
264 noncontiguous_vector_char += type_c->block_length*smpi_datatype_get_extent(type_c->old_type);
266 noncontiguous_vector_char += type_c->block_stride*smpi_datatype_get_extent(type_c->old_type);
271 * Create a Sub type vector to be able to serialize and unserialize it
272 * the structure s_smpi_mpi_vector_t is derived from s_smpi_subtype which
273 * required the functions unserialize and serialize
276 s_smpi_mpi_vector_t* smpi_datatype_vector_create( int block_stride,
279 MPI_Datatype old_type,
281 s_smpi_mpi_vector_t *new_t= xbt_new(s_smpi_mpi_vector_t,1);
282 new_t->base.serialize = &serialize_vector;
283 new_t->base.unserialize = &unserialize_vector;
284 new_t->base.subtype_free = &free_vector;
285 new_t->block_stride = block_stride;
286 new_t->block_length = block_length;
287 new_t->block_count = block_count;
288 new_t->old_type = old_type;
289 new_t->size_oldtype = size_oldtype;
293 void smpi_datatype_create(MPI_Datatype* new_type, int size,int lb, int ub, int has_subtype,
294 void *struct_type, int flags){
295 MPI_Datatype new_t= xbt_new(s_smpi_mpi_datatype_t,1);
297 new_t->has_subtype = size>0? has_subtype:0;
300 new_t->flags = flags;
301 new_t->substruct = struct_type;
307 MC_ignore(&(new_t->in_use), sizeof(new_t->in_use));
311 void smpi_datatype_free(MPI_Datatype* type){
313 if((*type)->flags & DT_FLAG_PREDEFINED)return;
315 //if still used, mark for deletion
316 if((*type)->in_use!=0){
317 (*type)->flags |=DT_FLAG_DESTROYED;
321 if ((*type)->has_subtype == 1){
322 ((s_smpi_subtype_t *)(*type)->substruct)->subtype_free(type);
323 xbt_free((*type)->substruct);
329 void smpi_datatype_use(MPI_Datatype type){
330 if(type)type->in_use++;
334 MC_ignore(&(type->in_use), sizeof(type->in_use));
339 void smpi_datatype_unuse(MPI_Datatype type){
340 if(type && type->in_use-- == 0 && (type->flags & DT_FLAG_DESTROYED))
341 smpi_datatype_free(&type);
345 MC_ignore(&(type->in_use), sizeof(type->in_use));
353 Contiguous Implementation
358 * Copies noncontiguous data into contiguous memory.
359 * @param contiguous_hvector - output hvector
360 * @param noncontiguous_hvector - input hvector
361 * @param type - pointer contening :
362 * - stride - stride of between noncontiguous data, in bytes
363 * - block_length - the width or height of blocked matrix
364 * - count - the number of rows of matrix
366 void serialize_contiguous( const void *noncontiguous_hvector,
367 void *contiguous_hvector,
371 s_smpi_mpi_contiguous_t* type_c = (s_smpi_mpi_contiguous_t*)type;
372 char* contiguous_vector_char = (char*)contiguous_hvector;
373 char* noncontiguous_vector_char = (char*)noncontiguous_hvector+type_c->lb;
374 memcpy(contiguous_vector_char,
375 noncontiguous_vector_char, count* type_c->block_count * type_c->size_oldtype);
378 * Copies contiguous data into noncontiguous memory.
379 * @param noncontiguous_vector - output hvector
380 * @param contiguous_vector - input hvector
381 * @param type - pointer contening :
382 * - stride - stride of between noncontiguous data, in bytes
383 * - block_length - the width or height of blocked matrix
384 * - count - the number of rows of matrix
386 void unserialize_contiguous( const void *contiguous_vector,
387 void *noncontiguous_vector,
391 s_smpi_mpi_contiguous_t* type_c = (s_smpi_mpi_contiguous_t*)type;
392 char* contiguous_vector_char = (char*)contiguous_vector;
393 char* noncontiguous_vector_char = (char*)noncontiguous_vector+type_c->lb;
395 memcpy(noncontiguous_vector_char,
396 contiguous_vector_char, count* type_c->block_count * type_c->size_oldtype);
399 void free_contiguous(MPI_Datatype* d){
403 * Create a Sub type contiguous to be able to serialize and unserialize it
404 * the structure s_smpi_mpi_contiguous_t is derived from s_smpi_subtype which
405 * required the functions unserialize and serialize
408 s_smpi_mpi_contiguous_t* smpi_datatype_contiguous_create( MPI_Aint lb,
410 MPI_Datatype old_type,
412 s_smpi_mpi_contiguous_t *new_t= xbt_new(s_smpi_mpi_contiguous_t,1);
413 new_t->base.serialize = &serialize_contiguous;
414 new_t->base.unserialize = &unserialize_contiguous;
415 new_t->base.subtype_free = &free_contiguous;
417 new_t->block_count = block_count;
418 new_t->old_type = old_type;
419 new_t->size_oldtype = size_oldtype;
426 int smpi_datatype_contiguous(int count, MPI_Datatype old_type, MPI_Datatype* new_type, MPI_Aint lb)
429 if(old_type->has_subtype){
430 //handle this case as a hvector with stride equals to the extent of the datatype
431 return smpi_datatype_hvector(count, 1, smpi_datatype_get_extent(old_type), old_type, new_type);
434 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb,
437 smpi_datatype_size(old_type));
439 smpi_datatype_create(new_type,
440 count * smpi_datatype_size(old_type),
441 lb,lb + count * smpi_datatype_size(old_type),
442 1,subtype, DT_FLAG_CONTIGUOUS);
447 int smpi_datatype_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type)
450 if (blocklen<0) return MPI_ERR_ARG;
454 lb=smpi_datatype_lb(old_type);
455 ub=((count-1)*stride+blocklen-1)*smpi_datatype_get_extent(old_type)+smpi_datatype_ub(old_type);
457 if(old_type->has_subtype || stride != blocklen){
460 s_smpi_mpi_vector_t* subtype = smpi_datatype_vector_create( stride,
464 smpi_datatype_size(old_type));
465 smpi_datatype_create(new_type,
466 count * (blocklen) * smpi_datatype_size(old_type), lb,
473 /* in this situation the data are contignous thus it's not
474 * required to serialize and unserialize it*/
475 smpi_datatype_create(new_type, count * blocklen *
476 smpi_datatype_size(old_type), 0, ((count -1) * stride + blocklen)*
477 smpi_datatype_size(old_type),
480 DT_FLAG_VECTOR|DT_FLAG_CONTIGUOUS);
486 void free_vector(MPI_Datatype* d){
490 Hvector Implementation - Vector with stride in bytes
495 * Copies noncontiguous data into contiguous memory.
496 * @param contiguous_hvector - output hvector
497 * @param noncontiguous_hvector - input hvector
498 * @param type - pointer contening :
499 * - stride - stride of between noncontiguous data, in bytes
500 * - block_length - the width or height of blocked matrix
501 * - count - the number of rows of matrix
503 void serialize_hvector( const void *noncontiguous_hvector,
504 void *contiguous_hvector,
508 s_smpi_mpi_hvector_t* type_c = (s_smpi_mpi_hvector_t*)type;
510 char* contiguous_vector_char = (char*)contiguous_hvector;
511 char* noncontiguous_vector_char = (char*)noncontiguous_hvector;
513 for (i = 0; i < type_c->block_count * count; i++) {
514 if (type_c->old_type->has_subtype == 0)
515 memcpy(contiguous_vector_char,
516 noncontiguous_vector_char, type_c->block_length * type_c->size_oldtype);
518 ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_vector_char,
519 contiguous_vector_char,
520 type_c->block_length,
521 type_c->old_type->substruct);
523 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
524 if((i+1)%type_c->block_count ==0)
525 noncontiguous_vector_char += type_c->block_length*type_c->size_oldtype;
527 noncontiguous_vector_char += type_c->block_stride;
531 * Copies contiguous data into noncontiguous memory.
532 * @param noncontiguous_vector - output hvector
533 * @param contiguous_vector - input hvector
534 * @param type - pointer contening :
535 * - stride - stride of between noncontiguous data, in bytes
536 * - block_length - the width or height of blocked matrix
537 * - count - the number of rows of matrix
539 void unserialize_hvector( const void *contiguous_vector,
540 void *noncontiguous_vector,
544 s_smpi_mpi_hvector_t* type_c = (s_smpi_mpi_hvector_t*)type;
547 char* contiguous_vector_char = (char*)contiguous_vector;
548 char* noncontiguous_vector_char = (char*)noncontiguous_vector;
550 for (i = 0; i < type_c->block_count * count; i++) {
551 if (type_c->old_type->has_subtype == 0)
552 memcpy(noncontiguous_vector_char,
553 contiguous_vector_char, type_c->block_length * type_c->size_oldtype);
555 ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_vector_char,
556 noncontiguous_vector_char,
557 type_c->block_length,
558 type_c->old_type->substruct);
559 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
560 if((i+1)%type_c->block_count ==0)
561 noncontiguous_vector_char += type_c->block_length*type_c->size_oldtype;
563 noncontiguous_vector_char += type_c->block_stride;
568 * Create a Sub type vector to be able to serialize and unserialize it
569 * the structure s_smpi_mpi_vector_t is derived from s_smpi_subtype which
570 * required the functions unserialize and serialize
573 s_smpi_mpi_hvector_t* smpi_datatype_hvector_create( MPI_Aint block_stride,
576 MPI_Datatype old_type,
578 s_smpi_mpi_hvector_t *new_t= xbt_new(s_smpi_mpi_hvector_t,1);
579 new_t->base.serialize = &serialize_hvector;
580 new_t->base.unserialize = &unserialize_hvector;
581 new_t->base.subtype_free = &free_hvector;
582 new_t->block_stride = block_stride;
583 new_t->block_length = block_length;
584 new_t->block_count = block_count;
585 new_t->old_type = old_type;
586 new_t->size_oldtype = size_oldtype;
590 //do nothing for vector types
591 void free_hvector(MPI_Datatype* d){
594 int smpi_datatype_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type)
597 if (blocklen<0) return MPI_ERR_ARG;
601 lb=smpi_datatype_lb(old_type);
602 ub=((count-1)*stride)+(blocklen-1)*smpi_datatype_get_extent(old_type)+smpi_datatype_ub(old_type);
604 if(old_type->has_subtype || stride != blocklen*smpi_datatype_get_extent(old_type)){
605 s_smpi_mpi_hvector_t* subtype = smpi_datatype_hvector_create( stride,
609 smpi_datatype_size(old_type));
611 smpi_datatype_create(new_type, count * blocklen * smpi_datatype_size(old_type),
618 smpi_datatype_create(new_type, count * blocklen *
619 smpi_datatype_size(old_type),0,count * blocklen *
620 smpi_datatype_size(old_type),
623 DT_FLAG_VECTOR|DT_FLAG_CONTIGUOUS);
631 Indexed Implementation
635 * Copies noncontiguous data into contiguous memory.
636 * @param contiguous_indexed - output indexed
637 * @param noncontiguous_indexed - input indexed
638 * @param type - pointer contening :
639 * - block_lengths - the width or height of blocked matrix
640 * - block_indices - indices of each data, in element
641 * - count - the number of rows of matrix
643 void serialize_indexed( const void *noncontiguous_indexed,
644 void *contiguous_indexed,
648 s_smpi_mpi_indexed_t* type_c = (s_smpi_mpi_indexed_t*)type;
650 char* contiguous_indexed_char = (char*)contiguous_indexed;
651 char* noncontiguous_indexed_char = (char*)noncontiguous_indexed+type_c->block_indices[0] * type_c->size_oldtype;
652 for(j=0; j<count;j++){
653 for (i = 0; i < type_c->block_count; i++) {
654 if (type_c->old_type->has_subtype == 0)
655 memcpy(contiguous_indexed_char,
656 noncontiguous_indexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
658 ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_indexed_char,
659 contiguous_indexed_char,
660 type_c->block_lengths[i],
661 type_c->old_type->substruct);
664 contiguous_indexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
665 if (i<type_c->block_count-1)noncontiguous_indexed_char = (char*)noncontiguous_indexed + type_c->block_indices[i+1]*smpi_datatype_get_extent(type_c->old_type);
666 else noncontiguous_indexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
668 noncontiguous_indexed=(void*)noncontiguous_indexed_char;
672 * Copies contiguous data into noncontiguous memory.
673 * @param noncontiguous_indexed - output indexed
674 * @param contiguous_indexed - input indexed
675 * @param type - pointer contening :
676 * - block_lengths - the width or height of blocked matrix
677 * - block_indices - indices of each data, in element
678 * - count - the number of rows of matrix
680 void unserialize_indexed( const void *contiguous_indexed,
681 void *noncontiguous_indexed,
686 s_smpi_mpi_indexed_t* type_c = (s_smpi_mpi_indexed_t*)type;
688 char* contiguous_indexed_char = (char*)contiguous_indexed;
689 char* noncontiguous_indexed_char = (char*)noncontiguous_indexed+type_c->block_indices[0]*smpi_datatype_get_extent(type_c->old_type);
690 for(j=0; j<count;j++){
691 for (i = 0; i < type_c->block_count; i++) {
692 if (type_c->old_type->has_subtype == 0)
693 memcpy(noncontiguous_indexed_char ,
694 contiguous_indexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
696 ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_indexed_char,
697 noncontiguous_indexed_char,
698 type_c->block_lengths[i],
699 type_c->old_type->substruct);
701 contiguous_indexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
702 if (i<type_c->block_count-1)
703 noncontiguous_indexed_char = (char*)noncontiguous_indexed + type_c->block_indices[i+1]*smpi_datatype_get_extent(type_c->old_type);
704 else noncontiguous_indexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
706 noncontiguous_indexed=(void*)noncontiguous_indexed_char;
710 void free_indexed(MPI_Datatype* type){
711 xbt_free(((s_smpi_mpi_indexed_t *)(*type)->substruct)->block_lengths);
712 xbt_free(((s_smpi_mpi_indexed_t *)(*type)->substruct)->block_indices);
716 * Create a Sub type indexed to be able to serialize and unserialize it
717 * the structure s_smpi_mpi_indexed_t is derived from s_smpi_subtype which
718 * required the functions unserialize and serialize
720 s_smpi_mpi_indexed_t* smpi_datatype_indexed_create( int* block_lengths,
723 MPI_Datatype old_type,
725 s_smpi_mpi_indexed_t *new_t= xbt_new(s_smpi_mpi_indexed_t,1);
726 new_t->base.serialize = &serialize_indexed;
727 new_t->base.unserialize = &unserialize_indexed;
728 new_t->base.subtype_free = &free_indexed;
729 //TODO : add a custom function for each time to clean these
730 new_t->block_lengths= xbt_new(int, block_count);
731 new_t->block_indices= xbt_new(int, block_count);
733 for(i=0;i<block_count;i++){
734 new_t->block_lengths[i]=block_lengths[i];
735 new_t->block_indices[i]=block_indices[i];
737 new_t->block_count = block_count;
738 new_t->old_type = old_type;
739 new_t->size_oldtype = size_oldtype;
744 int smpi_datatype_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
753 lb=indices[0]*smpi_datatype_get_extent(old_type);
754 ub=indices[0]*smpi_datatype_get_extent(old_type) + blocklens[0]*smpi_datatype_ub(old_type);
757 for(i=0; i< count; i++){
760 size += blocklens[i];
762 if(indices[i]*smpi_datatype_get_extent(old_type)+smpi_datatype_lb(old_type)<lb)
763 lb = indices[i]*smpi_datatype_get_extent(old_type)+smpi_datatype_lb(old_type);
764 if(indices[i]*smpi_datatype_get_extent(old_type)+blocklens[i]*smpi_datatype_ub(old_type)>ub)
765 ub = indices[i]*smpi_datatype_get_extent(old_type)+blocklens[i]*smpi_datatype_ub(old_type);
767 if ( (i< count -1) && (indices[i]+blocklens[i] != indices[i+1]) )contiguous=0;
769 if (old_type->has_subtype == 1)
773 s_smpi_mpi_indexed_t* subtype = smpi_datatype_indexed_create( blocklens,
777 smpi_datatype_size(old_type));
778 smpi_datatype_create(new_type, size *
779 smpi_datatype_size(old_type),lb,ub,1, subtype, DT_FLAG_DATA);
781 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb,
784 smpi_datatype_size(old_type));
785 smpi_datatype_create(new_type, size *
786 smpi_datatype_size(old_type),lb,ub,1, subtype, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
794 Hindexed Implementation - Indexed with indices in bytes
798 * Copies noncontiguous data into contiguous memory.
799 * @param contiguous_hindexed - output hindexed
800 * @param noncontiguous_hindexed - input hindexed
801 * @param type - pointer contening :
802 * - block_lengths - the width or height of blocked matrix
803 * - block_indices - indices of each data, in bytes
804 * - count - the number of rows of matrix
806 void serialize_hindexed( const void *noncontiguous_hindexed,
807 void *contiguous_hindexed,
811 s_smpi_mpi_hindexed_t* type_c = (s_smpi_mpi_hindexed_t*)type;
813 char* contiguous_hindexed_char = (char*)contiguous_hindexed;
814 char* noncontiguous_hindexed_char = (char*)noncontiguous_hindexed+ type_c->block_indices[0];
815 for(j=0; j<count;j++){
816 for (i = 0; i < type_c->block_count; i++) {
817 if (type_c->old_type->has_subtype == 0)
818 memcpy(contiguous_hindexed_char,
819 noncontiguous_hindexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
821 ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_hindexed_char,
822 contiguous_hindexed_char,
823 type_c->block_lengths[i],
824 type_c->old_type->substruct);
826 contiguous_hindexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
827 if (i<type_c->block_count-1)noncontiguous_hindexed_char = (char*)noncontiguous_hindexed + type_c->block_indices[i+1];
828 else noncontiguous_hindexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
830 noncontiguous_hindexed=(void*)noncontiguous_hindexed_char;
834 * Copies contiguous data into noncontiguous memory.
835 * @param noncontiguous_hindexed - output hindexed
836 * @param contiguous_hindexed - input hindexed
837 * @param type - pointer contening :
838 * - block_lengths - the width or height of blocked matrix
839 * - block_indices - indices of each data, in bytes
840 * - count - the number of rows of matrix
842 void unserialize_hindexed( const void *contiguous_hindexed,
843 void *noncontiguous_hindexed,
847 s_smpi_mpi_hindexed_t* type_c = (s_smpi_mpi_hindexed_t*)type;
850 char* contiguous_hindexed_char = (char*)contiguous_hindexed;
851 char* noncontiguous_hindexed_char = (char*)noncontiguous_hindexed+ type_c->block_indices[0];
852 for(j=0; j<count;j++){
853 for (i = 0; i < type_c->block_count; i++) {
854 if (type_c->old_type->has_subtype == 0)
855 memcpy(noncontiguous_hindexed_char,
856 contiguous_hindexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
858 ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_hindexed_char,
859 noncontiguous_hindexed_char,
860 type_c->block_lengths[i],
861 type_c->old_type->substruct);
863 contiguous_hindexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
864 if (i<type_c->block_count-1)noncontiguous_hindexed_char = (char*)noncontiguous_hindexed + type_c->block_indices[i+1];
865 else noncontiguous_hindexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
867 noncontiguous_hindexed=(void*)noncontiguous_hindexed_char;
871 void free_hindexed(MPI_Datatype* type){
872 xbt_free(((s_smpi_mpi_hindexed_t *)(*type)->substruct)->block_lengths);
873 xbt_free(((s_smpi_mpi_hindexed_t *)(*type)->substruct)->block_indices);
877 * Create a Sub type hindexed to be able to serialize and unserialize it
878 * the structure s_smpi_mpi_hindexed_t is derived from s_smpi_subtype which
879 * required the functions unserialize and serialize
881 s_smpi_mpi_hindexed_t* smpi_datatype_hindexed_create( int* block_lengths,
882 MPI_Aint* block_indices,
884 MPI_Datatype old_type,
886 s_smpi_mpi_hindexed_t *new_t= xbt_new(s_smpi_mpi_hindexed_t,1);
887 new_t->base.serialize = &serialize_hindexed;
888 new_t->base.unserialize = &unserialize_hindexed;
889 new_t->base.subtype_free = &free_hindexed;
890 //TODO : add a custom function for each time to clean these
891 new_t->block_lengths= xbt_new(int, block_count);
892 new_t->block_indices= xbt_new(MPI_Aint, block_count);
894 for(i=0;i<block_count;i++){
895 new_t->block_lengths[i]=block_lengths[i];
896 new_t->block_indices[i]=block_indices[i];
898 new_t->block_count = block_count;
899 new_t->old_type = old_type;
900 new_t->size_oldtype = size_oldtype;
905 int smpi_datatype_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
914 lb=indices[0] + smpi_datatype_lb(old_type);
915 ub=indices[0] + blocklens[0]*smpi_datatype_ub(old_type);
917 for(i=0; i< count; i++){
920 size += blocklens[i];
922 if(indices[i]+smpi_datatype_lb(old_type)<lb) lb = indices[i]+smpi_datatype_lb(old_type);
923 if(indices[i]+blocklens[i]*smpi_datatype_ub(old_type)>ub) ub = indices[i]+blocklens[i]*smpi_datatype_ub(old_type);
925 if ( (i< count -1) && (indices[i]+blocklens[i]*smpi_datatype_size(old_type) != indices[i+1]) )contiguous=0;
927 if (old_type->has_subtype == 1 || lb!=0)
931 s_smpi_mpi_hindexed_t* subtype = smpi_datatype_hindexed_create( blocklens,
935 smpi_datatype_size(old_type));
936 smpi_datatype_create(new_type, size * smpi_datatype_size(old_type),
939 ,1, subtype, DT_FLAG_DATA);
941 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb,
944 smpi_datatype_size(old_type));
945 smpi_datatype_create(new_type, size * smpi_datatype_size(old_type),
946 0,size * smpi_datatype_size(old_type),
947 1, subtype, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
955 struct Implementation - Indexed with indices in bytes
959 * Copies noncontiguous data into contiguous memory.
960 * @param contiguous_struct - output struct
961 * @param noncontiguous_struct - input struct
962 * @param type - pointer contening :
963 * - stride - stride of between noncontiguous data
964 * - block_length - the width or height of blocked matrix
965 * - count - the number of rows of matrix
967 void serialize_struct( const void *noncontiguous_struct,
968 void *contiguous_struct,
972 s_smpi_mpi_struct_t* type_c = (s_smpi_mpi_struct_t*)type;
974 char* contiguous_struct_char = (char*)contiguous_struct;
975 char* noncontiguous_struct_char = (char*)noncontiguous_struct+ type_c->block_indices[0];
976 for(j=0; j<count;j++){
977 for (i = 0; i < type_c->block_count; i++) {
978 if (type_c->old_types[i]->has_subtype == 0)
979 memcpy(contiguous_struct_char,
980 noncontiguous_struct_char, type_c->block_lengths[i] * smpi_datatype_size(type_c->old_types[i]));
982 ((s_smpi_subtype_t*)type_c->old_types[i]->substruct)->serialize( noncontiguous_struct_char,
983 contiguous_struct_char,
984 type_c->block_lengths[i],
985 type_c->old_types[i]->substruct);
988 contiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_size(type_c->old_types[i]);
989 if (i<type_c->block_count-1)noncontiguous_struct_char = (char*)noncontiguous_struct + type_c->block_indices[i+1];
990 else noncontiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_types[i]);//let's hope this is MPI_UB ?
992 noncontiguous_struct=(void*)noncontiguous_struct_char;
996 * Copies contiguous data into noncontiguous memory.
997 * @param noncontiguous_struct - output struct
998 * @param contiguous_struct - input struct
999 * @param type - pointer contening :
1000 * - stride - stride of between noncontiguous data
1001 * - block_length - the width or height of blocked matrix
1002 * - count - the number of rows of matrix
1004 void unserialize_struct( const void *contiguous_struct,
1005 void *noncontiguous_struct,
1009 s_smpi_mpi_struct_t* type_c = (s_smpi_mpi_struct_t*)type;
1012 char* contiguous_struct_char = (char*)contiguous_struct;
1013 char* noncontiguous_struct_char = (char*)noncontiguous_struct+ type_c->block_indices[0];
1014 for(j=0; j<count;j++){
1015 for (i = 0; i < type_c->block_count; i++) {
1016 if (type_c->old_types[i]->has_subtype == 0)
1017 memcpy(noncontiguous_struct_char,
1018 contiguous_struct_char, type_c->block_lengths[i] * smpi_datatype_size(type_c->old_types[i]));
1020 ((s_smpi_subtype_t*)type_c->old_types[i]->substruct)->unserialize( contiguous_struct_char,
1021 noncontiguous_struct_char,
1022 type_c->block_lengths[i],
1023 type_c->old_types[i]->substruct);
1025 contiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_size(type_c->old_types[i]);
1026 if (i<type_c->block_count-1)noncontiguous_struct_char = (char*)noncontiguous_struct + type_c->block_indices[i+1];
1027 else noncontiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_types[i]);
1029 noncontiguous_struct=(void*)noncontiguous_struct_char;
1034 void free_struct(MPI_Datatype* type){
1035 xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->block_lengths);
1036 xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->block_indices);
1037 xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->old_types);
1041 * Create a Sub type struct to be able to serialize and unserialize it
1042 * the structure s_smpi_mpi_struct_t is derived from s_smpi_subtype which
1043 * required the functions unserialize and serialize
1045 s_smpi_mpi_struct_t* smpi_datatype_struct_create( int* block_lengths,
1046 MPI_Aint* block_indices,
1048 MPI_Datatype* old_types){
1049 s_smpi_mpi_struct_t *new_t= xbt_new(s_smpi_mpi_struct_t,1);
1050 new_t->base.serialize = &serialize_struct;
1051 new_t->base.unserialize = &unserialize_struct;
1052 new_t->base.subtype_free = &free_struct;
1053 //TODO : add a custom function for each time to clean these
1054 new_t->block_lengths= xbt_new(int, block_count);
1055 new_t->block_indices= xbt_new(MPI_Aint, block_count);
1056 new_t->old_types= xbt_new(MPI_Datatype, block_count);
1058 for(i=0;i<block_count;i++){
1059 new_t->block_lengths[i]=block_lengths[i];
1060 new_t->block_indices[i]=block_indices[i];
1061 new_t->old_types[i]=old_types[i];
1063 //new_t->block_lengths = block_lengths;
1064 //new_t->block_indices = block_indices;
1065 new_t->block_count = block_count;
1066 //new_t->old_types = old_types;
1071 int smpi_datatype_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type)
1080 lb=indices[0] + smpi_datatype_lb(old_types[0]);
1081 ub=indices[0] + blocklens[0]*smpi_datatype_ub(old_types[0]);
1085 for(i=0; i< count; i++){
1088 if (old_types[i]->has_subtype == 1)
1091 size += blocklens[i]*smpi_datatype_size(old_types[i]);
1092 if (old_types[i]==MPI_LB){
1096 if (old_types[i]==MPI_UB){
1101 if(!forced_lb && indices[i]+smpi_datatype_lb(old_types[i])<lb) lb = indices[i];
1102 if(!forced_ub && indices[i]+blocklens[i]*smpi_datatype_ub(old_types[i])>ub) ub = indices[i]+blocklens[i]*smpi_datatype_ub(old_types[i]);
1104 if ( (i< count -1) && (indices[i]+blocklens[i]*smpi_datatype_size(old_types[i]) != indices[i+1]) )contiguous=0;
1108 s_smpi_mpi_struct_t* subtype = smpi_datatype_struct_create( blocklens,
1113 smpi_datatype_create(new_type, size, lb, ub,1, subtype, DT_FLAG_DATA);
1115 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb,
1119 smpi_datatype_create(new_type, size, lb, ub,1, subtype, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
1124 void smpi_datatype_commit(MPI_Datatype *datatype)
1126 (*datatype)->flags= ((*datatype)->flags | DT_FLAG_COMMITED);
1129 typedef struct s_smpi_mpi_op {
1130 MPI_User_function *func;
1134 #define MAX_OP(a, b) (b) = (a) < (b) ? (b) : (a)
1135 #define MIN_OP(a, b) (b) = (a) < (b) ? (a) : (b)
1136 #define SUM_OP(a, b) (b) += (a)
1137 #define PROD_OP(a, b) (b) *= (a)
1138 #define LAND_OP(a, b) (b) = (a) && (b)
1139 #define LOR_OP(a, b) (b) = (a) || (b)
1140 #define LXOR_OP(a, b) (b) = (!(a) && (b)) || ((a) && !(b))
1141 #define BAND_OP(a, b) (b) &= (a)
1142 #define BOR_OP(a, b) (b) |= (a)
1143 #define BXOR_OP(a, b) (b) ^= (a)
1144 #define MAXLOC_OP(a, b) (b) = (a.value) < (b.value) ? (b) : (a)
1145 #define MINLOC_OP(a, b) (b) = (a.value) < (b.value) ? (a) : (b)
1146 //TODO : MINLOC & MAXLOC
1148 #define APPLY_FUNC(a, b, length, type, func) \
1151 type* x = (type*)(a); \
1152 type* y = (type*)(b); \
1153 for(i = 0; i < *(length); i++) { \
1158 static void max_func(void *a, void *b, int *length,
1159 MPI_Datatype * datatype)
1161 if (*datatype == MPI_CHAR) {
1162 APPLY_FUNC(a, b, length, char, MAX_OP);
1163 } else if (*datatype == MPI_SHORT) {
1164 APPLY_FUNC(a, b, length, short, MAX_OP);
1165 } else if (*datatype == MPI_INT) {
1166 APPLY_FUNC(a, b, length, int, MAX_OP);
1167 } else if (*datatype == MPI_LONG) {
1168 APPLY_FUNC(a, b, length, long, MAX_OP);
1169 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1170 APPLY_FUNC(a, b, length, unsigned short, MAX_OP);
1171 } else if (*datatype == MPI_UNSIGNED) {
1172 APPLY_FUNC(a, b, length, unsigned int, MAX_OP);
1173 } else if (*datatype == MPI_UNSIGNED_LONG) {
1174 APPLY_FUNC(a, b, length, unsigned long, MAX_OP);
1175 } else if (*datatype == MPI_FLOAT) {
1176 APPLY_FUNC(a, b, length, float, MAX_OP);
1177 } else if (*datatype == MPI_DOUBLE) {
1178 APPLY_FUNC(a, b, length, double, MAX_OP);
1179 } else if (*datatype == MPI_LONG_DOUBLE) {
1180 APPLY_FUNC(a, b, length, long double, MAX_OP);
1184 static void min_func(void *a, void *b, int *length,
1185 MPI_Datatype * datatype)
1187 if (*datatype == MPI_CHAR) {
1188 APPLY_FUNC(a, b, length, char, MIN_OP);
1189 } else if (*datatype == MPI_SHORT) {
1190 APPLY_FUNC(a, b, length, short, MIN_OP);
1191 } else if (*datatype == MPI_INT) {
1192 APPLY_FUNC(a, b, length, int, MIN_OP);
1193 } else if (*datatype == MPI_LONG) {
1194 APPLY_FUNC(a, b, length, long, MIN_OP);
1195 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1196 APPLY_FUNC(a, b, length, unsigned short, MIN_OP);
1197 } else if (*datatype == MPI_UNSIGNED) {
1198 APPLY_FUNC(a, b, length, unsigned int, MIN_OP);
1199 } else if (*datatype == MPI_UNSIGNED_LONG) {
1200 APPLY_FUNC(a, b, length, unsigned long, MIN_OP);
1201 } else if (*datatype == MPI_FLOAT) {
1202 APPLY_FUNC(a, b, length, float, MIN_OP);
1203 } else if (*datatype == MPI_DOUBLE) {
1204 APPLY_FUNC(a, b, length, double, MIN_OP);
1205 } else if (*datatype == MPI_LONG_DOUBLE) {
1206 APPLY_FUNC(a, b, length, long double, MIN_OP);
1210 static void sum_func(void *a, void *b, int *length,
1211 MPI_Datatype * datatype)
1213 if (*datatype == MPI_CHAR) {
1214 APPLY_FUNC(a, b, length, char, SUM_OP);
1215 } else if (*datatype == MPI_SHORT) {
1216 APPLY_FUNC(a, b, length, short, SUM_OP);
1217 } else if (*datatype == MPI_INT) {
1218 APPLY_FUNC(a, b, length, int, SUM_OP);
1219 } else if (*datatype == MPI_LONG) {
1220 APPLY_FUNC(a, b, length, long, SUM_OP);
1221 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1222 APPLY_FUNC(a, b, length, unsigned short, SUM_OP);
1223 } else if (*datatype == MPI_UNSIGNED) {
1224 APPLY_FUNC(a, b, length, unsigned int, SUM_OP);
1225 } else if (*datatype == MPI_UNSIGNED_LONG) {
1226 APPLY_FUNC(a, b, length, unsigned long, SUM_OP);
1227 } else if (*datatype == MPI_FLOAT) {
1228 APPLY_FUNC(a, b, length, float, SUM_OP);
1229 } else if (*datatype == MPI_DOUBLE) {
1230 APPLY_FUNC(a, b, length, double, SUM_OP);
1231 } else if (*datatype == MPI_LONG_DOUBLE) {
1232 APPLY_FUNC(a, b, length, long double, SUM_OP);
1233 } else if (*datatype == MPI_C_FLOAT_COMPLEX) {
1234 APPLY_FUNC(a, b, length, float _Complex, SUM_OP);
1235 } else if (*datatype == MPI_C_DOUBLE_COMPLEX) {
1236 APPLY_FUNC(a, b, length, double _Complex, SUM_OP);
1237 } else if (*datatype == MPI_C_LONG_DOUBLE_COMPLEX) {
1238 APPLY_FUNC(a, b, length, long double _Complex, SUM_OP);
1242 static void prod_func(void *a, void *b, int *length,
1243 MPI_Datatype * datatype)
1245 if (*datatype == MPI_CHAR) {
1246 APPLY_FUNC(a, b, length, char, PROD_OP);
1247 } else if (*datatype == MPI_SHORT) {
1248 APPLY_FUNC(a, b, length, short, PROD_OP);
1249 } else if (*datatype == MPI_INT) {
1250 APPLY_FUNC(a, b, length, int, PROD_OP);
1251 } else if (*datatype == MPI_LONG) {
1252 APPLY_FUNC(a, b, length, long, PROD_OP);
1253 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1254 APPLY_FUNC(a, b, length, unsigned short, PROD_OP);
1255 } else if (*datatype == MPI_UNSIGNED) {
1256 APPLY_FUNC(a, b, length, unsigned int, PROD_OP);
1257 } else if (*datatype == MPI_UNSIGNED_LONG) {
1258 APPLY_FUNC(a, b, length, unsigned long, PROD_OP);
1259 } else if (*datatype == MPI_FLOAT) {
1260 APPLY_FUNC(a, b, length, float, PROD_OP);
1261 } else if (*datatype == MPI_DOUBLE) {
1262 APPLY_FUNC(a, b, length, double, PROD_OP);
1263 } else if (*datatype == MPI_LONG_DOUBLE) {
1264 APPLY_FUNC(a, b, length, long double, PROD_OP);
1265 } else if (*datatype == MPI_C_FLOAT_COMPLEX) {
1266 APPLY_FUNC(a, b, length, float _Complex, PROD_OP);
1267 } else if (*datatype == MPI_C_DOUBLE_COMPLEX) {
1268 APPLY_FUNC(a, b, length, double _Complex, PROD_OP);
1269 } else if (*datatype == MPI_C_LONG_DOUBLE_COMPLEX) {
1270 APPLY_FUNC(a, b, length, long double _Complex, PROD_OP);
1274 static void land_func(void *a, void *b, int *length,
1275 MPI_Datatype * datatype)
1277 if (*datatype == MPI_CHAR) {
1278 APPLY_FUNC(a, b, length, char, LAND_OP);
1279 } else if (*datatype == MPI_SHORT) {
1280 APPLY_FUNC(a, b, length, short, LAND_OP);
1281 } else if (*datatype == MPI_INT) {
1282 APPLY_FUNC(a, b, length, int, LAND_OP);
1283 } else if (*datatype == MPI_LONG) {
1284 APPLY_FUNC(a, b, length, long, LAND_OP);
1285 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1286 APPLY_FUNC(a, b, length, unsigned short, LAND_OP);
1287 } else if (*datatype == MPI_UNSIGNED) {
1288 APPLY_FUNC(a, b, length, unsigned int, LAND_OP);
1289 } else if (*datatype == MPI_UNSIGNED_LONG) {
1290 APPLY_FUNC(a, b, length, unsigned long, LAND_OP);
1291 } else if (*datatype == MPI_C_BOOL) {
1292 APPLY_FUNC(a, b, length, _Bool, LAND_OP);
1296 static void lor_func(void *a, void *b, int *length,
1297 MPI_Datatype * datatype)
1299 if (*datatype == MPI_CHAR) {
1300 APPLY_FUNC(a, b, length, char, LOR_OP);
1301 } else if (*datatype == MPI_SHORT) {
1302 APPLY_FUNC(a, b, length, short, LOR_OP);
1303 } else if (*datatype == MPI_INT) {
1304 APPLY_FUNC(a, b, length, int, LOR_OP);
1305 } else if (*datatype == MPI_LONG) {
1306 APPLY_FUNC(a, b, length, long, LOR_OP);
1307 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1308 APPLY_FUNC(a, b, length, unsigned short, LOR_OP);
1309 } else if (*datatype == MPI_UNSIGNED) {
1310 APPLY_FUNC(a, b, length, unsigned int, LOR_OP);
1311 } else if (*datatype == MPI_UNSIGNED_LONG) {
1312 APPLY_FUNC(a, b, length, unsigned long, LOR_OP);
1313 } else if (*datatype == MPI_C_BOOL) {
1314 APPLY_FUNC(a, b, length, _Bool, LOR_OP);
1318 static void lxor_func(void *a, void *b, int *length,
1319 MPI_Datatype * datatype)
1321 if (*datatype == MPI_CHAR) {
1322 APPLY_FUNC(a, b, length, char, LXOR_OP);
1323 } else if (*datatype == MPI_SHORT) {
1324 APPLY_FUNC(a, b, length, short, LXOR_OP);
1325 } else if (*datatype == MPI_INT) {
1326 APPLY_FUNC(a, b, length, int, LXOR_OP);
1327 } else if (*datatype == MPI_LONG) {
1328 APPLY_FUNC(a, b, length, long, LXOR_OP);
1329 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1330 APPLY_FUNC(a, b, length, unsigned short, LXOR_OP);
1331 } else if (*datatype == MPI_UNSIGNED) {
1332 APPLY_FUNC(a, b, length, unsigned int, LXOR_OP);
1333 } else if (*datatype == MPI_UNSIGNED_LONG) {
1334 APPLY_FUNC(a, b, length, unsigned long, LXOR_OP);
1335 } else if (*datatype == MPI_C_BOOL) {
1336 APPLY_FUNC(a, b, length, _Bool, LXOR_OP);
1340 static void band_func(void *a, void *b, int *length,
1341 MPI_Datatype * datatype)
1343 if (*datatype == MPI_CHAR) {
1344 APPLY_FUNC(a, b, length, char, BAND_OP);
1346 if (*datatype == MPI_SHORT) {
1347 APPLY_FUNC(a, b, length, short, BAND_OP);
1348 } else if (*datatype == MPI_INT) {
1349 APPLY_FUNC(a, b, length, int, BAND_OP);
1350 } else if (*datatype == MPI_LONG) {
1351 APPLY_FUNC(a, b, length, long, BAND_OP);
1352 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1353 APPLY_FUNC(a, b, length, unsigned short, BAND_OP);
1354 } else if (*datatype == MPI_UNSIGNED) {
1355 APPLY_FUNC(a, b, length, unsigned int, BAND_OP);
1356 } else if (*datatype == MPI_UNSIGNED_LONG) {
1357 APPLY_FUNC(a, b, length, unsigned long, BAND_OP);
1358 } else if (*datatype == MPI_BYTE) {
1359 APPLY_FUNC(a, b, length, uint8_t, BAND_OP);
1363 static void bor_func(void *a, void *b, int *length,
1364 MPI_Datatype * datatype)
1366 if (*datatype == MPI_CHAR) {
1367 APPLY_FUNC(a, b, length, char, BOR_OP);
1368 } else if (*datatype == MPI_SHORT) {
1369 APPLY_FUNC(a, b, length, short, BOR_OP);
1370 } else if (*datatype == MPI_INT) {
1371 APPLY_FUNC(a, b, length, int, BOR_OP);
1372 } else if (*datatype == MPI_LONG) {
1373 APPLY_FUNC(a, b, length, long, BOR_OP);
1374 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1375 APPLY_FUNC(a, b, length, unsigned short, BOR_OP);
1376 } else if (*datatype == MPI_UNSIGNED) {
1377 APPLY_FUNC(a, b, length, unsigned int, BOR_OP);
1378 } else if (*datatype == MPI_UNSIGNED_LONG) {
1379 APPLY_FUNC(a, b, length, unsigned long, BOR_OP);
1380 } else if (*datatype == MPI_BYTE) {
1381 APPLY_FUNC(a, b, length, uint8_t, BOR_OP);
1385 static void bxor_func(void *a, void *b, int *length,
1386 MPI_Datatype * datatype)
1388 if (*datatype == MPI_CHAR) {
1389 APPLY_FUNC(a, b, length, char, BXOR_OP);
1390 } else if (*datatype == MPI_SHORT) {
1391 APPLY_FUNC(a, b, length, short, BXOR_OP);
1392 } else if (*datatype == MPI_INT) {
1393 APPLY_FUNC(a, b, length, int, BXOR_OP);
1394 } else if (*datatype == MPI_LONG) {
1395 APPLY_FUNC(a, b, length, long, BXOR_OP);
1396 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1397 APPLY_FUNC(a, b, length, unsigned short, BXOR_OP);
1398 } else if (*datatype == MPI_UNSIGNED) {
1399 APPLY_FUNC(a, b, length, unsigned int, BXOR_OP);
1400 } else if (*datatype == MPI_UNSIGNED_LONG) {
1401 APPLY_FUNC(a, b, length, unsigned long, BXOR_OP);
1402 } else if (*datatype == MPI_BYTE) {
1403 APPLY_FUNC(a, b, length, uint8_t, BXOR_OP);
1407 static void minloc_func(void *a, void *b, int *length,
1408 MPI_Datatype * datatype)
1410 if (*datatype == MPI_FLOAT_INT) {
1411 APPLY_FUNC(a, b, length, float_int, MINLOC_OP);
1412 } else if (*datatype == MPI_LONG_INT) {
1413 APPLY_FUNC(a, b, length, long_int, MINLOC_OP);
1414 } else if (*datatype == MPI_DOUBLE_INT) {
1415 APPLY_FUNC(a, b, length, double_int, MINLOC_OP);
1416 } else if (*datatype == MPI_SHORT_INT) {
1417 APPLY_FUNC(a, b, length, short_int, MINLOC_OP);
1418 } else if (*datatype == MPI_2INT) {
1419 APPLY_FUNC(a, b, length, int_int, MINLOC_OP);
1420 } else if (*datatype == MPI_LONG_DOUBLE_INT) {
1421 APPLY_FUNC(a, b, length, long_double_int, MINLOC_OP);
1422 } else if (*datatype == MPI_2FLOAT) {
1423 APPLY_FUNC(a, b, length, float_float, MINLOC_OP);
1424 } else if (*datatype == MPI_2DOUBLE) {
1425 APPLY_FUNC(a, b, length, double_double, MINLOC_OP);
1429 static void maxloc_func(void *a, void *b, int *length,
1430 MPI_Datatype * datatype)
1432 if (*datatype == MPI_FLOAT_INT) {
1433 APPLY_FUNC(a, b, length, float_int, MAXLOC_OP);
1434 } else if (*datatype == MPI_LONG_INT) {
1435 APPLY_FUNC(a, b, length, long_int, MAXLOC_OP);
1436 } else if (*datatype == MPI_DOUBLE_INT) {
1437 APPLY_FUNC(a, b, length, double_int, MAXLOC_OP);
1438 } else if (*datatype == MPI_SHORT_INT) {
1439 APPLY_FUNC(a, b, length, short_int, MAXLOC_OP);
1440 } else if (*datatype == MPI_2INT) {
1441 APPLY_FUNC(a, b, length, int_int, MAXLOC_OP);
1442 } else if (*datatype == MPI_LONG_DOUBLE_INT) {
1443 APPLY_FUNC(a, b, length, long_double_int, MAXLOC_OP);
1444 } else if (*datatype == MPI_2FLOAT) {
1445 APPLY_FUNC(a, b, length, float_float, MAXLOC_OP);
1446 } else if (*datatype == MPI_2DOUBLE) {
1447 APPLY_FUNC(a, b, length, double_double, MAXLOC_OP);
1452 #define CREATE_MPI_OP(name, func) \
1453 static s_smpi_mpi_op_t mpi_##name = { &(func) /* func */, TRUE }; \
1454 MPI_Op name = &mpi_##name;
1456 CREATE_MPI_OP(MPI_MAX, max_func);
1457 CREATE_MPI_OP(MPI_MIN, min_func);
1458 CREATE_MPI_OP(MPI_SUM, sum_func);
1459 CREATE_MPI_OP(MPI_PROD, prod_func);
1460 CREATE_MPI_OP(MPI_LAND, land_func);
1461 CREATE_MPI_OP(MPI_LOR, lor_func);
1462 CREATE_MPI_OP(MPI_LXOR, lxor_func);
1463 CREATE_MPI_OP(MPI_BAND, band_func);
1464 CREATE_MPI_OP(MPI_BOR, bor_func);
1465 CREATE_MPI_OP(MPI_BXOR, bxor_func);
1466 CREATE_MPI_OP(MPI_MAXLOC, maxloc_func);
1467 CREATE_MPI_OP(MPI_MINLOC, minloc_func);
1469 MPI_Op smpi_op_new(MPI_User_function * function, int commute)
1472 op = xbt_new(s_smpi_mpi_op_t, 1);
1473 op->func = function;
1474 op-> is_commute = commute;
1478 int smpi_op_is_commute(MPI_Op op)
1480 return (op==MPI_OP_NULL) ? 1 : op-> is_commute;
1483 void smpi_op_destroy(MPI_Op op)
1488 void smpi_op_apply(MPI_Op op, void *invec, void *inoutvec, int *len,
1489 MPI_Datatype * datatype)
1491 op->func(invec, inoutvec, len, datatype);