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.
83 // Predefined data types
84 CREATE_MPI_DATATYPE(MPI_CHAR, char);
85 CREATE_MPI_DATATYPE(MPI_SHORT, short);
86 CREATE_MPI_DATATYPE(MPI_INT, int);
87 CREATE_MPI_DATATYPE(MPI_LONG, long);
88 CREATE_MPI_DATATYPE(MPI_LONG_LONG, long long);
89 CREATE_MPI_DATATYPE(MPI_SIGNED_CHAR, signed char);
90 CREATE_MPI_DATATYPE(MPI_UNSIGNED_CHAR, unsigned char);
91 CREATE_MPI_DATATYPE(MPI_UNSIGNED_SHORT, unsigned short);
92 CREATE_MPI_DATATYPE(MPI_UNSIGNED, unsigned int);
93 CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG, unsigned long);
94 CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG_LONG, unsigned long long);
95 CREATE_MPI_DATATYPE(MPI_FLOAT, float);
96 CREATE_MPI_DATATYPE(MPI_DOUBLE, double);
97 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE, long double);
98 CREATE_MPI_DATATYPE(MPI_WCHAR, wchar_t);
99 CREATE_MPI_DATATYPE(MPI_C_BOOL, _Bool);
100 CREATE_MPI_DATATYPE(MPI_INT8_T, int8_t);
101 CREATE_MPI_DATATYPE(MPI_INT16_T, int16_t);
102 CREATE_MPI_DATATYPE(MPI_INT32_T, int32_t);
103 CREATE_MPI_DATATYPE(MPI_INT64_T, int64_t);
104 CREATE_MPI_DATATYPE(MPI_UINT8_T, uint8_t);
105 CREATE_MPI_DATATYPE(MPI_UINT16_T, uint16_t);
106 CREATE_MPI_DATATYPE(MPI_UINT32_T, uint32_t);
107 CREATE_MPI_DATATYPE(MPI_UINT64_T, uint64_t);
108 CREATE_MPI_DATATYPE(MPI_C_FLOAT_COMPLEX, float _Complex);
109 CREATE_MPI_DATATYPE(MPI_C_DOUBLE_COMPLEX, double _Complex);
110 CREATE_MPI_DATATYPE(MPI_C_LONG_DOUBLE_COMPLEX, long double _Complex);
111 CREATE_MPI_DATATYPE(MPI_AINT, MPI_Aint);
112 CREATE_MPI_DATATYPE(MPI_OFFSET, MPI_Offset);
114 CREATE_MPI_DATATYPE(MPI_FLOAT_INT, float_int);
115 CREATE_MPI_DATATYPE(MPI_LONG_INT, long_int);
116 CREATE_MPI_DATATYPE(MPI_DOUBLE_INT, double_int);
117 CREATE_MPI_DATATYPE(MPI_SHORT_INT, short_int);
118 CREATE_MPI_DATATYPE(MPI_2INT, int_int);
119 CREATE_MPI_DATATYPE(MPI_2FLOAT, float_float);
120 CREATE_MPI_DATATYPE(MPI_2DOUBLE, double_double);
121 CREATE_MPI_DATATYPE(MPI_2LONG, long_long);
123 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE_INT, long_double_int);
125 CREATE_MPI_DATATYPE_NULL(MPI_UB);
126 CREATE_MPI_DATATYPE_NULL(MPI_LB);
127 CREATE_MPI_DATATYPE_NULL(MPI_PACKED);
129 CREATE_MPI_DATATYPE(MPI_PTR, void*);
131 /** Check if the datatype is usable for communications
133 int is_datatype_valid(MPI_Datatype datatype) {
134 return datatype != MPI_DATATYPE_NULL
135 && (datatype->flags & DT_FLAG_COMMITED);
138 size_t smpi_datatype_size(MPI_Datatype datatype)
140 return datatype->size;
143 MPI_Aint smpi_datatype_lb(MPI_Datatype datatype)
148 MPI_Aint smpi_datatype_ub(MPI_Datatype datatype)
153 MPI_Datatype smpi_datatype_dup(MPI_Datatype datatype)
155 MPI_Datatype new_t= xbt_new(s_smpi_mpi_datatype_t,1);
156 memcpy(new_t, datatype, sizeof(s_smpi_mpi_datatype_t));
157 if (datatype->has_subtype)
158 memcpy(new_t->substruct, datatype->substruct, sizeof(s_smpi_subtype_t));
162 int smpi_datatype_extent(MPI_Datatype datatype, MPI_Aint * lb,
166 *extent = datatype->ub - datatype->lb;
170 MPI_Aint smpi_datatype_get_extent(MPI_Datatype datatype){
171 return datatype->ub - datatype->lb;
174 int smpi_datatype_copy(void *sendbuf, int sendcount, MPI_Datatype sendtype,
175 void *recvbuf, int recvcount, MPI_Datatype recvtype)
178 if(smpi_privatize_global_variables){
179 switch_data_segment(smpi_process_index());
181 /* First check if we really have something to do */
182 if (recvcount > 0 && recvbuf != sendbuf) {
183 /* FIXME: treat packed cases */
184 sendcount *= smpi_datatype_size(sendtype);
185 recvcount *= smpi_datatype_size(recvtype);
186 count = sendcount < recvcount ? sendcount : recvcount;
188 if(sendtype->has_subtype == 0 && recvtype->has_subtype == 0) {
189 if(!_xbt_replay_is_active()) memcpy(recvbuf, sendbuf, count);
191 else if (sendtype->has_subtype == 0)
193 s_smpi_subtype_t *subtype = recvtype->substruct;
194 subtype->unserialize( sendbuf, recvbuf,1, subtype, MPI_REPLACE);
196 else if (recvtype->has_subtype == 0)
198 s_smpi_subtype_t *subtype = sendtype->substruct;
199 subtype->serialize(sendbuf, recvbuf,1, subtype);
201 s_smpi_subtype_t *subtype = sendtype->substruct;
204 void * buf_tmp = xbt_malloc(count);
206 subtype->serialize( sendbuf, buf_tmp,count/smpi_datatype_size(sendtype), subtype);
207 subtype = recvtype->substruct;
208 subtype->unserialize( buf_tmp, recvbuf,count/smpi_datatype_size(recvtype), subtype, MPI_REPLACE);
214 return sendcount > recvcount ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
218 * Copies noncontiguous data into contiguous memory.
219 * @param contiguous_vector - output vector
220 * @param noncontiguous_vector - input vector
221 * @param type - pointer contening :
222 * - stride - stride of between noncontiguous data
223 * - block_length - the width or height of blocked matrix
224 * - count - the number of rows of matrix
226 void serialize_vector( const void *noncontiguous_vector,
227 void *contiguous_vector,
231 s_smpi_mpi_vector_t* type_c = (s_smpi_mpi_vector_t*)type;
233 char* contiguous_vector_char = (char*)contiguous_vector;
234 char* noncontiguous_vector_char = (char*)noncontiguous_vector;
236 for (i = 0; i < type_c->block_count * count; i++) {
237 if (type_c->old_type->has_subtype == 0)
238 memcpy(contiguous_vector_char,
239 noncontiguous_vector_char, type_c->block_length * type_c->size_oldtype);
241 ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_vector_char,
242 contiguous_vector_char,
243 type_c->block_length,
244 type_c->old_type->substruct);
246 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
247 if((i+1)%type_c->block_count ==0)
248 noncontiguous_vector_char += type_c->block_length*smpi_datatype_get_extent(type_c->old_type);
250 noncontiguous_vector_char += type_c->block_stride*smpi_datatype_get_extent(type_c->old_type);
255 * Copies contiguous data into noncontiguous memory.
256 * @param noncontiguous_vector - output vector
257 * @param contiguous_vector - input vector
258 * @param type - pointer contening :
259 * - stride - stride of between noncontiguous data
260 * - block_length - the width or height of blocked matrix
261 * - count - the number of rows of matrix
263 void unserialize_vector( const void *contiguous_vector,
264 void *noncontiguous_vector,
269 s_smpi_mpi_vector_t* type_c = (s_smpi_mpi_vector_t*)type;
272 char* contiguous_vector_char = (char*)contiguous_vector;
273 char* noncontiguous_vector_char = (char*)noncontiguous_vector;
275 for (i = 0; i < type_c->block_count * count; i++) {
276 if (type_c->old_type->has_subtype == 0)
277 smpi_op_apply(op, contiguous_vector_char, noncontiguous_vector_char, &type_c->block_length,
279 /* memcpy(noncontiguous_vector_char,
280 contiguous_vector_char, type_c->block_length * type_c->size_oldtype);*/
282 ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_vector_char,
283 noncontiguous_vector_char,
284 type_c->block_length,
285 type_c->old_type->substruct,
287 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
288 if((i+1)%type_c->block_count ==0)
289 noncontiguous_vector_char += type_c->block_length*smpi_datatype_get_extent(type_c->old_type);
291 noncontiguous_vector_char += type_c->block_stride*smpi_datatype_get_extent(type_c->old_type);
296 * Create a Sub type vector to be able to serialize and unserialize it
297 * the structure s_smpi_mpi_vector_t is derived from s_smpi_subtype which
298 * required the functions unserialize and serialize
301 s_smpi_mpi_vector_t* smpi_datatype_vector_create( int block_stride,
304 MPI_Datatype old_type,
306 s_smpi_mpi_vector_t *new_t= xbt_new(s_smpi_mpi_vector_t,1);
307 new_t->base.serialize = &serialize_vector;
308 new_t->base.unserialize = &unserialize_vector;
309 new_t->base.subtype_free = &free_vector;
310 new_t->block_stride = block_stride;
311 new_t->block_length = block_length;
312 new_t->block_count = block_count;
313 smpi_datatype_use(old_type);
314 new_t->old_type = old_type;
315 new_t->size_oldtype = size_oldtype;
319 void smpi_datatype_create(MPI_Datatype* new_type, int size,int lb, int ub, int has_subtype,
320 void *struct_type, int flags){
321 MPI_Datatype new_t= xbt_new(s_smpi_mpi_datatype_t,1);
323 new_t->has_subtype = size>0? has_subtype:0;
326 new_t->flags = flags;
327 new_t->substruct = struct_type;
333 MC_ignore(&(new_t->in_use), sizeof(new_t->in_use));
337 void smpi_datatype_free(MPI_Datatype* type){
339 if((*type)->flags & DT_FLAG_PREDEFINED)return;
341 //if still used, mark for deletion
342 if((*type)->in_use!=0){
343 (*type)->flags |=DT_FLAG_DESTROYED;
347 if ((*type)->has_subtype == 1){
348 ((s_smpi_subtype_t *)(*type)->substruct)->subtype_free(type);
349 xbt_free((*type)->substruct);
352 *type = MPI_DATATYPE_NULL;
355 void smpi_datatype_use(MPI_Datatype type){
356 if(type)type->in_use++;
360 MC_ignore(&(type->in_use), sizeof(type->in_use));
365 void smpi_datatype_unuse(MPI_Datatype type){
366 if(type && type->in_use-- == 0 && (type->flags & DT_FLAG_DESTROYED))
367 smpi_datatype_free(&type);
371 MC_ignore(&(type->in_use), sizeof(type->in_use));
379 Contiguous Implementation
384 * Copies noncontiguous data into contiguous memory.
385 * @param contiguous_hvector - output hvector
386 * @param noncontiguous_hvector - input hvector
387 * @param type - pointer contening :
388 * - stride - stride of between noncontiguous data, in bytes
389 * - block_length - the width or height of blocked matrix
390 * - count - the number of rows of matrix
392 void serialize_contiguous( const void *noncontiguous_hvector,
393 void *contiguous_hvector,
397 s_smpi_mpi_contiguous_t* type_c = (s_smpi_mpi_contiguous_t*)type;
398 char* contiguous_vector_char = (char*)contiguous_hvector;
399 char* noncontiguous_vector_char = (char*)noncontiguous_hvector+type_c->lb;
400 memcpy(contiguous_vector_char,
401 noncontiguous_vector_char, count* type_c->block_count * type_c->size_oldtype);
404 * Copies contiguous data into noncontiguous memory.
405 * @param noncontiguous_vector - output hvector
406 * @param contiguous_vector - input hvector
407 * @param type - pointer contening :
408 * - stride - stride of between noncontiguous data, in bytes
409 * - block_length - the width or height of blocked matrix
410 * - count - the number of rows of matrix
412 void unserialize_contiguous( const void *contiguous_vector,
413 void *noncontiguous_vector,
418 s_smpi_mpi_contiguous_t* type_c = (s_smpi_mpi_contiguous_t*)type;
419 char* contiguous_vector_char = (char*)contiguous_vector;
420 char* noncontiguous_vector_char = (char*)noncontiguous_vector+type_c->lb;
421 int n= count* type_c->block_count;
422 smpi_op_apply(op, contiguous_vector_char, noncontiguous_vector_char, &n,
424 /*memcpy(noncontiguous_vector_char,
425 contiguous_vector_char, count* type_c->block_count * type_c->size_oldtype);*/
428 void free_contiguous(MPI_Datatype* d){
429 smpi_datatype_unuse(((s_smpi_mpi_indexed_t *)(*d)->substruct)->old_type);
433 * Create a Sub type contiguous to be able to serialize and unserialize it
434 * the structure s_smpi_mpi_contiguous_t is derived from s_smpi_subtype which
435 * required the functions unserialize and serialize
438 s_smpi_mpi_contiguous_t* smpi_datatype_contiguous_create( MPI_Aint lb,
440 MPI_Datatype old_type,
442 s_smpi_mpi_contiguous_t *new_t= xbt_new(s_smpi_mpi_contiguous_t,1);
443 new_t->base.serialize = &serialize_contiguous;
444 new_t->base.unserialize = &unserialize_contiguous;
445 new_t->base.subtype_free = &free_contiguous;
447 new_t->block_count = block_count;
448 new_t->old_type = old_type;
449 new_t->size_oldtype = size_oldtype;
450 smpi_datatype_use(old_type);
457 int smpi_datatype_contiguous(int count, MPI_Datatype old_type, MPI_Datatype* new_type, MPI_Aint lb)
460 if(old_type->has_subtype){
461 //handle this case as a hvector with stride equals to the extent of the datatype
462 return smpi_datatype_hvector(count, 1, smpi_datatype_get_extent(old_type), old_type, new_type);
465 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb,
468 smpi_datatype_size(old_type));
470 smpi_datatype_create(new_type,
471 count * smpi_datatype_size(old_type),
472 lb,lb + count * smpi_datatype_size(old_type),
473 1,subtype, DT_FLAG_CONTIGUOUS);
478 int smpi_datatype_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type)
481 if (blocklen<0) return MPI_ERR_ARG;
485 lb=smpi_datatype_lb(old_type);
486 ub=((count-1)*stride+blocklen-1)*smpi_datatype_get_extent(old_type)+smpi_datatype_ub(old_type);
488 if(old_type->has_subtype || stride != blocklen){
491 s_smpi_mpi_vector_t* subtype = smpi_datatype_vector_create( stride,
495 smpi_datatype_size(old_type));
496 smpi_datatype_create(new_type,
497 count * (blocklen) * smpi_datatype_size(old_type), lb,
504 /* in this situation the data are contignous thus it's not
505 * required to serialize and unserialize it*/
506 smpi_datatype_create(new_type, count * blocklen *
507 smpi_datatype_size(old_type), 0, ((count -1) * stride + blocklen)*
508 smpi_datatype_size(old_type),
511 DT_FLAG_VECTOR|DT_FLAG_CONTIGUOUS);
517 void free_vector(MPI_Datatype* d){
518 smpi_datatype_unuse(((s_smpi_mpi_indexed_t *)(*d)->substruct)->old_type);
522 Hvector Implementation - Vector with stride in bytes
527 * Copies noncontiguous data into contiguous memory.
528 * @param contiguous_hvector - output hvector
529 * @param noncontiguous_hvector - input hvector
530 * @param type - pointer contening :
531 * - stride - stride of between noncontiguous data, in bytes
532 * - block_length - the width or height of blocked matrix
533 * - count - the number of rows of matrix
535 void serialize_hvector( const void *noncontiguous_hvector,
536 void *contiguous_hvector,
540 s_smpi_mpi_hvector_t* type_c = (s_smpi_mpi_hvector_t*)type;
542 char* contiguous_vector_char = (char*)contiguous_hvector;
543 char* noncontiguous_vector_char = (char*)noncontiguous_hvector;
545 for (i = 0; i < type_c->block_count * count; i++) {
546 if (type_c->old_type->has_subtype == 0)
547 memcpy(contiguous_vector_char,
548 noncontiguous_vector_char, type_c->block_length * type_c->size_oldtype);
550 ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_vector_char,
551 contiguous_vector_char,
552 type_c->block_length,
553 type_c->old_type->substruct);
555 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
556 if((i+1)%type_c->block_count ==0)
557 noncontiguous_vector_char += type_c->block_length*type_c->size_oldtype;
559 noncontiguous_vector_char += type_c->block_stride;
563 * Copies contiguous data into noncontiguous memory.
564 * @param noncontiguous_vector - output hvector
565 * @param contiguous_vector - input hvector
566 * @param type - pointer contening :
567 * - stride - stride of between noncontiguous data, in bytes
568 * - block_length - the width or height of blocked matrix
569 * - count - the number of rows of matrix
571 void unserialize_hvector( const void *contiguous_vector,
572 void *noncontiguous_vector,
577 s_smpi_mpi_hvector_t* type_c = (s_smpi_mpi_hvector_t*)type;
580 char* contiguous_vector_char = (char*)contiguous_vector;
581 char* noncontiguous_vector_char = (char*)noncontiguous_vector;
583 for (i = 0; i < type_c->block_count * count; i++) {
584 if (type_c->old_type->has_subtype == 0)
585 smpi_op_apply(op, contiguous_vector_char, noncontiguous_vector_char, &type_c->block_length,
587 /*memcpy(noncontiguous_vector_char,
588 contiguous_vector_char, type_c->block_length * type_c->size_oldtype);*/
590 ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_vector_char,
591 noncontiguous_vector_char,
592 type_c->block_length,
593 type_c->old_type->substruct,
595 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
596 if((i+1)%type_c->block_count ==0)
597 noncontiguous_vector_char += type_c->block_length*type_c->size_oldtype;
599 noncontiguous_vector_char += type_c->block_stride;
604 * Create a Sub type vector to be able to serialize and unserialize it
605 * the structure s_smpi_mpi_vector_t is derived from s_smpi_subtype which
606 * required the functions unserialize and serialize
609 s_smpi_mpi_hvector_t* smpi_datatype_hvector_create( MPI_Aint block_stride,
612 MPI_Datatype old_type,
614 s_smpi_mpi_hvector_t *new_t= xbt_new(s_smpi_mpi_hvector_t,1);
615 new_t->base.serialize = &serialize_hvector;
616 new_t->base.unserialize = &unserialize_hvector;
617 new_t->base.subtype_free = &free_hvector;
618 new_t->block_stride = block_stride;
619 new_t->block_length = block_length;
620 new_t->block_count = block_count;
621 new_t->old_type = old_type;
622 new_t->size_oldtype = size_oldtype;
623 smpi_datatype_use(old_type);
627 //do nothing for vector types
628 void free_hvector(MPI_Datatype* d){
629 smpi_datatype_unuse(((s_smpi_mpi_indexed_t *)(*d)->substruct)->old_type);
632 int smpi_datatype_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type)
635 if (blocklen<0) return MPI_ERR_ARG;
639 lb=smpi_datatype_lb(old_type);
640 ub=((count-1)*stride)+(blocklen-1)*smpi_datatype_get_extent(old_type)+smpi_datatype_ub(old_type);
642 if(old_type->has_subtype || stride != blocklen*smpi_datatype_get_extent(old_type)){
643 s_smpi_mpi_hvector_t* subtype = smpi_datatype_hvector_create( stride,
647 smpi_datatype_size(old_type));
649 smpi_datatype_create(new_type, count * blocklen * smpi_datatype_size(old_type),
656 smpi_datatype_create(new_type, count * blocklen *
657 smpi_datatype_size(old_type),0,count * blocklen *
658 smpi_datatype_size(old_type),
661 DT_FLAG_VECTOR|DT_FLAG_CONTIGUOUS);
669 Indexed Implementation
673 * Copies noncontiguous data into contiguous memory.
674 * @param contiguous_indexed - output indexed
675 * @param noncontiguous_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 serialize_indexed( const void *noncontiguous_indexed,
682 void *contiguous_indexed,
686 s_smpi_mpi_indexed_t* type_c = (s_smpi_mpi_indexed_t*)type;
688 char* contiguous_indexed_char = (char*)contiguous_indexed;
689 char* noncontiguous_indexed_char = (char*)noncontiguous_indexed+type_c->block_indices[0] * type_c->size_oldtype;
690 for(j=0; j<count;j++){
691 for (i = 0; i < type_c->block_count; i++) {
692 if (type_c->old_type->has_subtype == 0)
693 memcpy(contiguous_indexed_char,
694 noncontiguous_indexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
696 ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_indexed_char,
697 contiguous_indexed_char,
698 type_c->block_lengths[i],
699 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)noncontiguous_indexed_char = (char*)noncontiguous_indexed + type_c->block_indices[i+1]*smpi_datatype_get_extent(type_c->old_type);
704 else noncontiguous_indexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
706 noncontiguous_indexed=(void*)noncontiguous_indexed_char;
710 * Copies contiguous data into noncontiguous memory.
711 * @param noncontiguous_indexed - output indexed
712 * @param contiguous_indexed - input indexed
713 * @param type - pointer contening :
714 * - block_lengths - the width or height of blocked matrix
715 * - block_indices - indices of each data, in element
716 * - count - the number of rows of matrix
718 void unserialize_indexed( const void *contiguous_indexed,
719 void *noncontiguous_indexed,
725 s_smpi_mpi_indexed_t* type_c = (s_smpi_mpi_indexed_t*)type;
727 char* contiguous_indexed_char = (char*)contiguous_indexed;
728 char* noncontiguous_indexed_char = (char*)noncontiguous_indexed+type_c->block_indices[0]*smpi_datatype_get_extent(type_c->old_type);
729 for(j=0; j<count;j++){
730 for (i = 0; i < type_c->block_count; i++) {
731 if (type_c->old_type->has_subtype == 0)
732 smpi_op_apply(op, contiguous_indexed_char, noncontiguous_indexed_char, &type_c->block_lengths[i],
734 /*memcpy(noncontiguous_indexed_char ,
735 contiguous_indexed_char, type_c->block_lengths[i] * type_c->size_oldtype);*/
737 ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_indexed_char,
738 noncontiguous_indexed_char,
739 type_c->block_lengths[i],
740 type_c->old_type->substruct,
743 contiguous_indexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
744 if (i<type_c->block_count-1)
745 noncontiguous_indexed_char = (char*)noncontiguous_indexed + type_c->block_indices[i+1]*smpi_datatype_get_extent(type_c->old_type);
746 else noncontiguous_indexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
748 noncontiguous_indexed=(void*)noncontiguous_indexed_char;
752 void free_indexed(MPI_Datatype* type){
753 xbt_free(((s_smpi_mpi_indexed_t *)(*type)->substruct)->block_lengths);
754 xbt_free(((s_smpi_mpi_indexed_t *)(*type)->substruct)->block_indices);
755 smpi_datatype_unuse(((s_smpi_mpi_indexed_t *)(*type)->substruct)->old_type);
759 * Create a Sub type indexed to be able to serialize and unserialize it
760 * the structure s_smpi_mpi_indexed_t is derived from s_smpi_subtype which
761 * required the functions unserialize and serialize
763 s_smpi_mpi_indexed_t* smpi_datatype_indexed_create( int* block_lengths,
766 MPI_Datatype old_type,
768 s_smpi_mpi_indexed_t *new_t= xbt_new(s_smpi_mpi_indexed_t,1);
769 new_t->base.serialize = &serialize_indexed;
770 new_t->base.unserialize = &unserialize_indexed;
771 new_t->base.subtype_free = &free_indexed;
772 //TODO : add a custom function for each time to clean these
773 new_t->block_lengths= xbt_new(int, block_count);
774 new_t->block_indices= xbt_new(int, block_count);
776 for(i=0;i<block_count;i++){
777 new_t->block_lengths[i]=block_lengths[i];
778 new_t->block_indices[i]=block_indices[i];
780 new_t->block_count = block_count;
781 smpi_datatype_use(old_type);
782 new_t->old_type = old_type;
783 new_t->size_oldtype = size_oldtype;
788 int smpi_datatype_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
797 lb=indices[0]*smpi_datatype_get_extent(old_type);
798 ub=indices[0]*smpi_datatype_get_extent(old_type) + blocklens[0]*smpi_datatype_ub(old_type);
801 for(i=0; i< count; i++){
804 size += blocklens[i];
806 if(indices[i]*smpi_datatype_get_extent(old_type)+smpi_datatype_lb(old_type)<lb)
807 lb = indices[i]*smpi_datatype_get_extent(old_type)+smpi_datatype_lb(old_type);
808 if(indices[i]*smpi_datatype_get_extent(old_type)+blocklens[i]*smpi_datatype_ub(old_type)>ub)
809 ub = indices[i]*smpi_datatype_get_extent(old_type)+blocklens[i]*smpi_datatype_ub(old_type);
811 if ( (i< count -1) && (indices[i]+blocklens[i] != indices[i+1]) )contiguous=0;
813 if (old_type->has_subtype == 1)
817 s_smpi_mpi_indexed_t* subtype = smpi_datatype_indexed_create( blocklens,
821 smpi_datatype_size(old_type));
822 smpi_datatype_create(new_type, size *
823 smpi_datatype_size(old_type),lb,ub,1, subtype, DT_FLAG_DATA);
825 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb,
828 smpi_datatype_size(old_type));
829 smpi_datatype_create(new_type, size *
830 smpi_datatype_size(old_type),lb,ub,1, subtype, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
838 Hindexed Implementation - Indexed with indices in bytes
842 * Copies noncontiguous data into contiguous memory.
843 * @param contiguous_hindexed - output hindexed
844 * @param noncontiguous_hindexed - input hindexed
845 * @param type - pointer contening :
846 * - block_lengths - the width or height of blocked matrix
847 * - block_indices - indices of each data, in bytes
848 * - count - the number of rows of matrix
850 void serialize_hindexed( const void *noncontiguous_hindexed,
851 void *contiguous_hindexed,
855 s_smpi_mpi_hindexed_t* type_c = (s_smpi_mpi_hindexed_t*)type;
857 char* contiguous_hindexed_char = (char*)contiguous_hindexed;
858 char* noncontiguous_hindexed_char = (char*)noncontiguous_hindexed+ type_c->block_indices[0];
859 for(j=0; j<count;j++){
860 for (i = 0; i < type_c->block_count; i++) {
861 if (type_c->old_type->has_subtype == 0)
862 memcpy(contiguous_hindexed_char,
863 noncontiguous_hindexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
865 ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_hindexed_char,
866 contiguous_hindexed_char,
867 type_c->block_lengths[i],
868 type_c->old_type->substruct);
870 contiguous_hindexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
871 if (i<type_c->block_count-1)noncontiguous_hindexed_char = (char*)noncontiguous_hindexed + type_c->block_indices[i+1];
872 else noncontiguous_hindexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
874 noncontiguous_hindexed=(void*)noncontiguous_hindexed_char;
878 * Copies contiguous data into noncontiguous memory.
879 * @param noncontiguous_hindexed - output hindexed
880 * @param contiguous_hindexed - input hindexed
881 * @param type - pointer contening :
882 * - block_lengths - the width or height of blocked matrix
883 * - block_indices - indices of each data, in bytes
884 * - count - the number of rows of matrix
886 void unserialize_hindexed( const void *contiguous_hindexed,
887 void *noncontiguous_hindexed,
892 s_smpi_mpi_hindexed_t* type_c = (s_smpi_mpi_hindexed_t*)type;
895 char* contiguous_hindexed_char = (char*)contiguous_hindexed;
896 char* noncontiguous_hindexed_char = (char*)noncontiguous_hindexed+ type_c->block_indices[0];
897 for(j=0; j<count;j++){
898 for (i = 0; i < type_c->block_count; i++) {
899 if (type_c->old_type->has_subtype == 0)
900 smpi_op_apply(op, contiguous_hindexed_char, noncontiguous_hindexed_char, &type_c->block_lengths[i],
902 /*memcpy(noncontiguous_hindexed_char,
903 contiguous_hindexed_char, type_c->block_lengths[i] * type_c->size_oldtype);*/
905 ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_hindexed_char,
906 noncontiguous_hindexed_char,
907 type_c->block_lengths[i],
908 type_c->old_type->substruct,
911 contiguous_hindexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
912 if (i<type_c->block_count-1)noncontiguous_hindexed_char = (char*)noncontiguous_hindexed + type_c->block_indices[i+1];
913 else noncontiguous_hindexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
915 noncontiguous_hindexed=(void*)noncontiguous_hindexed_char;
919 void free_hindexed(MPI_Datatype* type){
920 xbt_free(((s_smpi_mpi_hindexed_t *)(*type)->substruct)->block_lengths);
921 xbt_free(((s_smpi_mpi_hindexed_t *)(*type)->substruct)->block_indices);
922 smpi_datatype_unuse(((s_smpi_mpi_indexed_t *)(*type)->substruct)->old_type);
926 * Create a Sub type hindexed to be able to serialize and unserialize it
927 * the structure s_smpi_mpi_hindexed_t is derived from s_smpi_subtype which
928 * required the functions unserialize and serialize
930 s_smpi_mpi_hindexed_t* smpi_datatype_hindexed_create( int* block_lengths,
931 MPI_Aint* block_indices,
933 MPI_Datatype old_type,
935 s_smpi_mpi_hindexed_t *new_t= xbt_new(s_smpi_mpi_hindexed_t,1);
936 new_t->base.serialize = &serialize_hindexed;
937 new_t->base.unserialize = &unserialize_hindexed;
938 new_t->base.subtype_free = &free_hindexed;
939 //TODO : add a custom function for each time to clean these
940 new_t->block_lengths= xbt_new(int, block_count);
941 new_t->block_indices= xbt_new(MPI_Aint, block_count);
943 for(i=0;i<block_count;i++){
944 new_t->block_lengths[i]=block_lengths[i];
945 new_t->block_indices[i]=block_indices[i];
947 new_t->block_count = block_count;
948 new_t->old_type = old_type;
949 new_t->size_oldtype = size_oldtype;
954 int smpi_datatype_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
963 lb=indices[0] + smpi_datatype_lb(old_type);
964 ub=indices[0] + blocklens[0]*smpi_datatype_ub(old_type);
966 for(i=0; i< count; i++){
969 size += blocklens[i];
971 if(indices[i]+smpi_datatype_lb(old_type)<lb) lb = indices[i]+smpi_datatype_lb(old_type);
972 if(indices[i]+blocklens[i]*smpi_datatype_ub(old_type)>ub) ub = indices[i]+blocklens[i]*smpi_datatype_ub(old_type);
974 if ( (i< count -1) && (indices[i]+blocklens[i]*smpi_datatype_size(old_type) != indices[i+1]) )contiguous=0;
976 if (old_type->has_subtype == 1 || lb!=0)
980 s_smpi_mpi_hindexed_t* subtype = smpi_datatype_hindexed_create( blocklens,
984 smpi_datatype_size(old_type));
985 smpi_datatype_create(new_type, size * smpi_datatype_size(old_type),
988 ,1, subtype, DT_FLAG_DATA);
990 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb,
993 smpi_datatype_size(old_type));
994 smpi_datatype_create(new_type, size * smpi_datatype_size(old_type),
995 0,size * smpi_datatype_size(old_type),
996 1, subtype, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
1004 struct Implementation - Indexed with indices in bytes
1008 * Copies noncontiguous data into contiguous memory.
1009 * @param contiguous_struct - output struct
1010 * @param noncontiguous_struct - input struct
1011 * @param type - pointer contening :
1012 * - stride - stride of between noncontiguous data
1013 * - block_length - the width or height of blocked matrix
1014 * - count - the number of rows of matrix
1016 void serialize_struct( const void *noncontiguous_struct,
1017 void *contiguous_struct,
1021 s_smpi_mpi_struct_t* type_c = (s_smpi_mpi_struct_t*)type;
1023 char* contiguous_struct_char = (char*)contiguous_struct;
1024 char* noncontiguous_struct_char = (char*)noncontiguous_struct+ type_c->block_indices[0];
1025 for(j=0; j<count;j++){
1026 for (i = 0; i < type_c->block_count; i++) {
1027 if (type_c->old_types[i]->has_subtype == 0)
1028 memcpy(contiguous_struct_char,
1029 noncontiguous_struct_char, type_c->block_lengths[i] * smpi_datatype_size(type_c->old_types[i]));
1031 ((s_smpi_subtype_t*)type_c->old_types[i]->substruct)->serialize( noncontiguous_struct_char,
1032 contiguous_struct_char,
1033 type_c->block_lengths[i],
1034 type_c->old_types[i]->substruct);
1037 contiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_size(type_c->old_types[i]);
1038 if (i<type_c->block_count-1)noncontiguous_struct_char = (char*)noncontiguous_struct + type_c->block_indices[i+1];
1039 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 ?
1041 noncontiguous_struct=(void*)noncontiguous_struct_char;
1045 * Copies contiguous data into noncontiguous memory.
1046 * @param noncontiguous_struct - output struct
1047 * @param contiguous_struct - input struct
1048 * @param type - pointer contening :
1049 * - stride - stride of between noncontiguous data
1050 * - block_length - the width or height of blocked matrix
1051 * - count - the number of rows of matrix
1053 void unserialize_struct( const void *contiguous_struct,
1054 void *noncontiguous_struct,
1059 s_smpi_mpi_struct_t* type_c = (s_smpi_mpi_struct_t*)type;
1062 char* contiguous_struct_char = (char*)contiguous_struct;
1063 char* noncontiguous_struct_char = (char*)noncontiguous_struct+ type_c->block_indices[0];
1064 for(j=0; j<count;j++){
1065 for (i = 0; i < type_c->block_count; i++) {
1066 if (type_c->old_types[i]->has_subtype == 0)
1067 smpi_op_apply(op, contiguous_struct_char, noncontiguous_struct_char, &type_c->block_lengths[i],
1068 & type_c->old_types[i]);
1069 /*memcpy(noncontiguous_struct_char,
1070 contiguous_struct_char, type_c->block_lengths[i] * smpi_datatype_size(type_c->old_types[i]));*/
1072 ((s_smpi_subtype_t*)type_c->old_types[i]->substruct)->unserialize( contiguous_struct_char,
1073 noncontiguous_struct_char,
1074 type_c->block_lengths[i],
1075 type_c->old_types[i]->substruct,
1078 contiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_size(type_c->old_types[i]);
1079 if (i<type_c->block_count-1)noncontiguous_struct_char = (char*)noncontiguous_struct + type_c->block_indices[i+1];
1080 else noncontiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_types[i]);
1082 noncontiguous_struct=(void*)noncontiguous_struct_char;
1087 void free_struct(MPI_Datatype* type){
1088 xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->block_lengths);
1089 xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->block_indices);
1091 for (i = 0; i < ((s_smpi_mpi_struct_t *)(*type)->substruct)->block_count; i++)
1092 smpi_datatype_unuse(((s_smpi_mpi_struct_t *)(*type)->substruct)->old_types[i]);
1093 xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->old_types);
1097 * Create a Sub type struct to be able to serialize and unserialize it
1098 * the structure s_smpi_mpi_struct_t is derived from s_smpi_subtype which
1099 * required the functions unserialize and serialize
1101 s_smpi_mpi_struct_t* smpi_datatype_struct_create( int* block_lengths,
1102 MPI_Aint* block_indices,
1104 MPI_Datatype* old_types){
1105 s_smpi_mpi_struct_t *new_t= xbt_new(s_smpi_mpi_struct_t,1);
1106 new_t->base.serialize = &serialize_struct;
1107 new_t->base.unserialize = &unserialize_struct;
1108 new_t->base.subtype_free = &free_struct;
1109 //TODO : add a custom function for each time to clean these
1110 new_t->block_lengths= xbt_new(int, block_count);
1111 new_t->block_indices= xbt_new(MPI_Aint, block_count);
1112 new_t->old_types= xbt_new(MPI_Datatype, block_count);
1114 for(i=0;i<block_count;i++){
1115 new_t->block_lengths[i]=block_lengths[i];
1116 new_t->block_indices[i]=block_indices[i];
1117 new_t->old_types[i]=old_types[i];
1118 smpi_datatype_use(new_t->old_types[i]);
1120 //new_t->block_lengths = block_lengths;
1121 //new_t->block_indices = block_indices;
1122 new_t->block_count = block_count;
1123 //new_t->old_types = old_types;
1128 int smpi_datatype_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type)
1137 lb=indices[0] + smpi_datatype_lb(old_types[0]);
1138 ub=indices[0] + blocklens[0]*smpi_datatype_ub(old_types[0]);
1142 for(i=0; i< count; i++){
1145 if (old_types[i]->has_subtype == 1)
1148 size += blocklens[i]*smpi_datatype_size(old_types[i]);
1149 if (old_types[i]==MPI_LB){
1153 if (old_types[i]==MPI_UB){
1158 if(!forced_lb && indices[i]+smpi_datatype_lb(old_types[i])<lb) lb = indices[i];
1159 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]);
1161 if ( (i< count -1) && (indices[i]+blocklens[i]*smpi_datatype_size(old_types[i]) != indices[i+1]) )contiguous=0;
1165 s_smpi_mpi_struct_t* subtype = smpi_datatype_struct_create( blocklens,
1170 smpi_datatype_create(new_type, size, lb, ub,1, subtype, DT_FLAG_DATA);
1172 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb,
1176 smpi_datatype_create(new_type, size, lb, ub,1, subtype, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
1181 void smpi_datatype_commit(MPI_Datatype *datatype)
1183 (*datatype)->flags= ((*datatype)->flags | DT_FLAG_COMMITED);
1186 typedef struct s_smpi_mpi_op {
1187 MPI_User_function *func;
1191 #define MAX_OP(a, b) (b) = (a) < (b) ? (b) : (a)
1192 #define MIN_OP(a, b) (b) = (a) < (b) ? (a) : (b)
1193 #define SUM_OP(a, b) (b) += (a)
1194 #define PROD_OP(a, b) (b) *= (a)
1195 #define LAND_OP(a, b) (b) = (a) && (b)
1196 #define LOR_OP(a, b) (b) = (a) || (b)
1197 #define LXOR_OP(a, b) (b) = (!(a) && (b)) || ((a) && !(b))
1198 #define BAND_OP(a, b) (b) &= (a)
1199 #define BOR_OP(a, b) (b) |= (a)
1200 #define BXOR_OP(a, b) (b) ^= (a)
1201 #define MAXLOC_OP(a, b) (b) = (a.value) < (b.value) ? (b) : (a)
1202 #define MINLOC_OP(a, b) (b) = (a.value) < (b.value) ? (a) : (b)
1204 #define APPLY_FUNC(a, b, length, type, func) \
1207 type* x = (type*)(a); \
1208 type* y = (type*)(b); \
1209 for(i = 0; i < *(length); i++) { \
1214 static void max_func(void *a, void *b, int *length,
1215 MPI_Datatype * datatype)
1217 if (*datatype == MPI_CHAR) {
1218 APPLY_FUNC(a, b, length, char, MAX_OP);
1219 } else if (*datatype == MPI_SHORT) {
1220 APPLY_FUNC(a, b, length, short, MAX_OP);
1221 } else if (*datatype == MPI_INT) {
1222 APPLY_FUNC(a, b, length, int, MAX_OP);
1223 } else if (*datatype == MPI_LONG) {
1224 APPLY_FUNC(a, b, length, long, MAX_OP);
1225 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1226 APPLY_FUNC(a, b, length, unsigned short, MAX_OP);
1227 } else if (*datatype == MPI_UNSIGNED) {
1228 APPLY_FUNC(a, b, length, unsigned int, MAX_OP);
1229 } else if (*datatype == MPI_UNSIGNED_LONG) {
1230 APPLY_FUNC(a, b, length, unsigned long, MAX_OP);
1231 } else if (*datatype == MPI_FLOAT) {
1232 APPLY_FUNC(a, b, length, float, MAX_OP);
1233 } else if (*datatype == MPI_DOUBLE) {
1234 APPLY_FUNC(a, b, length, double, MAX_OP);
1235 } else if (*datatype == MPI_LONG_DOUBLE) {
1236 APPLY_FUNC(a, b, length, long double, MAX_OP);
1240 static void min_func(void *a, void *b, int *length,
1241 MPI_Datatype * datatype)
1243 if (*datatype == MPI_CHAR) {
1244 APPLY_FUNC(a, b, length, char, MIN_OP);
1245 } else if (*datatype == MPI_SHORT) {
1246 APPLY_FUNC(a, b, length, short, MIN_OP);
1247 } else if (*datatype == MPI_INT) {
1248 APPLY_FUNC(a, b, length, int, MIN_OP);
1249 } else if (*datatype == MPI_LONG) {
1250 APPLY_FUNC(a, b, length, long, MIN_OP);
1251 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1252 APPLY_FUNC(a, b, length, unsigned short, MIN_OP);
1253 } else if (*datatype == MPI_UNSIGNED) {
1254 APPLY_FUNC(a, b, length, unsigned int, MIN_OP);
1255 } else if (*datatype == MPI_UNSIGNED_LONG) {
1256 APPLY_FUNC(a, b, length, unsigned long, MIN_OP);
1257 } else if (*datatype == MPI_FLOAT) {
1258 APPLY_FUNC(a, b, length, float, MIN_OP);
1259 } else if (*datatype == MPI_DOUBLE) {
1260 APPLY_FUNC(a, b, length, double, MIN_OP);
1261 } else if (*datatype == MPI_LONG_DOUBLE) {
1262 APPLY_FUNC(a, b, length, long double, MIN_OP);
1266 static void sum_func(void *a, void *b, int *length,
1267 MPI_Datatype * datatype)
1269 if (*datatype == MPI_CHAR) {
1270 APPLY_FUNC(a, b, length, char, SUM_OP);
1271 } else if (*datatype == MPI_SHORT) {
1272 APPLY_FUNC(a, b, length, short, SUM_OP);
1273 } else if (*datatype == MPI_INT) {
1274 APPLY_FUNC(a, b, length, int, SUM_OP);
1275 } else if (*datatype == MPI_LONG) {
1276 APPLY_FUNC(a, b, length, long, SUM_OP);
1277 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1278 APPLY_FUNC(a, b, length, unsigned short, SUM_OP);
1279 } else if (*datatype == MPI_UNSIGNED) {
1280 APPLY_FUNC(a, b, length, unsigned int, SUM_OP);
1281 } else if (*datatype == MPI_UNSIGNED_LONG) {
1282 APPLY_FUNC(a, b, length, unsigned long, SUM_OP);
1283 } else if (*datatype == MPI_FLOAT) {
1284 APPLY_FUNC(a, b, length, float, SUM_OP);
1285 } else if (*datatype == MPI_DOUBLE) {
1286 APPLY_FUNC(a, b, length, double, SUM_OP);
1287 } else if (*datatype == MPI_LONG_DOUBLE) {
1288 APPLY_FUNC(a, b, length, long double, SUM_OP);
1289 } else if (*datatype == MPI_C_FLOAT_COMPLEX) {
1290 APPLY_FUNC(a, b, length, float _Complex, SUM_OP);
1291 } else if (*datatype == MPI_C_DOUBLE_COMPLEX) {
1292 APPLY_FUNC(a, b, length, double _Complex, SUM_OP);
1293 } else if (*datatype == MPI_C_LONG_DOUBLE_COMPLEX) {
1294 APPLY_FUNC(a, b, length, long double _Complex, SUM_OP);
1298 static void prod_func(void *a, void *b, int *length,
1299 MPI_Datatype * datatype)
1301 if (*datatype == MPI_CHAR) {
1302 APPLY_FUNC(a, b, length, char, PROD_OP);
1303 } else if (*datatype == MPI_SHORT) {
1304 APPLY_FUNC(a, b, length, short, PROD_OP);
1305 } else if (*datatype == MPI_INT) {
1306 APPLY_FUNC(a, b, length, int, PROD_OP);
1307 } else if (*datatype == MPI_LONG) {
1308 APPLY_FUNC(a, b, length, long, PROD_OP);
1309 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1310 APPLY_FUNC(a, b, length, unsigned short, PROD_OP);
1311 } else if (*datatype == MPI_UNSIGNED) {
1312 APPLY_FUNC(a, b, length, unsigned int, PROD_OP);
1313 } else if (*datatype == MPI_UNSIGNED_LONG) {
1314 APPLY_FUNC(a, b, length, unsigned long, PROD_OP);
1315 } else if (*datatype == MPI_FLOAT) {
1316 APPLY_FUNC(a, b, length, float, PROD_OP);
1317 } else if (*datatype == MPI_DOUBLE) {
1318 APPLY_FUNC(a, b, length, double, PROD_OP);
1319 } else if (*datatype == MPI_LONG_DOUBLE) {
1320 APPLY_FUNC(a, b, length, long double, PROD_OP);
1321 } else if (*datatype == MPI_C_FLOAT_COMPLEX) {
1322 APPLY_FUNC(a, b, length, float _Complex, PROD_OP);
1323 } else if (*datatype == MPI_C_DOUBLE_COMPLEX) {
1324 APPLY_FUNC(a, b, length, double _Complex, PROD_OP);
1325 } else if (*datatype == MPI_C_LONG_DOUBLE_COMPLEX) {
1326 APPLY_FUNC(a, b, length, long double _Complex, PROD_OP);
1330 static void land_func(void *a, void *b, int *length,
1331 MPI_Datatype * datatype)
1333 if (*datatype == MPI_CHAR) {
1334 APPLY_FUNC(a, b, length, char, LAND_OP);
1335 } else if (*datatype == MPI_SHORT) {
1336 APPLY_FUNC(a, b, length, short, LAND_OP);
1337 } else if (*datatype == MPI_INT) {
1338 APPLY_FUNC(a, b, length, int, LAND_OP);
1339 } else if (*datatype == MPI_LONG) {
1340 APPLY_FUNC(a, b, length, long, LAND_OP);
1341 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1342 APPLY_FUNC(a, b, length, unsigned short, LAND_OP);
1343 } else if (*datatype == MPI_UNSIGNED) {
1344 APPLY_FUNC(a, b, length, unsigned int, LAND_OP);
1345 } else if (*datatype == MPI_UNSIGNED_LONG) {
1346 APPLY_FUNC(a, b, length, unsigned long, LAND_OP);
1347 } else if (*datatype == MPI_C_BOOL) {
1348 APPLY_FUNC(a, b, length, _Bool, LAND_OP);
1352 static void lor_func(void *a, void *b, int *length,
1353 MPI_Datatype * datatype)
1355 if (*datatype == MPI_CHAR) {
1356 APPLY_FUNC(a, b, length, char, LOR_OP);
1357 } else if (*datatype == MPI_SHORT) {
1358 APPLY_FUNC(a, b, length, short, LOR_OP);
1359 } else if (*datatype == MPI_INT) {
1360 APPLY_FUNC(a, b, length, int, LOR_OP);
1361 } else if (*datatype == MPI_LONG) {
1362 APPLY_FUNC(a, b, length, long, LOR_OP);
1363 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1364 APPLY_FUNC(a, b, length, unsigned short, LOR_OP);
1365 } else if (*datatype == MPI_UNSIGNED) {
1366 APPLY_FUNC(a, b, length, unsigned int, LOR_OP);
1367 } else if (*datatype == MPI_UNSIGNED_LONG) {
1368 APPLY_FUNC(a, b, length, unsigned long, LOR_OP);
1369 } else if (*datatype == MPI_C_BOOL) {
1370 APPLY_FUNC(a, b, length, _Bool, LOR_OP);
1374 static void lxor_func(void *a, void *b, int *length,
1375 MPI_Datatype * datatype)
1377 if (*datatype == MPI_CHAR) {
1378 APPLY_FUNC(a, b, length, char, LXOR_OP);
1379 } else if (*datatype == MPI_SHORT) {
1380 APPLY_FUNC(a, b, length, short, LXOR_OP);
1381 } else if (*datatype == MPI_INT) {
1382 APPLY_FUNC(a, b, length, int, LXOR_OP);
1383 } else if (*datatype == MPI_LONG) {
1384 APPLY_FUNC(a, b, length, long, LXOR_OP);
1385 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1386 APPLY_FUNC(a, b, length, unsigned short, LXOR_OP);
1387 } else if (*datatype == MPI_UNSIGNED) {
1388 APPLY_FUNC(a, b, length, unsigned int, LXOR_OP);
1389 } else if (*datatype == MPI_UNSIGNED_LONG) {
1390 APPLY_FUNC(a, b, length, unsigned long, LXOR_OP);
1391 } else if (*datatype == MPI_C_BOOL) {
1392 APPLY_FUNC(a, b, length, _Bool, LXOR_OP);
1396 static void band_func(void *a, void *b, int *length,
1397 MPI_Datatype * datatype)
1399 if (*datatype == MPI_CHAR) {
1400 APPLY_FUNC(a, b, length, char, BAND_OP);
1402 if (*datatype == MPI_SHORT) {
1403 APPLY_FUNC(a, b, length, short, BAND_OP);
1404 } else if (*datatype == MPI_INT) {
1405 APPLY_FUNC(a, b, length, int, BAND_OP);
1406 } else if (*datatype == MPI_LONG) {
1407 APPLY_FUNC(a, b, length, long, BAND_OP);
1408 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1409 APPLY_FUNC(a, b, length, unsigned short, BAND_OP);
1410 } else if (*datatype == MPI_UNSIGNED) {
1411 APPLY_FUNC(a, b, length, unsigned int, BAND_OP);
1412 } else if (*datatype == MPI_UNSIGNED_LONG) {
1413 APPLY_FUNC(a, b, length, unsigned long, BAND_OP);
1414 } else if (*datatype == MPI_BYTE) {
1415 APPLY_FUNC(a, b, length, uint8_t, BAND_OP);
1419 static void bor_func(void *a, void *b, int *length,
1420 MPI_Datatype * datatype)
1422 if (*datatype == MPI_CHAR) {
1423 APPLY_FUNC(a, b, length, char, BOR_OP);
1424 } else if (*datatype == MPI_SHORT) {
1425 APPLY_FUNC(a, b, length, short, BOR_OP);
1426 } else if (*datatype == MPI_INT) {
1427 APPLY_FUNC(a, b, length, int, BOR_OP);
1428 } else if (*datatype == MPI_LONG) {
1429 APPLY_FUNC(a, b, length, long, BOR_OP);
1430 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1431 APPLY_FUNC(a, b, length, unsigned short, BOR_OP);
1432 } else if (*datatype == MPI_UNSIGNED) {
1433 APPLY_FUNC(a, b, length, unsigned int, BOR_OP);
1434 } else if (*datatype == MPI_UNSIGNED_LONG) {
1435 APPLY_FUNC(a, b, length, unsigned long, BOR_OP);
1436 } else if (*datatype == MPI_BYTE) {
1437 APPLY_FUNC(a, b, length, uint8_t, BOR_OP);
1441 static void bxor_func(void *a, void *b, int *length,
1442 MPI_Datatype * datatype)
1444 if (*datatype == MPI_CHAR) {
1445 APPLY_FUNC(a, b, length, char, BXOR_OP);
1446 } else if (*datatype == MPI_SHORT) {
1447 APPLY_FUNC(a, b, length, short, BXOR_OP);
1448 } else if (*datatype == MPI_INT) {
1449 APPLY_FUNC(a, b, length, int, BXOR_OP);
1450 } else if (*datatype == MPI_LONG) {
1451 APPLY_FUNC(a, b, length, long, BXOR_OP);
1452 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1453 APPLY_FUNC(a, b, length, unsigned short, BXOR_OP);
1454 } else if (*datatype == MPI_UNSIGNED) {
1455 APPLY_FUNC(a, b, length, unsigned int, BXOR_OP);
1456 } else if (*datatype == MPI_UNSIGNED_LONG) {
1457 APPLY_FUNC(a, b, length, unsigned long, BXOR_OP);
1458 } else if (*datatype == MPI_BYTE) {
1459 APPLY_FUNC(a, b, length, uint8_t, BXOR_OP);
1463 static void minloc_func(void *a, void *b, int *length,
1464 MPI_Datatype * datatype)
1466 if (*datatype == MPI_FLOAT_INT) {
1467 APPLY_FUNC(a, b, length, float_int, MINLOC_OP);
1468 } else if (*datatype == MPI_LONG_INT) {
1469 APPLY_FUNC(a, b, length, long_int, MINLOC_OP);
1470 } else if (*datatype == MPI_DOUBLE_INT) {
1471 APPLY_FUNC(a, b, length, double_int, MINLOC_OP);
1472 } else if (*datatype == MPI_SHORT_INT) {
1473 APPLY_FUNC(a, b, length, short_int, MINLOC_OP);
1474 } else if (*datatype == MPI_2INT) {
1475 APPLY_FUNC(a, b, length, int_int, MINLOC_OP);
1476 } else if (*datatype == MPI_LONG_DOUBLE_INT) {
1477 APPLY_FUNC(a, b, length, long_double_int, MINLOC_OP);
1478 } else if (*datatype == MPI_2FLOAT) {
1479 APPLY_FUNC(a, b, length, float_float, MINLOC_OP);
1480 } else if (*datatype == MPI_2DOUBLE) {
1481 APPLY_FUNC(a, b, length, double_double, MINLOC_OP);
1485 static void maxloc_func(void *a, void *b, int *length,
1486 MPI_Datatype * datatype)
1488 if (*datatype == MPI_FLOAT_INT) {
1489 APPLY_FUNC(a, b, length, float_int, MAXLOC_OP);
1490 } else if (*datatype == MPI_LONG_INT) {
1491 APPLY_FUNC(a, b, length, long_int, MAXLOC_OP);
1492 } else if (*datatype == MPI_DOUBLE_INT) {
1493 APPLY_FUNC(a, b, length, double_int, MAXLOC_OP);
1494 } else if (*datatype == MPI_SHORT_INT) {
1495 APPLY_FUNC(a, b, length, short_int, MAXLOC_OP);
1496 } else if (*datatype == MPI_2INT) {
1497 APPLY_FUNC(a, b, length, int_int, MAXLOC_OP);
1498 } else if (*datatype == MPI_LONG_DOUBLE_INT) {
1499 APPLY_FUNC(a, b, length, long_double_int, MAXLOC_OP);
1500 } else if (*datatype == MPI_2FLOAT) {
1501 APPLY_FUNC(a, b, length, float_float, MAXLOC_OP);
1502 } else if (*datatype == MPI_2DOUBLE) {
1503 APPLY_FUNC(a, b, length, double_double, MAXLOC_OP);
1507 static void replace_func(void *a, void *b, int *length,
1508 MPI_Datatype * datatype)
1510 memcpy(b, a, *length * smpi_datatype_size(*datatype));
1513 #define CREATE_MPI_OP(name, func) \
1514 static s_smpi_mpi_op_t mpi_##name = { &(func) /* func */, TRUE }; \
1515 MPI_Op name = &mpi_##name;
1517 CREATE_MPI_OP(MPI_MAX, max_func);
1518 CREATE_MPI_OP(MPI_MIN, min_func);
1519 CREATE_MPI_OP(MPI_SUM, sum_func);
1520 CREATE_MPI_OP(MPI_PROD, prod_func);
1521 CREATE_MPI_OP(MPI_LAND, land_func);
1522 CREATE_MPI_OP(MPI_LOR, lor_func);
1523 CREATE_MPI_OP(MPI_LXOR, lxor_func);
1524 CREATE_MPI_OP(MPI_BAND, band_func);
1525 CREATE_MPI_OP(MPI_BOR, bor_func);
1526 CREATE_MPI_OP(MPI_BXOR, bxor_func);
1527 CREATE_MPI_OP(MPI_MAXLOC, maxloc_func);
1528 CREATE_MPI_OP(MPI_MINLOC, minloc_func);
1529 CREATE_MPI_OP(MPI_REPLACE, replace_func);
1532 MPI_Op smpi_op_new(MPI_User_function * function, int commute)
1535 op = xbt_new(s_smpi_mpi_op_t, 1);
1536 op->func = function;
1537 op-> is_commute = commute;
1541 int smpi_op_is_commute(MPI_Op op)
1543 return (op==MPI_OP_NULL) ? 1 : op-> is_commute;
1546 void smpi_op_destroy(MPI_Op op)
1551 void smpi_op_apply(MPI_Op op, void *invec, void *inoutvec, int *len,
1552 MPI_Datatype * datatype)
1554 if(smpi_privatize_global_variables){ //we need to switch here, as the called function may silently touch global variables
1555 XBT_VERB("Applying operation, switch to the right data frame ");
1556 switch_data_segment(smpi_process_index());
1559 if(!_xbt_replay_is_active())
1560 op->func(invec, inoutvec, len, datatype);