1 /* smpi_mpi_dt.c -- MPI primitives to handle datatypes */
2 /* Copyright (c) 2009-2017. The SimGrid Team. All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "simgrid/modelchecker.h"
10 #include "smpi_mpi_dt_private.h"
11 #include "xbt/replay.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 =
197 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, key, sizeof(int)));
198 if (elem != nullptr && elem->copy_fn != MPI_NULL_COPY_FN) {
199 ret = elem->copy_fn(datatype, atoi(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, 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( 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 char* noncontiguous_vector_char = static_cast<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( 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 char* contiguous_vector_char = static_cast<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, 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)
431 if(type->sizeof_substruct!=0){
432 static_cast<s_smpi_subtype_t *>((type)->substruct)->subtype_use(&type);
436 MC_ignore(&(type->in_use), sizeof(type->in_use));
440 void smpi_datatype_unuse(MPI_Datatype type)
442 if (type == MPI_DATATYPE_NULL)
445 if (type->in_use > 0)
448 if(type->sizeof_substruct!=0){
449 static_cast<s_smpi_subtype_t *>((type)->substruct)->subtype_free(&type);
452 if (type->in_use == 0)
453 smpi_datatype_free(&type);
457 MC_ignore(&(type->in_use), sizeof(type->in_use));
461 /*Contiguous Implementation*/
463 /* Copies noncontiguous data into contiguous memory.
464 * @param contiguous_hvector - output hvector
465 * @param noncontiguous_hvector - input hvector
466 * @param type - pointer contening :
467 * - stride - stride of between noncontiguous data, in bytes
468 * - block_length - the width or height of blocked matrix
469 * - count - the number of rows of matrix
471 void serialize_contiguous( void* noncontiguous_hvector, void *contiguous_hvector, int count, void *type)
473 s_smpi_mpi_contiguous_t* type_c = reinterpret_cast<s_smpi_mpi_contiguous_t*>(type);
474 char* contiguous_vector_char = static_cast<char*>(contiguous_hvector);
475 char* noncontiguous_vector_char = static_cast<char*>(noncontiguous_hvector)+type_c->lb;
476 memcpy(contiguous_vector_char, noncontiguous_vector_char, count* type_c->block_count * type_c->size_oldtype);
478 /* Copies contiguous data into noncontiguous memory.
479 * @param noncontiguous_vector - output hvector
480 * @param contiguous_vector - input hvector
481 * @param type - pointer contening :
482 * - stride - stride of between noncontiguous data, in bytes
483 * - block_length - the width or height of blocked matrix
484 * - count - the number of rows of matrix
486 void unserialize_contiguous(void* contiguous_vector, void *noncontiguous_vector, int count, void *type, MPI_Op op)
488 s_smpi_mpi_contiguous_t* type_c = reinterpret_cast<s_smpi_mpi_contiguous_t*>(type);
489 char* contiguous_vector_char = static_cast<char*>(contiguous_vector);
490 char* noncontiguous_vector_char = static_cast<char*>(noncontiguous_vector)+type_c->lb;
491 int n= count* type_c->block_count;
492 smpi_op_apply(op, contiguous_vector_char, noncontiguous_vector_char, &n, &type_c->old_type);
495 void free_contiguous(MPI_Datatype* d){
496 smpi_datatype_unuse(reinterpret_cast<s_smpi_mpi_contiguous_t*>((*d)->substruct)->old_type);
499 void use_contiguous(MPI_Datatype* d){
500 smpi_datatype_use(reinterpret_cast<s_smpi_mpi_contiguous_t*>((*d)->substruct)->old_type);
503 /* Create a Sub type contiguous to be able to serialize and unserialize it the structure s_smpi_mpi_contiguous_t is
504 * derived from s_smpi_subtype which required the functions unserialize and serialize */
505 s_smpi_mpi_contiguous_t* smpi_datatype_contiguous_create( MPI_Aint lb, int block_count, MPI_Datatype old_type,
509 s_smpi_mpi_contiguous_t *new_t= xbt_new(s_smpi_mpi_contiguous_t,1);
510 new_t->base.serialize = &serialize_contiguous;
511 new_t->base.unserialize = &unserialize_contiguous;
512 new_t->base.subtype_free = &free_contiguous;
513 new_t->base.subtype_use = &use_contiguous;
515 new_t->block_count = block_count;
516 new_t->old_type = old_type;
517 smpi_datatype_use(old_type);
518 new_t->size_oldtype = size_oldtype;
519 smpi_datatype_use(old_type);
523 int smpi_datatype_contiguous(int count, MPI_Datatype old_type, MPI_Datatype* new_type, MPI_Aint lb)
526 if(old_type->sizeof_substruct){
527 //handle this case as a hvector with stride equals to the extent of the datatype
528 return smpi_datatype_hvector(count, 1, smpi_datatype_get_extent(old_type), old_type, new_type);
531 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb, count, old_type,smpi_datatype_size(old_type));
533 smpi_datatype_create(new_type, count * smpi_datatype_size(old_type),lb,lb + count * smpi_datatype_size(old_type),
534 sizeof(s_smpi_mpi_contiguous_t),subtype, DT_FLAG_CONTIGUOUS);
539 int smpi_datatype_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type)
547 lb=smpi_datatype_lb(old_type);
548 ub=((count-1)*stride+blocklen-1)*smpi_datatype_get_extent(old_type)+smpi_datatype_ub(old_type);
550 if(old_type->sizeof_substruct != 0 || stride != blocklen){
552 s_smpi_mpi_vector_t* subtype = smpi_datatype_vector_create(stride, blocklen, count, old_type,
553 smpi_datatype_size(old_type));
554 smpi_datatype_create(new_type, count * (blocklen) * smpi_datatype_size(old_type), lb, ub, sizeof(s_smpi_mpi_vector_t), subtype,
558 /* in this situation the data are contiguous thus it's not required to serialize and unserialize it*/
559 smpi_datatype_create(new_type, count * blocklen * smpi_datatype_size(old_type), 0, ((count -1) * stride + blocklen)*
560 smpi_datatype_size(old_type), 0, nullptr, DT_FLAG_VECTOR|DT_FLAG_CONTIGUOUS);
566 void free_vector(MPI_Datatype* d){
567 smpi_datatype_unuse(reinterpret_cast<s_smpi_mpi_indexed_t*>((*d)->substruct)->old_type);
570 void use_vector(MPI_Datatype* d){
571 smpi_datatype_use(reinterpret_cast<s_smpi_mpi_indexed_t*>((*d)->substruct)->old_type);
574 /* Hvector Implementation - Vector with stride in bytes */
576 /* Copies noncontiguous data into contiguous memory.
577 * @param contiguous_hvector - output hvector
578 * @param noncontiguous_hvector - input hvector
579 * @param type - pointer contening :
580 * - stride - stride of between noncontiguous data, in bytes
581 * - block_length - the width or height of blocked matrix
582 * - count - the number of rows of matrix
584 void serialize_hvector( void* noncontiguous_hvector, void *contiguous_hvector, int count, void *type)
586 s_smpi_mpi_hvector_t* type_c = reinterpret_cast<s_smpi_mpi_hvector_t*>(type);
588 char* contiguous_vector_char = static_cast<char*>(contiguous_hvector);
589 char* noncontiguous_vector_char = static_cast<char*>(noncontiguous_hvector);
591 for (i = 0; i < type_c->block_count * count; i++) {
592 if (type_c->old_type->sizeof_substruct == 0)
593 memcpy(contiguous_vector_char, noncontiguous_vector_char, type_c->block_length * type_c->size_oldtype);
595 static_cast<s_smpi_subtype_t*>(type_c->old_type->substruct)->serialize( noncontiguous_vector_char,
596 contiguous_vector_char,
597 type_c->block_length, type_c->old_type->substruct);
599 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
600 if((i+1)%type_c->block_count ==0)
601 noncontiguous_vector_char += type_c->block_length*type_c->size_oldtype;
603 noncontiguous_vector_char += type_c->block_stride;
606 /* Copies contiguous data into noncontiguous memory.
607 * @param noncontiguous_vector - output hvector
608 * @param contiguous_vector - input hvector
609 * @param type - pointer contening :
610 * - stride - stride of between noncontiguous data, in bytes
611 * - block_length - the width or height of blocked matrix
612 * - count - the number of rows of matrix
614 void unserialize_hvector( void* contiguous_vector, void *noncontiguous_vector, int count, void *type, MPI_Op op)
616 s_smpi_mpi_hvector_t* type_c = reinterpret_cast<s_smpi_mpi_hvector_t*>(type);
619 char* contiguous_vector_char = static_cast<char*>(contiguous_vector);
620 char* noncontiguous_vector_char = static_cast<char*>(noncontiguous_vector);
622 for (i = 0; i < type_c->block_count * count; i++) {
623 if (type_c->old_type->sizeof_substruct == 0)
624 smpi_op_apply(op, contiguous_vector_char, noncontiguous_vector_char, &type_c->block_length, &type_c->old_type);
626 static_cast<s_smpi_subtype_t*>(type_c->old_type->substruct)->unserialize( contiguous_vector_char, noncontiguous_vector_char,
627 type_c->block_length, type_c->old_type->substruct,
629 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
630 if((i+1)%type_c->block_count ==0)
631 noncontiguous_vector_char += type_c->block_length*type_c->size_oldtype;
633 noncontiguous_vector_char += type_c->block_stride;
637 /* Create a Sub type vector to be able to serialize and unserialize it the structure s_smpi_mpi_vector_t is derived
638 * from s_smpi_subtype which required the functions unserialize and serialize
641 s_smpi_mpi_hvector_t* smpi_datatype_hvector_create( MPI_Aint block_stride, int block_length, int block_count,
642 MPI_Datatype old_type, int size_oldtype){
643 s_smpi_mpi_hvector_t *new_t= xbt_new(s_smpi_mpi_hvector_t,1);
644 new_t->base.serialize = &serialize_hvector;
645 new_t->base.unserialize = &unserialize_hvector;
646 new_t->base.subtype_free = &free_hvector;
647 new_t->base.subtype_use = &use_hvector;
648 new_t->block_stride = block_stride;
649 new_t->block_length = block_length;
650 new_t->block_count = block_count;
651 new_t->old_type = old_type;
652 new_t->size_oldtype = size_oldtype;
653 smpi_datatype_use(old_type);
657 //do nothing for vector types
658 void free_hvector(MPI_Datatype* d){
659 smpi_datatype_unuse(reinterpret_cast<s_smpi_mpi_hvector_t*>((*d)->substruct)->old_type);
662 void use_hvector(MPI_Datatype* d){
663 smpi_datatype_use(reinterpret_cast<s_smpi_mpi_hvector_t*>((*d)->substruct)->old_type);
666 int smpi_datatype_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type)
674 lb=smpi_datatype_lb(old_type);
675 ub=((count-1)*stride)+(blocklen-1)*smpi_datatype_get_extent(old_type)+smpi_datatype_ub(old_type);
677 if(old_type->sizeof_substruct != 0 || stride != blocklen*smpi_datatype_get_extent(old_type)){
678 s_smpi_mpi_hvector_t* subtype = smpi_datatype_hvector_create( stride, blocklen, count, old_type,
679 smpi_datatype_size(old_type));
681 smpi_datatype_create(new_type, count * blocklen * smpi_datatype_size(old_type), lb,ub, sizeof(s_smpi_mpi_hvector_t), subtype, DT_FLAG_VECTOR);
684 smpi_datatype_create(new_type, count * blocklen * smpi_datatype_size(old_type),0,count * blocklen *
685 smpi_datatype_size(old_type), 0, nullptr, DT_FLAG_VECTOR|DT_FLAG_CONTIGUOUS);
691 /* Indexed Implementation */
693 /* Copies noncontiguous data into contiguous memory.
694 * @param contiguous_indexed - output indexed
695 * @param noncontiguous_indexed - input indexed
696 * @param type - pointer contening :
697 * - block_lengths - the width or height of blocked matrix
698 * - block_indices - indices of each data, in element
699 * - count - the number of rows of matrix
701 void serialize_indexed( void* noncontiguous_indexed, void *contiguous_indexed, int count, void *type)
703 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 char* noncontiguous_indexed_char = static_cast<char*>(noncontiguous_indexed)+type_c->block_indices[0] * type_c->size_oldtype;
706 for (int j = 0; j < count; j++) {
707 for (int 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<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< 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( 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);
737 char* contiguous_indexed_char = static_cast<char*>(contiguous_indexed);
738 char* noncontiguous_indexed_char =
739 static_cast<char*>(noncontiguous_indexed)+type_c->block_indices[0]*smpi_datatype_get_extent(type_c->old_type);
740 for (int j = 0; j < count; j++) {
741 for (int i = 0; i < type_c->block_count; i++) {
742 if (type_c->old_type->sizeof_substruct == 0)
743 smpi_op_apply(op, contiguous_indexed_char, noncontiguous_indexed_char, &type_c->block_lengths[i],
746 static_cast<s_smpi_subtype_t*>(type_c->old_type->substruct)->unserialize( contiguous_indexed_char,
747 noncontiguous_indexed_char,
748 type_c->block_lengths[i],
749 type_c->old_type->substruct, op);
751 contiguous_indexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
752 if (i<type_c->block_count-1)
753 noncontiguous_indexed_char =
754 static_cast<char*>(noncontiguous_indexed) + type_c->block_indices[i+1]*smpi_datatype_get_extent(type_c->old_type);
756 noncontiguous_indexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
758 noncontiguous_indexed=static_cast<void*>(noncontiguous_indexed_char);
762 void free_indexed(MPI_Datatype* type){
763 if((*type)->in_use==0){
764 xbt_free(reinterpret_cast<s_smpi_mpi_indexed_t*>((*type)->substruct)->block_lengths);
765 xbt_free(reinterpret_cast<s_smpi_mpi_indexed_t*>((*type)->substruct)->block_indices);
767 smpi_datatype_unuse(reinterpret_cast<s_smpi_mpi_indexed_t*>((*type)->substruct)->old_type);
770 void use_indexed(MPI_Datatype* type){
771 smpi_datatype_use(reinterpret_cast<s_smpi_mpi_indexed_t*>((*type)->substruct)->old_type);
775 /* Create a Sub type indexed to be able to serialize and unserialize it the structure s_smpi_mpi_indexed_t is derived
776 * from s_smpi_subtype which required the functions unserialize and serialize */
777 s_smpi_mpi_indexed_t* smpi_datatype_indexed_create( int* block_lengths, int* block_indices, int block_count,
778 MPI_Datatype old_type, int size_oldtype){
779 s_smpi_mpi_indexed_t *new_t= xbt_new(s_smpi_mpi_indexed_t,1);
780 new_t->base.serialize = &serialize_indexed;
781 new_t->base.unserialize = &unserialize_indexed;
782 new_t->base.subtype_free = &free_indexed;
783 new_t->base.subtype_use = &use_indexed;
784 new_t->block_lengths= xbt_new(int, block_count);
785 new_t->block_indices= xbt_new(int, block_count);
786 for (int i = 0; i < block_count; i++) {
787 new_t->block_lengths[i]=block_lengths[i];
788 new_t->block_indices[i]=block_indices[i];
790 new_t->block_count = block_count;
791 smpi_datatype_use(old_type);
792 new_t->old_type = old_type;
793 new_t->size_oldtype = size_oldtype;
797 int smpi_datatype_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
800 bool contiguous=true;
804 lb=indices[0]*smpi_datatype_get_extent(old_type);
805 ub=indices[0]*smpi_datatype_get_extent(old_type) + blocklens[0]*smpi_datatype_ub(old_type);
808 for (int i = 0; i < count; i++) {
809 if (blocklens[i] < 0)
811 size += blocklens[i];
813 if(indices[i]*smpi_datatype_get_extent(old_type)+smpi_datatype_lb(old_type)<lb)
814 lb = indices[i]*smpi_datatype_get_extent(old_type)+smpi_datatype_lb(old_type);
815 if(indices[i]*smpi_datatype_get_extent(old_type)+blocklens[i]*smpi_datatype_ub(old_type)>ub)
816 ub = indices[i]*smpi_datatype_get_extent(old_type)+blocklens[i]*smpi_datatype_ub(old_type);
818 if ( (i< count -1) && (indices[i]+blocklens[i] != indices[i+1]) )
821 if (old_type->sizeof_substruct != 0)
825 s_smpi_mpi_indexed_t* subtype = smpi_datatype_indexed_create( blocklens, indices, count, old_type,
826 smpi_datatype_size(old_type));
827 smpi_datatype_create(new_type, size * smpi_datatype_size(old_type),lb,ub,sizeof(s_smpi_mpi_indexed_t), subtype, DT_FLAG_DATA);
829 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb, size, 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_contiguous_t), subtype,
832 DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
836 /* Hindexed Implementation - Indexed with indices in bytes */
838 /* Copies noncontiguous data into contiguous memory.
839 * @param contiguous_hindexed - output hindexed
840 * @param noncontiguous_hindexed - input hindexed
841 * @param type - pointer contening :
842 * - block_lengths - the width or height of blocked matrix
843 * - block_indices - indices of each data, in bytes
844 * - count - the number of rows of matrix
846 void serialize_hindexed( void* noncontiguous_hindexed, void *contiguous_hindexed, int count, void *type)
848 s_smpi_mpi_hindexed_t* type_c = reinterpret_cast<s_smpi_mpi_hindexed_t*>(type);
849 char* contiguous_hindexed_char = static_cast<char*>(contiguous_hindexed);
850 char* noncontiguous_hindexed_char = static_cast<char*>(noncontiguous_hindexed)+ type_c->block_indices[0];
851 for (int j = 0; j < count; j++) {
852 for (int i = 0; i < type_c->block_count; i++) {
853 if (type_c->old_type->sizeof_substruct == 0)
854 memcpy(contiguous_hindexed_char, noncontiguous_hindexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
856 static_cast<s_smpi_subtype_t*>(type_c->old_type->substruct)->serialize( noncontiguous_hindexed_char,
857 contiguous_hindexed_char,
858 type_c->block_lengths[i],
859 type_c->old_type->substruct);
861 contiguous_hindexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
862 if (i<type_c->block_count-1)
863 noncontiguous_hindexed_char = static_cast<char*>(noncontiguous_hindexed) + type_c->block_indices[i+1];
865 noncontiguous_hindexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
867 noncontiguous_hindexed=static_cast<void*>(noncontiguous_hindexed_char);
870 /* Copies contiguous data into noncontiguous memory.
871 * @param noncontiguous_hindexed - output hindexed
872 * @param contiguous_hindexed - input hindexed
873 * @param type - pointer contening :
874 * - block_lengths - the width or height of blocked matrix
875 * - block_indices - indices of each data, in bytes
876 * - count - the number of rows of matrix
878 void unserialize_hindexed( void* contiguous_hindexed, void *noncontiguous_hindexed, int count, void *type,
881 s_smpi_mpi_hindexed_t* type_c = reinterpret_cast<s_smpi_mpi_hindexed_t*>(type);
883 char* contiguous_hindexed_char = static_cast<char*>(contiguous_hindexed);
884 char* noncontiguous_hindexed_char = static_cast<char*>(noncontiguous_hindexed)+ type_c->block_indices[0];
885 for (int j = 0; j < count; j++) {
886 for (int i = 0; i < type_c->block_count; i++) {
887 if (type_c->old_type->sizeof_substruct == 0)
888 smpi_op_apply(op, contiguous_hindexed_char, noncontiguous_hindexed_char, &type_c->block_lengths[i],
891 static_cast<s_smpi_subtype_t*>(type_c->old_type->substruct)->unserialize( contiguous_hindexed_char,
892 noncontiguous_hindexed_char,
893 type_c->block_lengths[i],
894 type_c->old_type->substruct, op);
896 contiguous_hindexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
897 if (i<type_c->block_count-1)
898 noncontiguous_hindexed_char = static_cast<char*>(noncontiguous_hindexed) + type_c->block_indices[i+1];
900 noncontiguous_hindexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
902 noncontiguous_hindexed=static_cast<void*>(noncontiguous_hindexed_char);
906 void free_hindexed(MPI_Datatype* type){
907 if((*type)->in_use==0){
908 xbt_free(reinterpret_cast<s_smpi_mpi_hindexed_t*>((*type)->substruct)->block_lengths);
909 xbt_free(reinterpret_cast<s_smpi_mpi_hindexed_t*>((*type)->substruct)->block_indices);
911 smpi_datatype_unuse(reinterpret_cast<s_smpi_mpi_hindexed_t*>((*type)->substruct)->old_type);
914 void use_hindexed(MPI_Datatype* type){
915 smpi_datatype_use(reinterpret_cast<s_smpi_mpi_hindexed_t*>((*type)->substruct)->old_type);
918 /* Create a Sub type hindexed to be able to serialize and unserialize it the structure s_smpi_mpi_hindexed_t is derived
919 * from s_smpi_subtype which required the functions unserialize and serialize
921 s_smpi_mpi_hindexed_t* smpi_datatype_hindexed_create( int* block_lengths, MPI_Aint* block_indices, int block_count,
922 MPI_Datatype old_type, int size_oldtype){
923 s_smpi_mpi_hindexed_t *new_t= xbt_new(s_smpi_mpi_hindexed_t,1);
924 new_t->base.serialize = &serialize_hindexed;
925 new_t->base.unserialize = &unserialize_hindexed;
926 new_t->base.subtype_free = &free_hindexed;
927 new_t->base.subtype_use = &use_hindexed;
928 new_t->block_lengths= xbt_new(int, block_count);
929 new_t->block_indices= xbt_new(MPI_Aint, block_count);
930 for(int i=0;i<block_count;i++){
931 new_t->block_lengths[i]=block_lengths[i];
932 new_t->block_indices[i]=block_indices[i];
934 new_t->block_count = block_count;
935 new_t->old_type = old_type;
936 smpi_datatype_use(old_type);
937 new_t->size_oldtype = size_oldtype;
941 int smpi_datatype_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
944 bool contiguous=true;
948 lb=indices[0] + smpi_datatype_lb(old_type);
949 ub=indices[0] + blocklens[0]*smpi_datatype_ub(old_type);
951 for (int i = 0; i < count; i++) {
952 if (blocklens[i] < 0)
954 size += blocklens[i];
956 if(indices[i]+smpi_datatype_lb(old_type)<lb)
957 lb = indices[i]+smpi_datatype_lb(old_type);
958 if(indices[i]+blocklens[i]*smpi_datatype_ub(old_type)>ub)
959 ub = indices[i]+blocklens[i]*smpi_datatype_ub(old_type);
961 if ( (i< count -1) && (indices[i]+blocklens[i]*(static_cast<int>(smpi_datatype_size(old_type))) != indices[i+1]) )
964 if (old_type->sizeof_substruct != 0 || lb!=0)
968 s_smpi_mpi_hindexed_t* subtype = smpi_datatype_hindexed_create( blocklens, indices, count, old_type,
969 smpi_datatype_size(old_type));
970 smpi_datatype_create(new_type, size * smpi_datatype_size(old_type), lb, ub ,sizeof(s_smpi_mpi_hindexed_t), subtype, DT_FLAG_DATA);
972 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create(lb,size, old_type, smpi_datatype_size(old_type));
973 smpi_datatype_create(new_type, size * smpi_datatype_size(old_type), 0,size * smpi_datatype_size(old_type),
974 1, subtype, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
979 /* struct Implementation - Indexed with indices in bytes */
981 /* Copies noncontiguous data into contiguous memory.
982 * @param contiguous_struct - output struct
983 * @param noncontiguous_struct - input struct
984 * @param type - pointer contening :
985 * - stride - stride of between noncontiguous data
986 * - block_length - the width or height of blocked matrix
987 * - count - the number of rows of matrix
989 void serialize_struct( void* noncontiguous_struct, void *contiguous_struct, int count, void *type)
991 s_smpi_mpi_struct_t* type_c = reinterpret_cast<s_smpi_mpi_struct_t*>(type);
992 char* contiguous_struct_char = static_cast<char*>(contiguous_struct);
993 char* noncontiguous_struct_char = static_cast<char*>(noncontiguous_struct)+ type_c->block_indices[0];
994 for (int j = 0; j < count; j++) {
995 for (int i = 0; i < type_c->block_count; i++) {
996 if (type_c->old_types[i]->sizeof_substruct == 0)
997 memcpy(contiguous_struct_char, noncontiguous_struct_char,
998 type_c->block_lengths[i] * smpi_datatype_size(type_c->old_types[i]));
1000 static_cast<s_smpi_subtype_t*>(type_c->old_types[i]->substruct)->serialize( noncontiguous_struct_char,
1001 contiguous_struct_char,
1002 type_c->block_lengths[i],
1003 type_c->old_types[i]->substruct);
1006 contiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_size(type_c->old_types[i]);
1007 if (i<type_c->block_count-1)
1008 noncontiguous_struct_char = static_cast<char*>(noncontiguous_struct) + type_c->block_indices[i+1];
1009 else //let's hope this is MPI_UB ?
1010 noncontiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_types[i]);
1012 noncontiguous_struct=static_cast<void*>(noncontiguous_struct_char);
1016 /* Copies contiguous data into noncontiguous memory.
1017 * @param noncontiguous_struct - output struct
1018 * @param contiguous_struct - input struct
1019 * @param type - pointer contening :
1020 * - stride - stride of between noncontiguous data
1021 * - block_length - the width or height of blocked matrix
1022 * - count - the number of rows of matrix
1024 void unserialize_struct( void* contiguous_struct, void *noncontiguous_struct, int count, void *type, MPI_Op op)
1026 s_smpi_mpi_struct_t* type_c = reinterpret_cast<s_smpi_mpi_struct_t*>(type);
1028 char* contiguous_struct_char = static_cast<char*>(contiguous_struct);
1029 char* noncontiguous_struct_char = static_cast<char*>(noncontiguous_struct)+ type_c->block_indices[0];
1030 for (int j = 0; j < count; j++) {
1031 for (int i = 0; i < type_c->block_count; i++) {
1032 if (type_c->old_types[i]->sizeof_substruct == 0)
1033 smpi_op_apply(op, contiguous_struct_char, noncontiguous_struct_char, &type_c->block_lengths[i],
1034 & type_c->old_types[i]);
1036 static_cast<s_smpi_subtype_t*>(type_c->old_types[i]->substruct)->unserialize( contiguous_struct_char,
1037 noncontiguous_struct_char,
1038 type_c->block_lengths[i],
1039 type_c->old_types[i]->substruct, op);
1041 contiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_size(type_c->old_types[i]);
1042 if (i<type_c->block_count-1)
1043 noncontiguous_struct_char = static_cast<char*>(noncontiguous_struct) + type_c->block_indices[i+1];
1045 noncontiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_types[i]);
1047 noncontiguous_struct=reinterpret_cast<void*>(noncontiguous_struct_char);
1051 void free_struct(MPI_Datatype* type){
1052 for (int i = 0; i < reinterpret_cast<s_smpi_mpi_struct_t*>((*type)->substruct)->block_count; i++)
1053 smpi_datatype_unuse(reinterpret_cast<s_smpi_mpi_struct_t*>((*type)->substruct)->old_types[i]);
1054 if((*type)->in_use==0){
1055 xbt_free(reinterpret_cast<s_smpi_mpi_struct_t*>((*type)->substruct)->block_lengths);
1056 xbt_free(reinterpret_cast<s_smpi_mpi_struct_t*>((*type)->substruct)->block_indices);
1057 xbt_free(reinterpret_cast<s_smpi_mpi_struct_t*>((*type)->substruct)->old_types);
1061 void use_struct(MPI_Datatype* type){
1062 for (int i = 0; i < reinterpret_cast<s_smpi_mpi_struct_t*>((*type)->substruct)->block_count; i++)
1063 smpi_datatype_use(reinterpret_cast<s_smpi_mpi_struct_t*>((*type)->substruct)->old_types[i]);
1066 /* Create a Sub type struct to be able to serialize and unserialize it the structure s_smpi_mpi_struct_t is derived
1067 * from s_smpi_subtype which required the functions unserialize and serialize
1069 s_smpi_mpi_struct_t* smpi_datatype_struct_create( int* block_lengths, MPI_Aint* block_indices, int block_count,
1070 MPI_Datatype* old_types){
1071 s_smpi_mpi_struct_t *new_t= xbt_new(s_smpi_mpi_struct_t,1);
1072 new_t->base.serialize = &serialize_struct;
1073 new_t->base.unserialize = &unserialize_struct;
1074 new_t->base.subtype_free = &free_struct;
1075 new_t->base.subtype_use = &use_struct;
1076 new_t->block_lengths= xbt_new(int, block_count);
1077 new_t->block_indices= xbt_new(MPI_Aint, block_count);
1078 new_t->old_types= xbt_new(MPI_Datatype, block_count);
1079 for (int i = 0; i < block_count; i++) {
1080 new_t->block_lengths[i]=block_lengths[i];
1081 new_t->block_indices[i]=block_indices[i];
1082 new_t->old_types[i]=old_types[i];
1083 smpi_datatype_use(new_t->old_types[i]);
1085 new_t->block_count = block_count;
1089 int smpi_datatype_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type)
1092 bool contiguous=true;
1097 lb=indices[0] + smpi_datatype_lb(old_types[0]);
1098 ub=indices[0] + blocklens[0]*smpi_datatype_ub(old_types[0]);
1100 bool forced_lb=false;
1101 bool forced_ub=false;
1102 for (int i = 0; i < count; i++) {
1105 if (old_types[i]->sizeof_substruct != 0)
1108 size += blocklens[i]*smpi_datatype_size(old_types[i]);
1109 if (old_types[i]==MPI_LB){
1113 if (old_types[i]==MPI_UB){
1118 if(!forced_lb && indices[i]+smpi_datatype_lb(old_types[i])<lb)
1120 if(!forced_ub && indices[i]+blocklens[i]*smpi_datatype_ub(old_types[i])>ub)
1121 ub = indices[i]+blocklens[i]*smpi_datatype_ub(old_types[i]);
1123 if ( (i< count -1) && (indices[i]+blocklens[i]*static_cast<int>(smpi_datatype_size(old_types[i])) != indices[i+1]) )
1128 s_smpi_mpi_struct_t* subtype = smpi_datatype_struct_create( blocklens, indices, count, old_types);
1130 smpi_datatype_create(new_type, size, lb, ub,sizeof(s_smpi_mpi_struct_t), subtype, DT_FLAG_DATA);
1132 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb, size, MPI_CHAR, 1);
1133 smpi_datatype_create(new_type, size, lb, ub,1, subtype, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
1138 void smpi_datatype_commit(MPI_Datatype *datatype)
1140 (*datatype)->flags= ((*datatype)->flags | DT_FLAG_COMMITED);
1143 typedef struct s_smpi_mpi_op {
1144 MPI_User_function *func;
1149 #define MAX_OP(a, b) (b) = (a) < (b) ? (b) : (a)
1150 #define MIN_OP(a, b) (b) = (a) < (b) ? (a) : (b)
1151 #define SUM_OP(a, b) (b) += (a)
1152 #define PROD_OP(a, b) (b) *= (a)
1153 #define LAND_OP(a, b) (b) = (a) && (b)
1154 #define LOR_OP(a, b) (b) = (a) || (b)
1155 #define LXOR_OP(a, b) (b) = (!(a) && (b)) || ((a) && !(b))
1156 #define BAND_OP(a, b) (b) &= (a)
1157 #define BOR_OP(a, b) (b) |= (a)
1158 #define BXOR_OP(a, b) (b) ^= (a)
1159 #define MAXLOC_OP(a, b) (b) = (a.value) < (b.value) ? (b) : (a)
1160 #define MINLOC_OP(a, b) (b) = (a.value) < (b.value) ? (a) : (b)
1162 #define APPLY_FUNC(a, b, length, type, func) \
1165 type* x = (type*)(a); \
1166 type* y = (type*)(b); \
1167 for(i = 0; i < *(length); i++) { \
1172 #define APPLY_OP_LOOP(dtype, type, op) \
1173 if (*datatype == dtype) {\
1174 APPLY_FUNC(a, b, length, type, op)\
1178 #define APPLY_BASIC_OP_LOOP(op)\
1179 APPLY_OP_LOOP(MPI_CHAR, char,op)\
1180 APPLY_OP_LOOP(MPI_SHORT, short,op)\
1181 APPLY_OP_LOOP(MPI_INT, int,op)\
1182 APPLY_OP_LOOP(MPI_LONG, long,op)\
1183 APPLY_OP_LOOP(MPI_LONG_LONG, long long,op)\
1184 APPLY_OP_LOOP(MPI_SIGNED_CHAR, signed char,op)\
1185 APPLY_OP_LOOP(MPI_UNSIGNED_CHAR, unsigned char,op)\
1186 APPLY_OP_LOOP(MPI_UNSIGNED_SHORT, unsigned short,op)\
1187 APPLY_OP_LOOP(MPI_UNSIGNED, unsigned int,op)\
1188 APPLY_OP_LOOP(MPI_UNSIGNED_LONG, unsigned long,op)\
1189 APPLY_OP_LOOP(MPI_UNSIGNED_LONG_LONG, unsigned long long,op)\
1190 APPLY_OP_LOOP(MPI_WCHAR, wchar_t,op)\
1191 APPLY_OP_LOOP(MPI_BYTE, int8_t,op)\
1192 APPLY_OP_LOOP(MPI_INT8_T, int8_t,op)\
1193 APPLY_OP_LOOP(MPI_INT16_T, int16_t,op)\
1194 APPLY_OP_LOOP(MPI_INT32_T, int32_t,op)\
1195 APPLY_OP_LOOP(MPI_INT64_T, int64_t,op)\
1196 APPLY_OP_LOOP(MPI_UINT8_T, uint8_t,op)\
1197 APPLY_OP_LOOP(MPI_UINT16_T, uint16_t,op)\
1198 APPLY_OP_LOOP(MPI_UINT32_T, uint32_t,op)\
1199 APPLY_OP_LOOP(MPI_UINT64_T, uint64_t,op)\
1200 APPLY_OP_LOOP(MPI_AINT, MPI_Aint,op)\
1201 APPLY_OP_LOOP(MPI_OFFSET, MPI_Offset,op)\
1202 APPLY_OP_LOOP(MPI_INTEGER1, int,op)\
1203 APPLY_OP_LOOP(MPI_INTEGER2, int16_t,op)\
1204 APPLY_OP_LOOP(MPI_INTEGER4, int32_t,op)\
1205 APPLY_OP_LOOP(MPI_INTEGER8, int64_t,op)
1207 #define APPLY_BOOL_OP_LOOP(op)\
1208 APPLY_OP_LOOP(MPI_C_BOOL, bool,op)
1210 #define APPLY_FLOAT_OP_LOOP(op)\
1211 APPLY_OP_LOOP(MPI_FLOAT, float,op)\
1212 APPLY_OP_LOOP(MPI_DOUBLE, double,op)\
1213 APPLY_OP_LOOP(MPI_LONG_DOUBLE, long double,op)\
1214 APPLY_OP_LOOP(MPI_REAL, float,op)\
1215 APPLY_OP_LOOP(MPI_REAL4, float,op)\
1216 APPLY_OP_LOOP(MPI_REAL8, float,op)\
1217 APPLY_OP_LOOP(MPI_REAL16, double,op)
1219 #define APPLY_COMPLEX_OP_LOOP(op)\
1220 APPLY_OP_LOOP(MPI_C_FLOAT_COMPLEX, float _Complex,op)\
1221 APPLY_OP_LOOP(MPI_C_DOUBLE_COMPLEX, double _Complex,op)\
1222 APPLY_OP_LOOP(MPI_C_LONG_DOUBLE_COMPLEX, long double _Complex,op)
1224 #define APPLY_PAIR_OP_LOOP(op)\
1225 APPLY_OP_LOOP(MPI_FLOAT_INT, float_int,op)\
1226 APPLY_OP_LOOP(MPI_LONG_INT, long_int,op)\
1227 APPLY_OP_LOOP(MPI_DOUBLE_INT, double_int,op)\
1228 APPLY_OP_LOOP(MPI_SHORT_INT, short_int,op)\
1229 APPLY_OP_LOOP(MPI_2INT, int_int,op)\
1230 APPLY_OP_LOOP(MPI_2FLOAT, float_float,op)\
1231 APPLY_OP_LOOP(MPI_2DOUBLE, double_double,op)\
1232 APPLY_OP_LOOP(MPI_LONG_DOUBLE_INT, long_double_int,op)\
1233 APPLY_OP_LOOP(MPI_2LONG, long_long,op)
1235 #define APPLY_END_OP_LOOP(op)\
1237 xbt_die("Failed to apply " #op " to type %s", (*datatype)->name);\
1241 static void max_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1243 APPLY_BASIC_OP_LOOP(MAX_OP)
1244 APPLY_FLOAT_OP_LOOP(MAX_OP)
1245 APPLY_END_OP_LOOP(MAX_OP)
1248 static void min_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1250 APPLY_BASIC_OP_LOOP(MIN_OP)
1251 APPLY_FLOAT_OP_LOOP(MIN_OP)
1252 APPLY_END_OP_LOOP(MIN_OP)
1255 static void sum_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1257 APPLY_BASIC_OP_LOOP(SUM_OP)
1258 APPLY_FLOAT_OP_LOOP(SUM_OP)
1259 APPLY_COMPLEX_OP_LOOP(SUM_OP)
1260 APPLY_END_OP_LOOP(SUM_OP)
1263 static void prod_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1265 APPLY_BASIC_OP_LOOP(PROD_OP)
1266 APPLY_FLOAT_OP_LOOP(PROD_OP)
1267 APPLY_COMPLEX_OP_LOOP(PROD_OP)
1268 APPLY_END_OP_LOOP(PROD_OP)
1271 static void land_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1273 APPLY_BASIC_OP_LOOP(LAND_OP)
1274 APPLY_BOOL_OP_LOOP(LAND_OP)
1275 APPLY_END_OP_LOOP(LAND_OP)
1278 static void lor_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1280 APPLY_BASIC_OP_LOOP(LOR_OP)
1281 APPLY_BOOL_OP_LOOP(LOR_OP)
1282 APPLY_END_OP_LOOP(LOR_OP)
1285 static void lxor_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1287 APPLY_BASIC_OP_LOOP(LXOR_OP)
1288 APPLY_BOOL_OP_LOOP(LXOR_OP)
1289 APPLY_END_OP_LOOP(LXOR_OP)
1292 static void band_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1294 APPLY_BASIC_OP_LOOP(BAND_OP)
1295 APPLY_BOOL_OP_LOOP(BAND_OP)
1296 APPLY_END_OP_LOOP(BAND_OP)
1299 static void bor_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1301 APPLY_BASIC_OP_LOOP(BOR_OP)
1302 APPLY_BOOL_OP_LOOP(BOR_OP)
1303 APPLY_END_OP_LOOP(BOR_OP)
1306 static void bxor_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1308 APPLY_BASIC_OP_LOOP(BXOR_OP)
1309 APPLY_BOOL_OP_LOOP(BXOR_OP)
1310 APPLY_END_OP_LOOP(BXOR_OP)
1313 static void minloc_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1315 APPLY_PAIR_OP_LOOP(MINLOC_OP)
1316 APPLY_END_OP_LOOP(MINLOC_OP)
1319 static void maxloc_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1321 APPLY_PAIR_OP_LOOP(MAXLOC_OP)
1322 APPLY_END_OP_LOOP(MAXLOC_OP)
1325 static void replace_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1327 memcpy(b, a, *length * smpi_datatype_size(*datatype));
1330 #define CREATE_MPI_OP(name, func) \
1331 static s_smpi_mpi_op_t mpi_##name = { &(func) /* func */, true, false }; \
1332 MPI_Op name = &mpi_##name;
1334 CREATE_MPI_OP(MPI_MAX, max_func);
1335 CREATE_MPI_OP(MPI_MIN, min_func);
1336 CREATE_MPI_OP(MPI_SUM, sum_func);
1337 CREATE_MPI_OP(MPI_PROD, prod_func);
1338 CREATE_MPI_OP(MPI_LAND, land_func);
1339 CREATE_MPI_OP(MPI_LOR, lor_func);
1340 CREATE_MPI_OP(MPI_LXOR, lxor_func);
1341 CREATE_MPI_OP(MPI_BAND, band_func);
1342 CREATE_MPI_OP(MPI_BOR, bor_func);
1343 CREATE_MPI_OP(MPI_BXOR, bxor_func);
1344 CREATE_MPI_OP(MPI_MAXLOC, maxloc_func);
1345 CREATE_MPI_OP(MPI_MINLOC, minloc_func);
1346 CREATE_MPI_OP(MPI_REPLACE, replace_func);
1348 MPI_Op smpi_op_new(MPI_User_function * function, bool commute)
1350 MPI_Op op = xbt_new(s_smpi_mpi_op_t, 1);
1351 op->func = function;
1352 op-> is_commute = commute;
1353 op-> is_fortran_op = false;
1357 bool smpi_op_is_commute(MPI_Op op)
1359 return (op==MPI_OP_NULL) ? true : op-> is_commute;
1362 void smpi_op_destroy(MPI_Op op)
1367 void smpi_op_set_fortran(MPI_Op op)
1369 //tell that we were created from fortran, so we need to translate the type to fortran when called
1370 op->is_fortran_op = true;
1373 void smpi_op_apply(MPI_Op op, void *invec, void *inoutvec, int *len, MPI_Datatype * datatype)
1378 if(smpi_privatize_global_variables){//we need to switch as the called function may silently touch global variables
1379 XBT_DEBUG("Applying operation, switch to the right data frame ");
1380 smpi_switch_data_segment(smpi_process_index());
1383 if(!smpi_process_get_replaying()){
1384 if(! op->is_fortran_op)
1385 op->func(invec, inoutvec, len, datatype);
1387 int tmp = smpi_type_c2f(*datatype);
1388 /* Unfortunately, the C and Fortran version of the MPI standard do not agree on the type here,
1389 thus the reinterpret_cast. */
1390 op->func(invec, inoutvec, len, reinterpret_cast<MPI_Datatype*>(&tmp) );
1395 int smpi_type_attr_delete(MPI_Datatype type, int keyval){
1396 smpi_type_key_elem elem =
1397 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
1400 if(elem->delete_fn!=MPI_NULL_DELETE_FN){
1401 void * value = nullptr;
1403 if(smpi_type_attr_get(type, keyval, &value, &flag)==MPI_SUCCESS){
1404 int ret = elem->delete_fn(type, keyval, value, &flag);
1405 if(ret!=MPI_SUCCESS)
1409 if(type->attributes==nullptr)
1412 xbt_dict_remove_ext(type->attributes, reinterpret_cast<const char*>(&keyval), sizeof(int));
1416 int smpi_type_attr_get(MPI_Datatype type, int keyval, void* attr_value, int* flag){
1417 smpi_type_key_elem elem =
1418 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
1421 if(type->attributes==nullptr){
1426 *static_cast<void**>(attr_value) = xbt_dict_get_ext(type->attributes, reinterpret_cast<const char*>(&keyval), sizeof(int));
1429 catch (xbt_ex& ex) {
1435 int smpi_type_attr_put(MPI_Datatype type, int keyval, void* attr_value){
1436 if(smpi_type_keyvals==nullptr)
1437 smpi_type_keyvals = xbt_dict_new_homogeneous(nullptr);
1438 smpi_type_key_elem elem =
1439 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
1443 void* value = nullptr;
1444 smpi_type_attr_get(type, keyval, &value, &flag);
1445 if(flag!=0 && elem->delete_fn!=MPI_NULL_DELETE_FN){
1446 int ret = elem->delete_fn(type, keyval, value, &flag);
1447 if(ret!=MPI_SUCCESS)
1450 if(type->attributes==nullptr)
1451 type->attributes = xbt_dict_new_homogeneous(nullptr);
1453 xbt_dict_set_ext(type->attributes, reinterpret_cast<const char*>(&keyval), sizeof(int), attr_value, nullptr);
1457 int smpi_type_keyval_create(MPI_Type_copy_attr_function* copy_fn, MPI_Type_delete_attr_function* delete_fn, int* keyval,
1459 if(smpi_type_keyvals==nullptr)
1460 smpi_type_keyvals = xbt_dict_new_homogeneous(nullptr);
1462 smpi_type_key_elem value = (smpi_type_key_elem) xbt_new0(s_smpi_mpi_type_key_elem_t,1);
1464 value->copy_fn=copy_fn;
1465 value->delete_fn=delete_fn;
1467 *keyval = type_keyval_id;
1468 xbt_dict_set_ext(smpi_type_keyvals,reinterpret_cast<const char*>(keyval), sizeof(int),reinterpret_cast<void*>(value), nullptr);
1473 int smpi_type_keyval_free(int* keyval){
1474 smpi_type_key_elem elem =
1475 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int)));
1479 xbt_dict_remove_ext(smpi_type_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int));
1484 int smpi_mpi_pack(void* inbuf, int incount, MPI_Datatype type, void* outbuf, int outcount, int* position,MPI_Comm comm){
1485 size_t size = smpi_datatype_size(type);
1486 if (outcount - *position < incount*static_cast<int>(size))
1487 return MPI_ERR_BUFFER;
1488 smpi_datatype_copy(inbuf, incount, type, static_cast<char*>(outbuf) + *position, outcount, MPI_CHAR);
1489 *position += incount * size;
1493 int smpi_mpi_unpack(void* inbuf, int insize, int* position, void* outbuf, int outcount, MPI_Datatype type,MPI_Comm comm){
1494 int size = static_cast<int>(smpi_datatype_size(type));
1495 if (outcount*size> insize)
1496 return MPI_ERR_BUFFER;
1497 smpi_datatype_copy(static_cast<char*>(inbuf) + *position, insize, MPI_CHAR, outbuf, outcount, type);
1498 *position += outcount * size;