1 /* smpi_mpi_dt.c -- MPI primitives to handle datatypes */
2 /* FIXME: a very incomplete implementation */
4 /* Copyright (c) 2009, 2010. The SimGrid Team.
5 * All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
15 #include "smpi_mpi_dt_private.h"
17 #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;
306 MC_ignore(&(new_t->in_use), sizeof(new_t->in_use));
309 void smpi_datatype_free(MPI_Datatype* type){
311 if((*type)->flags & DT_FLAG_PREDEFINED)return;
313 //if still used, mark for deletion
314 if((*type)->in_use!=0){
315 (*type)->flags |=DT_FLAG_DESTROYED;
319 if ((*type)->has_subtype == 1){
320 ((s_smpi_subtype_t *)(*type)->substruct)->subtype_free(type);
321 xbt_free((*type)->substruct);
327 void smpi_datatype_use(MPI_Datatype type){
328 if(type)type->in_use++;
331 MC_ignore(&(type->in_use), sizeof(type->in_use));
335 void smpi_datatype_unuse(MPI_Datatype type){
336 if(type && type->in_use-- == 0 && (type->flags & DT_FLAG_DESTROYED))
337 smpi_datatype_free(&type);
340 MC_ignore(&(type->in_use), sizeof(type->in_use));
347 Contiguous Implementation
352 * Copies noncontiguous data into contiguous memory.
353 * @param contiguous_hvector - output hvector
354 * @param noncontiguous_hvector - input hvector
355 * @param type - pointer contening :
356 * - stride - stride of between noncontiguous data, in bytes
357 * - block_length - the width or height of blocked matrix
358 * - count - the number of rows of matrix
360 void serialize_contiguous( const void *noncontiguous_hvector,
361 void *contiguous_hvector,
365 s_smpi_mpi_contiguous_t* type_c = (s_smpi_mpi_contiguous_t*)type;
366 char* contiguous_vector_char = (char*)contiguous_hvector;
367 char* noncontiguous_vector_char = (char*)noncontiguous_hvector+type_c->lb;
368 memcpy(contiguous_vector_char,
369 noncontiguous_vector_char, count* type_c->block_count * type_c->size_oldtype);
372 * Copies contiguous data into noncontiguous memory.
373 * @param noncontiguous_vector - output hvector
374 * @param contiguous_vector - input hvector
375 * @param type - pointer contening :
376 * - stride - stride of between noncontiguous data, in bytes
377 * - block_length - the width or height of blocked matrix
378 * - count - the number of rows of matrix
380 void unserialize_contiguous( const void *contiguous_vector,
381 void *noncontiguous_vector,
385 s_smpi_mpi_contiguous_t* type_c = (s_smpi_mpi_contiguous_t*)type;
386 char* contiguous_vector_char = (char*)contiguous_vector;
387 char* noncontiguous_vector_char = (char*)noncontiguous_vector+type_c->lb;
389 memcpy(noncontiguous_vector_char,
390 contiguous_vector_char, count* type_c->block_count * type_c->size_oldtype);
393 void free_contiguous(MPI_Datatype* d){
397 * Create a Sub type contiguous to be able to serialize and unserialize it
398 * the structure s_smpi_mpi_contiguous_t is derived from s_smpi_subtype which
399 * required the functions unserialize and serialize
402 s_smpi_mpi_contiguous_t* smpi_datatype_contiguous_create( MPI_Aint lb,
404 MPI_Datatype old_type,
406 s_smpi_mpi_contiguous_t *new_t= xbt_new(s_smpi_mpi_contiguous_t,1);
407 new_t->base.serialize = &serialize_contiguous;
408 new_t->base.unserialize = &unserialize_contiguous;
409 new_t->base.subtype_free = &free_contiguous;
411 new_t->block_count = block_count;
412 new_t->old_type = old_type;
413 new_t->size_oldtype = size_oldtype;
420 int smpi_datatype_contiguous(int count, MPI_Datatype old_type, MPI_Datatype* new_type, MPI_Aint lb)
423 if(old_type->has_subtype){
424 //handle this case as a hvector with stride equals to the extent of the datatype
425 return smpi_datatype_hvector(count, 1, smpi_datatype_get_extent(old_type), old_type, new_type);
428 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb,
431 smpi_datatype_size(old_type));
433 smpi_datatype_create(new_type,
434 count * smpi_datatype_size(old_type),
435 lb,lb + count * smpi_datatype_size(old_type),
436 1,subtype, DT_FLAG_CONTIGUOUS);
441 int smpi_datatype_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type)
444 if (blocklen<0) return MPI_ERR_ARG;
448 lb=smpi_datatype_lb(old_type);
449 ub=((count-1)*stride+blocklen-1)*smpi_datatype_get_extent(old_type)+smpi_datatype_ub(old_type);
451 if(old_type->has_subtype || stride != blocklen){
454 s_smpi_mpi_vector_t* subtype = smpi_datatype_vector_create( stride,
458 smpi_datatype_size(old_type));
459 smpi_datatype_create(new_type,
460 count * (blocklen) * smpi_datatype_size(old_type), lb,
467 /* in this situation the data are contignous thus it's not
468 * required to serialize and unserialize it*/
469 smpi_datatype_create(new_type, count * blocklen *
470 smpi_datatype_size(old_type), 0, ((count -1) * stride + blocklen)*
471 smpi_datatype_size(old_type),
474 DT_FLAG_VECTOR|DT_FLAG_CONTIGUOUS);
480 void free_vector(MPI_Datatype* d){
484 Hvector Implementation - Vector with stride in bytes
489 * Copies noncontiguous data into contiguous memory.
490 * @param contiguous_hvector - output hvector
491 * @param noncontiguous_hvector - input hvector
492 * @param type - pointer contening :
493 * - stride - stride of between noncontiguous data, in bytes
494 * - block_length - the width or height of blocked matrix
495 * - count - the number of rows of matrix
497 void serialize_hvector( const void *noncontiguous_hvector,
498 void *contiguous_hvector,
502 s_smpi_mpi_hvector_t* type_c = (s_smpi_mpi_hvector_t*)type;
504 char* contiguous_vector_char = (char*)contiguous_hvector;
505 char* noncontiguous_vector_char = (char*)noncontiguous_hvector;
507 for (i = 0; i < type_c->block_count * count; i++) {
508 if (type_c->old_type->has_subtype == 0)
509 memcpy(contiguous_vector_char,
510 noncontiguous_vector_char, type_c->block_length * type_c->size_oldtype);
512 ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_vector_char,
513 contiguous_vector_char,
514 type_c->block_length,
515 type_c->old_type->substruct);
517 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
518 if((i+1)%type_c->block_count ==0)
519 noncontiguous_vector_char += type_c->block_length*type_c->size_oldtype;
521 noncontiguous_vector_char += type_c->block_stride;
525 * Copies contiguous data into noncontiguous memory.
526 * @param noncontiguous_vector - output hvector
527 * @param contiguous_vector - input hvector
528 * @param type - pointer contening :
529 * - stride - stride of between noncontiguous data, in bytes
530 * - block_length - the width or height of blocked matrix
531 * - count - the number of rows of matrix
533 void unserialize_hvector( const void *contiguous_vector,
534 void *noncontiguous_vector,
538 s_smpi_mpi_hvector_t* type_c = (s_smpi_mpi_hvector_t*)type;
541 char* contiguous_vector_char = (char*)contiguous_vector;
542 char* noncontiguous_vector_char = (char*)noncontiguous_vector;
544 for (i = 0; i < type_c->block_count * count; i++) {
545 if (type_c->old_type->has_subtype == 0)
546 memcpy(noncontiguous_vector_char,
547 contiguous_vector_char, type_c->block_length * type_c->size_oldtype);
549 ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_vector_char,
550 noncontiguous_vector_char,
551 type_c->block_length,
552 type_c->old_type->substruct);
553 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
554 if((i+1)%type_c->block_count ==0)
555 noncontiguous_vector_char += type_c->block_length*type_c->size_oldtype;
557 noncontiguous_vector_char += type_c->block_stride;
562 * Create a Sub type vector to be able to serialize and unserialize it
563 * the structure s_smpi_mpi_vector_t is derived from s_smpi_subtype which
564 * required the functions unserialize and serialize
567 s_smpi_mpi_hvector_t* smpi_datatype_hvector_create( MPI_Aint block_stride,
570 MPI_Datatype old_type,
572 s_smpi_mpi_hvector_t *new_t= xbt_new(s_smpi_mpi_hvector_t,1);
573 new_t->base.serialize = &serialize_hvector;
574 new_t->base.unserialize = &unserialize_hvector;
575 new_t->base.subtype_free = &free_hvector;
576 new_t->block_stride = block_stride;
577 new_t->block_length = block_length;
578 new_t->block_count = block_count;
579 new_t->old_type = old_type;
580 new_t->size_oldtype = size_oldtype;
584 //do nothing for vector types
585 void free_hvector(MPI_Datatype* d){
588 int smpi_datatype_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type)
591 if (blocklen<0) return MPI_ERR_ARG;
595 lb=smpi_datatype_lb(old_type);
596 ub=((count-1)*stride)+(blocklen-1)*smpi_datatype_get_extent(old_type)+smpi_datatype_ub(old_type);
598 if(old_type->has_subtype || stride != blocklen*smpi_datatype_get_extent(old_type)){
599 s_smpi_mpi_hvector_t* subtype = smpi_datatype_hvector_create( stride,
603 smpi_datatype_size(old_type));
605 smpi_datatype_create(new_type, count * blocklen * smpi_datatype_size(old_type),
612 smpi_datatype_create(new_type, count * blocklen *
613 smpi_datatype_size(old_type),0,count * blocklen *
614 smpi_datatype_size(old_type),
617 DT_FLAG_VECTOR|DT_FLAG_CONTIGUOUS);
625 Indexed Implementation
629 * Copies noncontiguous data into contiguous memory.
630 * @param contiguous_indexed - output indexed
631 * @param noncontiguous_indexed - input indexed
632 * @param type - pointer contening :
633 * - block_lengths - the width or height of blocked matrix
634 * - block_indices - indices of each data, in element
635 * - count - the number of rows of matrix
637 void serialize_indexed( const void *noncontiguous_indexed,
638 void *contiguous_indexed,
642 s_smpi_mpi_indexed_t* type_c = (s_smpi_mpi_indexed_t*)type;
644 char* contiguous_indexed_char = (char*)contiguous_indexed;
645 char* noncontiguous_indexed_char = (char*)noncontiguous_indexed+type_c->block_indices[0] * type_c->size_oldtype;
646 for(j=0; j<count;j++){
647 for (i = 0; i < type_c->block_count; i++) {
648 if (type_c->old_type->has_subtype == 0)
649 memcpy(contiguous_indexed_char,
650 noncontiguous_indexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
652 ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_indexed_char,
653 contiguous_indexed_char,
654 type_c->block_lengths[i],
655 type_c->old_type->substruct);
658 contiguous_indexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
659 if (i<type_c->block_count-1)noncontiguous_indexed_char = (char*)noncontiguous_indexed + type_c->block_indices[i+1]*smpi_datatype_get_extent(type_c->old_type);
660 else noncontiguous_indexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
662 noncontiguous_indexed=(void*)noncontiguous_indexed_char;
666 * Copies contiguous data into noncontiguous memory.
667 * @param noncontiguous_indexed - output indexed
668 * @param contiguous_indexed - input indexed
669 * @param type - pointer contening :
670 * - block_lengths - the width or height of blocked matrix
671 * - block_indices - indices of each data, in element
672 * - count - the number of rows of matrix
674 void unserialize_indexed( const void *contiguous_indexed,
675 void *noncontiguous_indexed,
680 s_smpi_mpi_indexed_t* type_c = (s_smpi_mpi_indexed_t*)type;
682 char* contiguous_indexed_char = (char*)contiguous_indexed;
683 char* noncontiguous_indexed_char = (char*)noncontiguous_indexed+type_c->block_indices[0]*smpi_datatype_get_extent(type_c->old_type);
684 for(j=0; j<count;j++){
685 for (i = 0; i < type_c->block_count; i++) {
686 if (type_c->old_type->has_subtype == 0)
687 memcpy(noncontiguous_indexed_char ,
688 contiguous_indexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
690 ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_indexed_char,
691 noncontiguous_indexed_char,
692 type_c->block_lengths[i],
693 type_c->old_type->substruct);
695 contiguous_indexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
696 if (i<type_c->block_count-1)
697 noncontiguous_indexed_char = (char*)noncontiguous_indexed + type_c->block_indices[i+1]*smpi_datatype_get_extent(type_c->old_type);
698 else noncontiguous_indexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
700 noncontiguous_indexed=(void*)noncontiguous_indexed_char;
704 void free_indexed(MPI_Datatype* type){
705 xbt_free(((s_smpi_mpi_indexed_t *)(*type)->substruct)->block_lengths);
706 xbt_free(((s_smpi_mpi_indexed_t *)(*type)->substruct)->block_indices);
710 * Create a Sub type indexed to be able to serialize and unserialize it
711 * the structure s_smpi_mpi_indexed_t is derived from s_smpi_subtype which
712 * required the functions unserialize and serialize
714 s_smpi_mpi_indexed_t* smpi_datatype_indexed_create( int* block_lengths,
717 MPI_Datatype old_type,
719 s_smpi_mpi_indexed_t *new_t= xbt_new(s_smpi_mpi_indexed_t,1);
720 new_t->base.serialize = &serialize_indexed;
721 new_t->base.unserialize = &unserialize_indexed;
722 new_t->base.subtype_free = &free_indexed;
723 //TODO : add a custom function for each time to clean these
724 new_t->block_lengths= xbt_new(int, block_count);
725 new_t->block_indices= xbt_new(int, block_count);
727 for(i=0;i<block_count;i++){
728 new_t->block_lengths[i]=block_lengths[i];
729 new_t->block_indices[i]=block_indices[i];
731 new_t->block_count = block_count;
732 new_t->old_type = old_type;
733 new_t->size_oldtype = size_oldtype;
738 int smpi_datatype_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
747 lb=indices[0]*smpi_datatype_get_extent(old_type);
748 ub=indices[0]*smpi_datatype_get_extent(old_type) + blocklens[0]*smpi_datatype_ub(old_type);
751 for(i=0; i< count; i++){
754 size += blocklens[i];
756 if(indices[i]*smpi_datatype_get_extent(old_type)+smpi_datatype_lb(old_type)<lb)
757 lb = indices[i]*smpi_datatype_get_extent(old_type)+smpi_datatype_lb(old_type);
758 if(indices[i]*smpi_datatype_get_extent(old_type)+blocklens[i]*smpi_datatype_ub(old_type)>ub)
759 ub = indices[i]*smpi_datatype_get_extent(old_type)+blocklens[i]*smpi_datatype_ub(old_type);
761 if ( (i< count -1) && (indices[i]+blocklens[i] != indices[i+1]) )contiguous=0;
763 if (old_type->has_subtype == 1)
767 s_smpi_mpi_indexed_t* subtype = smpi_datatype_indexed_create( blocklens,
771 smpi_datatype_size(old_type));
772 smpi_datatype_create(new_type, size *
773 smpi_datatype_size(old_type),lb,ub,1, subtype, DT_FLAG_DATA);
775 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb,
778 smpi_datatype_size(old_type));
779 smpi_datatype_create(new_type, size *
780 smpi_datatype_size(old_type),lb,ub,1, subtype, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
788 Hindexed Implementation - Indexed with indices in bytes
792 * Copies noncontiguous data into contiguous memory.
793 * @param contiguous_hindexed - output hindexed
794 * @param noncontiguous_hindexed - input hindexed
795 * @param type - pointer contening :
796 * - block_lengths - the width or height of blocked matrix
797 * - block_indices - indices of each data, in bytes
798 * - count - the number of rows of matrix
800 void serialize_hindexed( const void *noncontiguous_hindexed,
801 void *contiguous_hindexed,
805 s_smpi_mpi_hindexed_t* type_c = (s_smpi_mpi_hindexed_t*)type;
807 char* contiguous_hindexed_char = (char*)contiguous_hindexed;
808 char* noncontiguous_hindexed_char = (char*)noncontiguous_hindexed+ type_c->block_indices[0];
809 for(j=0; j<count;j++){
810 for (i = 0; i < type_c->block_count; i++) {
811 if (type_c->old_type->has_subtype == 0)
812 memcpy(contiguous_hindexed_char,
813 noncontiguous_hindexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
815 ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_hindexed_char,
816 contiguous_hindexed_char,
817 type_c->block_lengths[i],
818 type_c->old_type->substruct);
820 contiguous_hindexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
821 if (i<type_c->block_count-1)noncontiguous_hindexed_char = (char*)noncontiguous_hindexed + type_c->block_indices[i+1];
822 else noncontiguous_hindexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
824 noncontiguous_hindexed=(void*)noncontiguous_hindexed_char;
828 * Copies contiguous data into noncontiguous memory.
829 * @param noncontiguous_hindexed - output hindexed
830 * @param contiguous_hindexed - input hindexed
831 * @param type - pointer contening :
832 * - block_lengths - the width or height of blocked matrix
833 * - block_indices - indices of each data, in bytes
834 * - count - the number of rows of matrix
836 void unserialize_hindexed( const void *contiguous_hindexed,
837 void *noncontiguous_hindexed,
841 s_smpi_mpi_hindexed_t* type_c = (s_smpi_mpi_hindexed_t*)type;
844 char* contiguous_hindexed_char = (char*)contiguous_hindexed;
845 char* noncontiguous_hindexed_char = (char*)noncontiguous_hindexed+ type_c->block_indices[0];
846 for(j=0; j<count;j++){
847 for (i = 0; i < type_c->block_count; i++) {
848 if (type_c->old_type->has_subtype == 0)
849 memcpy(noncontiguous_hindexed_char,
850 contiguous_hindexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
852 ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_hindexed_char,
853 noncontiguous_hindexed_char,
854 type_c->block_lengths[i],
855 type_c->old_type->substruct);
857 contiguous_hindexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
858 if (i<type_c->block_count-1)noncontiguous_hindexed_char = (char*)noncontiguous_hindexed + type_c->block_indices[i+1];
859 else noncontiguous_hindexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
861 noncontiguous_hindexed=(void*)noncontiguous_hindexed_char;
865 void free_hindexed(MPI_Datatype* type){
866 xbt_free(((s_smpi_mpi_hindexed_t *)(*type)->substruct)->block_lengths);
867 xbt_free(((s_smpi_mpi_hindexed_t *)(*type)->substruct)->block_indices);
871 * Create a Sub type hindexed to be able to serialize and unserialize it
872 * the structure s_smpi_mpi_hindexed_t is derived from s_smpi_subtype which
873 * required the functions unserialize and serialize
875 s_smpi_mpi_hindexed_t* smpi_datatype_hindexed_create( int* block_lengths,
876 MPI_Aint* block_indices,
878 MPI_Datatype old_type,
880 s_smpi_mpi_hindexed_t *new_t= xbt_new(s_smpi_mpi_hindexed_t,1);
881 new_t->base.serialize = &serialize_hindexed;
882 new_t->base.unserialize = &unserialize_hindexed;
883 new_t->base.subtype_free = &free_hindexed;
884 //TODO : add a custom function for each time to clean these
885 new_t->block_lengths= xbt_new(int, block_count);
886 new_t->block_indices= xbt_new(MPI_Aint, block_count);
888 for(i=0;i<block_count;i++){
889 new_t->block_lengths[i]=block_lengths[i];
890 new_t->block_indices[i]=block_indices[i];
892 new_t->block_count = block_count;
893 new_t->old_type = old_type;
894 new_t->size_oldtype = size_oldtype;
899 int smpi_datatype_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
908 lb=indices[0] + smpi_datatype_lb(old_type);
909 ub=indices[0] + blocklens[0]*smpi_datatype_ub(old_type);
911 for(i=0; i< count; i++){
914 size += blocklens[i];
916 if(indices[i]+smpi_datatype_lb(old_type)<lb) lb = indices[i]+smpi_datatype_lb(old_type);
917 if(indices[i]+blocklens[i]*smpi_datatype_ub(old_type)>ub) ub = indices[i]+blocklens[i]*smpi_datatype_ub(old_type);
919 if ( (i< count -1) && (indices[i]+blocklens[i]*smpi_datatype_size(old_type) != indices[i+1]) )contiguous=0;
921 if (old_type->has_subtype == 1 || lb!=0)
925 s_smpi_mpi_hindexed_t* subtype = smpi_datatype_hindexed_create( blocklens,
929 smpi_datatype_size(old_type));
930 smpi_datatype_create(new_type, size * smpi_datatype_size(old_type),
933 ,1, subtype, DT_FLAG_DATA);
935 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb,
938 smpi_datatype_size(old_type));
939 smpi_datatype_create(new_type, size * smpi_datatype_size(old_type),
940 0,size * smpi_datatype_size(old_type),
941 1, subtype, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
949 struct Implementation - Indexed with indices in bytes
953 * Copies noncontiguous data into contiguous memory.
954 * @param contiguous_struct - output struct
955 * @param noncontiguous_struct - input struct
956 * @param type - pointer contening :
957 * - stride - stride of between noncontiguous data
958 * - block_length - the width or height of blocked matrix
959 * - count - the number of rows of matrix
961 void serialize_struct( const void *noncontiguous_struct,
962 void *contiguous_struct,
966 s_smpi_mpi_struct_t* type_c = (s_smpi_mpi_struct_t*)type;
968 char* contiguous_struct_char = (char*)contiguous_struct;
969 char* noncontiguous_struct_char = (char*)noncontiguous_struct+ type_c->block_indices[0];
970 for(j=0; j<count;j++){
971 for (i = 0; i < type_c->block_count; i++) {
972 if (type_c->old_types[i]->has_subtype == 0)
973 memcpy(contiguous_struct_char,
974 noncontiguous_struct_char, type_c->block_lengths[i] * smpi_datatype_size(type_c->old_types[i]));
976 ((s_smpi_subtype_t*)type_c->old_types[i]->substruct)->serialize( noncontiguous_struct_char,
977 contiguous_struct_char,
978 type_c->block_lengths[i],
979 type_c->old_types[i]->substruct);
982 contiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_size(type_c->old_types[i]);
983 if (i<type_c->block_count-1)noncontiguous_struct_char = (char*)noncontiguous_struct + type_c->block_indices[i+1];
984 else noncontiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_types[i]);//let's hope this is MPI_UB ?
986 noncontiguous_struct=(void*)noncontiguous_struct_char;
990 * Copies contiguous data into noncontiguous memory.
991 * @param noncontiguous_struct - output struct
992 * @param contiguous_struct - input struct
993 * @param type - pointer contening :
994 * - stride - stride of between noncontiguous data
995 * - block_length - the width or height of blocked matrix
996 * - count - the number of rows of matrix
998 void unserialize_struct( const void *contiguous_struct,
999 void *noncontiguous_struct,
1003 s_smpi_mpi_struct_t* type_c = (s_smpi_mpi_struct_t*)type;
1006 char* contiguous_struct_char = (char*)contiguous_struct;
1007 char* noncontiguous_struct_char = (char*)noncontiguous_struct+ type_c->block_indices[0];
1008 for(j=0; j<count;j++){
1009 for (i = 0; i < type_c->block_count; i++) {
1010 if (type_c->old_types[i]->has_subtype == 0)
1011 memcpy(noncontiguous_struct_char,
1012 contiguous_struct_char, type_c->block_lengths[i] * smpi_datatype_size(type_c->old_types[i]));
1014 ((s_smpi_subtype_t*)type_c->old_types[i]->substruct)->unserialize( contiguous_struct_char,
1015 noncontiguous_struct_char,
1016 type_c->block_lengths[i],
1017 type_c->old_types[i]->substruct);
1019 contiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_size(type_c->old_types[i]);
1020 if (i<type_c->block_count-1)noncontiguous_struct_char = (char*)noncontiguous_struct + type_c->block_indices[i+1];
1021 else noncontiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_types[i]);
1023 noncontiguous_struct=(void*)noncontiguous_struct_char;
1028 void free_struct(MPI_Datatype* type){
1029 xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->block_lengths);
1030 xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->block_indices);
1031 xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->old_types);
1035 * Create a Sub type struct to be able to serialize and unserialize it
1036 * the structure s_smpi_mpi_struct_t is derived from s_smpi_subtype which
1037 * required the functions unserialize and serialize
1039 s_smpi_mpi_struct_t* smpi_datatype_struct_create( int* block_lengths,
1040 MPI_Aint* block_indices,
1042 MPI_Datatype* old_types){
1043 s_smpi_mpi_struct_t *new_t= xbt_new(s_smpi_mpi_struct_t,1);
1044 new_t->base.serialize = &serialize_struct;
1045 new_t->base.unserialize = &unserialize_struct;
1046 new_t->base.subtype_free = &free_struct;
1047 //TODO : add a custom function for each time to clean these
1048 new_t->block_lengths= xbt_new(int, block_count);
1049 new_t->block_indices= xbt_new(MPI_Aint, block_count);
1050 new_t->old_types= xbt_new(MPI_Datatype, block_count);
1052 for(i=0;i<block_count;i++){
1053 new_t->block_lengths[i]=block_lengths[i];
1054 new_t->block_indices[i]=block_indices[i];
1055 new_t->old_types[i]=old_types[i];
1057 //new_t->block_lengths = block_lengths;
1058 //new_t->block_indices = block_indices;
1059 new_t->block_count = block_count;
1060 //new_t->old_types = old_types;
1065 int smpi_datatype_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type)
1074 lb=indices[0] + smpi_datatype_lb(old_types[0]);
1075 ub=indices[0] + blocklens[0]*smpi_datatype_ub(old_types[0]);
1079 for(i=0; i< count; i++){
1082 if (old_types[i]->has_subtype == 1)
1085 size += blocklens[i]*smpi_datatype_size(old_types[i]);
1086 if (old_types[i]==MPI_LB){
1090 if (old_types[i]==MPI_UB){
1095 if(!forced_lb && indices[i]+smpi_datatype_lb(old_types[i])<lb) lb = indices[i];
1096 if(!forced_ub && indices[i]+blocklens[i]*smpi_datatype_ub(old_types[i])>ub) ub = indices[i]+blocklens[i]*smpi_datatype_ub(old_types[i]);
1098 if ( (i< count -1) && (indices[i]+blocklens[i]*smpi_datatype_size(old_types[i]) != indices[i+1]) )contiguous=0;
1102 s_smpi_mpi_struct_t* subtype = smpi_datatype_struct_create( blocklens,
1107 smpi_datatype_create(new_type, size, lb, ub,1, subtype, DT_FLAG_DATA);
1109 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb,
1113 smpi_datatype_create(new_type, size, lb, ub,1, subtype, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
1118 void smpi_datatype_commit(MPI_Datatype *datatype)
1120 (*datatype)->flags= ((*datatype)->flags | DT_FLAG_COMMITED);
1123 typedef struct s_smpi_mpi_op {
1124 MPI_User_function *func;
1128 #define MAX_OP(a, b) (b) = (a) < (b) ? (b) : (a)
1129 #define MIN_OP(a, b) (b) = (a) < (b) ? (a) : (b)
1130 #define SUM_OP(a, b) (b) += (a)
1131 #define PROD_OP(a, b) (b) *= (a)
1132 #define LAND_OP(a, b) (b) = (a) && (b)
1133 #define LOR_OP(a, b) (b) = (a) || (b)
1134 #define LXOR_OP(a, b) (b) = (!(a) && (b)) || ((a) && !(b))
1135 #define BAND_OP(a, b) (b) &= (a)
1136 #define BOR_OP(a, b) (b) |= (a)
1137 #define BXOR_OP(a, b) (b) ^= (a)
1138 #define MAXLOC_OP(a, b) (b) = (a.value) < (b.value) ? (b) : (a)
1139 #define MINLOC_OP(a, b) (b) = (a.value) < (b.value) ? (a) : (b)
1140 //TODO : MINLOC & MAXLOC
1142 #define APPLY_FUNC(a, b, length, type, func) \
1145 type* x = (type*)(a); \
1146 type* y = (type*)(b); \
1147 for(i = 0; i < *(length); i++) { \
1152 static void max_func(void *a, void *b, int *length,
1153 MPI_Datatype * datatype)
1155 if (*datatype == MPI_CHAR) {
1156 APPLY_FUNC(a, b, length, char, MAX_OP);
1157 } else if (*datatype == MPI_SHORT) {
1158 APPLY_FUNC(a, b, length, short, MAX_OP);
1159 } else if (*datatype == MPI_INT) {
1160 APPLY_FUNC(a, b, length, int, MAX_OP);
1161 } else if (*datatype == MPI_LONG) {
1162 APPLY_FUNC(a, b, length, long, MAX_OP);
1163 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1164 APPLY_FUNC(a, b, length, unsigned short, MAX_OP);
1165 } else if (*datatype == MPI_UNSIGNED) {
1166 APPLY_FUNC(a, b, length, unsigned int, MAX_OP);
1167 } else if (*datatype == MPI_UNSIGNED_LONG) {
1168 APPLY_FUNC(a, b, length, unsigned long, MAX_OP);
1169 } else if (*datatype == MPI_FLOAT) {
1170 APPLY_FUNC(a, b, length, float, MAX_OP);
1171 } else if (*datatype == MPI_DOUBLE) {
1172 APPLY_FUNC(a, b, length, double, MAX_OP);
1173 } else if (*datatype == MPI_LONG_DOUBLE) {
1174 APPLY_FUNC(a, b, length, long double, MAX_OP);
1178 static void min_func(void *a, void *b, int *length,
1179 MPI_Datatype * datatype)
1181 if (*datatype == MPI_CHAR) {
1182 APPLY_FUNC(a, b, length, char, MIN_OP);
1183 } else if (*datatype == MPI_SHORT) {
1184 APPLY_FUNC(a, b, length, short, MIN_OP);
1185 } else if (*datatype == MPI_INT) {
1186 APPLY_FUNC(a, b, length, int, MIN_OP);
1187 } else if (*datatype == MPI_LONG) {
1188 APPLY_FUNC(a, b, length, long, MIN_OP);
1189 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1190 APPLY_FUNC(a, b, length, unsigned short, MIN_OP);
1191 } else if (*datatype == MPI_UNSIGNED) {
1192 APPLY_FUNC(a, b, length, unsigned int, MIN_OP);
1193 } else if (*datatype == MPI_UNSIGNED_LONG) {
1194 APPLY_FUNC(a, b, length, unsigned long, MIN_OP);
1195 } else if (*datatype == MPI_FLOAT) {
1196 APPLY_FUNC(a, b, length, float, MIN_OP);
1197 } else if (*datatype == MPI_DOUBLE) {
1198 APPLY_FUNC(a, b, length, double, MIN_OP);
1199 } else if (*datatype == MPI_LONG_DOUBLE) {
1200 APPLY_FUNC(a, b, length, long double, MIN_OP);
1204 static void sum_func(void *a, void *b, int *length,
1205 MPI_Datatype * datatype)
1207 if (*datatype == MPI_CHAR) {
1208 APPLY_FUNC(a, b, length, char, SUM_OP);
1209 } else if (*datatype == MPI_SHORT) {
1210 APPLY_FUNC(a, b, length, short, SUM_OP);
1211 } else if (*datatype == MPI_INT) {
1212 APPLY_FUNC(a, b, length, int, SUM_OP);
1213 } else if (*datatype == MPI_LONG) {
1214 APPLY_FUNC(a, b, length, long, SUM_OP);
1215 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1216 APPLY_FUNC(a, b, length, unsigned short, SUM_OP);
1217 } else if (*datatype == MPI_UNSIGNED) {
1218 APPLY_FUNC(a, b, length, unsigned int, SUM_OP);
1219 } else if (*datatype == MPI_UNSIGNED_LONG) {
1220 APPLY_FUNC(a, b, length, unsigned long, SUM_OP);
1221 } else if (*datatype == MPI_FLOAT) {
1222 APPLY_FUNC(a, b, length, float, SUM_OP);
1223 } else if (*datatype == MPI_DOUBLE) {
1224 APPLY_FUNC(a, b, length, double, SUM_OP);
1225 } else if (*datatype == MPI_LONG_DOUBLE) {
1226 APPLY_FUNC(a, b, length, long double, SUM_OP);
1227 } else if (*datatype == MPI_C_FLOAT_COMPLEX) {
1228 APPLY_FUNC(a, b, length, float _Complex, SUM_OP);
1229 } else if (*datatype == MPI_C_DOUBLE_COMPLEX) {
1230 APPLY_FUNC(a, b, length, double _Complex, SUM_OP);
1231 } else if (*datatype == MPI_C_LONG_DOUBLE_COMPLEX) {
1232 APPLY_FUNC(a, b, length, long double _Complex, SUM_OP);
1236 static void prod_func(void *a, void *b, int *length,
1237 MPI_Datatype * datatype)
1239 if (*datatype == MPI_CHAR) {
1240 APPLY_FUNC(a, b, length, char, PROD_OP);
1241 } else if (*datatype == MPI_SHORT) {
1242 APPLY_FUNC(a, b, length, short, PROD_OP);
1243 } else if (*datatype == MPI_INT) {
1244 APPLY_FUNC(a, b, length, int, PROD_OP);
1245 } else if (*datatype == MPI_LONG) {
1246 APPLY_FUNC(a, b, length, long, PROD_OP);
1247 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1248 APPLY_FUNC(a, b, length, unsigned short, PROD_OP);
1249 } else if (*datatype == MPI_UNSIGNED) {
1250 APPLY_FUNC(a, b, length, unsigned int, PROD_OP);
1251 } else if (*datatype == MPI_UNSIGNED_LONG) {
1252 APPLY_FUNC(a, b, length, unsigned long, PROD_OP);
1253 } else if (*datatype == MPI_FLOAT) {
1254 APPLY_FUNC(a, b, length, float, PROD_OP);
1255 } else if (*datatype == MPI_DOUBLE) {
1256 APPLY_FUNC(a, b, length, double, PROD_OP);
1257 } else if (*datatype == MPI_LONG_DOUBLE) {
1258 APPLY_FUNC(a, b, length, long double, PROD_OP);
1259 } else if (*datatype == MPI_C_FLOAT_COMPLEX) {
1260 APPLY_FUNC(a, b, length, float _Complex, PROD_OP);
1261 } else if (*datatype == MPI_C_DOUBLE_COMPLEX) {
1262 APPLY_FUNC(a, b, length, double _Complex, PROD_OP);
1263 } else if (*datatype == MPI_C_LONG_DOUBLE_COMPLEX) {
1264 APPLY_FUNC(a, b, length, long double _Complex, PROD_OP);
1268 static void land_func(void *a, void *b, int *length,
1269 MPI_Datatype * datatype)
1271 if (*datatype == MPI_CHAR) {
1272 APPLY_FUNC(a, b, length, char, LAND_OP);
1273 } else if (*datatype == MPI_SHORT) {
1274 APPLY_FUNC(a, b, length, short, LAND_OP);
1275 } else if (*datatype == MPI_INT) {
1276 APPLY_FUNC(a, b, length, int, LAND_OP);
1277 } else if (*datatype == MPI_LONG) {
1278 APPLY_FUNC(a, b, length, long, LAND_OP);
1279 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1280 APPLY_FUNC(a, b, length, unsigned short, LAND_OP);
1281 } else if (*datatype == MPI_UNSIGNED) {
1282 APPLY_FUNC(a, b, length, unsigned int, LAND_OP);
1283 } else if (*datatype == MPI_UNSIGNED_LONG) {
1284 APPLY_FUNC(a, b, length, unsigned long, LAND_OP);
1285 } else if (*datatype == MPI_C_BOOL) {
1286 APPLY_FUNC(a, b, length, _Bool, LAND_OP);
1290 static void lor_func(void *a, void *b, int *length,
1291 MPI_Datatype * datatype)
1293 if (*datatype == MPI_CHAR) {
1294 APPLY_FUNC(a, b, length, char, LOR_OP);
1295 } else if (*datatype == MPI_SHORT) {
1296 APPLY_FUNC(a, b, length, short, LOR_OP);
1297 } else if (*datatype == MPI_INT) {
1298 APPLY_FUNC(a, b, length, int, LOR_OP);
1299 } else if (*datatype == MPI_LONG) {
1300 APPLY_FUNC(a, b, length, long, LOR_OP);
1301 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1302 APPLY_FUNC(a, b, length, unsigned short, LOR_OP);
1303 } else if (*datatype == MPI_UNSIGNED) {
1304 APPLY_FUNC(a, b, length, unsigned int, LOR_OP);
1305 } else if (*datatype == MPI_UNSIGNED_LONG) {
1306 APPLY_FUNC(a, b, length, unsigned long, LOR_OP);
1307 } else if (*datatype == MPI_C_BOOL) {
1308 APPLY_FUNC(a, b, length, _Bool, LOR_OP);
1312 static void lxor_func(void *a, void *b, int *length,
1313 MPI_Datatype * datatype)
1315 if (*datatype == MPI_CHAR) {
1316 APPLY_FUNC(a, b, length, char, LXOR_OP);
1317 } else if (*datatype == MPI_SHORT) {
1318 APPLY_FUNC(a, b, length, short, LXOR_OP);
1319 } else if (*datatype == MPI_INT) {
1320 APPLY_FUNC(a, b, length, int, LXOR_OP);
1321 } else if (*datatype == MPI_LONG) {
1322 APPLY_FUNC(a, b, length, long, LXOR_OP);
1323 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1324 APPLY_FUNC(a, b, length, unsigned short, LXOR_OP);
1325 } else if (*datatype == MPI_UNSIGNED) {
1326 APPLY_FUNC(a, b, length, unsigned int, LXOR_OP);
1327 } else if (*datatype == MPI_UNSIGNED_LONG) {
1328 APPLY_FUNC(a, b, length, unsigned long, LXOR_OP);
1329 } else if (*datatype == MPI_C_BOOL) {
1330 APPLY_FUNC(a, b, length, _Bool, LXOR_OP);
1334 static void band_func(void *a, void *b, int *length,
1335 MPI_Datatype * datatype)
1337 if (*datatype == MPI_CHAR) {
1338 APPLY_FUNC(a, b, length, char, BAND_OP);
1340 if (*datatype == MPI_SHORT) {
1341 APPLY_FUNC(a, b, length, short, BAND_OP);
1342 } else if (*datatype == MPI_INT) {
1343 APPLY_FUNC(a, b, length, int, BAND_OP);
1344 } else if (*datatype == MPI_LONG) {
1345 APPLY_FUNC(a, b, length, long, BAND_OP);
1346 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1347 APPLY_FUNC(a, b, length, unsigned short, BAND_OP);
1348 } else if (*datatype == MPI_UNSIGNED) {
1349 APPLY_FUNC(a, b, length, unsigned int, BAND_OP);
1350 } else if (*datatype == MPI_UNSIGNED_LONG) {
1351 APPLY_FUNC(a, b, length, unsigned long, BAND_OP);
1352 } else if (*datatype == MPI_BYTE) {
1353 APPLY_FUNC(a, b, length, uint8_t, BAND_OP);
1357 static void bor_func(void *a, void *b, int *length,
1358 MPI_Datatype * datatype)
1360 if (*datatype == MPI_CHAR) {
1361 APPLY_FUNC(a, b, length, char, BOR_OP);
1362 } else if (*datatype == MPI_SHORT) {
1363 APPLY_FUNC(a, b, length, short, BOR_OP);
1364 } else if (*datatype == MPI_INT) {
1365 APPLY_FUNC(a, b, length, int, BOR_OP);
1366 } else if (*datatype == MPI_LONG) {
1367 APPLY_FUNC(a, b, length, long, BOR_OP);
1368 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1369 APPLY_FUNC(a, b, length, unsigned short, BOR_OP);
1370 } else if (*datatype == MPI_UNSIGNED) {
1371 APPLY_FUNC(a, b, length, unsigned int, BOR_OP);
1372 } else if (*datatype == MPI_UNSIGNED_LONG) {
1373 APPLY_FUNC(a, b, length, unsigned long, BOR_OP);
1374 } else if (*datatype == MPI_BYTE) {
1375 APPLY_FUNC(a, b, length, uint8_t, BOR_OP);
1379 static void bxor_func(void *a, void *b, int *length,
1380 MPI_Datatype * datatype)
1382 if (*datatype == MPI_CHAR) {
1383 APPLY_FUNC(a, b, length, char, BXOR_OP);
1384 } else if (*datatype == MPI_SHORT) {
1385 APPLY_FUNC(a, b, length, short, BXOR_OP);
1386 } else if (*datatype == MPI_INT) {
1387 APPLY_FUNC(a, b, length, int, BXOR_OP);
1388 } else if (*datatype == MPI_LONG) {
1389 APPLY_FUNC(a, b, length, long, BXOR_OP);
1390 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1391 APPLY_FUNC(a, b, length, unsigned short, BXOR_OP);
1392 } else if (*datatype == MPI_UNSIGNED) {
1393 APPLY_FUNC(a, b, length, unsigned int, BXOR_OP);
1394 } else if (*datatype == MPI_UNSIGNED_LONG) {
1395 APPLY_FUNC(a, b, length, unsigned long, BXOR_OP);
1396 } else if (*datatype == MPI_BYTE) {
1397 APPLY_FUNC(a, b, length, uint8_t, BXOR_OP);
1401 static void minloc_func(void *a, void *b, int *length,
1402 MPI_Datatype * datatype)
1404 if (*datatype == MPI_FLOAT_INT) {
1405 APPLY_FUNC(a, b, length, float_int, MINLOC_OP);
1406 } else if (*datatype == MPI_LONG_INT) {
1407 APPLY_FUNC(a, b, length, long_int, MINLOC_OP);
1408 } else if (*datatype == MPI_DOUBLE_INT) {
1409 APPLY_FUNC(a, b, length, double_int, MINLOC_OP);
1410 } else if (*datatype == MPI_SHORT_INT) {
1411 APPLY_FUNC(a, b, length, short_int, MINLOC_OP);
1412 } else if (*datatype == MPI_2INT) {
1413 APPLY_FUNC(a, b, length, int_int, MINLOC_OP);
1414 } else if (*datatype == MPI_LONG_DOUBLE_INT) {
1415 APPLY_FUNC(a, b, length, long_double_int, MINLOC_OP);
1416 } else if (*datatype == MPI_2FLOAT) {
1417 APPLY_FUNC(a, b, length, float_float, MINLOC_OP);
1418 } else if (*datatype == MPI_2DOUBLE) {
1419 APPLY_FUNC(a, b, length, double_double, MINLOC_OP);
1423 static void maxloc_func(void *a, void *b, int *length,
1424 MPI_Datatype * datatype)
1426 if (*datatype == MPI_FLOAT_INT) {
1427 APPLY_FUNC(a, b, length, float_int, MAXLOC_OP);
1428 } else if (*datatype == MPI_LONG_INT) {
1429 APPLY_FUNC(a, b, length, long_int, MAXLOC_OP);
1430 } else if (*datatype == MPI_DOUBLE_INT) {
1431 APPLY_FUNC(a, b, length, double_int, MAXLOC_OP);
1432 } else if (*datatype == MPI_SHORT_INT) {
1433 APPLY_FUNC(a, b, length, short_int, MAXLOC_OP);
1434 } else if (*datatype == MPI_2INT) {
1435 APPLY_FUNC(a, b, length, int_int, MAXLOC_OP);
1436 } else if (*datatype == MPI_LONG_DOUBLE_INT) {
1437 APPLY_FUNC(a, b, length, long_double_int, MAXLOC_OP);
1438 } else if (*datatype == MPI_2FLOAT) {
1439 APPLY_FUNC(a, b, length, float_float, MAXLOC_OP);
1440 } else if (*datatype == MPI_2DOUBLE) {
1441 APPLY_FUNC(a, b, length, double_double, MAXLOC_OP);
1446 #define CREATE_MPI_OP(name, func) \
1447 static s_smpi_mpi_op_t mpi_##name = { &(func) /* func */, TRUE }; \
1448 MPI_Op name = &mpi_##name;
1450 CREATE_MPI_OP(MPI_MAX, max_func);
1451 CREATE_MPI_OP(MPI_MIN, min_func);
1452 CREATE_MPI_OP(MPI_SUM, sum_func);
1453 CREATE_MPI_OP(MPI_PROD, prod_func);
1454 CREATE_MPI_OP(MPI_LAND, land_func);
1455 CREATE_MPI_OP(MPI_LOR, lor_func);
1456 CREATE_MPI_OP(MPI_LXOR, lxor_func);
1457 CREATE_MPI_OP(MPI_BAND, band_func);
1458 CREATE_MPI_OP(MPI_BOR, bor_func);
1459 CREATE_MPI_OP(MPI_BXOR, bxor_func);
1460 CREATE_MPI_OP(MPI_MAXLOC, maxloc_func);
1461 CREATE_MPI_OP(MPI_MINLOC, minloc_func);
1463 MPI_Op smpi_op_new(MPI_User_function * function, int commute)
1466 op = xbt_new(s_smpi_mpi_op_t, 1);
1467 op->func = function;
1468 op-> is_commute = commute;
1472 int smpi_op_is_commute(MPI_Op op)
1474 return (op==MPI_OP_NULL) ? 1 : op-> is_commute;
1477 void smpi_op_destroy(MPI_Op op)
1482 void smpi_op_apply(MPI_Op op, void *invec, void *inoutvec, int *len,
1483 MPI_Datatype * datatype)
1485 op->func(invec, inoutvec, len, datatype);