1 /* smpi_mpi_dt.c -- MPI primitives to handle datatypes */
2 /* FIXME: a very incomplete implementation */
4 /* Copyright (c) 2009-2014. 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 "xbt/replay.h"
18 #include "simgrid/modelchecker.h"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_mpi_dt, smpi,
21 "Logging specific to SMPI (datatype)");
23 #define CREATE_MPI_DATATYPE(name, type) \
24 static s_smpi_mpi_datatype_t mpi_##name = { \
25 sizeof(type), /* size */ \
26 0, /*was 1 has_subtype*/ \
28 sizeof(type), /* ub = lb + size */ \
29 DT_FLAG_BASIC, /* flags */ \
30 NULL /* pointer on extended struct*/ \
32 MPI_Datatype name = &mpi_##name;
34 #define CREATE_MPI_DATATYPE_NULL(name) \
35 static s_smpi_mpi_datatype_t mpi_##name = { \
37 0, /*was 1 has_subtype*/ \
39 0, /* ub = lb + size */ \
40 DT_FLAG_BASIC, /* flags */ \
41 NULL /* pointer on extended struct*/ \
43 MPI_Datatype name = &mpi_##name;
45 //The following are datatypes for the MPI functions MPI_MAXLOC and MPI_MINLOC.
79 // Predefined data types
80 CREATE_MPI_DATATYPE(MPI_CHAR, char);
81 CREATE_MPI_DATATYPE(MPI_SHORT, short);
82 CREATE_MPI_DATATYPE(MPI_INT, int);
83 CREATE_MPI_DATATYPE(MPI_LONG, long);
84 CREATE_MPI_DATATYPE(MPI_LONG_LONG, long long);
85 CREATE_MPI_DATATYPE(MPI_SIGNED_CHAR, signed char);
86 CREATE_MPI_DATATYPE(MPI_UNSIGNED_CHAR, unsigned char);
87 CREATE_MPI_DATATYPE(MPI_UNSIGNED_SHORT, unsigned short);
88 CREATE_MPI_DATATYPE(MPI_UNSIGNED, unsigned int);
89 CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG, unsigned long);
90 CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG_LONG, unsigned long long);
91 CREATE_MPI_DATATYPE(MPI_FLOAT, float);
92 CREATE_MPI_DATATYPE(MPI_DOUBLE, double);
93 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE, long double);
94 CREATE_MPI_DATATYPE(MPI_WCHAR, wchar_t);
95 CREATE_MPI_DATATYPE(MPI_C_BOOL, _Bool);
96 CREATE_MPI_DATATYPE(MPI_INT8_T, int8_t);
97 CREATE_MPI_DATATYPE(MPI_INT16_T, int16_t);
98 CREATE_MPI_DATATYPE(MPI_INT32_T, int32_t);
99 CREATE_MPI_DATATYPE(MPI_INT64_T, int64_t);
100 CREATE_MPI_DATATYPE(MPI_UINT8_T, uint8_t);
101 CREATE_MPI_DATATYPE(MPI_UINT16_T, uint16_t);
102 CREATE_MPI_DATATYPE(MPI_UINT32_T, uint32_t);
103 CREATE_MPI_DATATYPE(MPI_UINT64_T, uint64_t);
104 CREATE_MPI_DATATYPE(MPI_C_FLOAT_COMPLEX, float _Complex);
105 CREATE_MPI_DATATYPE(MPI_C_DOUBLE_COMPLEX, double _Complex);
106 CREATE_MPI_DATATYPE(MPI_C_LONG_DOUBLE_COMPLEX, long double _Complex);
107 CREATE_MPI_DATATYPE(MPI_AINT, MPI_Aint);
108 CREATE_MPI_DATATYPE(MPI_OFFSET, MPI_Offset);
110 CREATE_MPI_DATATYPE(MPI_FLOAT_INT, float_int);
111 CREATE_MPI_DATATYPE(MPI_LONG_INT, long_int);
112 CREATE_MPI_DATATYPE(MPI_DOUBLE_INT, double_int);
113 CREATE_MPI_DATATYPE(MPI_SHORT_INT, short_int);
114 CREATE_MPI_DATATYPE(MPI_2INT, int_int);
115 CREATE_MPI_DATATYPE(MPI_2FLOAT, float_float);
116 CREATE_MPI_DATATYPE(MPI_2DOUBLE, double_double);
118 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE_INT, long_double_int);
120 CREATE_MPI_DATATYPE_NULL(MPI_UB);
121 CREATE_MPI_DATATYPE_NULL(MPI_LB);
122 CREATE_MPI_DATATYPE_NULL(MPI_PACKED);
124 CREATE_MPI_DATATYPE(MPI_PTR, void*);
127 size_t smpi_datatype_size(MPI_Datatype datatype)
129 return datatype->size;
134 MPI_Aint smpi_datatype_lb(MPI_Datatype datatype)
139 MPI_Aint smpi_datatype_ub(MPI_Datatype datatype)
144 int smpi_datatype_extent(MPI_Datatype datatype, MPI_Aint * lb,
148 *extent = datatype->ub - datatype->lb;
152 MPI_Aint smpi_datatype_get_extent(MPI_Datatype datatype){
153 return datatype->ub - datatype->lb;
156 int smpi_datatype_copy(void *sendbuf, int sendcount, MPI_Datatype sendtype,
157 void *recvbuf, int recvcount, MPI_Datatype recvtype)
161 /* First check if we really have something to do */
162 if (recvcount > 0 && recvbuf != sendbuf) {
163 /* FIXME: treat packed cases */
164 sendcount *= smpi_datatype_size(sendtype);
165 recvcount *= smpi_datatype_size(recvtype);
166 count = sendcount < recvcount ? sendcount : recvcount;
168 if(sendtype->has_subtype == 0 && recvtype->has_subtype == 0) {
169 if(!_xbt_replay_is_active()) memcpy(recvbuf, sendbuf, count);
171 else if (sendtype->has_subtype == 0)
173 s_smpi_subtype_t *subtype = recvtype->substruct;
174 subtype->unserialize( sendbuf, recvbuf,1, subtype);
176 else if (recvtype->has_subtype == 0)
178 s_smpi_subtype_t *subtype = sendtype->substruct;
179 subtype->serialize(sendbuf, recvbuf,1, subtype);
181 s_smpi_subtype_t *subtype = sendtype->substruct;
184 void * buf_tmp = xbt_malloc(count);
186 subtype->serialize( sendbuf, buf_tmp,count/smpi_datatype_size(sendtype), subtype);
187 subtype = recvtype->substruct;
188 subtype->unserialize( buf_tmp, recvbuf,count/smpi_datatype_size(recvtype), subtype);
194 return sendcount > recvcount ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
198 * Copies noncontiguous data into contiguous memory.
199 * @param contiguous_vector - output vector
200 * @param noncontiguous_vector - input vector
201 * @param type - pointer contening :
202 * - stride - stride of between noncontiguous data
203 * - block_length - the width or height of blocked matrix
204 * - count - the number of rows of matrix
206 void serialize_vector( const void *noncontiguous_vector,
207 void *contiguous_vector,
211 s_smpi_mpi_vector_t* type_c = (s_smpi_mpi_vector_t*)type;
213 char* contiguous_vector_char = (char*)contiguous_vector;
214 char* noncontiguous_vector_char = (char*)noncontiguous_vector;
216 for (i = 0; i < type_c->block_count * count; i++) {
217 if (type_c->old_type->has_subtype == 0)
218 memcpy(contiguous_vector_char,
219 noncontiguous_vector_char, type_c->block_length * type_c->size_oldtype);
221 ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_vector_char,
222 contiguous_vector_char,
223 type_c->block_length,
224 type_c->old_type->substruct);
226 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
227 if((i+1)%type_c->block_count ==0)
228 noncontiguous_vector_char += type_c->block_length*smpi_datatype_get_extent(type_c->old_type);
230 noncontiguous_vector_char += type_c->block_stride*smpi_datatype_get_extent(type_c->old_type);
235 * Copies contiguous data into noncontiguous memory.
236 * @param noncontiguous_vector - output vector
237 * @param contiguous_vector - input vector
238 * @param type - pointer contening :
239 * - stride - stride of between noncontiguous data
240 * - block_length - the width or height of blocked matrix
241 * - count - the number of rows of matrix
243 void unserialize_vector( const void *contiguous_vector,
244 void *noncontiguous_vector,
248 s_smpi_mpi_vector_t* type_c = (s_smpi_mpi_vector_t*)type;
251 char* contiguous_vector_char = (char*)contiguous_vector;
252 char* noncontiguous_vector_char = (char*)noncontiguous_vector;
254 for (i = 0; i < type_c->block_count * count; i++) {
255 if (type_c->old_type->has_subtype == 0)
256 memcpy(noncontiguous_vector_char,
257 contiguous_vector_char, type_c->block_length * type_c->size_oldtype);
259 ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_vector_char,
260 noncontiguous_vector_char,
261 type_c->block_length,
262 type_c->old_type->substruct);
263 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
264 if((i+1)%type_c->block_count ==0)
265 noncontiguous_vector_char += type_c->block_length*smpi_datatype_get_extent(type_c->old_type);
267 noncontiguous_vector_char += type_c->block_stride*smpi_datatype_get_extent(type_c->old_type);
272 * Create a Sub type vector to be able to serialize and unserialize it
273 * the structure s_smpi_mpi_vector_t is derived from s_smpi_subtype which
274 * required the functions unserialize and serialize
277 s_smpi_mpi_vector_t* smpi_datatype_vector_create( int block_stride,
280 MPI_Datatype old_type,
282 s_smpi_mpi_vector_t *new_t= xbt_new(s_smpi_mpi_vector_t,1);
283 new_t->base.serialize = &serialize_vector;
284 new_t->base.unserialize = &unserialize_vector;
285 new_t->base.subtype_free = &free_vector;
286 new_t->block_stride = block_stride;
287 new_t->block_length = block_length;
288 new_t->block_count = block_count;
289 new_t->old_type = old_type;
290 new_t->size_oldtype = size_oldtype;
294 void smpi_datatype_create(MPI_Datatype* new_type, int size,int lb, int ub, int has_subtype,
295 void *struct_type, int flags){
296 MPI_Datatype new_t= xbt_new(s_smpi_mpi_datatype_t,1);
298 new_t->has_subtype = size>0? has_subtype:0;
301 new_t->flags = flags;
302 new_t->substruct = struct_type;
308 MC_ignore(&(new_t->in_use), sizeof(new_t->in_use));
312 void smpi_datatype_free(MPI_Datatype* type){
314 if((*type)->flags & DT_FLAG_PREDEFINED)return;
316 //if still used, mark for deletion
317 if((*type)->in_use!=0){
318 (*type)->flags |=DT_FLAG_DESTROYED;
322 if ((*type)->has_subtype == 1){
323 ((s_smpi_subtype_t *)(*type)->substruct)->subtype_free(type);
324 xbt_free((*type)->substruct);
330 void smpi_datatype_use(MPI_Datatype type){
331 if(type)type->in_use++;
335 MC_ignore(&(type->in_use), sizeof(type->in_use));
340 void smpi_datatype_unuse(MPI_Datatype type){
341 if(type && type->in_use-- == 0 && (type->flags & DT_FLAG_DESTROYED))
342 smpi_datatype_free(&type);
346 MC_ignore(&(type->in_use), sizeof(type->in_use));
354 Contiguous Implementation
359 * Copies noncontiguous data into contiguous memory.
360 * @param contiguous_hvector - output hvector
361 * @param noncontiguous_hvector - input hvector
362 * @param type - pointer contening :
363 * - stride - stride of between noncontiguous data, in bytes
364 * - block_length - the width or height of blocked matrix
365 * - count - the number of rows of matrix
367 void serialize_contiguous( const void *noncontiguous_hvector,
368 void *contiguous_hvector,
372 s_smpi_mpi_contiguous_t* type_c = (s_smpi_mpi_contiguous_t*)type;
373 char* contiguous_vector_char = (char*)contiguous_hvector;
374 char* noncontiguous_vector_char = (char*)noncontiguous_hvector+type_c->lb;
375 memcpy(contiguous_vector_char,
376 noncontiguous_vector_char, count* type_c->block_count * type_c->size_oldtype);
379 * Copies contiguous data into noncontiguous memory.
380 * @param noncontiguous_vector - output hvector
381 * @param contiguous_vector - input hvector
382 * @param type - pointer contening :
383 * - stride - stride of between noncontiguous data, in bytes
384 * - block_length - the width or height of blocked matrix
385 * - count - the number of rows of matrix
387 void unserialize_contiguous( const void *contiguous_vector,
388 void *noncontiguous_vector,
392 s_smpi_mpi_contiguous_t* type_c = (s_smpi_mpi_contiguous_t*)type;
393 char* contiguous_vector_char = (char*)contiguous_vector;
394 char* noncontiguous_vector_char = (char*)noncontiguous_vector+type_c->lb;
396 memcpy(noncontiguous_vector_char,
397 contiguous_vector_char, count* type_c->block_count * type_c->size_oldtype);
400 void free_contiguous(MPI_Datatype* d){
404 * Create a Sub type contiguous to be able to serialize and unserialize it
405 * the structure s_smpi_mpi_contiguous_t is derived from s_smpi_subtype which
406 * required the functions unserialize and serialize
409 s_smpi_mpi_contiguous_t* smpi_datatype_contiguous_create( MPI_Aint lb,
411 MPI_Datatype old_type,
413 s_smpi_mpi_contiguous_t *new_t= xbt_new(s_smpi_mpi_contiguous_t,1);
414 new_t->base.serialize = &serialize_contiguous;
415 new_t->base.unserialize = &unserialize_contiguous;
416 new_t->base.subtype_free = &free_contiguous;
418 new_t->block_count = block_count;
419 new_t->old_type = old_type;
420 new_t->size_oldtype = size_oldtype;
427 int smpi_datatype_contiguous(int count, MPI_Datatype old_type, MPI_Datatype* new_type, MPI_Aint lb)
430 if(old_type->has_subtype){
431 //handle this case as a hvector with stride equals to the extent of the datatype
432 return smpi_datatype_hvector(count, 1, smpi_datatype_get_extent(old_type), old_type, new_type);
435 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb,
438 smpi_datatype_size(old_type));
440 smpi_datatype_create(new_type,
441 count * smpi_datatype_size(old_type),
442 lb,lb + count * smpi_datatype_size(old_type),
443 1,subtype, DT_FLAG_CONTIGUOUS);
448 int smpi_datatype_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type)
451 if (blocklen<0) return MPI_ERR_ARG;
455 lb=smpi_datatype_lb(old_type);
456 ub=((count-1)*stride+blocklen-1)*smpi_datatype_get_extent(old_type)+smpi_datatype_ub(old_type);
458 if(old_type->has_subtype || stride != blocklen){
461 s_smpi_mpi_vector_t* subtype = smpi_datatype_vector_create( stride,
465 smpi_datatype_size(old_type));
466 smpi_datatype_create(new_type,
467 count * (blocklen) * smpi_datatype_size(old_type), lb,
474 /* in this situation the data are contignous thus it's not
475 * required to serialize and unserialize it*/
476 smpi_datatype_create(new_type, count * blocklen *
477 smpi_datatype_size(old_type), 0, ((count -1) * stride + blocklen)*
478 smpi_datatype_size(old_type),
481 DT_FLAG_VECTOR|DT_FLAG_CONTIGUOUS);
487 void free_vector(MPI_Datatype* d){
491 Hvector Implementation - Vector with stride in bytes
496 * Copies noncontiguous data into contiguous memory.
497 * @param contiguous_hvector - output hvector
498 * @param noncontiguous_hvector - input hvector
499 * @param type - pointer contening :
500 * - stride - stride of between noncontiguous data, in bytes
501 * - block_length - the width or height of blocked matrix
502 * - count - the number of rows of matrix
504 void serialize_hvector( const void *noncontiguous_hvector,
505 void *contiguous_hvector,
509 s_smpi_mpi_hvector_t* type_c = (s_smpi_mpi_hvector_t*)type;
511 char* contiguous_vector_char = (char*)contiguous_hvector;
512 char* noncontiguous_vector_char = (char*)noncontiguous_hvector;
514 for (i = 0; i < type_c->block_count * count; i++) {
515 if (type_c->old_type->has_subtype == 0)
516 memcpy(contiguous_vector_char,
517 noncontiguous_vector_char, type_c->block_length * type_c->size_oldtype);
519 ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_vector_char,
520 contiguous_vector_char,
521 type_c->block_length,
522 type_c->old_type->substruct);
524 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
525 if((i+1)%type_c->block_count ==0)
526 noncontiguous_vector_char += type_c->block_length*type_c->size_oldtype;
528 noncontiguous_vector_char += type_c->block_stride;
532 * Copies contiguous data into noncontiguous memory.
533 * @param noncontiguous_vector - output hvector
534 * @param contiguous_vector - input hvector
535 * @param type - pointer contening :
536 * - stride - stride of between noncontiguous data, in bytes
537 * - block_length - the width or height of blocked matrix
538 * - count - the number of rows of matrix
540 void unserialize_hvector( const void *contiguous_vector,
541 void *noncontiguous_vector,
545 s_smpi_mpi_hvector_t* type_c = (s_smpi_mpi_hvector_t*)type;
548 char* contiguous_vector_char = (char*)contiguous_vector;
549 char* noncontiguous_vector_char = (char*)noncontiguous_vector;
551 for (i = 0; i < type_c->block_count * count; i++) {
552 if (type_c->old_type->has_subtype == 0)
553 memcpy(noncontiguous_vector_char,
554 contiguous_vector_char, type_c->block_length * type_c->size_oldtype);
556 ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_vector_char,
557 noncontiguous_vector_char,
558 type_c->block_length,
559 type_c->old_type->substruct);
560 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
561 if((i+1)%type_c->block_count ==0)
562 noncontiguous_vector_char += type_c->block_length*type_c->size_oldtype;
564 noncontiguous_vector_char += type_c->block_stride;
569 * Create a Sub type vector to be able to serialize and unserialize it
570 * the structure s_smpi_mpi_vector_t is derived from s_smpi_subtype which
571 * required the functions unserialize and serialize
574 s_smpi_mpi_hvector_t* smpi_datatype_hvector_create( MPI_Aint block_stride,
577 MPI_Datatype old_type,
579 s_smpi_mpi_hvector_t *new_t= xbt_new(s_smpi_mpi_hvector_t,1);
580 new_t->base.serialize = &serialize_hvector;
581 new_t->base.unserialize = &unserialize_hvector;
582 new_t->base.subtype_free = &free_hvector;
583 new_t->block_stride = block_stride;
584 new_t->block_length = block_length;
585 new_t->block_count = block_count;
586 new_t->old_type = old_type;
587 new_t->size_oldtype = size_oldtype;
591 //do nothing for vector types
592 void free_hvector(MPI_Datatype* d){
595 int smpi_datatype_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type)
598 if (blocklen<0) return MPI_ERR_ARG;
602 lb=smpi_datatype_lb(old_type);
603 ub=((count-1)*stride)+(blocklen-1)*smpi_datatype_get_extent(old_type)+smpi_datatype_ub(old_type);
605 if(old_type->has_subtype || stride != blocklen*smpi_datatype_get_extent(old_type)){
606 s_smpi_mpi_hvector_t* subtype = smpi_datatype_hvector_create( stride,
610 smpi_datatype_size(old_type));
612 smpi_datatype_create(new_type, count * blocklen * smpi_datatype_size(old_type),
619 smpi_datatype_create(new_type, count * blocklen *
620 smpi_datatype_size(old_type),0,count * blocklen *
621 smpi_datatype_size(old_type),
624 DT_FLAG_VECTOR|DT_FLAG_CONTIGUOUS);
632 Indexed Implementation
636 * Copies noncontiguous data into contiguous memory.
637 * @param contiguous_indexed - output indexed
638 * @param noncontiguous_indexed - input indexed
639 * @param type - pointer contening :
640 * - block_lengths - the width or height of blocked matrix
641 * - block_indices - indices of each data, in element
642 * - count - the number of rows of matrix
644 void serialize_indexed( const void *noncontiguous_indexed,
645 void *contiguous_indexed,
649 s_smpi_mpi_indexed_t* type_c = (s_smpi_mpi_indexed_t*)type;
651 char* contiguous_indexed_char = (char*)contiguous_indexed;
652 char* noncontiguous_indexed_char = (char*)noncontiguous_indexed+type_c->block_indices[0] * type_c->size_oldtype;
653 for(j=0; j<count;j++){
654 for (i = 0; i < type_c->block_count; i++) {
655 if (type_c->old_type->has_subtype == 0)
656 memcpy(contiguous_indexed_char,
657 noncontiguous_indexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
659 ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_indexed_char,
660 contiguous_indexed_char,
661 type_c->block_lengths[i],
662 type_c->old_type->substruct);
665 contiguous_indexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
666 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);
667 else noncontiguous_indexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
669 noncontiguous_indexed=(void*)noncontiguous_indexed_char;
673 * Copies contiguous data into noncontiguous memory.
674 * @param noncontiguous_indexed - output indexed
675 * @param contiguous_indexed - input indexed
676 * @param type - pointer contening :
677 * - block_lengths - the width or height of blocked matrix
678 * - block_indices - indices of each data, in element
679 * - count - the number of rows of matrix
681 void unserialize_indexed( const void *contiguous_indexed,
682 void *noncontiguous_indexed,
687 s_smpi_mpi_indexed_t* type_c = (s_smpi_mpi_indexed_t*)type;
689 char* contiguous_indexed_char = (char*)contiguous_indexed;
690 char* noncontiguous_indexed_char = (char*)noncontiguous_indexed+type_c->block_indices[0]*smpi_datatype_get_extent(type_c->old_type);
691 for(j=0; j<count;j++){
692 for (i = 0; i < type_c->block_count; i++) {
693 if (type_c->old_type->has_subtype == 0)
694 memcpy(noncontiguous_indexed_char ,
695 contiguous_indexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
697 ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_indexed_char,
698 noncontiguous_indexed_char,
699 type_c->block_lengths[i],
700 type_c->old_type->substruct);
702 contiguous_indexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
703 if (i<type_c->block_count-1)
704 noncontiguous_indexed_char = (char*)noncontiguous_indexed + type_c->block_indices[i+1]*smpi_datatype_get_extent(type_c->old_type);
705 else noncontiguous_indexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
707 noncontiguous_indexed=(void*)noncontiguous_indexed_char;
711 void free_indexed(MPI_Datatype* type){
712 xbt_free(((s_smpi_mpi_indexed_t *)(*type)->substruct)->block_lengths);
713 xbt_free(((s_smpi_mpi_indexed_t *)(*type)->substruct)->block_indices);
717 * Create a Sub type indexed to be able to serialize and unserialize it
718 * the structure s_smpi_mpi_indexed_t is derived from s_smpi_subtype which
719 * required the functions unserialize and serialize
721 s_smpi_mpi_indexed_t* smpi_datatype_indexed_create( int* block_lengths,
724 MPI_Datatype old_type,
726 s_smpi_mpi_indexed_t *new_t= xbt_new(s_smpi_mpi_indexed_t,1);
727 new_t->base.serialize = &serialize_indexed;
728 new_t->base.unserialize = &unserialize_indexed;
729 new_t->base.subtype_free = &free_indexed;
730 //TODO : add a custom function for each time to clean these
731 new_t->block_lengths= xbt_new(int, block_count);
732 new_t->block_indices= xbt_new(int, block_count);
734 for(i=0;i<block_count;i++){
735 new_t->block_lengths[i]=block_lengths[i];
736 new_t->block_indices[i]=block_indices[i];
738 new_t->block_count = block_count;
739 new_t->old_type = old_type;
740 new_t->size_oldtype = size_oldtype;
745 int smpi_datatype_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
754 lb=indices[0]*smpi_datatype_get_extent(old_type);
755 ub=indices[0]*smpi_datatype_get_extent(old_type) + blocklens[0]*smpi_datatype_ub(old_type);
758 for(i=0; i< count; i++){
761 size += blocklens[i];
763 if(indices[i]*smpi_datatype_get_extent(old_type)+smpi_datatype_lb(old_type)<lb)
764 lb = indices[i]*smpi_datatype_get_extent(old_type)+smpi_datatype_lb(old_type);
765 if(indices[i]*smpi_datatype_get_extent(old_type)+blocklens[i]*smpi_datatype_ub(old_type)>ub)
766 ub = indices[i]*smpi_datatype_get_extent(old_type)+blocklens[i]*smpi_datatype_ub(old_type);
768 if ( (i< count -1) && (indices[i]+blocklens[i] != indices[i+1]) )contiguous=0;
770 if (old_type->has_subtype == 1)
774 s_smpi_mpi_indexed_t* subtype = smpi_datatype_indexed_create( blocklens,
778 smpi_datatype_size(old_type));
779 smpi_datatype_create(new_type, size *
780 smpi_datatype_size(old_type),lb,ub,1, subtype, DT_FLAG_DATA);
782 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb,
785 smpi_datatype_size(old_type));
786 smpi_datatype_create(new_type, size *
787 smpi_datatype_size(old_type),lb,ub,1, subtype, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
795 Hindexed Implementation - Indexed with indices in bytes
799 * Copies noncontiguous data into contiguous memory.
800 * @param contiguous_hindexed - output hindexed
801 * @param noncontiguous_hindexed - input hindexed
802 * @param type - pointer contening :
803 * - block_lengths - the width or height of blocked matrix
804 * - block_indices - indices of each data, in bytes
805 * - count - the number of rows of matrix
807 void serialize_hindexed( const void *noncontiguous_hindexed,
808 void *contiguous_hindexed,
812 s_smpi_mpi_hindexed_t* type_c = (s_smpi_mpi_hindexed_t*)type;
814 char* contiguous_hindexed_char = (char*)contiguous_hindexed;
815 char* noncontiguous_hindexed_char = (char*)noncontiguous_hindexed+ type_c->block_indices[0];
816 for(j=0; j<count;j++){
817 for (i = 0; i < type_c->block_count; i++) {
818 if (type_c->old_type->has_subtype == 0)
819 memcpy(contiguous_hindexed_char,
820 noncontiguous_hindexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
822 ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_hindexed_char,
823 contiguous_hindexed_char,
824 type_c->block_lengths[i],
825 type_c->old_type->substruct);
827 contiguous_hindexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
828 if (i<type_c->block_count-1)noncontiguous_hindexed_char = (char*)noncontiguous_hindexed + type_c->block_indices[i+1];
829 else noncontiguous_hindexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
831 noncontiguous_hindexed=(void*)noncontiguous_hindexed_char;
835 * Copies contiguous data into noncontiguous memory.
836 * @param noncontiguous_hindexed - output hindexed
837 * @param contiguous_hindexed - input hindexed
838 * @param type - pointer contening :
839 * - block_lengths - the width or height of blocked matrix
840 * - block_indices - indices of each data, in bytes
841 * - count - the number of rows of matrix
843 void unserialize_hindexed( const void *contiguous_hindexed,
844 void *noncontiguous_hindexed,
848 s_smpi_mpi_hindexed_t* type_c = (s_smpi_mpi_hindexed_t*)type;
851 char* contiguous_hindexed_char = (char*)contiguous_hindexed;
852 char* noncontiguous_hindexed_char = (char*)noncontiguous_hindexed+ type_c->block_indices[0];
853 for(j=0; j<count;j++){
854 for (i = 0; i < type_c->block_count; i++) {
855 if (type_c->old_type->has_subtype == 0)
856 memcpy(noncontiguous_hindexed_char,
857 contiguous_hindexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
859 ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_hindexed_char,
860 noncontiguous_hindexed_char,
861 type_c->block_lengths[i],
862 type_c->old_type->substruct);
864 contiguous_hindexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
865 if (i<type_c->block_count-1)noncontiguous_hindexed_char = (char*)noncontiguous_hindexed + type_c->block_indices[i+1];
866 else noncontiguous_hindexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
868 noncontiguous_hindexed=(void*)noncontiguous_hindexed_char;
872 void free_hindexed(MPI_Datatype* type){
873 xbt_free(((s_smpi_mpi_hindexed_t *)(*type)->substruct)->block_lengths);
874 xbt_free(((s_smpi_mpi_hindexed_t *)(*type)->substruct)->block_indices);
878 * Create a Sub type hindexed to be able to serialize and unserialize it
879 * the structure s_smpi_mpi_hindexed_t is derived from s_smpi_subtype which
880 * required the functions unserialize and serialize
882 s_smpi_mpi_hindexed_t* smpi_datatype_hindexed_create( int* block_lengths,
883 MPI_Aint* block_indices,
885 MPI_Datatype old_type,
887 s_smpi_mpi_hindexed_t *new_t= xbt_new(s_smpi_mpi_hindexed_t,1);
888 new_t->base.serialize = &serialize_hindexed;
889 new_t->base.unserialize = &unserialize_hindexed;
890 new_t->base.subtype_free = &free_hindexed;
891 //TODO : add a custom function for each time to clean these
892 new_t->block_lengths= xbt_new(int, block_count);
893 new_t->block_indices= xbt_new(MPI_Aint, block_count);
895 for(i=0;i<block_count;i++){
896 new_t->block_lengths[i]=block_lengths[i];
897 new_t->block_indices[i]=block_indices[i];
899 new_t->block_count = block_count;
900 new_t->old_type = old_type;
901 new_t->size_oldtype = size_oldtype;
906 int smpi_datatype_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
915 lb=indices[0] + smpi_datatype_lb(old_type);
916 ub=indices[0] + blocklens[0]*smpi_datatype_ub(old_type);
918 for(i=0; i< count; i++){
921 size += blocklens[i];
923 if(indices[i]+smpi_datatype_lb(old_type)<lb) lb = indices[i]+smpi_datatype_lb(old_type);
924 if(indices[i]+blocklens[i]*smpi_datatype_ub(old_type)>ub) ub = indices[i]+blocklens[i]*smpi_datatype_ub(old_type);
926 if ( (i< count -1) && (indices[i]+blocklens[i]*smpi_datatype_size(old_type) != indices[i+1]) )contiguous=0;
928 if (old_type->has_subtype == 1 || lb!=0)
932 s_smpi_mpi_hindexed_t* subtype = smpi_datatype_hindexed_create( blocklens,
936 smpi_datatype_size(old_type));
937 smpi_datatype_create(new_type, size * smpi_datatype_size(old_type),
940 ,1, subtype, DT_FLAG_DATA);
942 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb,
945 smpi_datatype_size(old_type));
946 smpi_datatype_create(new_type, size * smpi_datatype_size(old_type),
947 0,size * smpi_datatype_size(old_type),
948 1, subtype, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
956 struct Implementation - Indexed with indices in bytes
960 * Copies noncontiguous data into contiguous memory.
961 * @param contiguous_struct - output struct
962 * @param noncontiguous_struct - input struct
963 * @param type - pointer contening :
964 * - stride - stride of between noncontiguous data
965 * - block_length - the width or height of blocked matrix
966 * - count - the number of rows of matrix
968 void serialize_struct( const void *noncontiguous_struct,
969 void *contiguous_struct,
973 s_smpi_mpi_struct_t* type_c = (s_smpi_mpi_struct_t*)type;
975 char* contiguous_struct_char = (char*)contiguous_struct;
976 char* noncontiguous_struct_char = (char*)noncontiguous_struct+ type_c->block_indices[0];
977 for(j=0; j<count;j++){
978 for (i = 0; i < type_c->block_count; i++) {
979 if (type_c->old_types[i]->has_subtype == 0)
980 memcpy(contiguous_struct_char,
981 noncontiguous_struct_char, type_c->block_lengths[i] * smpi_datatype_size(type_c->old_types[i]));
983 ((s_smpi_subtype_t*)type_c->old_types[i]->substruct)->serialize( noncontiguous_struct_char,
984 contiguous_struct_char,
985 type_c->block_lengths[i],
986 type_c->old_types[i]->substruct);
989 contiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_size(type_c->old_types[i]);
990 if (i<type_c->block_count-1)noncontiguous_struct_char = (char*)noncontiguous_struct + type_c->block_indices[i+1];
991 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 ?
993 noncontiguous_struct=(void*)noncontiguous_struct_char;
997 * Copies contiguous data into noncontiguous memory.
998 * @param noncontiguous_struct - output struct
999 * @param contiguous_struct - input struct
1000 * @param type - pointer contening :
1001 * - stride - stride of between noncontiguous data
1002 * - block_length - the width or height of blocked matrix
1003 * - count - the number of rows of matrix
1005 void unserialize_struct( const void *contiguous_struct,
1006 void *noncontiguous_struct,
1010 s_smpi_mpi_struct_t* type_c = (s_smpi_mpi_struct_t*)type;
1013 char* contiguous_struct_char = (char*)contiguous_struct;
1014 char* noncontiguous_struct_char = (char*)noncontiguous_struct+ type_c->block_indices[0];
1015 for(j=0; j<count;j++){
1016 for (i = 0; i < type_c->block_count; i++) {
1017 if (type_c->old_types[i]->has_subtype == 0)
1018 memcpy(noncontiguous_struct_char,
1019 contiguous_struct_char, type_c->block_lengths[i] * smpi_datatype_size(type_c->old_types[i]));
1021 ((s_smpi_subtype_t*)type_c->old_types[i]->substruct)->unserialize( contiguous_struct_char,
1022 noncontiguous_struct_char,
1023 type_c->block_lengths[i],
1024 type_c->old_types[i]->substruct);
1026 contiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_size(type_c->old_types[i]);
1027 if (i<type_c->block_count-1)noncontiguous_struct_char = (char*)noncontiguous_struct + type_c->block_indices[i+1];
1028 else noncontiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_types[i]);
1030 noncontiguous_struct=(void*)noncontiguous_struct_char;
1035 void free_struct(MPI_Datatype* type){
1036 xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->block_lengths);
1037 xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->block_indices);
1038 xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->old_types);
1042 * Create a Sub type struct to be able to serialize and unserialize it
1043 * the structure s_smpi_mpi_struct_t is derived from s_smpi_subtype which
1044 * required the functions unserialize and serialize
1046 s_smpi_mpi_struct_t* smpi_datatype_struct_create( int* block_lengths,
1047 MPI_Aint* block_indices,
1049 MPI_Datatype* old_types){
1050 s_smpi_mpi_struct_t *new_t= xbt_new(s_smpi_mpi_struct_t,1);
1051 new_t->base.serialize = &serialize_struct;
1052 new_t->base.unserialize = &unserialize_struct;
1053 new_t->base.subtype_free = &free_struct;
1054 //TODO : add a custom function for each time to clean these
1055 new_t->block_lengths= xbt_new(int, block_count);
1056 new_t->block_indices= xbt_new(MPI_Aint, block_count);
1057 new_t->old_types= xbt_new(MPI_Datatype, block_count);
1059 for(i=0;i<block_count;i++){
1060 new_t->block_lengths[i]=block_lengths[i];
1061 new_t->block_indices[i]=block_indices[i];
1062 new_t->old_types[i]=old_types[i];
1064 //new_t->block_lengths = block_lengths;
1065 //new_t->block_indices = block_indices;
1066 new_t->block_count = block_count;
1067 //new_t->old_types = old_types;
1072 int smpi_datatype_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type)
1081 lb=indices[0] + smpi_datatype_lb(old_types[0]);
1082 ub=indices[0] + blocklens[0]*smpi_datatype_ub(old_types[0]);
1086 for(i=0; i< count; i++){
1089 if (old_types[i]->has_subtype == 1)
1092 size += blocklens[i]*smpi_datatype_size(old_types[i]);
1093 if (old_types[i]==MPI_LB){
1097 if (old_types[i]==MPI_UB){
1102 if(!forced_lb && indices[i]+smpi_datatype_lb(old_types[i])<lb) lb = indices[i];
1103 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]);
1105 if ( (i< count -1) && (indices[i]+blocklens[i]*smpi_datatype_size(old_types[i]) != indices[i+1]) )contiguous=0;
1109 s_smpi_mpi_struct_t* subtype = smpi_datatype_struct_create( blocklens,
1114 smpi_datatype_create(new_type, size, lb, ub,1, subtype, DT_FLAG_DATA);
1116 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb,
1120 smpi_datatype_create(new_type, size, lb, ub,1, subtype, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
1125 void smpi_datatype_commit(MPI_Datatype *datatype)
1127 (*datatype)->flags= ((*datatype)->flags | DT_FLAG_COMMITED);
1130 typedef struct s_smpi_mpi_op {
1131 MPI_User_function *func;
1135 #define MAX_OP(a, b) (b) = (a) < (b) ? (b) : (a)
1136 #define MIN_OP(a, b) (b) = (a) < (b) ? (a) : (b)
1137 #define SUM_OP(a, b) (b) += (a)
1138 #define PROD_OP(a, b) (b) *= (a)
1139 #define LAND_OP(a, b) (b) = (a) && (b)
1140 #define LOR_OP(a, b) (b) = (a) || (b)
1141 #define LXOR_OP(a, b) (b) = (!(a) && (b)) || ((a) && !(b))
1142 #define BAND_OP(a, b) (b) &= (a)
1143 #define BOR_OP(a, b) (b) |= (a)
1144 #define BXOR_OP(a, b) (b) ^= (a)
1145 #define MAXLOC_OP(a, b) (b) = (a.value) < (b.value) ? (b) : (a)
1146 #define MINLOC_OP(a, b) (b) = (a.value) < (b.value) ? (a) : (b)
1147 //TODO : MINLOC & MAXLOC
1149 #define APPLY_FUNC(a, b, length, type, func) \
1152 type* x = (type*)(a); \
1153 type* y = (type*)(b); \
1154 for(i = 0; i < *(length); i++) { \
1159 static void max_func(void *a, void *b, int *length,
1160 MPI_Datatype * datatype)
1162 if (*datatype == MPI_CHAR) {
1163 APPLY_FUNC(a, b, length, char, MAX_OP);
1164 } else if (*datatype == MPI_SHORT) {
1165 APPLY_FUNC(a, b, length, short, MAX_OP);
1166 } else if (*datatype == MPI_INT) {
1167 APPLY_FUNC(a, b, length, int, MAX_OP);
1168 } else if (*datatype == MPI_LONG) {
1169 APPLY_FUNC(a, b, length, long, MAX_OP);
1170 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1171 APPLY_FUNC(a, b, length, unsigned short, MAX_OP);
1172 } else if (*datatype == MPI_UNSIGNED) {
1173 APPLY_FUNC(a, b, length, unsigned int, MAX_OP);
1174 } else if (*datatype == MPI_UNSIGNED_LONG) {
1175 APPLY_FUNC(a, b, length, unsigned long, MAX_OP);
1176 } else if (*datatype == MPI_FLOAT) {
1177 APPLY_FUNC(a, b, length, float, MAX_OP);
1178 } else if (*datatype == MPI_DOUBLE) {
1179 APPLY_FUNC(a, b, length, double, MAX_OP);
1180 } else if (*datatype == MPI_LONG_DOUBLE) {
1181 APPLY_FUNC(a, b, length, long double, MAX_OP);
1185 static void min_func(void *a, void *b, int *length,
1186 MPI_Datatype * datatype)
1188 if (*datatype == MPI_CHAR) {
1189 APPLY_FUNC(a, b, length, char, MIN_OP);
1190 } else if (*datatype == MPI_SHORT) {
1191 APPLY_FUNC(a, b, length, short, MIN_OP);
1192 } else if (*datatype == MPI_INT) {
1193 APPLY_FUNC(a, b, length, int, MIN_OP);
1194 } else if (*datatype == MPI_LONG) {
1195 APPLY_FUNC(a, b, length, long, MIN_OP);
1196 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1197 APPLY_FUNC(a, b, length, unsigned short, MIN_OP);
1198 } else if (*datatype == MPI_UNSIGNED) {
1199 APPLY_FUNC(a, b, length, unsigned int, MIN_OP);
1200 } else if (*datatype == MPI_UNSIGNED_LONG) {
1201 APPLY_FUNC(a, b, length, unsigned long, MIN_OP);
1202 } else if (*datatype == MPI_FLOAT) {
1203 APPLY_FUNC(a, b, length, float, MIN_OP);
1204 } else if (*datatype == MPI_DOUBLE) {
1205 APPLY_FUNC(a, b, length, double, MIN_OP);
1206 } else if (*datatype == MPI_LONG_DOUBLE) {
1207 APPLY_FUNC(a, b, length, long double, MIN_OP);
1211 static void sum_func(void *a, void *b, int *length,
1212 MPI_Datatype * datatype)
1214 if (*datatype == MPI_CHAR) {
1215 APPLY_FUNC(a, b, length, char, SUM_OP);
1216 } else if (*datatype == MPI_SHORT) {
1217 APPLY_FUNC(a, b, length, short, SUM_OP);
1218 } else if (*datatype == MPI_INT) {
1219 APPLY_FUNC(a, b, length, int, SUM_OP);
1220 } else if (*datatype == MPI_LONG) {
1221 APPLY_FUNC(a, b, length, long, SUM_OP);
1222 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1223 APPLY_FUNC(a, b, length, unsigned short, SUM_OP);
1224 } else if (*datatype == MPI_UNSIGNED) {
1225 APPLY_FUNC(a, b, length, unsigned int, SUM_OP);
1226 } else if (*datatype == MPI_UNSIGNED_LONG) {
1227 APPLY_FUNC(a, b, length, unsigned long, SUM_OP);
1228 } else if (*datatype == MPI_FLOAT) {
1229 APPLY_FUNC(a, b, length, float, SUM_OP);
1230 } else if (*datatype == MPI_DOUBLE) {
1231 APPLY_FUNC(a, b, length, double, SUM_OP);
1232 } else if (*datatype == MPI_LONG_DOUBLE) {
1233 APPLY_FUNC(a, b, length, long double, SUM_OP);
1234 } else if (*datatype == MPI_C_FLOAT_COMPLEX) {
1235 APPLY_FUNC(a, b, length, float _Complex, SUM_OP);
1236 } else if (*datatype == MPI_C_DOUBLE_COMPLEX) {
1237 APPLY_FUNC(a, b, length, double _Complex, SUM_OP);
1238 } else if (*datatype == MPI_C_LONG_DOUBLE_COMPLEX) {
1239 APPLY_FUNC(a, b, length, long double _Complex, SUM_OP);
1243 static void prod_func(void *a, void *b, int *length,
1244 MPI_Datatype * datatype)
1246 if (*datatype == MPI_CHAR) {
1247 APPLY_FUNC(a, b, length, char, PROD_OP);
1248 } else if (*datatype == MPI_SHORT) {
1249 APPLY_FUNC(a, b, length, short, PROD_OP);
1250 } else if (*datatype == MPI_INT) {
1251 APPLY_FUNC(a, b, length, int, PROD_OP);
1252 } else if (*datatype == MPI_LONG) {
1253 APPLY_FUNC(a, b, length, long, PROD_OP);
1254 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1255 APPLY_FUNC(a, b, length, unsigned short, PROD_OP);
1256 } else if (*datatype == MPI_UNSIGNED) {
1257 APPLY_FUNC(a, b, length, unsigned int, PROD_OP);
1258 } else if (*datatype == MPI_UNSIGNED_LONG) {
1259 APPLY_FUNC(a, b, length, unsigned long, PROD_OP);
1260 } else if (*datatype == MPI_FLOAT) {
1261 APPLY_FUNC(a, b, length, float, PROD_OP);
1262 } else if (*datatype == MPI_DOUBLE) {
1263 APPLY_FUNC(a, b, length, double, PROD_OP);
1264 } else if (*datatype == MPI_LONG_DOUBLE) {
1265 APPLY_FUNC(a, b, length, long double, PROD_OP);
1266 } else if (*datatype == MPI_C_FLOAT_COMPLEX) {
1267 APPLY_FUNC(a, b, length, float _Complex, PROD_OP);
1268 } else if (*datatype == MPI_C_DOUBLE_COMPLEX) {
1269 APPLY_FUNC(a, b, length, double _Complex, PROD_OP);
1270 } else if (*datatype == MPI_C_LONG_DOUBLE_COMPLEX) {
1271 APPLY_FUNC(a, b, length, long double _Complex, PROD_OP);
1275 static void land_func(void *a, void *b, int *length,
1276 MPI_Datatype * datatype)
1278 if (*datatype == MPI_CHAR) {
1279 APPLY_FUNC(a, b, length, char, LAND_OP);
1280 } else if (*datatype == MPI_SHORT) {
1281 APPLY_FUNC(a, b, length, short, LAND_OP);
1282 } else if (*datatype == MPI_INT) {
1283 APPLY_FUNC(a, b, length, int, LAND_OP);
1284 } else if (*datatype == MPI_LONG) {
1285 APPLY_FUNC(a, b, length, long, LAND_OP);
1286 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1287 APPLY_FUNC(a, b, length, unsigned short, LAND_OP);
1288 } else if (*datatype == MPI_UNSIGNED) {
1289 APPLY_FUNC(a, b, length, unsigned int, LAND_OP);
1290 } else if (*datatype == MPI_UNSIGNED_LONG) {
1291 APPLY_FUNC(a, b, length, unsigned long, LAND_OP);
1292 } else if (*datatype == MPI_C_BOOL) {
1293 APPLY_FUNC(a, b, length, _Bool, LAND_OP);
1297 static void lor_func(void *a, void *b, int *length,
1298 MPI_Datatype * datatype)
1300 if (*datatype == MPI_CHAR) {
1301 APPLY_FUNC(a, b, length, char, LOR_OP);
1302 } else if (*datatype == MPI_SHORT) {
1303 APPLY_FUNC(a, b, length, short, LOR_OP);
1304 } else if (*datatype == MPI_INT) {
1305 APPLY_FUNC(a, b, length, int, LOR_OP);
1306 } else if (*datatype == MPI_LONG) {
1307 APPLY_FUNC(a, b, length, long, LOR_OP);
1308 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1309 APPLY_FUNC(a, b, length, unsigned short, LOR_OP);
1310 } else if (*datatype == MPI_UNSIGNED) {
1311 APPLY_FUNC(a, b, length, unsigned int, LOR_OP);
1312 } else if (*datatype == MPI_UNSIGNED_LONG) {
1313 APPLY_FUNC(a, b, length, unsigned long, LOR_OP);
1314 } else if (*datatype == MPI_C_BOOL) {
1315 APPLY_FUNC(a, b, length, _Bool, LOR_OP);
1319 static void lxor_func(void *a, void *b, int *length,
1320 MPI_Datatype * datatype)
1322 if (*datatype == MPI_CHAR) {
1323 APPLY_FUNC(a, b, length, char, LXOR_OP);
1324 } else if (*datatype == MPI_SHORT) {
1325 APPLY_FUNC(a, b, length, short, LXOR_OP);
1326 } else if (*datatype == MPI_INT) {
1327 APPLY_FUNC(a, b, length, int, LXOR_OP);
1328 } else if (*datatype == MPI_LONG) {
1329 APPLY_FUNC(a, b, length, long, LXOR_OP);
1330 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1331 APPLY_FUNC(a, b, length, unsigned short, LXOR_OP);
1332 } else if (*datatype == MPI_UNSIGNED) {
1333 APPLY_FUNC(a, b, length, unsigned int, LXOR_OP);
1334 } else if (*datatype == MPI_UNSIGNED_LONG) {
1335 APPLY_FUNC(a, b, length, unsigned long, LXOR_OP);
1336 } else if (*datatype == MPI_C_BOOL) {
1337 APPLY_FUNC(a, b, length, _Bool, LXOR_OP);
1341 static void band_func(void *a, void *b, int *length,
1342 MPI_Datatype * datatype)
1344 if (*datatype == MPI_CHAR) {
1345 APPLY_FUNC(a, b, length, char, BAND_OP);
1347 if (*datatype == MPI_SHORT) {
1348 APPLY_FUNC(a, b, length, short, BAND_OP);
1349 } else if (*datatype == MPI_INT) {
1350 APPLY_FUNC(a, b, length, int, BAND_OP);
1351 } else if (*datatype == MPI_LONG) {
1352 APPLY_FUNC(a, b, length, long, BAND_OP);
1353 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1354 APPLY_FUNC(a, b, length, unsigned short, BAND_OP);
1355 } else if (*datatype == MPI_UNSIGNED) {
1356 APPLY_FUNC(a, b, length, unsigned int, BAND_OP);
1357 } else if (*datatype == MPI_UNSIGNED_LONG) {
1358 APPLY_FUNC(a, b, length, unsigned long, BAND_OP);
1359 } else if (*datatype == MPI_BYTE) {
1360 APPLY_FUNC(a, b, length, uint8_t, BAND_OP);
1364 static void bor_func(void *a, void *b, int *length,
1365 MPI_Datatype * datatype)
1367 if (*datatype == MPI_CHAR) {
1368 APPLY_FUNC(a, b, length, char, BOR_OP);
1369 } else if (*datatype == MPI_SHORT) {
1370 APPLY_FUNC(a, b, length, short, BOR_OP);
1371 } else if (*datatype == MPI_INT) {
1372 APPLY_FUNC(a, b, length, int, BOR_OP);
1373 } else if (*datatype == MPI_LONG) {
1374 APPLY_FUNC(a, b, length, long, BOR_OP);
1375 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1376 APPLY_FUNC(a, b, length, unsigned short, BOR_OP);
1377 } else if (*datatype == MPI_UNSIGNED) {
1378 APPLY_FUNC(a, b, length, unsigned int, BOR_OP);
1379 } else if (*datatype == MPI_UNSIGNED_LONG) {
1380 APPLY_FUNC(a, b, length, unsigned long, BOR_OP);
1381 } else if (*datatype == MPI_BYTE) {
1382 APPLY_FUNC(a, b, length, uint8_t, BOR_OP);
1386 static void bxor_func(void *a, void *b, int *length,
1387 MPI_Datatype * datatype)
1389 if (*datatype == MPI_CHAR) {
1390 APPLY_FUNC(a, b, length, char, BXOR_OP);
1391 } else if (*datatype == MPI_SHORT) {
1392 APPLY_FUNC(a, b, length, short, BXOR_OP);
1393 } else if (*datatype == MPI_INT) {
1394 APPLY_FUNC(a, b, length, int, BXOR_OP);
1395 } else if (*datatype == MPI_LONG) {
1396 APPLY_FUNC(a, b, length, long, BXOR_OP);
1397 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1398 APPLY_FUNC(a, b, length, unsigned short, BXOR_OP);
1399 } else if (*datatype == MPI_UNSIGNED) {
1400 APPLY_FUNC(a, b, length, unsigned int, BXOR_OP);
1401 } else if (*datatype == MPI_UNSIGNED_LONG) {
1402 APPLY_FUNC(a, b, length, unsigned long, BXOR_OP);
1403 } else if (*datatype == MPI_BYTE) {
1404 APPLY_FUNC(a, b, length, uint8_t, BXOR_OP);
1408 static void minloc_func(void *a, void *b, int *length,
1409 MPI_Datatype * datatype)
1411 if (*datatype == MPI_FLOAT_INT) {
1412 APPLY_FUNC(a, b, length, float_int, MINLOC_OP);
1413 } else if (*datatype == MPI_LONG_INT) {
1414 APPLY_FUNC(a, b, length, long_int, MINLOC_OP);
1415 } else if (*datatype == MPI_DOUBLE_INT) {
1416 APPLY_FUNC(a, b, length, double_int, MINLOC_OP);
1417 } else if (*datatype == MPI_SHORT_INT) {
1418 APPLY_FUNC(a, b, length, short_int, MINLOC_OP);
1419 } else if (*datatype == MPI_2INT) {
1420 APPLY_FUNC(a, b, length, int_int, MINLOC_OP);
1421 } else if (*datatype == MPI_LONG_DOUBLE_INT) {
1422 APPLY_FUNC(a, b, length, long_double_int, MINLOC_OP);
1423 } else if (*datatype == MPI_2FLOAT) {
1424 APPLY_FUNC(a, b, length, float_float, MINLOC_OP);
1425 } else if (*datatype == MPI_2DOUBLE) {
1426 APPLY_FUNC(a, b, length, double_double, MINLOC_OP);
1430 static void maxloc_func(void *a, void *b, int *length,
1431 MPI_Datatype * datatype)
1433 if (*datatype == MPI_FLOAT_INT) {
1434 APPLY_FUNC(a, b, length, float_int, MAXLOC_OP);
1435 } else if (*datatype == MPI_LONG_INT) {
1436 APPLY_FUNC(a, b, length, long_int, MAXLOC_OP);
1437 } else if (*datatype == MPI_DOUBLE_INT) {
1438 APPLY_FUNC(a, b, length, double_int, MAXLOC_OP);
1439 } else if (*datatype == MPI_SHORT_INT) {
1440 APPLY_FUNC(a, b, length, short_int, MAXLOC_OP);
1441 } else if (*datatype == MPI_2INT) {
1442 APPLY_FUNC(a, b, length, int_int, MAXLOC_OP);
1443 } else if (*datatype == MPI_LONG_DOUBLE_INT) {
1444 APPLY_FUNC(a, b, length, long_double_int, MAXLOC_OP);
1445 } else if (*datatype == MPI_2FLOAT) {
1446 APPLY_FUNC(a, b, length, float_float, MAXLOC_OP);
1447 } else if (*datatype == MPI_2DOUBLE) {
1448 APPLY_FUNC(a, b, length, double_double, MAXLOC_OP);
1453 #define CREATE_MPI_OP(name, func) \
1454 static s_smpi_mpi_op_t mpi_##name = { &(func) /* func */, TRUE }; \
1455 MPI_Op name = &mpi_##name;
1457 CREATE_MPI_OP(MPI_MAX, max_func);
1458 CREATE_MPI_OP(MPI_MIN, min_func);
1459 CREATE_MPI_OP(MPI_SUM, sum_func);
1460 CREATE_MPI_OP(MPI_PROD, prod_func);
1461 CREATE_MPI_OP(MPI_LAND, land_func);
1462 CREATE_MPI_OP(MPI_LOR, lor_func);
1463 CREATE_MPI_OP(MPI_LXOR, lxor_func);
1464 CREATE_MPI_OP(MPI_BAND, band_func);
1465 CREATE_MPI_OP(MPI_BOR, bor_func);
1466 CREATE_MPI_OP(MPI_BXOR, bxor_func);
1467 CREATE_MPI_OP(MPI_MAXLOC, maxloc_func);
1468 CREATE_MPI_OP(MPI_MINLOC, minloc_func);
1470 MPI_Op smpi_op_new(MPI_User_function * function, int commute)
1473 op = xbt_new(s_smpi_mpi_op_t, 1);
1474 op->func = function;
1475 op-> is_commute = commute;
1479 int smpi_op_is_commute(MPI_Op op)
1481 return (op==MPI_OP_NULL) ? 1 : op-> is_commute;
1484 void smpi_op_destroy(MPI_Op op)
1489 void smpi_op_apply(MPI_Op op, void *invec, void *inoutvec, int *len,
1490 MPI_Datatype * datatype)
1492 if(smpi_privatize_global_variables){ //we need to switch here, as the called function may silently touch global variables
1493 XBT_VERB("Applying operation, switch to the right data frame ");
1494 switch_data_segment(smpi_process_index());
1497 if(!_xbt_replay_is_active())
1498 op->func(invec, inoutvec, len, datatype);