1 /* smpi_mpi_dt.c -- MPI primitives to handle datatypes */
2 /* FIXME: a very incomplete implementation */
4 /* Copyright (c) 2009, 2010. The SimGrid Team.
5 * All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
15 #include "smpi_mpi_dt_private.h"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_mpi_dt, smpi,
18 "Logging specific to SMPI (datatype)");
20 #define CREATE_MPI_DATATYPE(name, type) \
21 static s_smpi_mpi_datatype_t mpi_##name = { \
22 sizeof(type), /* size */ \
23 0, /*was 1 has_subtype*/ \
25 sizeof(type), /* ub = lb + size */ \
26 DT_FLAG_BASIC, /* flags */ \
27 NULL /* pointer on extended struct*/ \
29 MPI_Datatype name = &mpi_##name;
31 #define CREATE_MPI_DATATYPE_NULL(name) \
32 static s_smpi_mpi_datatype_t mpi_##name = { \
34 0, /*was 1 has_subtype*/ \
36 0, /* ub = lb + size */ \
37 DT_FLAG_BASIC, /* flags */ \
38 NULL /* pointer on extended struct*/ \
40 MPI_Datatype name = &mpi_##name;
42 //The following are datatypes for the MPI functions MPI_MAXLOC and MPI_MINLOC.
76 // Predefined data types
77 CREATE_MPI_DATATYPE(MPI_CHAR, char);
78 CREATE_MPI_DATATYPE(MPI_SHORT, short);
79 CREATE_MPI_DATATYPE(MPI_INT, int);
80 CREATE_MPI_DATATYPE(MPI_LONG, long);
81 CREATE_MPI_DATATYPE(MPI_LONG_LONG, long long);
82 CREATE_MPI_DATATYPE(MPI_SIGNED_CHAR, signed char);
83 CREATE_MPI_DATATYPE(MPI_UNSIGNED_CHAR, unsigned char);
84 CREATE_MPI_DATATYPE(MPI_UNSIGNED_SHORT, unsigned short);
85 CREATE_MPI_DATATYPE(MPI_UNSIGNED, unsigned int);
86 CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG, unsigned long);
87 CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG_LONG, unsigned long long);
88 CREATE_MPI_DATATYPE(MPI_FLOAT, float);
89 CREATE_MPI_DATATYPE(MPI_DOUBLE, double);
90 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE, long double);
91 CREATE_MPI_DATATYPE(MPI_WCHAR, wchar_t);
92 CREATE_MPI_DATATYPE(MPI_C_BOOL, _Bool);
93 CREATE_MPI_DATATYPE(MPI_INT8_T, int8_t);
94 CREATE_MPI_DATATYPE(MPI_INT16_T, int16_t);
95 CREATE_MPI_DATATYPE(MPI_INT32_T, int32_t);
96 CREATE_MPI_DATATYPE(MPI_INT64_T, int64_t);
97 CREATE_MPI_DATATYPE(MPI_UINT8_T, uint8_t);
98 CREATE_MPI_DATATYPE(MPI_UINT16_T, uint16_t);
99 CREATE_MPI_DATATYPE(MPI_UINT32_T, uint32_t);
100 CREATE_MPI_DATATYPE(MPI_UINT64_T, uint64_t);
101 CREATE_MPI_DATATYPE(MPI_C_FLOAT_COMPLEX, float _Complex);
102 CREATE_MPI_DATATYPE(MPI_C_DOUBLE_COMPLEX, double _Complex);
103 CREATE_MPI_DATATYPE(MPI_C_LONG_DOUBLE_COMPLEX, long double _Complex);
104 CREATE_MPI_DATATYPE(MPI_AINT, MPI_Aint);
105 CREATE_MPI_DATATYPE(MPI_OFFSET, MPI_Offset);
107 CREATE_MPI_DATATYPE(MPI_FLOAT_INT, float_int);
108 CREATE_MPI_DATATYPE(MPI_LONG_INT, long_int);
109 CREATE_MPI_DATATYPE(MPI_DOUBLE_INT, double_int);
110 CREATE_MPI_DATATYPE(MPI_SHORT_INT, short_int);
111 CREATE_MPI_DATATYPE(MPI_2INT, int_int);
112 CREATE_MPI_DATATYPE(MPI_2FLOAT, float_float);
113 CREATE_MPI_DATATYPE(MPI_2DOUBLE, double_double);
115 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE_INT, long_double_int);
117 CREATE_MPI_DATATYPE_NULL(MPI_UB);
118 CREATE_MPI_DATATYPE_NULL(MPI_LB);
119 CREATE_MPI_DATATYPE_NULL(MPI_PACKED);
121 CREATE_MPI_DATATYPE(MPI_PTR, void*);
124 size_t smpi_datatype_size(MPI_Datatype datatype)
126 return datatype->size;
131 MPI_Aint smpi_datatype_lb(MPI_Datatype datatype)
136 MPI_Aint smpi_datatype_ub(MPI_Datatype datatype)
141 int smpi_datatype_extent(MPI_Datatype datatype, MPI_Aint * lb,
145 *extent = datatype->ub - datatype->lb;
149 MPI_Aint smpi_datatype_get_extent(MPI_Datatype datatype){
150 return datatype->ub - datatype->lb;
153 int smpi_datatype_copy(void *sendbuf, int sendcount, MPI_Datatype sendtype,
154 void *recvbuf, int recvcount, MPI_Datatype recvtype)
158 /* First check if we really have something to do */
159 if (recvcount > 0 && recvbuf != sendbuf) {
160 /* FIXME: treat packed cases */
161 sendcount *= smpi_datatype_size(sendtype);
162 recvcount *= smpi_datatype_size(recvtype);
163 count = sendcount < recvcount ? sendcount : recvcount;
165 if(sendtype->has_subtype == 0 && recvtype->has_subtype == 0) {
166 memcpy(recvbuf, sendbuf, count);
168 else if (sendtype->has_subtype == 0)
170 s_smpi_subtype_t *subtype = recvtype->substruct;
171 subtype->unserialize( sendbuf, recvbuf,1, subtype);
173 else if (recvtype->has_subtype == 0)
175 s_smpi_subtype_t *subtype = sendtype->substruct;
176 subtype->serialize(sendbuf, recvbuf,1, subtype);
178 s_smpi_subtype_t *subtype = sendtype->substruct;
180 s_smpi_mpi_vector_t* type_c = (s_smpi_mpi_vector_t*)sendtype;
182 void * buf_tmp = xbt_malloc(count * type_c->size_oldtype);
184 subtype->serialize( sendbuf, buf_tmp,1, subtype);
185 subtype = recvtype->substruct;
186 subtype->unserialize(recvbuf, buf_tmp,1, subtype);
192 return sendcount > recvcount ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
196 * Copies noncontiguous data into contiguous memory.
197 * @param contiguous_vector - output vector
198 * @param noncontiguous_vector - input vector
199 * @param type - pointer contening :
200 * - stride - stride of between noncontiguous data
201 * - block_length - the width or height of blocked matrix
202 * - count - the number of rows of matrix
204 void serialize_vector( const void *noncontiguous_vector,
205 void *contiguous_vector,
209 s_smpi_mpi_vector_t* type_c = (s_smpi_mpi_vector_t*)type;
211 char* contiguous_vector_char = (char*)contiguous_vector;
212 char* noncontiguous_vector_char = (char*)noncontiguous_vector;
214 for (i = 0; i < type_c->block_count * count; i++) {
215 if (type_c->old_type->has_subtype == 0)
216 memcpy(contiguous_vector_char,
217 noncontiguous_vector_char, type_c->block_length * type_c->size_oldtype);
219 ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_vector_char,
220 contiguous_vector_char,
221 type_c->block_length,
222 type_c->old_type->substruct);
224 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
225 noncontiguous_vector_char += type_c->block_stride*smpi_datatype_get_extent(type_c->old_type);
230 * Copies contiguous data into noncontiguous memory.
231 * @param noncontiguous_vector - output vector
232 * @param contiguous_vector - input vector
233 * @param type - pointer contening :
234 * - stride - stride of between noncontiguous data
235 * - block_length - the width or height of blocked matrix
236 * - count - the number of rows of matrix
238 void unserialize_vector( const void *contiguous_vector,
239 void *noncontiguous_vector,
243 s_smpi_mpi_vector_t* type_c = (s_smpi_mpi_vector_t*)type;
246 char* contiguous_vector_char = (char*)contiguous_vector;
247 char* noncontiguous_vector_char = (char*)noncontiguous_vector;
249 for (i = 0; i < type_c->block_count * count; i++) {
250 if (type_c->old_type->has_subtype == 0)
251 memcpy(noncontiguous_vector_char,
252 contiguous_vector_char, type_c->block_length * type_c->size_oldtype);
254 ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_vector_char,
255 noncontiguous_vector_char,
256 type_c->block_length,
257 type_c->old_type->substruct);
258 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
259 noncontiguous_vector_char += type_c->block_stride*smpi_datatype_get_extent(type_c->old_type);
264 * Create a Sub type vector to be able to serialize and unserialize it
265 * the structure s_smpi_mpi_vector_t is derived from s_smpi_subtype which
266 * required the functions unserialize and serialize
269 s_smpi_mpi_vector_t* smpi_datatype_vector_create( int block_stride,
272 MPI_Datatype old_type,
274 s_smpi_mpi_vector_t *new_t= xbt_new(s_smpi_mpi_vector_t,1);
275 new_t->base.serialize = &serialize_vector;
276 new_t->base.unserialize = &unserialize_vector;
277 new_t->base.subtype_free = &free_vector;
278 new_t->block_stride = block_stride;
279 new_t->block_length = block_length;
280 new_t->block_count = block_count;
281 new_t->old_type = old_type;
282 new_t->size_oldtype = size_oldtype;
286 void smpi_datatype_create(MPI_Datatype* new_type, int size,int lb, int ub, int has_subtype,
287 void *struct_type, int flags){
288 MPI_Datatype new_t= xbt_new(s_smpi_mpi_datatype_t,1);
290 new_t->has_subtype = has_subtype;
293 new_t->flags = flags;
294 new_t->substruct = struct_type;
299 void smpi_datatype_free(MPI_Datatype* type){
301 if((*type)->flags & DT_FLAG_PREDEFINED)return;
303 //if still used, mark for deletion
304 if((*type)->in_use!=0){
305 (*type)->flags |=DT_FLAG_DESTROYED;
309 if ((*type)->has_subtype == 1){
310 ((s_smpi_subtype_t *)(*type)->substruct)->subtype_free(type);
311 xbt_free((*type)->substruct);
317 void smpi_datatype_use(MPI_Datatype type){
318 if(type)type->in_use++;
322 void smpi_datatype_unuse(MPI_Datatype type){
323 if(type && type->in_use-- == 0 && (type->flags & DT_FLAG_DESTROYED))
324 smpi_datatype_free(&type);
327 int smpi_datatype_contiguous(int count, MPI_Datatype old_type, MPI_Datatype* new_type)
330 if(old_type->has_subtype){
331 //handle this case as a hvector with stride equals to the extent of the datatype
332 return smpi_datatype_hvector(count, 1, smpi_datatype_get_extent(old_type), old_type, new_type);
334 smpi_datatype_create(new_type,
335 count * smpi_datatype_size(old_type),
336 0,count * smpi_datatype_size(old_type),
337 0,NULL, DT_FLAG_CONTIGUOUS);
342 int smpi_datatype_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type)
345 if (blocklen<=0) return MPI_ERR_ARG;
349 lb=smpi_datatype_lb(old_type);
350 ub=((count-1)*stride+blocklen-1)*smpi_datatype_get_extent(old_type)+smpi_datatype_ub(old_type);
352 if(old_type->has_subtype || stride != blocklen){
355 s_smpi_mpi_vector_t* subtype = smpi_datatype_vector_create( stride,
359 smpi_datatype_size(old_type));
360 smpi_datatype_create(new_type,
361 count * (blocklen) * smpi_datatype_size(old_type), lb,
368 /* in this situation the data are contignous thus it's not
369 * required to serialize and unserialize it*/
370 smpi_datatype_create(new_type, count * blocklen *
371 smpi_datatype_size(old_type), 0, ((count -1) * stride + blocklen)*
372 smpi_datatype_size(old_type),
375 DT_FLAG_VECTOR|DT_FLAG_CONTIGUOUS);
381 void free_vector(MPI_Datatype* d){
385 Hvector Implementation - Vector with stride in bytes
390 * Copies noncontiguous data into contiguous memory.
391 * @param contiguous_hvector - output hvector
392 * @param noncontiguous_hvector - input hvector
393 * @param type - pointer contening :
394 * - stride - stride of between noncontiguous data, in bytes
395 * - block_length - the width or height of blocked matrix
396 * - count - the number of rows of matrix
398 void serialize_hvector( const void *noncontiguous_hvector,
399 void *contiguous_hvector,
403 s_smpi_mpi_hvector_t* type_c = (s_smpi_mpi_hvector_t*)type;
405 char* contiguous_vector_char = (char*)contiguous_hvector;
406 char* noncontiguous_vector_char = (char*)noncontiguous_hvector;
408 for (i = 0; i < type_c->block_count * count; i++) {
409 if (type_c->old_type->has_subtype == 0)
410 memcpy(contiguous_vector_char,
411 noncontiguous_vector_char, type_c->block_length * type_c->size_oldtype);
413 ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_vector_char,
414 contiguous_vector_char,
415 type_c->block_length,
416 type_c->old_type->substruct);
418 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
419 noncontiguous_vector_char += type_c->block_stride;
423 * Copies contiguous data into noncontiguous memory.
424 * @param noncontiguous_vector - output hvector
425 * @param contiguous_vector - input hvector
426 * @param type - pointer contening :
427 * - stride - stride of between noncontiguous data, in bytes
428 * - block_length - the width or height of blocked matrix
429 * - count - the number of rows of matrix
431 void unserialize_hvector( const void *contiguous_vector,
432 void *noncontiguous_vector,
436 s_smpi_mpi_hvector_t* type_c = (s_smpi_mpi_hvector_t*)type;
439 char* contiguous_vector_char = (char*)contiguous_vector;
440 char* noncontiguous_vector_char = (char*)noncontiguous_vector;
442 for (i = 0; i < type_c->block_count * count; i++) {
443 if (type_c->old_type->has_subtype == 0)
444 memcpy(noncontiguous_vector_char,
445 contiguous_vector_char, type_c->block_length * type_c->size_oldtype);
447 ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_vector_char,
448 noncontiguous_vector_char,
449 type_c->block_length,
450 type_c->old_type->substruct);
451 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
452 noncontiguous_vector_char += type_c->block_stride;
457 * Create a Sub type vector to be able to serialize and unserialize it
458 * the structure s_smpi_mpi_vector_t is derived from s_smpi_subtype which
459 * required the functions unserialize and serialize
462 s_smpi_mpi_hvector_t* smpi_datatype_hvector_create( MPI_Aint block_stride,
465 MPI_Datatype old_type,
467 s_smpi_mpi_hvector_t *new_t= xbt_new(s_smpi_mpi_hvector_t,1);
468 new_t->base.serialize = &serialize_hvector;
469 new_t->base.unserialize = &unserialize_hvector;
470 new_t->base.subtype_free = &free_hvector;
471 new_t->block_stride = block_stride;
472 new_t->block_length = block_length;
473 new_t->block_count = block_count;
474 new_t->old_type = old_type;
475 new_t->size_oldtype = size_oldtype;
479 //do nothing for vector types
480 void free_hvector(MPI_Datatype* d){
483 int smpi_datatype_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type)
486 if (blocklen<=0) return MPI_ERR_ARG;
490 lb=smpi_datatype_lb(old_type);
491 ub=((count-1)*stride)+(blocklen-1)*smpi_datatype_get_extent(old_type)+smpi_datatype_ub(old_type);
493 if(old_type->has_subtype || stride != blocklen*smpi_datatype_get_extent(old_type)){
494 s_smpi_mpi_hvector_t* subtype = smpi_datatype_hvector_create( stride,
498 smpi_datatype_size(old_type));
500 smpi_datatype_create(new_type, count * blocklen * smpi_datatype_size(old_type),
507 smpi_datatype_create(new_type, count * blocklen *
508 smpi_datatype_size(old_type),0,count * blocklen *
509 smpi_datatype_size(old_type),
512 DT_FLAG_VECTOR|DT_FLAG_CONTIGUOUS);
520 Indexed Implementation
524 * Copies noncontiguous data into contiguous memory.
525 * @param contiguous_indexed - output indexed
526 * @param noncontiguous_indexed - input indexed
527 * @param type - pointer contening :
528 * - block_lengths - the width or height of blocked matrix
529 * - block_indices - indices of each data, in element
530 * - count - the number of rows of matrix
532 void serialize_indexed( const void *noncontiguous_indexed,
533 void *contiguous_indexed,
537 s_smpi_mpi_indexed_t* type_c = (s_smpi_mpi_indexed_t*)type;
539 char* contiguous_indexed_char = (char*)contiguous_indexed;
540 char* noncontiguous_indexed_char = (char*)noncontiguous_indexed;
541 for(j=0; j<count;j++){
542 for (i = 0; i < type_c->block_count; i++) {
543 if (type_c->old_type->has_subtype == 0)
544 memcpy(contiguous_indexed_char,
545 noncontiguous_indexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
547 ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_indexed_char,
548 contiguous_indexed_char,
549 type_c->block_lengths[i],
550 type_c->old_type->substruct);
553 contiguous_indexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
554 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);
555 else noncontiguous_indexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
557 noncontiguous_indexed=(void*)noncontiguous_indexed_char;
561 * Copies contiguous data into noncontiguous memory.
562 * @param noncontiguous_indexed - output indexed
563 * @param contiguous_indexed - input indexed
564 * @param type - pointer contening :
565 * - block_lengths - the width or height of blocked matrix
566 * - block_indices - indices of each data, in element
567 * - count - the number of rows of matrix
569 void unserialize_indexed( const void *contiguous_indexed,
570 void *noncontiguous_indexed,
574 s_smpi_mpi_indexed_t* type_c = (s_smpi_mpi_indexed_t*)type;
577 char* contiguous_indexed_char = (char*)contiguous_indexed;
578 char* noncontiguous_indexed_char = (char*)noncontiguous_indexed;
579 for(j=0; j<count;j++){
580 for (i = 0; i < type_c->block_count; i++) {
581 if (type_c->old_type->has_subtype == 0)
582 memcpy(noncontiguous_indexed_char,
583 contiguous_indexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
585 ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_indexed_char,
586 noncontiguous_indexed_char,
587 type_c->block_lengths[i],
588 type_c->old_type->substruct);
590 contiguous_indexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
591 if (i<type_c->block_count-1)
592 noncontiguous_indexed_char = (char*)noncontiguous_indexed + type_c->block_indices[i+1]*smpi_datatype_get_extent(type_c->old_type);
593 else noncontiguous_indexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
595 noncontiguous_indexed=(void*)noncontiguous_indexed_char;
599 void free_indexed(MPI_Datatype* type){
600 xbt_free(((s_smpi_mpi_indexed_t *)(*type)->substruct)->block_lengths);
601 xbt_free(((s_smpi_mpi_indexed_t *)(*type)->substruct)->block_indices);
605 * Create a Sub type indexed to be able to serialize and unserialize it
606 * the structure s_smpi_mpi_indexed_t is derived from s_smpi_subtype which
607 * required the functions unserialize and serialize
609 s_smpi_mpi_indexed_t* smpi_datatype_indexed_create( int* block_lengths,
612 MPI_Datatype old_type,
614 s_smpi_mpi_indexed_t *new_t= xbt_new(s_smpi_mpi_indexed_t,1);
615 new_t->base.serialize = &serialize_indexed;
616 new_t->base.unserialize = &unserialize_indexed;
617 new_t->base.subtype_free = &free_indexed;
618 //TODO : add a custom function for each time to clean these
619 new_t->block_lengths= xbt_new(int, block_count);
620 new_t->block_indices= xbt_new(int, block_count);
622 for(i=0;i<block_count;i++){
623 new_t->block_lengths[i]=block_lengths[i];
624 new_t->block_indices[i]=block_indices[i];
626 new_t->block_count = block_count;
627 new_t->old_type = old_type;
628 new_t->size_oldtype = size_oldtype;
633 int smpi_datatype_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
642 lb=indices[0]*smpi_datatype_get_extent(old_type);
643 ub=indices[0]*smpi_datatype_get_extent(old_type) + blocklens[0]*smpi_datatype_ub(old_type);
646 for(i=0; i< count; i++){
649 size += blocklens[i];
651 if(indices[i]*smpi_datatype_get_extent(old_type)+smpi_datatype_lb(old_type)<lb)
652 lb = indices[i]*smpi_datatype_get_extent(old_type)+smpi_datatype_lb(old_type);
653 if(indices[i]*smpi_datatype_get_extent(old_type)+blocklens[i]*smpi_datatype_ub(old_type)>ub)
654 ub = indices[i]*smpi_datatype_get_extent(old_type)+blocklens[i]*smpi_datatype_ub(old_type);
656 if ( (i< count -1) && (indices[i]+blocklens[i] != indices[i+1]) )contiguous=0;
658 if (old_type->has_subtype == 1)
662 s_smpi_mpi_indexed_t* subtype = smpi_datatype_indexed_create( blocklens,
666 smpi_datatype_size(old_type));
667 smpi_datatype_create(new_type, size *
668 smpi_datatype_size(old_type),lb,ub,1, subtype, DT_FLAG_DATA);
670 smpi_datatype_create(new_type, size *
671 smpi_datatype_size(old_type),0,size *
672 smpi_datatype_size(old_type),0, NULL, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
680 Hindexed Implementation - Indexed with indices in bytes
684 * Copies noncontiguous data into contiguous memory.
685 * @param contiguous_hindexed - output hindexed
686 * @param noncontiguous_hindexed - input hindexed
687 * @param type - pointer contening :
688 * - block_lengths - the width or height of blocked matrix
689 * - block_indices - indices of each data, in bytes
690 * - count - the number of rows of matrix
692 void serialize_hindexed( const void *noncontiguous_hindexed,
693 void *contiguous_hindexed,
697 s_smpi_mpi_hindexed_t* type_c = (s_smpi_mpi_hindexed_t*)type;
699 char* contiguous_hindexed_char = (char*)contiguous_hindexed;
700 char* noncontiguous_hindexed_char = (char*)noncontiguous_hindexed;
701 for(j=0; j<count;j++){
702 for (i = 0; i < type_c->block_count; i++) {
703 if (type_c->old_type->has_subtype == 0)
704 memcpy(contiguous_hindexed_char,
705 noncontiguous_hindexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
707 ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_hindexed_char,
708 contiguous_hindexed_char,
709 type_c->block_lengths[i],
710 type_c->old_type->substruct);
712 contiguous_hindexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
713 if (i<type_c->block_count-1)noncontiguous_hindexed_char = (char*)noncontiguous_hindexed + type_c->block_indices[i+1];
714 else noncontiguous_hindexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
716 noncontiguous_hindexed=(void*)noncontiguous_hindexed_char;
720 * Copies contiguous data into noncontiguous memory.
721 * @param noncontiguous_hindexed - output hindexed
722 * @param contiguous_hindexed - input hindexed
723 * @param type - pointer contening :
724 * - block_lengths - the width or height of blocked matrix
725 * - block_indices - indices of each data, in bytes
726 * - count - the number of rows of matrix
728 void unserialize_hindexed( const void *contiguous_hindexed,
729 void *noncontiguous_hindexed,
733 s_smpi_mpi_hindexed_t* type_c = (s_smpi_mpi_hindexed_t*)type;
736 char* contiguous_hindexed_char = (char*)contiguous_hindexed;
737 char* noncontiguous_hindexed_char = (char*)noncontiguous_hindexed;
738 for(j=0; j<count;j++){
739 for (i = 0; i < type_c->block_count; i++) {
740 if (type_c->old_type->has_subtype == 0)
741 memcpy(noncontiguous_hindexed_char,
742 contiguous_hindexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
744 ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_hindexed_char,
745 noncontiguous_hindexed_char,
746 type_c->block_lengths[i],
747 type_c->old_type->substruct);
749 contiguous_hindexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
750 if (i<type_c->block_count-1)noncontiguous_hindexed_char = (char*)noncontiguous_hindexed + type_c->block_indices[i+1];
751 else noncontiguous_hindexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
753 noncontiguous_hindexed=(void*)noncontiguous_hindexed_char;
757 void free_hindexed(MPI_Datatype* type){
758 xbt_free(((s_smpi_mpi_hindexed_t *)(*type)->substruct)->block_lengths);
759 xbt_free(((s_smpi_mpi_hindexed_t *)(*type)->substruct)->block_indices);
763 * Create a Sub type hindexed to be able to serialize and unserialize it
764 * the structure s_smpi_mpi_hindexed_t is derived from s_smpi_subtype which
765 * required the functions unserialize and serialize
767 s_smpi_mpi_hindexed_t* smpi_datatype_hindexed_create( int* block_lengths,
768 MPI_Aint* block_indices,
770 MPI_Datatype old_type,
772 s_smpi_mpi_hindexed_t *new_t= xbt_new(s_smpi_mpi_hindexed_t,1);
773 new_t->base.serialize = &serialize_hindexed;
774 new_t->base.unserialize = &unserialize_hindexed;
775 new_t->base.subtype_free = &free_hindexed;
776 //TODO : add a custom function for each time to clean these
777 new_t->block_lengths= xbt_new(int, block_count);
778 new_t->block_indices= xbt_new(MPI_Aint, block_count);
780 for(i=0;i<block_count;i++){
781 new_t->block_lengths[i]=block_lengths[i];
782 new_t->block_indices[i]=block_indices[i];
784 new_t->block_count = block_count;
785 new_t->old_type = old_type;
786 new_t->size_oldtype = size_oldtype;
791 int smpi_datatype_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
800 lb=indices[0] + smpi_datatype_lb(old_type);
801 ub=indices[0] + blocklens[0]*smpi_datatype_ub(old_type);
803 for(i=0; i< count; i++){
806 size += blocklens[i];
808 if(indices[i]+smpi_datatype_lb(old_type)<lb) lb = indices[i]+smpi_datatype_lb(old_type);
809 if(indices[i]+blocklens[i]*smpi_datatype_ub(old_type)>ub) ub = indices[i]+blocklens[i]*smpi_datatype_ub(old_type);
811 if ( (i< count -1) && (indices[i]+blocklens[i]*smpi_datatype_size(old_type) != indices[i+1]) )contiguous=0;
813 if (old_type->has_subtype == 1 || lb!=0)
817 s_smpi_mpi_hindexed_t* subtype = smpi_datatype_hindexed_create( blocklens,
821 smpi_datatype_size(old_type));
822 smpi_datatype_create(new_type, size * smpi_datatype_size(old_type),
825 ,1, subtype, DT_FLAG_DATA);
827 smpi_datatype_create(new_type, size * smpi_datatype_size(old_type),
828 0,size * smpi_datatype_size(old_type),
829 0, NULL, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
837 struct Implementation - Indexed with indices in bytes
841 * Copies noncontiguous data into contiguous memory.
842 * @param contiguous_struct - output struct
843 * @param noncontiguous_struct - input struct
844 * @param type - pointer contening :
845 * - stride - stride of between noncontiguous data
846 * - block_length - the width or height of blocked matrix
847 * - count - the number of rows of matrix
849 void serialize_struct( const void *noncontiguous_struct,
850 void *contiguous_struct,
854 s_smpi_mpi_struct_t* type_c = (s_smpi_mpi_struct_t*)type;
856 char* contiguous_struct_char = (char*)contiguous_struct;
857 char* noncontiguous_struct_char = (char*)noncontiguous_struct;
858 for(j=0; j<count;j++){
859 for (i = 0; i < type_c->block_count; i++) {
860 if (type_c->old_types[i]->has_subtype == 0)
861 memcpy(contiguous_struct_char,
862 noncontiguous_struct_char, type_c->block_lengths[i] * smpi_datatype_size(type_c->old_types[i]));
864 ((s_smpi_subtype_t*)type_c->old_types[i]->substruct)->serialize( noncontiguous_struct_char,
865 contiguous_struct_char,
866 type_c->block_lengths[i],
867 type_c->old_types[i]->substruct);
870 contiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_size(type_c->old_types[i]);
871 if (i<type_c->block_count-1)noncontiguous_struct_char = (char*)noncontiguous_struct + type_c->block_indices[i+1];
872 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 ?
874 noncontiguous_struct=(void*)noncontiguous_struct_char;
878 * Copies contiguous data into noncontiguous memory.
879 * @param noncontiguous_struct - output struct
880 * @param contiguous_struct - input struct
881 * @param type - pointer contening :
882 * - stride - stride of between noncontiguous data
883 * - block_length - the width or height of blocked matrix
884 * - count - the number of rows of matrix
886 void unserialize_struct( const void *contiguous_struct,
887 void *noncontiguous_struct,
891 s_smpi_mpi_struct_t* type_c = (s_smpi_mpi_struct_t*)type;
894 char* contiguous_struct_char = (char*)contiguous_struct;
895 char* noncontiguous_struct_char = (char*)noncontiguous_struct;
896 for(j=0; j<count;j++){
897 for (i = 0; i < type_c->block_count; i++) {
898 if (type_c->old_types[i]->has_subtype == 0)
899 memcpy(noncontiguous_struct_char,
900 contiguous_struct_char, type_c->block_lengths[i] * smpi_datatype_size(type_c->old_types[i]));
902 ((s_smpi_subtype_t*)type_c->old_types[i]->substruct)->unserialize( contiguous_struct_char,
903 noncontiguous_struct_char,
904 type_c->block_lengths[i],
905 type_c->old_types[i]->substruct);
907 contiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_size(type_c->old_types[i]);
908 if (i<type_c->block_count-1)noncontiguous_struct_char = (char*)noncontiguous_struct + type_c->block_indices[i+1];
909 else noncontiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_types[i]);
911 noncontiguous_struct=(void*)noncontiguous_struct_char;
916 void free_struct(MPI_Datatype* type){
917 xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->block_lengths);
918 xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->block_indices);
919 xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->old_types);
923 * Create a Sub type struct to be able to serialize and unserialize it
924 * the structure s_smpi_mpi_struct_t is derived from s_smpi_subtype which
925 * required the functions unserialize and serialize
927 s_smpi_mpi_struct_t* smpi_datatype_struct_create( int* block_lengths,
928 MPI_Aint* block_indices,
930 MPI_Datatype* old_types){
931 s_smpi_mpi_struct_t *new_t= xbt_new(s_smpi_mpi_struct_t,1);
932 new_t->base.serialize = &serialize_struct;
933 new_t->base.unserialize = &unserialize_struct;
934 new_t->base.subtype_free = &free_struct;
935 //TODO : add a custom function for each time to clean these
936 new_t->block_lengths= xbt_new(int, block_count);
937 new_t->block_indices= xbt_new(MPI_Aint, block_count);
938 new_t->old_types= xbt_new(MPI_Datatype, block_count);
940 for(i=0;i<block_count;i++){
941 new_t->block_lengths[i]=block_lengths[i];
942 new_t->block_indices[i]=block_indices[i];
943 new_t->old_types[i]=old_types[i];
945 //new_t->block_lengths = block_lengths;
946 //new_t->block_indices = block_indices;
947 new_t->block_count = block_count;
948 //new_t->old_types = old_types;
953 int smpi_datatype_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type)
962 lb=indices[0] + smpi_datatype_lb(old_types[0]);
963 ub=indices[0] + blocklens[0]*smpi_datatype_ub(old_types[0]);
967 for(i=0; i< count; i++){
970 if (old_types[i]->has_subtype == 1)
973 size += blocklens[i]*smpi_datatype_size(old_types[i]);
974 if (old_types[i]==MPI_LB){
978 if (old_types[i]==MPI_UB){
983 if(!forced_lb && indices[i]+smpi_datatype_lb(old_types[i])<lb) lb = indices[i];
984 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]);
986 if ( (i< count -1) && (indices[i]+blocklens[i]*smpi_datatype_size(old_types[i]) != indices[i+1]) )contiguous=0;
990 s_smpi_mpi_struct_t* subtype = smpi_datatype_struct_create( blocklens,
995 smpi_datatype_create(new_type, size, lb, ub,1, subtype, DT_FLAG_DATA);
997 smpi_datatype_create(new_type, size, lb, ub,0, NULL, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
1002 void smpi_datatype_commit(MPI_Datatype *datatype)
1004 (*datatype)->flags= ((*datatype)->flags | DT_FLAG_COMMITED);
1007 typedef struct s_smpi_mpi_op {
1008 MPI_User_function *func;
1011 #define MAX_OP(a, b) (b) = (a) < (b) ? (b) : (a)
1012 #define MIN_OP(a, b) (b) = (a) < (b) ? (a) : (b)
1013 #define SUM_OP(a, b) (b) += (a)
1014 #define PROD_OP(a, b) (b) *= (a)
1015 #define LAND_OP(a, b) (b) = (a) && (b)
1016 #define LOR_OP(a, b) (b) = (a) || (b)
1017 #define LXOR_OP(a, b) (b) = (!(a) && (b)) || ((a) && !(b))
1018 #define BAND_OP(a, b) (b) &= (a)
1019 #define BOR_OP(a, b) (b) |= (a)
1020 #define BXOR_OP(a, b) (b) ^= (a)
1021 #define MAXLOC_OP(a, b) (b) = (a.value) < (b.value) ? (b) : (a)
1022 #define MINLOC_OP(a, b) (b) = (a.value) < (b.value) ? (a) : (b)
1023 //TODO : MINLOC & MAXLOC
1025 #define APPLY_FUNC(a, b, length, type, func) \
1028 type* x = (type*)(a); \
1029 type* y = (type*)(b); \
1030 for(i = 0; i < *(length); i++) { \
1035 static void max_func(void *a, void *b, int *length,
1036 MPI_Datatype * datatype)
1038 if (*datatype == MPI_CHAR) {
1039 APPLY_FUNC(a, b, length, char, MAX_OP);
1040 } else if (*datatype == MPI_SHORT) {
1041 APPLY_FUNC(a, b, length, short, MAX_OP);
1042 } else if (*datatype == MPI_INT) {
1043 APPLY_FUNC(a, b, length, int, MAX_OP);
1044 } else if (*datatype == MPI_LONG) {
1045 APPLY_FUNC(a, b, length, long, MAX_OP);
1046 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1047 APPLY_FUNC(a, b, length, unsigned short, MAX_OP);
1048 } else if (*datatype == MPI_UNSIGNED) {
1049 APPLY_FUNC(a, b, length, unsigned int, MAX_OP);
1050 } else if (*datatype == MPI_UNSIGNED_LONG) {
1051 APPLY_FUNC(a, b, length, unsigned long, MAX_OP);
1052 } else if (*datatype == MPI_FLOAT) {
1053 APPLY_FUNC(a, b, length, float, MAX_OP);
1054 } else if (*datatype == MPI_DOUBLE) {
1055 APPLY_FUNC(a, b, length, double, MAX_OP);
1056 } else if (*datatype == MPI_LONG_DOUBLE) {
1057 APPLY_FUNC(a, b, length, long double, MAX_OP);
1061 static void min_func(void *a, void *b, int *length,
1062 MPI_Datatype * datatype)
1064 if (*datatype == MPI_CHAR) {
1065 APPLY_FUNC(a, b, length, char, MIN_OP);
1066 } else if (*datatype == MPI_SHORT) {
1067 APPLY_FUNC(a, b, length, short, MIN_OP);
1068 } else if (*datatype == MPI_INT) {
1069 APPLY_FUNC(a, b, length, int, MIN_OP);
1070 } else if (*datatype == MPI_LONG) {
1071 APPLY_FUNC(a, b, length, long, MIN_OP);
1072 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1073 APPLY_FUNC(a, b, length, unsigned short, MIN_OP);
1074 } else if (*datatype == MPI_UNSIGNED) {
1075 APPLY_FUNC(a, b, length, unsigned int, MIN_OP);
1076 } else if (*datatype == MPI_UNSIGNED_LONG) {
1077 APPLY_FUNC(a, b, length, unsigned long, MIN_OP);
1078 } else if (*datatype == MPI_FLOAT) {
1079 APPLY_FUNC(a, b, length, float, MIN_OP);
1080 } else if (*datatype == MPI_DOUBLE) {
1081 APPLY_FUNC(a, b, length, double, MIN_OP);
1082 } else if (*datatype == MPI_LONG_DOUBLE) {
1083 APPLY_FUNC(a, b, length, long double, MIN_OP);
1087 static void sum_func(void *a, void *b, int *length,
1088 MPI_Datatype * datatype)
1090 if (*datatype == MPI_CHAR) {
1091 APPLY_FUNC(a, b, length, char, SUM_OP);
1092 } else if (*datatype == MPI_SHORT) {
1093 APPLY_FUNC(a, b, length, short, SUM_OP);
1094 } else if (*datatype == MPI_INT) {
1095 APPLY_FUNC(a, b, length, int, SUM_OP);
1096 } else if (*datatype == MPI_LONG) {
1097 APPLY_FUNC(a, b, length, long, SUM_OP);
1098 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1099 APPLY_FUNC(a, b, length, unsigned short, SUM_OP);
1100 } else if (*datatype == MPI_UNSIGNED) {
1101 APPLY_FUNC(a, b, length, unsigned int, SUM_OP);
1102 } else if (*datatype == MPI_UNSIGNED_LONG) {
1103 APPLY_FUNC(a, b, length, unsigned long, SUM_OP);
1104 } else if (*datatype == MPI_FLOAT) {
1105 APPLY_FUNC(a, b, length, float, SUM_OP);
1106 } else if (*datatype == MPI_DOUBLE) {
1107 APPLY_FUNC(a, b, length, double, SUM_OP);
1108 } else if (*datatype == MPI_LONG_DOUBLE) {
1109 APPLY_FUNC(a, b, length, long double, SUM_OP);
1110 } else if (*datatype == MPI_C_FLOAT_COMPLEX) {
1111 APPLY_FUNC(a, b, length, float _Complex, SUM_OP);
1112 } else if (*datatype == MPI_C_DOUBLE_COMPLEX) {
1113 APPLY_FUNC(a, b, length, double _Complex, SUM_OP);
1114 } else if (*datatype == MPI_C_LONG_DOUBLE_COMPLEX) {
1115 APPLY_FUNC(a, b, length, long double _Complex, SUM_OP);
1119 static void prod_func(void *a, void *b, int *length,
1120 MPI_Datatype * datatype)
1122 if (*datatype == MPI_CHAR) {
1123 APPLY_FUNC(a, b, length, char, PROD_OP);
1124 } else if (*datatype == MPI_SHORT) {
1125 APPLY_FUNC(a, b, length, short, PROD_OP);
1126 } else if (*datatype == MPI_INT) {
1127 APPLY_FUNC(a, b, length, int, PROD_OP);
1128 } else if (*datatype == MPI_LONG) {
1129 APPLY_FUNC(a, b, length, long, PROD_OP);
1130 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1131 APPLY_FUNC(a, b, length, unsigned short, PROD_OP);
1132 } else if (*datatype == MPI_UNSIGNED) {
1133 APPLY_FUNC(a, b, length, unsigned int, PROD_OP);
1134 } else if (*datatype == MPI_UNSIGNED_LONG) {
1135 APPLY_FUNC(a, b, length, unsigned long, PROD_OP);
1136 } else if (*datatype == MPI_FLOAT) {
1137 APPLY_FUNC(a, b, length, float, PROD_OP);
1138 } else if (*datatype == MPI_DOUBLE) {
1139 APPLY_FUNC(a, b, length, double, PROD_OP);
1140 } else if (*datatype == MPI_LONG_DOUBLE) {
1141 APPLY_FUNC(a, b, length, long double, PROD_OP);
1142 } else if (*datatype == MPI_C_FLOAT_COMPLEX) {
1143 APPLY_FUNC(a, b, length, float _Complex, PROD_OP);
1144 } else if (*datatype == MPI_C_DOUBLE_COMPLEX) {
1145 APPLY_FUNC(a, b, length, double _Complex, PROD_OP);
1146 } else if (*datatype == MPI_C_LONG_DOUBLE_COMPLEX) {
1147 APPLY_FUNC(a, b, length, long double _Complex, PROD_OP);
1151 static void land_func(void *a, void *b, int *length,
1152 MPI_Datatype * datatype)
1154 if (*datatype == MPI_CHAR) {
1155 APPLY_FUNC(a, b, length, char, LAND_OP);
1156 } else if (*datatype == MPI_SHORT) {
1157 APPLY_FUNC(a, b, length, short, LAND_OP);
1158 } else if (*datatype == MPI_INT) {
1159 APPLY_FUNC(a, b, length, int, LAND_OP);
1160 } else if (*datatype == MPI_LONG) {
1161 APPLY_FUNC(a, b, length, long, LAND_OP);
1162 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1163 APPLY_FUNC(a, b, length, unsigned short, LAND_OP);
1164 } else if (*datatype == MPI_UNSIGNED) {
1165 APPLY_FUNC(a, b, length, unsigned int, LAND_OP);
1166 } else if (*datatype == MPI_UNSIGNED_LONG) {
1167 APPLY_FUNC(a, b, length, unsigned long, LAND_OP);
1168 } else if (*datatype == MPI_C_BOOL) {
1169 APPLY_FUNC(a, b, length, _Bool, LAND_OP);
1173 static void lor_func(void *a, void *b, int *length,
1174 MPI_Datatype * datatype)
1176 if (*datatype == MPI_CHAR) {
1177 APPLY_FUNC(a, b, length, char, LOR_OP);
1178 } else if (*datatype == MPI_SHORT) {
1179 APPLY_FUNC(a, b, length, short, LOR_OP);
1180 } else if (*datatype == MPI_INT) {
1181 APPLY_FUNC(a, b, length, int, LOR_OP);
1182 } else if (*datatype == MPI_LONG) {
1183 APPLY_FUNC(a, b, length, long, LOR_OP);
1184 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1185 APPLY_FUNC(a, b, length, unsigned short, LOR_OP);
1186 } else if (*datatype == MPI_UNSIGNED) {
1187 APPLY_FUNC(a, b, length, unsigned int, LOR_OP);
1188 } else if (*datatype == MPI_UNSIGNED_LONG) {
1189 APPLY_FUNC(a, b, length, unsigned long, LOR_OP);
1190 } else if (*datatype == MPI_C_BOOL) {
1191 APPLY_FUNC(a, b, length, _Bool, LOR_OP);
1195 static void lxor_func(void *a, void *b, int *length,
1196 MPI_Datatype * datatype)
1198 if (*datatype == MPI_CHAR) {
1199 APPLY_FUNC(a, b, length, char, LXOR_OP);
1200 } else if (*datatype == MPI_SHORT) {
1201 APPLY_FUNC(a, b, length, short, LXOR_OP);
1202 } else if (*datatype == MPI_INT) {
1203 APPLY_FUNC(a, b, length, int, LXOR_OP);
1204 } else if (*datatype == MPI_LONG) {
1205 APPLY_FUNC(a, b, length, long, LXOR_OP);
1206 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1207 APPLY_FUNC(a, b, length, unsigned short, LXOR_OP);
1208 } else if (*datatype == MPI_UNSIGNED) {
1209 APPLY_FUNC(a, b, length, unsigned int, LXOR_OP);
1210 } else if (*datatype == MPI_UNSIGNED_LONG) {
1211 APPLY_FUNC(a, b, length, unsigned long, LXOR_OP);
1212 } else if (*datatype == MPI_C_BOOL) {
1213 APPLY_FUNC(a, b, length, _Bool, LXOR_OP);
1217 static void band_func(void *a, void *b, int *length,
1218 MPI_Datatype * datatype)
1220 if (*datatype == MPI_CHAR) {
1221 APPLY_FUNC(a, b, length, char, BAND_OP);
1223 if (*datatype == MPI_SHORT) {
1224 APPLY_FUNC(a, b, length, short, BAND_OP);
1225 } else if (*datatype == MPI_INT) {
1226 APPLY_FUNC(a, b, length, int, BAND_OP);
1227 } else if (*datatype == MPI_LONG) {
1228 APPLY_FUNC(a, b, length, long, BAND_OP);
1229 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1230 APPLY_FUNC(a, b, length, unsigned short, BAND_OP);
1231 } else if (*datatype == MPI_UNSIGNED) {
1232 APPLY_FUNC(a, b, length, unsigned int, BAND_OP);
1233 } else if (*datatype == MPI_UNSIGNED_LONG) {
1234 APPLY_FUNC(a, b, length, unsigned long, BAND_OP);
1235 } else if (*datatype == MPI_BYTE) {
1236 APPLY_FUNC(a, b, length, uint8_t, BAND_OP);
1240 static void bor_func(void *a, void *b, int *length,
1241 MPI_Datatype * datatype)
1243 if (*datatype == MPI_CHAR) {
1244 APPLY_FUNC(a, b, length, char, BOR_OP);
1245 } else if (*datatype == MPI_SHORT) {
1246 APPLY_FUNC(a, b, length, short, BOR_OP);
1247 } else if (*datatype == MPI_INT) {
1248 APPLY_FUNC(a, b, length, int, BOR_OP);
1249 } else if (*datatype == MPI_LONG) {
1250 APPLY_FUNC(a, b, length, long, BOR_OP);
1251 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1252 APPLY_FUNC(a, b, length, unsigned short, BOR_OP);
1253 } else if (*datatype == MPI_UNSIGNED) {
1254 APPLY_FUNC(a, b, length, unsigned int, BOR_OP);
1255 } else if (*datatype == MPI_UNSIGNED_LONG) {
1256 APPLY_FUNC(a, b, length, unsigned long, BOR_OP);
1257 } else if (*datatype == MPI_BYTE) {
1258 APPLY_FUNC(a, b, length, uint8_t, BOR_OP);
1262 static void bxor_func(void *a, void *b, int *length,
1263 MPI_Datatype * datatype)
1265 if (*datatype == MPI_CHAR) {
1266 APPLY_FUNC(a, b, length, char, BXOR_OP);
1267 } else if (*datatype == MPI_SHORT) {
1268 APPLY_FUNC(a, b, length, short, BXOR_OP);
1269 } else if (*datatype == MPI_INT) {
1270 APPLY_FUNC(a, b, length, int, BXOR_OP);
1271 } else if (*datatype == MPI_LONG) {
1272 APPLY_FUNC(a, b, length, long, BXOR_OP);
1273 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1274 APPLY_FUNC(a, b, length, unsigned short, BXOR_OP);
1275 } else if (*datatype == MPI_UNSIGNED) {
1276 APPLY_FUNC(a, b, length, unsigned int, BXOR_OP);
1277 } else if (*datatype == MPI_UNSIGNED_LONG) {
1278 APPLY_FUNC(a, b, length, unsigned long, BXOR_OP);
1279 } else if (*datatype == MPI_BYTE) {
1280 APPLY_FUNC(a, b, length, uint8_t, BXOR_OP);
1284 static void minloc_func(void *a, void *b, int *length,
1285 MPI_Datatype * datatype)
1287 if (*datatype == MPI_FLOAT_INT) {
1288 APPLY_FUNC(a, b, length, float_int, MINLOC_OP);
1289 } else if (*datatype == MPI_LONG_INT) {
1290 APPLY_FUNC(a, b, length, long_int, MINLOC_OP);
1291 } else if (*datatype == MPI_DOUBLE_INT) {
1292 APPLY_FUNC(a, b, length, double_int, MINLOC_OP);
1293 } else if (*datatype == MPI_SHORT_INT) {
1294 APPLY_FUNC(a, b, length, short_int, MINLOC_OP);
1295 } else if (*datatype == MPI_2INT) {
1296 APPLY_FUNC(a, b, length, int_int, MINLOC_OP);
1297 } else if (*datatype == MPI_LONG_DOUBLE_INT) {
1298 APPLY_FUNC(a, b, length, long_double_int, MINLOC_OP);
1299 } else if (*datatype == MPI_2FLOAT) {
1300 APPLY_FUNC(a, b, length, float_float, MINLOC_OP);
1301 } else if (*datatype == MPI_2DOUBLE) {
1302 APPLY_FUNC(a, b, length, double_double, MINLOC_OP);
1306 static void maxloc_func(void *a, void *b, int *length,
1307 MPI_Datatype * datatype)
1309 if (*datatype == MPI_FLOAT_INT) {
1310 APPLY_FUNC(a, b, length, float_int, MAXLOC_OP);
1311 } else if (*datatype == MPI_LONG_INT) {
1312 APPLY_FUNC(a, b, length, long_int, MAXLOC_OP);
1313 } else if (*datatype == MPI_DOUBLE_INT) {
1314 APPLY_FUNC(a, b, length, double_int, MAXLOC_OP);
1315 } else if (*datatype == MPI_SHORT_INT) {
1316 APPLY_FUNC(a, b, length, short_int, MAXLOC_OP);
1317 } else if (*datatype == MPI_2INT) {
1318 APPLY_FUNC(a, b, length, int_int, MAXLOC_OP);
1319 } else if (*datatype == MPI_LONG_DOUBLE_INT) {
1320 APPLY_FUNC(a, b, length, long_double_int, MAXLOC_OP);
1321 } else if (*datatype == MPI_2FLOAT) {
1322 APPLY_FUNC(a, b, length, float_float, MAXLOC_OP);
1323 } else if (*datatype == MPI_2DOUBLE) {
1324 APPLY_FUNC(a, b, length, double_double, MAXLOC_OP);
1329 #define CREATE_MPI_OP(name, func) \
1330 static s_smpi_mpi_op_t mpi_##name = { &(func) /* func */ }; \
1331 MPI_Op name = &mpi_##name;
1333 CREATE_MPI_OP(MPI_MAX, max_func);
1334 CREATE_MPI_OP(MPI_MIN, min_func);
1335 CREATE_MPI_OP(MPI_SUM, sum_func);
1336 CREATE_MPI_OP(MPI_PROD, prod_func);
1337 CREATE_MPI_OP(MPI_LAND, land_func);
1338 CREATE_MPI_OP(MPI_LOR, lor_func);
1339 CREATE_MPI_OP(MPI_LXOR, lxor_func);
1340 CREATE_MPI_OP(MPI_BAND, band_func);
1341 CREATE_MPI_OP(MPI_BOR, bor_func);
1342 CREATE_MPI_OP(MPI_BXOR, bxor_func);
1343 CREATE_MPI_OP(MPI_MAXLOC, maxloc_func);
1344 CREATE_MPI_OP(MPI_MINLOC, minloc_func);
1346 MPI_Op smpi_op_new(MPI_User_function * function, int commute)
1350 //FIXME: add commute param
1351 op = xbt_new(s_smpi_mpi_op_t, 1);
1352 op->func = function;
1356 void smpi_op_destroy(MPI_Op op)
1361 void smpi_op_apply(MPI_Op op, void *invec, void *inoutvec, int *len,
1362 MPI_Datatype * datatype)
1364 op->func(invec, inoutvec, len, datatype);