1 /* smpi_mpi_dt.c -- MPI primitives to handle datatypes */
2 /* Copyright (c) 2009-2015. The SimGrid Team.
3 * All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
13 #include "smpi_mpi_dt_private.h"
15 #include "xbt/replay.h"
17 #include "simgrid/modelchecker.h"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_mpi_dt, smpi, "Logging specific to SMPI (datatype)");
21 xbt_dict_t smpi_type_keyvals = nullptr;
22 int type_keyval_id=0;//avoid collisions
24 #define CREATE_MPI_DATATYPE(name, type) \
25 static s_smpi_mpi_datatype_t mpi_##name = { \
27 sizeof(type), /* size */ \
28 0, /*was 1 sizeof_substruct*/ \
30 sizeof(type), /* ub = lb + size */ \
31 DT_FLAG_BASIC, /* flags */ \
32 nullptr, /* attributes */ \
33 nullptr, /* pointer on extended struct*/ \
34 0 /* in_use counter */ \
36 const MPI_Datatype name = &mpi_##name;
38 #define CREATE_MPI_DATATYPE_NULL(name) \
39 static s_smpi_mpi_datatype_t mpi_##name = { \
42 0, /* was 1 sizeof_substruct*/ \
44 0, /* ub = lb + size */ \
45 DT_FLAG_BASIC, /* flags */ \
46 nullptr, /* attributes */ \
47 nullptr, /* pointer on extended struct*/ \
48 0 /* in_use counter */ \
50 const MPI_Datatype name = &mpi_##name;
52 //The following are datatypes for the MPI functions MPI_MAXLOC and MPI_MINLOC.
93 // Predefined data types
94 CREATE_MPI_DATATYPE(MPI_CHAR, char);
95 CREATE_MPI_DATATYPE(MPI_SHORT, short);
96 CREATE_MPI_DATATYPE(MPI_INT, int);
97 CREATE_MPI_DATATYPE(MPI_LONG, long);
98 CREATE_MPI_DATATYPE(MPI_LONG_LONG, long long);
99 CREATE_MPI_DATATYPE(MPI_SIGNED_CHAR, signed char);
100 CREATE_MPI_DATATYPE(MPI_UNSIGNED_CHAR, unsigned char);
101 CREATE_MPI_DATATYPE(MPI_UNSIGNED_SHORT, unsigned short);
102 CREATE_MPI_DATATYPE(MPI_UNSIGNED, unsigned int);
103 CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG, unsigned long);
104 CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG_LONG, unsigned long long);
105 CREATE_MPI_DATATYPE(MPI_FLOAT, float);
106 CREATE_MPI_DATATYPE(MPI_DOUBLE, double);
107 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE, long double);
108 CREATE_MPI_DATATYPE(MPI_WCHAR, wchar_t);
109 CREATE_MPI_DATATYPE(MPI_C_BOOL, bool);
110 CREATE_MPI_DATATYPE(MPI_BYTE, int8_t);
111 CREATE_MPI_DATATYPE(MPI_INT8_T, int8_t);
112 CREATE_MPI_DATATYPE(MPI_INT16_T, int16_t);
113 CREATE_MPI_DATATYPE(MPI_INT32_T, int32_t);
114 CREATE_MPI_DATATYPE(MPI_INT64_T, int64_t);
115 CREATE_MPI_DATATYPE(MPI_UINT8_T, uint8_t);
116 CREATE_MPI_DATATYPE(MPI_UINT16_T, uint16_t);
117 CREATE_MPI_DATATYPE(MPI_UINT32_T, uint32_t);
118 CREATE_MPI_DATATYPE(MPI_UINT64_T, uint64_t);
119 CREATE_MPI_DATATYPE(MPI_C_FLOAT_COMPLEX, float _Complex);
120 CREATE_MPI_DATATYPE(MPI_C_DOUBLE_COMPLEX, double _Complex);
121 CREATE_MPI_DATATYPE(MPI_C_LONG_DOUBLE_COMPLEX, long double _Complex);
122 CREATE_MPI_DATATYPE(MPI_AINT, MPI_Aint);
123 CREATE_MPI_DATATYPE(MPI_OFFSET, MPI_Offset);
125 CREATE_MPI_DATATYPE(MPI_FLOAT_INT, float_int);
126 CREATE_MPI_DATATYPE(MPI_LONG_INT, long_int);
127 CREATE_MPI_DATATYPE(MPI_DOUBLE_INT, double_int);
128 CREATE_MPI_DATATYPE(MPI_SHORT_INT, short_int);
129 CREATE_MPI_DATATYPE(MPI_2INT, int_int);
130 CREATE_MPI_DATATYPE(MPI_2FLOAT, float_float);
131 CREATE_MPI_DATATYPE(MPI_2DOUBLE, double_double);
132 CREATE_MPI_DATATYPE(MPI_2LONG, long_long);
134 CREATE_MPI_DATATYPE(MPI_REAL, float);
135 CREATE_MPI_DATATYPE(MPI_REAL4, float);
136 CREATE_MPI_DATATYPE(MPI_REAL8, float);
137 CREATE_MPI_DATATYPE(MPI_REAL16, double);
138 CREATE_MPI_DATATYPE_NULL(MPI_COMPLEX8);
139 CREATE_MPI_DATATYPE_NULL(MPI_COMPLEX16);
140 CREATE_MPI_DATATYPE_NULL(MPI_COMPLEX32);
141 CREATE_MPI_DATATYPE(MPI_INTEGER1, int);
142 CREATE_MPI_DATATYPE(MPI_INTEGER2, int16_t);
143 CREATE_MPI_DATATYPE(MPI_INTEGER4, int32_t);
144 CREATE_MPI_DATATYPE(MPI_INTEGER8, int64_t);
145 CREATE_MPI_DATATYPE(MPI_INTEGER16, integer128_t);
147 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE_INT, long_double_int);
149 CREATE_MPI_DATATYPE_NULL(MPI_UB);
150 CREATE_MPI_DATATYPE_NULL(MPI_LB);
151 CREATE_MPI_DATATYPE(MPI_PACKED, char);
153 CREATE_MPI_DATATYPE(MPI_PTR, void*);
155 /** Check if the datatype is usable for communications */
156 bool is_datatype_valid(MPI_Datatype datatype) {
157 return datatype != MPI_DATATYPE_NULL && ((datatype->flags & DT_FLAG_COMMITED) != 0);
160 size_t smpi_datatype_size(MPI_Datatype datatype)
162 return datatype->size;
165 MPI_Aint smpi_datatype_lb(MPI_Datatype datatype)
170 MPI_Aint smpi_datatype_ub(MPI_Datatype datatype)
175 int smpi_datatype_dup(MPI_Datatype datatype, MPI_Datatype* new_t)
178 *new_t= xbt_new(s_smpi_mpi_datatype_t,1);
179 memcpy(*new_t, datatype, sizeof(s_smpi_mpi_datatype_t));
181 (*new_t)->flags &= ~DT_FLAG_PREDEFINED;
182 if (datatype->sizeof_substruct){
183 (*new_t)->substruct=xbt_malloc(datatype->sizeof_substruct);
184 memcpy((*new_t)->substruct, datatype->substruct, datatype->sizeof_substruct);
187 (*new_t)->name = xbt_strdup(datatype->name);
188 if(datatype->attributes !=nullptr){
189 (*new_t)->attributes = xbt_dict_new_homogeneous(nullptr);
190 xbt_dict_cursor_t cursor = nullptr;
195 xbt_dict_foreach (datatype->attributes, cursor, key, value_in) {
196 smpi_type_key_elem elem = static_cast<smpi_type_key_elem>(
197 xbt_dict_get_or_null_ext(smpi_type_keyvals, reinterpret_cast<const char*>(key), sizeof(int)));
198 if (elem != nullptr && elem->copy_fn != MPI_NULL_COPY_FN) {
199 ret = elem->copy_fn(datatype, *key, nullptr, value_in, &value_out, &flag);
200 if (ret != MPI_SUCCESS) {
201 smpi_datatype_unuse(*new_t);
202 *new_t = MPI_DATATYPE_NULL;
203 xbt_dict_cursor_free(&cursor);
207 xbt_dict_set_ext((*new_t)->attributes, reinterpret_cast<const char*>(key), sizeof(int), value_out, nullptr);
214 int smpi_datatype_extent(MPI_Datatype datatype, MPI_Aint * lb, MPI_Aint * extent)
216 if(datatype == MPI_DATATYPE_NULL){
222 *extent = datatype->ub - datatype->lb;
226 MPI_Aint smpi_datatype_get_extent(MPI_Datatype datatype){
227 if(datatype == MPI_DATATYPE_NULL){
230 return datatype->ub - datatype->lb;
233 void smpi_datatype_get_name(MPI_Datatype datatype, char* name, int* length){
234 *length = strlen(datatype->name);
235 strncpy(name, datatype->name, *length+1);
238 void smpi_datatype_set_name(MPI_Datatype datatype, char* name){
239 if(datatype->name!=nullptr && (datatype->flags & DT_FLAG_PREDEFINED) == 0)
240 xbt_free(datatype->name);
241 datatype->name = xbt_strdup(name);
244 int smpi_datatype_copy(void *sendbuf, int sendcount, MPI_Datatype sendtype,
245 void *recvbuf, int recvcount, MPI_Datatype recvtype)
248 if(smpi_privatize_global_variables){
249 smpi_switch_data_segment(smpi_process_index());
251 /* First check if we really have something to do */
252 if (recvcount > 0 && recvbuf != sendbuf) {
253 /* FIXME: treat packed cases */
254 sendcount *= smpi_datatype_size(sendtype);
255 recvcount *= smpi_datatype_size(recvtype);
256 count = sendcount < recvcount ? sendcount : recvcount;
258 if(sendtype->sizeof_substruct == 0 && recvtype->sizeof_substruct == 0) {
259 if(!smpi_process_get_replaying())
260 memcpy(recvbuf, sendbuf, count);
262 else if (sendtype->sizeof_substruct == 0)
264 s_smpi_subtype_t *subtype = static_cast<s_smpi_subtype_t*>(recvtype->substruct);
265 subtype->unserialize( sendbuf, recvbuf, recvcount/smpi_datatype_size(recvtype), subtype, MPI_REPLACE);
267 else if (recvtype->sizeof_substruct == 0)
269 s_smpi_subtype_t *subtype = static_cast<s_smpi_subtype_t*>(sendtype->substruct);
270 subtype->serialize(sendbuf, recvbuf, sendcount/smpi_datatype_size(sendtype), subtype);
272 s_smpi_subtype_t *subtype = static_cast<s_smpi_subtype_t*>(sendtype->substruct);
274 void * buf_tmp = xbt_malloc(count);
276 subtype->serialize( sendbuf, buf_tmp,count/smpi_datatype_size(sendtype), subtype);
277 subtype = static_cast<s_smpi_subtype_t*>(recvtype->substruct);
278 subtype->unserialize( buf_tmp, recvbuf,count/smpi_datatype_size(recvtype), subtype, MPI_REPLACE);
284 return sendcount > recvcount ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
288 * Copies noncontiguous data into contiguous memory.
289 * @param contiguous_vector - output vector
290 * @param noncontiguous_vector - input vector
291 * @param type - pointer containing :
292 * - stride - stride of between noncontiguous data
293 * - block_length - the width or height of blocked matrix
294 * - count - the number of rows of matrix
296 void serialize_vector( const void *noncontiguous_vector, void *contiguous_vector, int count, void *type)
298 s_smpi_mpi_vector_t* type_c = reinterpret_cast<s_smpi_mpi_vector_t*>(type);
300 char* contiguous_vector_char = static_cast<char*>(contiguous_vector);
301 const char* noncontiguous_vector_char = static_cast<const char*>(noncontiguous_vector);
303 for (i = 0; i < type_c->block_count * count; i++) {
304 if (type_c->old_type->sizeof_substruct == 0)
305 memcpy(contiguous_vector_char, noncontiguous_vector_char, type_c->block_length * type_c->size_oldtype);
307 static_cast<s_smpi_subtype_t*>(type_c->old_type->substruct)->serialize( noncontiguous_vector_char,
308 contiguous_vector_char,
309 type_c->block_length, type_c->old_type->substruct);
311 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
312 if((i+1)%type_c->block_count ==0)
313 noncontiguous_vector_char += type_c->block_length*smpi_datatype_get_extent(type_c->old_type);
315 noncontiguous_vector_char += type_c->block_stride*smpi_datatype_get_extent(type_c->old_type);
320 * Copies contiguous data into noncontiguous memory.
321 * @param noncontiguous_vector - output vector
322 * @param contiguous_vector - input vector
323 * @param type - pointer contening :
324 * - stride - stride of between noncontiguous data
325 * - block_length - the width or height of blocked matrix
326 * - count - the number of rows of matrix
328 void unserialize_vector( const void *contiguous_vector, void *noncontiguous_vector, int count, void *type, MPI_Op op)
330 s_smpi_mpi_vector_t* type_c = reinterpret_cast<s_smpi_mpi_vector_t*>(type);
333 const char* contiguous_vector_char = static_cast<const char*>(contiguous_vector);
334 char* noncontiguous_vector_char = static_cast<char*>(noncontiguous_vector);
336 for (i = 0; i < type_c->block_count * count; i++) {
337 if (type_c->old_type->sizeof_substruct == 0)
338 smpi_op_apply(op, contiguous_vector_char, noncontiguous_vector_char, &type_c->block_length,
341 static_cast<s_smpi_subtype_t*>(type_c->old_type->substruct)->unserialize(contiguous_vector_char, noncontiguous_vector_char,
342 type_c->block_length,type_c->old_type->substruct,
344 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
345 if((i+1)%type_c->block_count ==0)
346 noncontiguous_vector_char += type_c->block_length*smpi_datatype_get_extent(type_c->old_type);
348 noncontiguous_vector_char += type_c->block_stride*smpi_datatype_get_extent(type_c->old_type);
352 /* Create a Sub type vector to be able to serialize and unserialize it the structure s_smpi_mpi_vector_t is derived
353 * from s_smpi_subtype which required the functions unserialize and serialize */
354 s_smpi_mpi_vector_t* smpi_datatype_vector_create( int block_stride, int block_length, int block_count,
355 MPI_Datatype old_type, int size_oldtype){
356 s_smpi_mpi_vector_t *new_t= xbt_new(s_smpi_mpi_vector_t,1);
357 new_t->base.serialize = &serialize_vector;
358 new_t->base.unserialize = &unserialize_vector;
359 new_t->base.subtype_free = &free_vector;
360 new_t->base.subtype_use = &use_vector;
361 new_t->block_stride = block_stride;
362 new_t->block_length = block_length;
363 new_t->block_count = block_count;
364 smpi_datatype_use(old_type);
365 new_t->old_type = old_type;
366 new_t->size_oldtype = size_oldtype;
370 void smpi_datatype_create(MPI_Datatype* new_type, int size,int lb, int ub, int sizeof_substruct, void *struct_type,
372 MPI_Datatype new_t= xbt_new(s_smpi_mpi_datatype_t,1);
373 new_t->name = nullptr;
375 new_t->sizeof_substruct = size>0? sizeof_substruct:0;
378 new_t->flags = flags;
379 new_t->substruct = struct_type;
381 new_t->attributes=nullptr;
386 MC_ignore(&(new_t->in_use), sizeof(new_t->in_use));
390 void smpi_datatype_free(MPI_Datatype* type){
391 xbt_assert((*type)->in_use >= 0);
393 if((*type)->flags & DT_FLAG_PREDEFINED)
396 //if still used, mark for deletion
397 if((*type)->in_use!=0){
398 (*type)->flags |=DT_FLAG_DESTROYED;
402 if((*type)->attributes !=nullptr){
403 xbt_dict_cursor_t cursor = nullptr;
407 xbt_dict_foreach((*type)->attributes, cursor, key, value){
408 smpi_type_key_elem elem =
409 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, reinterpret_cast<const char*>(key), sizeof(int)));
410 if(elem!=nullptr && elem->delete_fn!=nullptr)
411 elem->delete_fn(*type,*key, value, &flag);
413 xbt_dict_free(&(*type)->attributes);
416 if ((*type)->sizeof_substruct != 0){
417 //((s_smpi_subtype_t *)(*type)->substruct)->subtype_free(type);
418 xbt_free((*type)->substruct);
420 xbt_free((*type)->name);
422 *type = MPI_DATATYPE_NULL;
425 void smpi_datatype_use(MPI_Datatype type){
427 if(type != MPI_DATATYPE_NULL)
430 if(type->sizeof_substruct!=0){
431 static_cast<s_smpi_subtype_t *>((type)->substruct)->subtype_use(&type);
435 MC_ignore(&(type->in_use), sizeof(type->in_use));
439 void smpi_datatype_unuse(MPI_Datatype type)
441 if (type == MPI_DATATYPE_NULL)
444 if (type->in_use > 0)
447 if(type->sizeof_substruct!=0){
448 static_cast<s_smpi_subtype_t *>((type)->substruct)->subtype_free(&type);
451 if (type->in_use == 0)
452 smpi_datatype_free(&type);
456 MC_ignore(&(type->in_use), sizeof(type->in_use));
460 /*Contiguous Implementation*/
462 /* Copies noncontiguous data into contiguous memory.
463 * @param contiguous_hvector - output hvector
464 * @param noncontiguous_hvector - input hvector
465 * @param type - pointer contening :
466 * - stride - stride of between noncontiguous data, in bytes
467 * - block_length - the width or height of blocked matrix
468 * - count - the number of rows of matrix
470 void serialize_contiguous( const void *noncontiguous_hvector, void *contiguous_hvector, int count, void *type)
472 s_smpi_mpi_contiguous_t* type_c = reinterpret_cast<s_smpi_mpi_contiguous_t*>(type);
473 char* contiguous_vector_char = static_cast<char*>(contiguous_hvector);
474 const char* noncontiguous_vector_char = static_cast<const char*>(noncontiguous_hvector)+type_c->lb;
475 memcpy(contiguous_vector_char, noncontiguous_vector_char, count* type_c->block_count * type_c->size_oldtype);
477 /* Copies contiguous data into noncontiguous memory.
478 * @param noncontiguous_vector - output hvector
479 * @param contiguous_vector - input hvector
480 * @param type - pointer contening :
481 * - stride - stride of between noncontiguous data, in bytes
482 * - block_length - the width or height of blocked matrix
483 * - count - the number of rows of matrix
485 void unserialize_contiguous(const void *contiguous_vector, void *noncontiguous_vector, int count, void *type, MPI_Op op)
487 s_smpi_mpi_contiguous_t* type_c = reinterpret_cast<s_smpi_mpi_contiguous_t*>(type);
488 const char* contiguous_vector_char = static_cast<const char*>(contiguous_vector);
489 char* noncontiguous_vector_char = static_cast<char*>(noncontiguous_vector)+type_c->lb;
490 int n= count* type_c->block_count;
491 smpi_op_apply(op, contiguous_vector_char, noncontiguous_vector_char, &n, &type_c->old_type);
494 void free_contiguous(MPI_Datatype* d){
495 smpi_datatype_unuse(reinterpret_cast<s_smpi_mpi_contiguous_t*>((*d)->substruct)->old_type);
498 void use_contiguous(MPI_Datatype* d){
499 smpi_datatype_use(reinterpret_cast<s_smpi_mpi_contiguous_t*>((*d)->substruct)->old_type);
502 /* Create a Sub type contiguous to be able to serialize and unserialize it the structure s_smpi_mpi_contiguous_t is
503 * derived from s_smpi_subtype which required the functions unserialize and serialize */
504 s_smpi_mpi_contiguous_t* smpi_datatype_contiguous_create( MPI_Aint lb, int block_count, MPI_Datatype old_type,
508 s_smpi_mpi_contiguous_t *new_t= xbt_new(s_smpi_mpi_contiguous_t,1);
509 new_t->base.serialize = &serialize_contiguous;
510 new_t->base.unserialize = &unserialize_contiguous;
511 new_t->base.subtype_free = &free_contiguous;
512 new_t->base.subtype_use = &use_contiguous;
514 new_t->block_count = block_count;
515 new_t->old_type = old_type;
516 smpi_datatype_use(old_type);
517 new_t->size_oldtype = size_oldtype;
518 smpi_datatype_use(old_type);
522 int smpi_datatype_contiguous(int count, MPI_Datatype old_type, MPI_Datatype* new_type, MPI_Aint lb)
525 if(old_type->sizeof_substruct){
526 //handle this case as a hvector with stride equals to the extent of the datatype
527 return smpi_datatype_hvector(count, 1, smpi_datatype_get_extent(old_type), old_type, new_type);
530 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb, count, old_type,smpi_datatype_size(old_type));
532 smpi_datatype_create(new_type, count * smpi_datatype_size(old_type),lb,lb + count * smpi_datatype_size(old_type),
533 sizeof(s_smpi_mpi_contiguous_t),subtype, DT_FLAG_CONTIGUOUS);
538 int smpi_datatype_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type)
546 lb=smpi_datatype_lb(old_type);
547 ub=((count-1)*stride+blocklen-1)*smpi_datatype_get_extent(old_type)+smpi_datatype_ub(old_type);
549 if(old_type->sizeof_substruct != 0 || stride != blocklen){
551 s_smpi_mpi_vector_t* subtype = smpi_datatype_vector_create(stride, blocklen, count, old_type,
552 smpi_datatype_size(old_type));
553 smpi_datatype_create(new_type, count * (blocklen) * smpi_datatype_size(old_type), lb, ub, sizeof(s_smpi_mpi_vector_t), subtype,
557 /* in this situation the data are contiguous thus it's not required to serialize and unserialize it*/
558 smpi_datatype_create(new_type, count * blocklen * smpi_datatype_size(old_type), 0, ((count -1) * stride + blocklen)*
559 smpi_datatype_size(old_type), 0, nullptr, DT_FLAG_VECTOR|DT_FLAG_CONTIGUOUS);
565 void free_vector(MPI_Datatype* d){
566 smpi_datatype_unuse(reinterpret_cast<s_smpi_mpi_indexed_t*>((*d)->substruct)->old_type);
569 void use_vector(MPI_Datatype* d){
570 smpi_datatype_use(reinterpret_cast<s_smpi_mpi_indexed_t*>((*d)->substruct)->old_type);
573 /* Hvector Implementation - Vector with stride in bytes */
575 /* Copies noncontiguous data into contiguous memory.
576 * @param contiguous_hvector - output hvector
577 * @param noncontiguous_hvector - input hvector
578 * @param type - pointer contening :
579 * - stride - stride of between noncontiguous data, in bytes
580 * - block_length - the width or height of blocked matrix
581 * - count - the number of rows of matrix
583 void serialize_hvector( const void *noncontiguous_hvector, void *contiguous_hvector, int count, void *type)
585 s_smpi_mpi_hvector_t* type_c = reinterpret_cast<s_smpi_mpi_hvector_t*>(type);
587 char* contiguous_vector_char = static_cast<char*>(contiguous_hvector);
588 const char* noncontiguous_vector_char = static_cast<const char*>(noncontiguous_hvector);
590 for (i = 0; i < type_c->block_count * count; i++) {
591 if (type_c->old_type->sizeof_substruct == 0)
592 memcpy(contiguous_vector_char, noncontiguous_vector_char, type_c->block_length * type_c->size_oldtype);
594 static_cast<s_smpi_subtype_t*>(type_c->old_type->substruct)->serialize( noncontiguous_vector_char,
595 contiguous_vector_char,
596 type_c->block_length, type_c->old_type->substruct);
598 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
599 if((i+1)%type_c->block_count ==0)
600 noncontiguous_vector_char += type_c->block_length*type_c->size_oldtype;
602 noncontiguous_vector_char += type_c->block_stride;
605 /* Copies contiguous data into noncontiguous memory.
606 * @param noncontiguous_vector - output hvector
607 * @param contiguous_vector - input hvector
608 * @param type - pointer contening :
609 * - stride - stride of between noncontiguous data, in bytes
610 * - block_length - the width or height of blocked matrix
611 * - count - the number of rows of matrix
613 void unserialize_hvector( const void *contiguous_vector, void *noncontiguous_vector, int count, void *type, MPI_Op op)
615 s_smpi_mpi_hvector_t* type_c = reinterpret_cast<s_smpi_mpi_hvector_t*>(type);
618 const char* contiguous_vector_char = static_cast<const char*>(contiguous_vector);
619 char* noncontiguous_vector_char = static_cast<char*>(noncontiguous_vector);
621 for (i = 0; i < type_c->block_count * count; i++) {
622 if (type_c->old_type->sizeof_substruct == 0)
623 smpi_op_apply(op, contiguous_vector_char, noncontiguous_vector_char, &type_c->block_length, &type_c->old_type);
625 static_cast<s_smpi_subtype_t*>(type_c->old_type->substruct)->unserialize( contiguous_vector_char, noncontiguous_vector_char,
626 type_c->block_length, type_c->old_type->substruct,
628 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
629 if((i+1)%type_c->block_count ==0)
630 noncontiguous_vector_char += type_c->block_length*type_c->size_oldtype;
632 noncontiguous_vector_char += type_c->block_stride;
636 /* Create a Sub type vector to be able to serialize and unserialize it the structure s_smpi_mpi_vector_t is derived
637 * from s_smpi_subtype which required the functions unserialize and serialize
640 s_smpi_mpi_hvector_t* smpi_datatype_hvector_create( MPI_Aint block_stride, int block_length, int block_count,
641 MPI_Datatype old_type, int size_oldtype){
642 s_smpi_mpi_hvector_t *new_t= xbt_new(s_smpi_mpi_hvector_t,1);
643 new_t->base.serialize = &serialize_hvector;
644 new_t->base.unserialize = &unserialize_hvector;
645 new_t->base.subtype_free = &free_hvector;
646 new_t->base.subtype_use = &use_hvector;
647 new_t->block_stride = block_stride;
648 new_t->block_length = block_length;
649 new_t->block_count = block_count;
650 new_t->old_type = old_type;
651 new_t->size_oldtype = size_oldtype;
652 smpi_datatype_use(old_type);
656 //do nothing for vector types
657 void free_hvector(MPI_Datatype* d){
658 smpi_datatype_unuse(reinterpret_cast<s_smpi_mpi_hvector_t*>((*d)->substruct)->old_type);
661 void use_hvector(MPI_Datatype* d){
662 smpi_datatype_use(reinterpret_cast<s_smpi_mpi_hvector_t*>((*d)->substruct)->old_type);
665 int smpi_datatype_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type)
673 lb=smpi_datatype_lb(old_type);
674 ub=((count-1)*stride)+(blocklen-1)*smpi_datatype_get_extent(old_type)+smpi_datatype_ub(old_type);
676 if(old_type->sizeof_substruct != 0 || stride != blocklen*smpi_datatype_get_extent(old_type)){
677 s_smpi_mpi_hvector_t* subtype = smpi_datatype_hvector_create( stride, blocklen, count, old_type,
678 smpi_datatype_size(old_type));
680 smpi_datatype_create(new_type, count * blocklen * smpi_datatype_size(old_type), lb,ub, sizeof(s_smpi_mpi_hvector_t), subtype, DT_FLAG_VECTOR);
683 smpi_datatype_create(new_type, count * blocklen * smpi_datatype_size(old_type),0,count * blocklen *
684 smpi_datatype_size(old_type), 0, nullptr, DT_FLAG_VECTOR|DT_FLAG_CONTIGUOUS);
690 /* Indexed Implementation */
692 /* Copies noncontiguous data into contiguous memory.
693 * @param contiguous_indexed - output indexed
694 * @param noncontiguous_indexed - input indexed
695 * @param type - pointer contening :
696 * - block_lengths - the width or height of blocked matrix
697 * - block_indices - indices of each data, in element
698 * - count - the number of rows of matrix
700 void serialize_indexed( const void *noncontiguous_indexed, void *contiguous_indexed, int count, void *type)
702 s_smpi_mpi_indexed_t* type_c = reinterpret_cast<s_smpi_mpi_indexed_t*>(type);
704 char* contiguous_indexed_char = static_cast<char*>(contiguous_indexed);
705 const char* noncontiguous_indexed_char = static_cast<const char*>(noncontiguous_indexed)+type_c->block_indices[0] * type_c->size_oldtype;
706 for(j=0; j<count;j++){
707 for (i = 0; i < type_c->block_count; i++) {
708 if (type_c->old_type->sizeof_substruct == 0)
709 memcpy(contiguous_indexed_char, noncontiguous_indexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
711 static_cast<s_smpi_subtype_t*>(type_c->old_type->substruct)->serialize( noncontiguous_indexed_char,
712 contiguous_indexed_char,
713 type_c->block_lengths[i],
714 type_c->old_type->substruct);
716 contiguous_indexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
717 if (i<type_c->block_count-1)
718 noncontiguous_indexed_char =
719 static_cast<const char*>(noncontiguous_indexed) + type_c->block_indices[i+1]*smpi_datatype_get_extent(type_c->old_type);
721 noncontiguous_indexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
723 noncontiguous_indexed=static_cast<const void*>(noncontiguous_indexed_char);
726 /* Copies contiguous data into noncontiguous memory.
727 * @param noncontiguous_indexed - output indexed
728 * @param contiguous_indexed - input indexed
729 * @param type - pointer contening :
730 * - block_lengths - the width or height of blocked matrix
731 * - block_indices - indices of each data, in element
732 * - count - the number of rows of matrix
734 void unserialize_indexed( const void *contiguous_indexed, void *noncontiguous_indexed, int count, void *type, MPI_Op op)
736 s_smpi_mpi_indexed_t* type_c = reinterpret_cast<s_smpi_mpi_indexed_t*>(type);
738 const char* contiguous_indexed_char = static_cast<const char*>(contiguous_indexed);
739 char* noncontiguous_indexed_char =
740 static_cast<char*>(noncontiguous_indexed)+type_c->block_indices[0]*smpi_datatype_get_extent(type_c->old_type);
741 for(j=0; j<count;j++){
742 for (i = 0; i < type_c->block_count; i++) {
743 if (type_c->old_type->sizeof_substruct == 0)
744 smpi_op_apply(op, contiguous_indexed_char, noncontiguous_indexed_char, &type_c->block_lengths[i],
747 static_cast<s_smpi_subtype_t*>(type_c->old_type->substruct)->unserialize( contiguous_indexed_char,
748 noncontiguous_indexed_char,
749 type_c->block_lengths[i],
750 type_c->old_type->substruct, op);
752 contiguous_indexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
753 if (i<type_c->block_count-1)
754 noncontiguous_indexed_char =
755 static_cast<char*>(noncontiguous_indexed) + type_c->block_indices[i+1]*smpi_datatype_get_extent(type_c->old_type);
757 noncontiguous_indexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
759 noncontiguous_indexed=static_cast<void*>(noncontiguous_indexed_char);
763 void free_indexed(MPI_Datatype* type){
764 if((*type)->in_use==0){
765 xbt_free(reinterpret_cast<s_smpi_mpi_indexed_t*>((*type)->substruct)->block_lengths);
766 xbt_free(reinterpret_cast<s_smpi_mpi_indexed_t*>((*type)->substruct)->block_indices);
768 smpi_datatype_unuse(reinterpret_cast<s_smpi_mpi_indexed_t*>((*type)->substruct)->old_type);
771 void use_indexed(MPI_Datatype* type){
772 smpi_datatype_use(reinterpret_cast<s_smpi_mpi_indexed_t*>((*type)->substruct)->old_type);
776 /* Create a Sub type indexed to be able to serialize and unserialize it the structure s_smpi_mpi_indexed_t is derived
777 * from s_smpi_subtype which required the functions unserialize and serialize */
778 s_smpi_mpi_indexed_t* smpi_datatype_indexed_create( int* block_lengths, int* block_indices, int block_count,
779 MPI_Datatype old_type, int size_oldtype){
780 s_smpi_mpi_indexed_t *new_t= xbt_new(s_smpi_mpi_indexed_t,1);
781 new_t->base.serialize = &serialize_indexed;
782 new_t->base.unserialize = &unserialize_indexed;
783 new_t->base.subtype_free = &free_indexed;
784 new_t->base.subtype_use = &use_indexed;
785 new_t->block_lengths= xbt_new(int, block_count);
786 new_t->block_indices= xbt_new(int, block_count);
788 for(i=0;i<block_count;i++){
789 new_t->block_lengths[i]=block_lengths[i];
790 new_t->block_indices[i]=block_indices[i];
792 new_t->block_count = block_count;
793 smpi_datatype_use(old_type);
794 new_t->old_type = old_type;
795 new_t->size_oldtype = size_oldtype;
799 int smpi_datatype_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
804 bool contiguous=true;
808 lb=indices[0]*smpi_datatype_get_extent(old_type);
809 ub=indices[0]*smpi_datatype_get_extent(old_type) + blocklens[0]*smpi_datatype_ub(old_type);
812 for(i=0; i< count; i++){
815 size += blocklens[i];
817 if(indices[i]*smpi_datatype_get_extent(old_type)+smpi_datatype_lb(old_type)<lb)
818 lb = indices[i]*smpi_datatype_get_extent(old_type)+smpi_datatype_lb(old_type);
819 if(indices[i]*smpi_datatype_get_extent(old_type)+blocklens[i]*smpi_datatype_ub(old_type)>ub)
820 ub = indices[i]*smpi_datatype_get_extent(old_type)+blocklens[i]*smpi_datatype_ub(old_type);
822 if ( (i< count -1) && (indices[i]+blocklens[i] != indices[i+1]) )
825 if (old_type->sizeof_substruct != 0)
829 s_smpi_mpi_indexed_t* subtype = smpi_datatype_indexed_create( blocklens, indices, count, old_type,
830 smpi_datatype_size(old_type));
831 smpi_datatype_create(new_type, size * smpi_datatype_size(old_type),lb,ub,sizeof(s_smpi_mpi_indexed_t), subtype, DT_FLAG_DATA);
833 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb, size, old_type,
834 smpi_datatype_size(old_type));
835 smpi_datatype_create(new_type, size * smpi_datatype_size(old_type), lb, ub, sizeof(s_smpi_mpi_contiguous_t), subtype,
836 DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
841 /* Hindexed Implementation - Indexed with indices in bytes */
843 /* Copies noncontiguous data into contiguous memory.
844 * @param contiguous_hindexed - output hindexed
845 * @param noncontiguous_hindexed - input hindexed
846 * @param type - pointer contening :
847 * - block_lengths - the width or height of blocked matrix
848 * - block_indices - indices of each data, in bytes
849 * - count - the number of rows of matrix
851 void serialize_hindexed( const void *noncontiguous_hindexed, void *contiguous_hindexed, int count, void *type)
853 s_smpi_mpi_hindexed_t* type_c = reinterpret_cast<s_smpi_mpi_hindexed_t*>(type);
855 char* contiguous_hindexed_char = static_cast<char*>(contiguous_hindexed);
856 const char* noncontiguous_hindexed_char = static_cast<const char*>(noncontiguous_hindexed)+ type_c->block_indices[0];
857 for(j=0; j<count;j++){
858 for (i = 0; i < type_c->block_count; i++) {
859 if (type_c->old_type->sizeof_substruct == 0)
860 memcpy(contiguous_hindexed_char, noncontiguous_hindexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
862 static_cast<s_smpi_subtype_t*>(type_c->old_type->substruct)->serialize( noncontiguous_hindexed_char,
863 contiguous_hindexed_char,
864 type_c->block_lengths[i],
865 type_c->old_type->substruct);
867 contiguous_hindexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
868 if (i<type_c->block_count-1)
869 noncontiguous_hindexed_char = static_cast<const char*>(noncontiguous_hindexed) + type_c->block_indices[i+1];
871 noncontiguous_hindexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
873 noncontiguous_hindexed=reinterpret_cast<const void*>(noncontiguous_hindexed_char);
876 /* Copies contiguous data into noncontiguous memory.
877 * @param noncontiguous_hindexed - output hindexed
878 * @param contiguous_hindexed - input hindexed
879 * @param type - pointer contening :
880 * - block_lengths - the width or height of blocked matrix
881 * - block_indices - indices of each data, in bytes
882 * - count - the number of rows of matrix
884 void unserialize_hindexed( const void *contiguous_hindexed, void *noncontiguous_hindexed, int count, void *type,
887 s_smpi_mpi_hindexed_t* type_c = reinterpret_cast<s_smpi_mpi_hindexed_t*>(type);
890 const char* contiguous_hindexed_char = static_cast<const char*>(contiguous_hindexed);
891 char* noncontiguous_hindexed_char = static_cast<char*>(noncontiguous_hindexed)+ type_c->block_indices[0];
892 for(j=0; j<count;j++){
893 for (i = 0; i < type_c->block_count; i++) {
894 if (type_c->old_type->sizeof_substruct == 0)
895 smpi_op_apply(op, contiguous_hindexed_char, noncontiguous_hindexed_char, &type_c->block_lengths[i],
898 static_cast<s_smpi_subtype_t*>(type_c->old_type->substruct)->unserialize( contiguous_hindexed_char,
899 noncontiguous_hindexed_char,
900 type_c->block_lengths[i],
901 type_c->old_type->substruct, op);
903 contiguous_hindexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
904 if (i<type_c->block_count-1)
905 noncontiguous_hindexed_char = static_cast<char*>(noncontiguous_hindexed) + type_c->block_indices[i+1];
907 noncontiguous_hindexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
909 noncontiguous_hindexed=reinterpret_cast<void*>(noncontiguous_hindexed_char);
913 void free_hindexed(MPI_Datatype* type){
914 if((*type)->in_use==0){
915 xbt_free(reinterpret_cast<s_smpi_mpi_hindexed_t*>((*type)->substruct)->block_lengths);
916 xbt_free(reinterpret_cast<s_smpi_mpi_hindexed_t*>((*type)->substruct)->block_indices);
918 smpi_datatype_unuse(reinterpret_cast<s_smpi_mpi_hindexed_t*>((*type)->substruct)->old_type);
921 void use_hindexed(MPI_Datatype* type){
922 smpi_datatype_use(reinterpret_cast<s_smpi_mpi_hindexed_t*>((*type)->substruct)->old_type);
925 /* Create a Sub type hindexed to be able to serialize and unserialize it the structure s_smpi_mpi_hindexed_t is derived
926 * from s_smpi_subtype which required the functions unserialize and serialize
928 s_smpi_mpi_hindexed_t* smpi_datatype_hindexed_create( int* block_lengths, MPI_Aint* block_indices, int block_count,
929 MPI_Datatype old_type, int size_oldtype){
930 s_smpi_mpi_hindexed_t *new_t= xbt_new(s_smpi_mpi_hindexed_t,1);
931 new_t->base.serialize = &serialize_hindexed;
932 new_t->base.unserialize = &unserialize_hindexed;
933 new_t->base.subtype_free = &free_hindexed;
934 new_t->base.subtype_use = &use_hindexed;
935 new_t->block_lengths= xbt_new(int, block_count);
936 new_t->block_indices= xbt_new(MPI_Aint, block_count);
937 for(int i=0;i<block_count;i++){
938 new_t->block_lengths[i]=block_lengths[i];
939 new_t->block_indices[i]=block_indices[i];
941 new_t->block_count = block_count;
942 new_t->old_type = old_type;
943 smpi_datatype_use(old_type);
944 new_t->size_oldtype = size_oldtype;
948 int smpi_datatype_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
953 bool contiguous=true;
957 lb=indices[0] + smpi_datatype_lb(old_type);
958 ub=indices[0] + blocklens[0]*smpi_datatype_ub(old_type);
960 for(i=0; i< count; i++){
963 size += blocklens[i];
965 if(indices[i]+smpi_datatype_lb(old_type)<lb)
966 lb = indices[i]+smpi_datatype_lb(old_type);
967 if(indices[i]+blocklens[i]*smpi_datatype_ub(old_type)>ub)
968 ub = indices[i]+blocklens[i]*smpi_datatype_ub(old_type);
970 if ( (i< count -1) && (indices[i]+blocklens[i]*(static_cast<int>(smpi_datatype_size(old_type))) != indices[i+1]) )
973 if (old_type->sizeof_substruct != 0 || lb!=0)
977 s_smpi_mpi_hindexed_t* subtype = smpi_datatype_hindexed_create( blocklens, indices, count, old_type,
978 smpi_datatype_size(old_type));
979 smpi_datatype_create(new_type, size * smpi_datatype_size(old_type), lb, ub ,sizeof(s_smpi_mpi_hindexed_t), subtype, DT_FLAG_DATA);
981 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create(lb,size, old_type, smpi_datatype_size(old_type));
982 smpi_datatype_create(new_type, size * smpi_datatype_size(old_type), 0,size * smpi_datatype_size(old_type),
983 1, subtype, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
989 /* struct Implementation - Indexed with indices in bytes */
991 /* Copies noncontiguous data into contiguous memory.
992 * @param contiguous_struct - output struct
993 * @param noncontiguous_struct - input struct
994 * @param type - pointer contening :
995 * - stride - stride of between noncontiguous data
996 * - block_length - the width or height of blocked matrix
997 * - count - the number of rows of matrix
999 void serialize_struct( const void *noncontiguous_struct, void *contiguous_struct, int count, void *type)
1001 s_smpi_mpi_struct_t* type_c = reinterpret_cast<s_smpi_mpi_struct_t*>(type);
1003 char* contiguous_struct_char = static_cast<char*>(contiguous_struct);
1004 const char* noncontiguous_struct_char = static_cast<const char*>(noncontiguous_struct)+ type_c->block_indices[0];
1005 for(j=0; j<count;j++){
1006 for (i = 0; i < type_c->block_count; i++) {
1007 if (type_c->old_types[i]->sizeof_substruct == 0)
1008 memcpy(contiguous_struct_char, noncontiguous_struct_char,
1009 type_c->block_lengths[i] * smpi_datatype_size(type_c->old_types[i]));
1011 static_cast<s_smpi_subtype_t*>(type_c->old_types[i]->substruct)->serialize( noncontiguous_struct_char,
1012 contiguous_struct_char,
1013 type_c->block_lengths[i],
1014 type_c->old_types[i]->substruct);
1017 contiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_size(type_c->old_types[i]);
1018 if (i<type_c->block_count-1)
1019 noncontiguous_struct_char = static_cast<const char*>(noncontiguous_struct) + type_c->block_indices[i+1];
1020 else //let's hope this is MPI_UB ?
1021 noncontiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_types[i]);
1023 noncontiguous_struct=reinterpret_cast<const void*>(noncontiguous_struct_char);
1027 /* Copies contiguous data into noncontiguous memory.
1028 * @param noncontiguous_struct - output struct
1029 * @param contiguous_struct - input struct
1030 * @param type - pointer contening :
1031 * - stride - stride of between noncontiguous data
1032 * - block_length - the width or height of blocked matrix
1033 * - count - the number of rows of matrix
1035 void unserialize_struct( const void *contiguous_struct, void *noncontiguous_struct, int count, void *type, MPI_Op op)
1037 s_smpi_mpi_struct_t* type_c = reinterpret_cast<s_smpi_mpi_struct_t*>(type);
1040 const char* contiguous_struct_char = static_cast<const char*>(contiguous_struct);
1041 char* noncontiguous_struct_char = static_cast<char*>(noncontiguous_struct)+ type_c->block_indices[0];
1042 for(j=0; j<count;j++){
1043 for (i = 0; i < type_c->block_count; i++) {
1044 if (type_c->old_types[i]->sizeof_substruct == 0)
1045 smpi_op_apply(op, contiguous_struct_char, noncontiguous_struct_char, &type_c->block_lengths[i],
1046 & type_c->old_types[i]);
1048 static_cast<s_smpi_subtype_t*>(type_c->old_types[i]->substruct)->unserialize( contiguous_struct_char,
1049 noncontiguous_struct_char,
1050 type_c->block_lengths[i],
1051 type_c->old_types[i]->substruct, op);
1053 contiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_size(type_c->old_types[i]);
1054 if (i<type_c->block_count-1)
1055 noncontiguous_struct_char = static_cast<char*>(noncontiguous_struct) + type_c->block_indices[i+1];
1057 noncontiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_types[i]);
1059 noncontiguous_struct=reinterpret_cast<void*>(noncontiguous_struct_char);
1063 void free_struct(MPI_Datatype* type){
1065 for (i = 0; i < reinterpret_cast<s_smpi_mpi_struct_t*>((*type)->substruct)->block_count; i++)
1066 smpi_datatype_unuse(reinterpret_cast<s_smpi_mpi_struct_t*>((*type)->substruct)->old_types[i]);
1067 if((*type)->in_use==0){
1068 xbt_free(reinterpret_cast<s_smpi_mpi_struct_t*>((*type)->substruct)->block_lengths);
1069 xbt_free(reinterpret_cast<s_smpi_mpi_struct_t*>((*type)->substruct)->block_indices);
1070 xbt_free(reinterpret_cast<s_smpi_mpi_struct_t*>((*type)->substruct)->old_types);
1074 void use_struct(MPI_Datatype* type){
1076 for (i = 0; i < reinterpret_cast<s_smpi_mpi_struct_t*>((*type)->substruct)->block_count; i++)
1077 smpi_datatype_use(reinterpret_cast<s_smpi_mpi_struct_t*>((*type)->substruct)->old_types[i]);
1080 /* Create a Sub type struct to be able to serialize and unserialize it the structure s_smpi_mpi_struct_t is derived
1081 * from s_smpi_subtype which required the functions unserialize and serialize
1083 s_smpi_mpi_struct_t* smpi_datatype_struct_create( int* block_lengths, MPI_Aint* block_indices, int block_count,
1084 MPI_Datatype* old_types){
1085 s_smpi_mpi_struct_t *new_t= xbt_new(s_smpi_mpi_struct_t,1);
1086 new_t->base.serialize = &serialize_struct;
1087 new_t->base.unserialize = &unserialize_struct;
1088 new_t->base.subtype_free = &free_struct;
1089 new_t->base.subtype_use = &use_struct;
1090 new_t->block_lengths= xbt_new(int, block_count);
1091 new_t->block_indices= xbt_new(MPI_Aint, block_count);
1092 new_t->old_types= xbt_new(MPI_Datatype, block_count);
1094 for(i=0;i<block_count;i++){
1095 new_t->block_lengths[i]=block_lengths[i];
1096 new_t->block_indices[i]=block_indices[i];
1097 new_t->old_types[i]=old_types[i];
1098 smpi_datatype_use(new_t->old_types[i]);
1100 new_t->block_count = block_count;
1104 int smpi_datatype_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type)
1108 bool contiguous=true;
1113 lb=indices[0] + smpi_datatype_lb(old_types[0]);
1114 ub=indices[0] + blocklens[0]*smpi_datatype_ub(old_types[0]);
1116 bool forced_lb=false;
1117 bool forced_ub=false;
1118 for(i=0; i< count; i++){
1121 if (old_types[i]->sizeof_substruct != 0)
1124 size += blocklens[i]*smpi_datatype_size(old_types[i]);
1125 if (old_types[i]==MPI_LB){
1129 if (old_types[i]==MPI_UB){
1134 if(!forced_lb && indices[i]+smpi_datatype_lb(old_types[i])<lb)
1136 if(!forced_ub && indices[i]+blocklens[i]*smpi_datatype_ub(old_types[i])>ub)
1137 ub = indices[i]+blocklens[i]*smpi_datatype_ub(old_types[i]);
1139 if ( (i< count -1) && (indices[i]+blocklens[i]*static_cast<int>(smpi_datatype_size(old_types[i])) != indices[i+1]) )
1144 s_smpi_mpi_struct_t* subtype = smpi_datatype_struct_create( blocklens, indices, count, old_types);
1146 smpi_datatype_create(new_type, size, lb, ub,sizeof(s_smpi_mpi_struct_t), subtype, DT_FLAG_DATA);
1148 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb, size, MPI_CHAR, 1);
1149 smpi_datatype_create(new_type, size, lb, ub,1, subtype, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
1154 void smpi_datatype_commit(MPI_Datatype *datatype)
1156 (*datatype)->flags= ((*datatype)->flags | DT_FLAG_COMMITED);
1159 typedef struct s_smpi_mpi_op {
1160 MPI_User_function *func;
1164 #define MAX_OP(a, b) (b) = (a) < (b) ? (b) : (a)
1165 #define MIN_OP(a, b) (b) = (a) < (b) ? (a) : (b)
1166 #define SUM_OP(a, b) (b) += (a)
1167 #define PROD_OP(a, b) (b) *= (a)
1168 #define LAND_OP(a, b) (b) = (a) && (b)
1169 #define LOR_OP(a, b) (b) = (a) || (b)
1170 #define LXOR_OP(a, b) (b) = (!(a) && (b)) || ((a) && !(b))
1171 #define BAND_OP(a, b) (b) &= (a)
1172 #define BOR_OP(a, b) (b) |= (a)
1173 #define BXOR_OP(a, b) (b) ^= (a)
1174 #define MAXLOC_OP(a, b) (b) = (a.value) < (b.value) ? (b) : (a)
1175 #define MINLOC_OP(a, b) (b) = (a.value) < (b.value) ? (a) : (b)
1177 #define APPLY_FUNC(a, b, length, type, func) \
1180 type* x = (type*)(a); \
1181 type* y = (type*)(b); \
1182 for(i = 0; i < *(length); i++) { \
1187 static void max_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1189 if (*datatype == MPI_CHAR) {
1190 APPLY_FUNC(a, b, length, char, MAX_OP)
1191 } else if (*datatype == MPI_SHORT) {
1192 APPLY_FUNC(a, b, length, short, MAX_OP)
1193 } else if (*datatype == MPI_INT) {
1194 APPLY_FUNC(a, b, length, int, MAX_OP)
1195 } else if (*datatype == MPI_LONG) {
1196 APPLY_FUNC(a, b, length, long, MAX_OP)
1197 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1198 APPLY_FUNC(a, b, length, unsigned short, MAX_OP)
1199 } else if (*datatype == MPI_UNSIGNED) {
1200 APPLY_FUNC(a, b, length, unsigned int, MAX_OP)
1201 } else if (*datatype == MPI_UNSIGNED_LONG) {
1202 APPLY_FUNC(a, b, length, unsigned long, MAX_OP)
1203 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1204 APPLY_FUNC(a, b, length, unsigned char, MAX_OP)
1205 } else if (*datatype == MPI_FLOAT) {
1206 APPLY_FUNC(a, b, length, float, MAX_OP)
1207 } else if (*datatype == MPI_DOUBLE) {
1208 APPLY_FUNC(a, b, length, double, MAX_OP)
1209 } else if (*datatype == MPI_LONG_DOUBLE) {
1210 APPLY_FUNC(a, b, length, long double, MAX_OP)
1212 xbt_die("Failed to apply MAX_OP to type %s", (*datatype)->name);
1216 static void min_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1218 if (*datatype == MPI_CHAR) {
1219 APPLY_FUNC(a, b, length, char, MIN_OP)
1220 } else if (*datatype == MPI_SHORT) {
1221 APPLY_FUNC(a, b, length, short, MIN_OP)
1222 } else if (*datatype == MPI_INT) {
1223 APPLY_FUNC(a, b, length, int, MIN_OP)
1224 } else if (*datatype == MPI_LONG) {
1225 APPLY_FUNC(a, b, length, long, MIN_OP)
1226 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1227 APPLY_FUNC(a, b, length, unsigned short, MIN_OP)
1228 } else if (*datatype == MPI_UNSIGNED) {
1229 APPLY_FUNC(a, b, length, unsigned int, MIN_OP)
1230 } else if (*datatype == MPI_UNSIGNED_LONG) {
1231 APPLY_FUNC(a, b, length, unsigned long, MIN_OP)
1232 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1233 APPLY_FUNC(a, b, length, unsigned char, MIN_OP)
1234 } else if (*datatype == MPI_FLOAT) {
1235 APPLY_FUNC(a, b, length, float, MIN_OP)
1236 } else if (*datatype == MPI_DOUBLE) {
1237 APPLY_FUNC(a, b, length, double, MIN_OP)
1238 } else if (*datatype == MPI_LONG_DOUBLE) {
1239 APPLY_FUNC(a, b, length, long double, MIN_OP)
1241 xbt_die("Failed to apply MIN_OP to type %s", (*datatype)->name);
1245 static void sum_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1247 if (*datatype == MPI_CHAR) {
1248 APPLY_FUNC(a, b, length, char, SUM_OP)
1249 } else if (*datatype == MPI_SHORT) {
1250 APPLY_FUNC(a, b, length, short, SUM_OP)
1251 } else if (*datatype == MPI_INT) {
1252 APPLY_FUNC(a, b, length, int, SUM_OP)
1253 } else if (*datatype == MPI_LONG) {
1254 APPLY_FUNC(a, b, length, long, SUM_OP)
1255 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1256 APPLY_FUNC(a, b, length, unsigned short, SUM_OP)
1257 } else if (*datatype == MPI_UNSIGNED) {
1258 APPLY_FUNC(a, b, length, unsigned int, SUM_OP)
1259 } else if (*datatype == MPI_UNSIGNED_LONG) {
1260 APPLY_FUNC(a, b, length, unsigned long, SUM_OP)
1261 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1262 APPLY_FUNC(a, b, length, unsigned char, SUM_OP)
1263 } else if (*datatype == MPI_FLOAT) {
1264 APPLY_FUNC(a, b, length, float, SUM_OP)
1265 } else if (*datatype == MPI_DOUBLE) {
1266 APPLY_FUNC(a, b, length, double, SUM_OP)
1267 } else if (*datatype == MPI_LONG_DOUBLE) {
1268 APPLY_FUNC(a, b, length, long double, SUM_OP)
1269 } else if (*datatype == MPI_C_FLOAT_COMPLEX) {
1270 APPLY_FUNC(a, b, length, float _Complex, SUM_OP)
1271 } else if (*datatype == MPI_C_DOUBLE_COMPLEX) {
1272 APPLY_FUNC(a, b, length, double _Complex, SUM_OP)
1273 } else if (*datatype == MPI_C_LONG_DOUBLE_COMPLEX) {
1274 APPLY_FUNC(a, b, length, long double _Complex, SUM_OP)
1276 xbt_die("Failed to apply SUM_OP to type %s", (*datatype)->name);
1280 static void prod_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1282 if (*datatype == MPI_CHAR) {
1283 APPLY_FUNC(a, b, length, char, PROD_OP)
1284 } else if (*datatype == MPI_SHORT) {
1285 APPLY_FUNC(a, b, length, short, PROD_OP)
1286 } else if (*datatype == MPI_INT) {
1287 APPLY_FUNC(a, b, length, int, PROD_OP)
1288 } else if (*datatype == MPI_LONG) {
1289 APPLY_FUNC(a, b, length, long, PROD_OP)
1290 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1291 APPLY_FUNC(a, b, length, unsigned short, PROD_OP)
1292 } else if (*datatype == MPI_UNSIGNED) {
1293 APPLY_FUNC(a, b, length, unsigned int, PROD_OP)
1294 } else if (*datatype == MPI_UNSIGNED_LONG) {
1295 APPLY_FUNC(a, b, length, unsigned long, PROD_OP)
1296 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1297 APPLY_FUNC(a, b, length, unsigned char, PROD_OP)
1298 } else if (*datatype == MPI_FLOAT) {
1299 APPLY_FUNC(a, b, length, float, PROD_OP)
1300 } else if (*datatype == MPI_DOUBLE) {
1301 APPLY_FUNC(a, b, length, double, PROD_OP)
1302 } else if (*datatype == MPI_LONG_DOUBLE) {
1303 APPLY_FUNC(a, b, length, long double, PROD_OP)
1304 } else if (*datatype == MPI_C_FLOAT_COMPLEX) {
1305 APPLY_FUNC(a, b, length, float _Complex, PROD_OP)
1306 } else if (*datatype == MPI_C_DOUBLE_COMPLEX) {
1307 APPLY_FUNC(a, b, length, double _Complex, PROD_OP)
1308 } else if (*datatype == MPI_C_LONG_DOUBLE_COMPLEX) {
1309 APPLY_FUNC(a, b, length, long double _Complex, PROD_OP)
1311 xbt_die("Failed to apply PROD_OP to type %s", (*datatype)->name);
1315 static void land_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1317 if (*datatype == MPI_CHAR) {
1318 APPLY_FUNC(a, b, length, char, LAND_OP)
1319 } else if (*datatype == MPI_SHORT) {
1320 APPLY_FUNC(a, b, length, short, LAND_OP)
1321 } else if (*datatype == MPI_INT) {
1322 APPLY_FUNC(a, b, length, int, LAND_OP)
1323 } else if (*datatype == MPI_LONG) {
1324 APPLY_FUNC(a, b, length, long, LAND_OP)
1325 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1326 APPLY_FUNC(a, b, length, unsigned short, LAND_OP)
1327 } else if (*datatype == MPI_UNSIGNED) {
1328 APPLY_FUNC(a, b, length, unsigned int, LAND_OP)
1329 } else if (*datatype == MPI_UNSIGNED_LONG) {
1330 APPLY_FUNC(a, b, length, unsigned long, LAND_OP)
1331 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1332 APPLY_FUNC(a, b, length, unsigned char, LAND_OP)
1333 } else if (*datatype == MPI_C_BOOL) {
1334 APPLY_FUNC(a, b, length, bool, LAND_OP)
1336 xbt_die("Failed to apply LAND_OP to type %s", (*datatype)->name);
1340 static void lor_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1342 if (*datatype == MPI_CHAR) {
1343 APPLY_FUNC(a, b, length, char, LOR_OP)
1344 } else if (*datatype == MPI_SHORT) {
1345 APPLY_FUNC(a, b, length, short, LOR_OP)
1346 } else if (*datatype == MPI_INT) {
1347 APPLY_FUNC(a, b, length, int, LOR_OP)
1348 } else if (*datatype == MPI_LONG) {
1349 APPLY_FUNC(a, b, length, long, LOR_OP)
1350 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1351 APPLY_FUNC(a, b, length, unsigned short, LOR_OP)
1352 } else if (*datatype == MPI_UNSIGNED) {
1353 APPLY_FUNC(a, b, length, unsigned int, LOR_OP)
1354 } else if (*datatype == MPI_UNSIGNED_LONG) {
1355 APPLY_FUNC(a, b, length, unsigned long, LOR_OP)
1356 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1357 APPLY_FUNC(a, b, length, unsigned char, LOR_OP)
1358 } else if (*datatype == MPI_C_BOOL) {
1359 APPLY_FUNC(a, b, length, bool, LOR_OP)
1361 xbt_die("Failed to apply LOR_OP to type %s", (*datatype)->name);
1365 static void lxor_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1367 if (*datatype == MPI_CHAR) {
1368 APPLY_FUNC(a, b, length, char, LXOR_OP)
1369 } else if (*datatype == MPI_SHORT) {
1370 APPLY_FUNC(a, b, length, short, LXOR_OP)
1371 } else if (*datatype == MPI_INT) {
1372 APPLY_FUNC(a, b, length, int, LXOR_OP)
1373 } else if (*datatype == MPI_LONG) {
1374 APPLY_FUNC(a, b, length, long, LXOR_OP)
1375 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1376 APPLY_FUNC(a, b, length, unsigned short, LXOR_OP)
1377 } else if (*datatype == MPI_UNSIGNED) {
1378 APPLY_FUNC(a, b, length, unsigned int, LXOR_OP)
1379 } else if (*datatype == MPI_UNSIGNED_LONG) {
1380 APPLY_FUNC(a, b, length, unsigned long, LXOR_OP)
1381 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1382 APPLY_FUNC(a, b, length, unsigned char, LXOR_OP)
1383 } else if (*datatype == MPI_C_BOOL) {
1384 APPLY_FUNC(a, b, length, bool, LXOR_OP)
1386 xbt_die("Failed to apply LXOR_OP to type %s", (*datatype)->name);
1390 static void band_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1392 if (*datatype == MPI_CHAR) {
1393 APPLY_FUNC(a, b, length, char, BAND_OP)
1394 }else if (*datatype == MPI_SHORT) {
1395 APPLY_FUNC(a, b, length, short, BAND_OP)
1396 } else if (*datatype == MPI_INT) {
1397 APPLY_FUNC(a, b, length, int, BAND_OP)
1398 } else if (*datatype == MPI_LONG) {
1399 APPLY_FUNC(a, b, length, long, BAND_OP)
1400 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1401 APPLY_FUNC(a, b, length, unsigned short, BAND_OP)
1402 } else if (*datatype == MPI_UNSIGNED) {
1403 APPLY_FUNC(a, b, length, unsigned int, BAND_OP)
1404 } else if (*datatype == MPI_UNSIGNED_LONG) {
1405 APPLY_FUNC(a, b, length, unsigned long, BAND_OP)
1406 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1407 APPLY_FUNC(a, b, length, unsigned char, BAND_OP)
1408 } else if (*datatype == MPI_BYTE) {
1409 APPLY_FUNC(a, b, length, uint8_t, BAND_OP)
1411 xbt_die("Failed to apply BAND_OP to type %s", (*datatype)->name);
1415 static void bor_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1417 if (*datatype == MPI_CHAR) {
1418 APPLY_FUNC(a, b, length, char, BOR_OP)
1419 } else if (*datatype == MPI_SHORT) {
1420 APPLY_FUNC(a, b, length, short, BOR_OP)
1421 } else if (*datatype == MPI_INT) {
1422 APPLY_FUNC(a, b, length, int, BOR_OP)
1423 } else if (*datatype == MPI_LONG) {
1424 APPLY_FUNC(a, b, length, long, BOR_OP)
1425 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1426 APPLY_FUNC(a, b, length, unsigned short, BOR_OP)
1427 } else if (*datatype == MPI_UNSIGNED) {
1428 APPLY_FUNC(a, b, length, unsigned int, BOR_OP)
1429 } else if (*datatype == MPI_UNSIGNED_LONG) {
1430 APPLY_FUNC(a, b, length, unsigned long, BOR_OP)
1431 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1432 APPLY_FUNC(a, b, length, unsigned char, BOR_OP)
1433 } else if (*datatype == MPI_BYTE) {
1434 APPLY_FUNC(a, b, length, uint8_t, BOR_OP)
1436 xbt_die("Failed to apply BOR_OP to type %s", (*datatype)->name);
1440 static void bxor_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1442 if (*datatype == MPI_CHAR) {
1443 APPLY_FUNC(a, b, length, char, BXOR_OP)
1444 } else if (*datatype == MPI_SHORT) {
1445 APPLY_FUNC(a, b, length, short, BXOR_OP)
1446 } else if (*datatype == MPI_INT) {
1447 APPLY_FUNC(a, b, length, int, BXOR_OP)
1448 } else if (*datatype == MPI_LONG) {
1449 APPLY_FUNC(a, b, length, long, BXOR_OP)
1450 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1451 APPLY_FUNC(a, b, length, unsigned short, BXOR_OP)
1452 } else if (*datatype == MPI_UNSIGNED) {
1453 APPLY_FUNC(a, b, length, unsigned int, BXOR_OP)
1454 } else if (*datatype == MPI_UNSIGNED_LONG) {
1455 APPLY_FUNC(a, b, length, unsigned long, BXOR_OP)
1456 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1457 APPLY_FUNC(a, b, length, unsigned char, BXOR_OP)
1458 } else if (*datatype == MPI_BYTE) {
1459 APPLY_FUNC(a, b, length, uint8_t, BXOR_OP)
1461 xbt_die("Failed to apply BXOR_OP to type %s", (*datatype)->name);
1465 static void minloc_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1467 if (*datatype == MPI_FLOAT_INT) {
1468 APPLY_FUNC(a, b, length, float_int, MINLOC_OP)
1469 } else if (*datatype == MPI_LONG_INT) {
1470 APPLY_FUNC(a, b, length, long_int, MINLOC_OP)
1471 } else if (*datatype == MPI_DOUBLE_INT) {
1472 APPLY_FUNC(a, b, length, double_int, MINLOC_OP)
1473 } else if (*datatype == MPI_SHORT_INT) {
1474 APPLY_FUNC(a, b, length, short_int, MINLOC_OP)
1475 } else if (*datatype == MPI_2LONG) {
1476 APPLY_FUNC(a, b, length, long_long, MINLOC_OP)
1477 } else if (*datatype == MPI_2INT) {
1478 APPLY_FUNC(a, b, length, int_int, MINLOC_OP)
1479 } else if (*datatype == MPI_LONG_DOUBLE_INT) {
1480 APPLY_FUNC(a, b, length, long_double_int, MINLOC_OP)
1481 } else if (*datatype == MPI_2FLOAT) {
1482 APPLY_FUNC(a, b, length, float_float, MINLOC_OP)
1483 } else if (*datatype == MPI_2DOUBLE) {
1484 APPLY_FUNC(a, b, length, double_double, MINLOC_OP)
1486 xbt_die("Failed to apply MINLOC_OP to type %s", (*datatype)->name);
1490 static void maxloc_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1492 if (*datatype == MPI_FLOAT_INT) {
1493 APPLY_FUNC(a, b, length, float_int, MAXLOC_OP)
1494 } else if (*datatype == MPI_LONG_INT) {
1495 APPLY_FUNC(a, b, length, long_int, MAXLOC_OP)
1496 } else if (*datatype == MPI_DOUBLE_INT) {
1497 APPLY_FUNC(a, b, length, double_int, MAXLOC_OP)
1498 } else if (*datatype == MPI_SHORT_INT) {
1499 APPLY_FUNC(a, b, length, short_int, MAXLOC_OP)
1500 } else if (*datatype == MPI_2LONG) {
1501 APPLY_FUNC(a, b, length, long_long, MAXLOC_OP)
1502 } else if (*datatype == MPI_2INT) {
1503 APPLY_FUNC(a, b, length, int_int, MAXLOC_OP)
1504 } else if (*datatype == MPI_LONG_DOUBLE_INT) {
1505 APPLY_FUNC(a, b, length, long_double_int, MAXLOC_OP)
1506 } else if (*datatype == MPI_2FLOAT) {
1507 APPLY_FUNC(a, b, length, float_float, MAXLOC_OP)
1508 } else if (*datatype == MPI_2DOUBLE) {
1509 APPLY_FUNC(a, b, length, double_double, MAXLOC_OP)
1511 xbt_die("Failed to apply MAXLOC_OP to type %s", (*datatype)->name);
1515 static void replace_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1517 memcpy(b, a, *length * smpi_datatype_size(*datatype));
1520 #define CREATE_MPI_OP(name, func) \
1521 static s_smpi_mpi_op_t mpi_##name = { &(func) /* func */, true }; \
1522 MPI_Op name = &mpi_##name;
1524 CREATE_MPI_OP(MPI_MAX, max_func);
1525 CREATE_MPI_OP(MPI_MIN, min_func);
1526 CREATE_MPI_OP(MPI_SUM, sum_func);
1527 CREATE_MPI_OP(MPI_PROD, prod_func);
1528 CREATE_MPI_OP(MPI_LAND, land_func);
1529 CREATE_MPI_OP(MPI_LOR, lor_func);
1530 CREATE_MPI_OP(MPI_LXOR, lxor_func);
1531 CREATE_MPI_OP(MPI_BAND, band_func);
1532 CREATE_MPI_OP(MPI_BOR, bor_func);
1533 CREATE_MPI_OP(MPI_BXOR, bxor_func);
1534 CREATE_MPI_OP(MPI_MAXLOC, maxloc_func);
1535 CREATE_MPI_OP(MPI_MINLOC, minloc_func);
1536 CREATE_MPI_OP(MPI_REPLACE, replace_func);
1538 MPI_Op smpi_op_new(MPI_User_function * function, bool commute)
1541 op = xbt_new(s_smpi_mpi_op_t, 1);
1542 op->func = function;
1543 op-> is_commute = commute;
1547 bool smpi_op_is_commute(MPI_Op op)
1549 return (op==MPI_OP_NULL) ? true : op-> is_commute;
1552 void smpi_op_destroy(MPI_Op op)
1557 void smpi_op_apply(MPI_Op op, const void *invec, void *inoutvec, int *len, MPI_Datatype * datatype)
1562 if(smpi_privatize_global_variables){//we need to switch as the called function may silently touch global variables
1563 XBT_DEBUG("Applying operation, switch to the right data frame ");
1564 smpi_switch_data_segment(smpi_process_index());
1567 if(!smpi_process_get_replaying())
1568 op->func(const_cast<void*>(invec), inoutvec, len, datatype);
1571 int smpi_type_attr_delete(MPI_Datatype type, int keyval){
1572 smpi_type_key_elem elem =
1573 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
1576 if(elem->delete_fn!=MPI_NULL_DELETE_FN){
1577 void * value = nullptr;
1579 if(smpi_type_attr_get(type, keyval, &value, &flag)==MPI_SUCCESS){
1580 int ret = elem->delete_fn(type, keyval, value, &flag);
1581 if(ret!=MPI_SUCCESS)
1585 if(type->attributes==nullptr)
1588 xbt_dict_remove_ext(type->attributes, reinterpret_cast<const char*>(&keyval), sizeof(int));
1592 int smpi_type_attr_get(MPI_Datatype type, int keyval, void* attr_value, int* flag){
1593 smpi_type_key_elem elem =
1594 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
1597 if(type->attributes==nullptr){
1602 *static_cast<void**>(attr_value) = xbt_dict_get_ext(type->attributes, reinterpret_cast<const char*>(&keyval), sizeof(int));
1605 catch (xbt_ex& ex) {
1611 int smpi_type_attr_put(MPI_Datatype type, int keyval, void* attr_value){
1612 if(smpi_type_keyvals==nullptr)
1613 smpi_type_keyvals = xbt_dict_new_homogeneous(nullptr);
1614 smpi_type_key_elem elem =
1615 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
1619 void* value = nullptr;
1620 smpi_type_attr_get(type, keyval, &value, &flag);
1621 if(flag!=0 && elem->delete_fn!=MPI_NULL_DELETE_FN){
1622 int ret = elem->delete_fn(type, keyval, value, &flag);
1623 if(ret!=MPI_SUCCESS)
1626 if(type->attributes==nullptr)
1627 type->attributes = xbt_dict_new_homogeneous(nullptr);
1629 xbt_dict_set_ext(type->attributes, reinterpret_cast<const char*>(&keyval), sizeof(int), attr_value, nullptr);
1633 int smpi_type_keyval_create(MPI_Type_copy_attr_function* copy_fn, MPI_Type_delete_attr_function* delete_fn, int* keyval,
1635 if(smpi_type_keyvals==nullptr)
1636 smpi_type_keyvals = xbt_dict_new_homogeneous(nullptr);
1638 smpi_type_key_elem value = (smpi_type_key_elem) xbt_new0(s_smpi_mpi_type_key_elem_t,1);
1640 value->copy_fn=copy_fn;
1641 value->delete_fn=delete_fn;
1643 *keyval = type_keyval_id;
1644 xbt_dict_set_ext(smpi_type_keyvals,reinterpret_cast<const char*>(keyval), sizeof(int),reinterpret_cast<void*>(value), nullptr);
1649 int smpi_type_keyval_free(int* keyval){
1650 smpi_type_key_elem elem =
1651 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int)));
1655 xbt_dict_remove_ext(smpi_type_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int));
1660 int smpi_mpi_pack(void* inbuf, int incount, MPI_Datatype type, void* outbuf, int outcount, int* position,MPI_Comm comm){
1661 size_t size = smpi_datatype_size(type);
1662 if (outcount - *position < incount*static_cast<int>(size))
1663 return MPI_ERR_BUFFER;
1664 smpi_datatype_copy(inbuf, incount, type, static_cast<char*>(outbuf) + *position, outcount, MPI_CHAR);
1665 *position += incount * size;
1669 int smpi_mpi_unpack(void* inbuf, int insize, int* position, void* outbuf, int outcount, MPI_Datatype type,MPI_Comm comm){
1670 int size = static_cast<int>(smpi_datatype_size(type));
1671 if (outcount*size> insize)
1672 return MPI_ERR_BUFFER;
1673 smpi_datatype_copy(static_cast<char*>(inbuf) + *position, insize, MPI_CHAR, outbuf, outcount, type);
1674 *position += outcount * size;