1 /* smpi_datatype.cpp -- 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. */
10 #include "simgrid/modelchecker.h"
11 #include "xbt/replay.h"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_datatype, smpi, "Logging specific to SMPI (datatype)");
21 xbt_dict_t smpi_type_keyvals = nullptr;
22 int type_keyval_id=0;//avoid collisions
25 #define CREATE_MPI_DATATYPE(name, type) \
26 static Datatype mpi_##name ( \
28 sizeof(type), /* size */ \
30 sizeof(type), /* ub = lb + size */ \
31 DT_FLAG_BASIC /* flags */ \
33 const MPI_Datatype name = &mpi_##name;
35 #define CREATE_MPI_DATATYPE_NULL(name) \
36 static Datatype mpi_##name ( \
40 0, /* ub = lb + size */ \
41 DT_FLAG_BASIC /* flags */ \
43 const MPI_Datatype name = &mpi_##name;
45 // Predefined data types
46 CREATE_MPI_DATATYPE(MPI_CHAR, char);
47 CREATE_MPI_DATATYPE(MPI_SHORT, short);
48 CREATE_MPI_DATATYPE(MPI_INT, int);
49 CREATE_MPI_DATATYPE(MPI_LONG, long);
50 CREATE_MPI_DATATYPE(MPI_LONG_LONG, long long);
51 CREATE_MPI_DATATYPE(MPI_SIGNED_CHAR, signed char);
52 CREATE_MPI_DATATYPE(MPI_UNSIGNED_CHAR, unsigned char);
53 CREATE_MPI_DATATYPE(MPI_UNSIGNED_SHORT, unsigned short);
54 CREATE_MPI_DATATYPE(MPI_UNSIGNED, unsigned int);
55 CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG, unsigned long);
56 CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG_LONG, unsigned long long);
57 CREATE_MPI_DATATYPE(MPI_FLOAT, float);
58 CREATE_MPI_DATATYPE(MPI_DOUBLE, double);
59 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE, long double);
60 CREATE_MPI_DATATYPE(MPI_WCHAR, wchar_t);
61 CREATE_MPI_DATATYPE(MPI_C_BOOL, bool);
62 CREATE_MPI_DATATYPE(MPI_BYTE, int8_t);
63 CREATE_MPI_DATATYPE(MPI_INT8_T, int8_t);
64 CREATE_MPI_DATATYPE(MPI_INT16_T, int16_t);
65 CREATE_MPI_DATATYPE(MPI_INT32_T, int32_t);
66 CREATE_MPI_DATATYPE(MPI_INT64_T, int64_t);
67 CREATE_MPI_DATATYPE(MPI_UINT8_T, uint8_t);
68 CREATE_MPI_DATATYPE(MPI_UINT16_T, uint16_t);
69 CREATE_MPI_DATATYPE(MPI_UINT32_T, uint32_t);
70 CREATE_MPI_DATATYPE(MPI_UINT64_T, uint64_t);
71 CREATE_MPI_DATATYPE(MPI_C_FLOAT_COMPLEX, float _Complex);
72 CREATE_MPI_DATATYPE(MPI_C_DOUBLE_COMPLEX, double _Complex);
73 CREATE_MPI_DATATYPE(MPI_C_LONG_DOUBLE_COMPLEX, long double _Complex);
74 CREATE_MPI_DATATYPE(MPI_AINT, MPI_Aint);
75 CREATE_MPI_DATATYPE(MPI_OFFSET, MPI_Offset);
77 CREATE_MPI_DATATYPE(MPI_FLOAT_INT, float_int);
78 CREATE_MPI_DATATYPE(MPI_LONG_INT, long_int);
79 CREATE_MPI_DATATYPE(MPI_DOUBLE_INT, double_int);
80 CREATE_MPI_DATATYPE(MPI_SHORT_INT, short_int);
81 CREATE_MPI_DATATYPE(MPI_2INT, int_int);
82 CREATE_MPI_DATATYPE(MPI_2FLOAT, float_float);
83 CREATE_MPI_DATATYPE(MPI_2DOUBLE, double_double);
84 CREATE_MPI_DATATYPE(MPI_2LONG, long_long);
86 CREATE_MPI_DATATYPE(MPI_REAL, float);
87 CREATE_MPI_DATATYPE(MPI_REAL4, float);
88 CREATE_MPI_DATATYPE(MPI_REAL8, float);
89 CREATE_MPI_DATATYPE(MPI_REAL16, double);
90 CREATE_MPI_DATATYPE_NULL(MPI_COMPLEX8);
91 CREATE_MPI_DATATYPE_NULL(MPI_COMPLEX16);
92 CREATE_MPI_DATATYPE_NULL(MPI_COMPLEX32);
93 CREATE_MPI_DATATYPE(MPI_INTEGER1, int);
94 CREATE_MPI_DATATYPE(MPI_INTEGER2, int16_t);
95 CREATE_MPI_DATATYPE(MPI_INTEGER4, int32_t);
96 CREATE_MPI_DATATYPE(MPI_INTEGER8, int64_t);
97 CREATE_MPI_DATATYPE(MPI_INTEGER16, integer128_t);
99 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE_INT, long_double_int);
101 CREATE_MPI_DATATYPE_NULL(MPI_UB);
102 CREATE_MPI_DATATYPE_NULL(MPI_LB);
103 CREATE_MPI_DATATYPE(MPI_PACKED, char);
105 CREATE_MPI_DATATYPE(MPI_PTR, void*);
110 Datatype::Datatype(int size,MPI_Aint lb, MPI_Aint ub, int flags) : name_(nullptr), size_(size), lb_(lb), ub_(ub), flags_(flags), attributes_(nullptr), in_use_(1){
113 MC_ignore(&(in_use_), sizeof(in_use_));
117 //for predefined types, so in_use = 0.
118 Datatype::Datatype(char* name, int size,MPI_Aint lb, MPI_Aint ub, int flags) : name_(name), size_(size), lb_(lb), ub_(ub), flags_(flags), attributes_(nullptr), in_use_(0){
121 MC_ignore(&(in_use_), sizeof(in_use_));
125 Datatype::Datatype(Datatype *datatype, int* ret) : name_(nullptr), lb_(datatype->lb_), ub_(datatype->ub_), flags_(datatype->flags_), attributes_(nullptr), in_use_(1)
127 flags_ &= ~DT_FLAG_PREDEFINED;
130 name_ = xbt_strdup(datatype->name_);
131 if(datatype->attributes_ !=nullptr){
132 attributes_ = xbt_dict_new_homogeneous(nullptr);
133 xbt_dict_cursor_t cursor = nullptr;
138 xbt_dict_foreach (datatype->attributes_, cursor, key, value_in) {
139 smpi_type_key_elem elem =
140 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, key, sizeof(int)));
141 if (elem != nullptr && elem->copy_fn != MPI_NULL_COPY_FN) {
142 *ret = elem->copy_fn(datatype, atoi(key), nullptr, value_in, &value_out, &flag);
143 if (*ret != MPI_SUCCESS) {
144 xbt_dict_cursor_free(&cursor);
148 xbt_dict_set_ext(attributes_, key, sizeof(int), value_out, nullptr);
154 Datatype::~Datatype(){
155 xbt_assert(in_use_ >= 0);
157 if(flags_ & DT_FLAG_PREDEFINED)
160 //if still used, mark for deletion
162 flags_ |=DT_FLAG_DESTROYED;
166 if(attributes_ !=nullptr){
167 xbt_dict_cursor_t cursor = nullptr;
171 xbt_dict_foreach(attributes_, cursor, key, value){
172 smpi_type_key_elem elem =
173 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, key, sizeof(int)));
174 if(elem!=nullptr && elem->delete_fn!=nullptr)
175 elem->delete_fn(this,*key, value, &flag);
177 xbt_dict_free(&attributes_);
184 void Datatype::use(){
190 MC_ignore(&(in_use_), sizeof(in_use_));
194 void Datatype::unuse()
204 MC_ignore(&(in_use_), sizeof(in_use_));
208 void Datatype::commit()
210 flags_ |= DT_FLAG_COMMITED;
214 bool Datatype::is_valid(){
215 return (flags_ & DT_FLAG_COMMITED);
218 size_t Datatype::size(){
222 int Datatype::flags(){
226 void Datatype::addflag(int flag){
230 MPI_Aint Datatype::lb(){
234 MPI_Aint Datatype::ub(){
238 char* Datatype::name(){
243 int Datatype::extent(MPI_Aint * lb, MPI_Aint * extent){
249 MPI_Aint Datatype::get_extent(){
253 void Datatype::get_name(char* name, int* length){
254 *length = strlen(name_);
255 strncpy(name, name_, *length+1);
258 void Datatype::set_name(char* name){
259 if(name_!=nullptr && (flags_ & DT_FLAG_PREDEFINED) == 0)
261 name_ = xbt_strdup(name);
264 int Datatype::attr_delete(int keyval){
265 smpi_type_key_elem elem =
266 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
269 if(elem->delete_fn!=MPI_NULL_DELETE_FN){
270 void * value = nullptr;
272 if(this->attr_get(keyval, &value, &flag)==MPI_SUCCESS){
273 int ret = elem->delete_fn(this, keyval, value, &flag);
278 if(attributes_==nullptr)
281 xbt_dict_remove_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int));
286 int Datatype::attr_get(int keyval, void* attr_value, int* flag){
287 smpi_type_key_elem elem =
288 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
291 if(attributes_==nullptr){
296 *static_cast<void**>(attr_value) = xbt_dict_get_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int));
305 int Datatype::attr_put(int keyval, void* attr_value){
306 if(smpi_type_keyvals==nullptr)
307 smpi_type_keyvals = xbt_dict_new_homogeneous(nullptr);
308 smpi_type_key_elem elem =
309 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
313 void* value = nullptr;
314 this->attr_get(keyval, &value, &flag);
315 if(flag!=0 && elem->delete_fn!=MPI_NULL_DELETE_FN){
316 int ret = elem->delete_fn(this, keyval, value, &flag);
320 if(attributes_==nullptr)
321 attributes_ = xbt_dict_new_homogeneous(nullptr);
323 xbt_dict_set_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int), attr_value, nullptr);
327 int Datatype::keyval_create(MPI_Type_copy_attr_function* copy_fn, MPI_Type_delete_attr_function* delete_fn, int* keyval, void* extra_state){
328 if(smpi_type_keyvals==nullptr)
329 smpi_type_keyvals = xbt_dict_new_homogeneous(nullptr);
331 smpi_type_key_elem value = (smpi_type_key_elem) xbt_new0(s_smpi_mpi_type_key_elem_t,1);
333 value->copy_fn=copy_fn;
334 value->delete_fn=delete_fn;
336 *keyval = type_keyval_id;
337 xbt_dict_set_ext(smpi_type_keyvals,reinterpret_cast<const char*>(keyval), sizeof(int),reinterpret_cast<void*>(value), nullptr);
342 int Datatype::keyval_free(int* keyval){
343 smpi_type_key_elem elem =
344 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int)));
348 xbt_dict_remove_ext(smpi_type_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int));
354 int Datatype::pack(void* inbuf, int incount, void* outbuf, int outcount, int* position,MPI_Comm comm){
355 if (outcount - *position < incount*static_cast<int>(size_))
356 return MPI_ERR_BUFFER;
357 Datatype::copy(inbuf, incount, this, static_cast<char*>(outbuf) + *position, outcount, MPI_CHAR);
358 *position += incount * size_;
362 int Datatype::unpack(void* inbuf, int insize, int* position, void* outbuf, int outcount,MPI_Comm comm){
363 if (outcount*(int)size_> insize)
364 return MPI_ERR_BUFFER;
365 Datatype::copy(static_cast<char*>(inbuf) + *position, insize, MPI_CHAR, outbuf, outcount, this);
366 *position += outcount * size_;
371 int Datatype::copy(void *sendbuf, int sendcount, MPI_Datatype sendtype,
372 void *recvbuf, int recvcount, MPI_Datatype recvtype){
374 if(smpi_privatize_global_variables){
375 smpi_switch_data_segment(smpi_process_index());
377 /* First check if we really have something to do */
378 if (recvcount > 0 && recvbuf != sendbuf) {
379 sendcount *= sendtype->size();
380 recvcount *= recvtype->size();
381 count = sendcount < recvcount ? sendcount : recvcount;
383 if(!(sendtype->flags() & DT_FLAG_DERIVED) && !(recvtype->flags() & DT_FLAG_DERIVED)) {
384 if(!smpi_process_get_replaying())
385 memcpy(recvbuf, sendbuf, count);
387 else if (!(sendtype->flags() & DT_FLAG_DERIVED))
389 recvtype->unserialize( sendbuf, recvbuf, recvcount/recvtype->size(), MPI_REPLACE);
391 else if (!(recvtype->flags() & DT_FLAG_DERIVED))
393 sendtype->serialize(sendbuf, recvbuf, sendcount/sendtype->size());
396 void * buf_tmp = xbt_malloc(count);
398 sendtype->serialize( sendbuf, buf_tmp,count/sendtype->size());
399 recvtype->unserialize( buf_tmp, recvbuf,count/recvtype->size(), MPI_REPLACE);
405 return sendcount > recvcount ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
408 //Default serialization method : memcpy.
409 void Datatype::serialize( void* noncontiguous_buf, void *contiguous_buf, int count){
410 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
411 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+lb_;
412 memcpy(contiguous_buf_char, noncontiguous_buf_char, count*size_);
416 void Datatype::unserialize( void* contiguous_buf, void *noncontiguous_buf, int count, MPI_Op op){
417 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
418 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+lb_;
421 op->apply( contiguous_buf_char, noncontiguous_buf_char, &n, this);
424 int Datatype::create_contiguous(int count, MPI_Datatype old_type, MPI_Aint lb, MPI_Datatype* new_type){
425 if(old_type->flags_ & DT_FLAG_DERIVED){
426 //handle this case as a hvector with stride equals to the extent of the datatype
427 return create_hvector(count, 1, old_type->get_extent(), old_type, new_type);
430 *new_type = new Type_Contiguous(count * old_type->size(), lb, lb + count * old_type->size(),
431 DT_FLAG_DERIVED, count, old_type);
433 *new_type = new Datatype(count * old_type->size(), lb, lb + count * old_type->size(),0);
437 int Datatype::create_vector(int count, int block_length, int stride, MPI_Datatype old_type, MPI_Datatype* new_type)
446 ub=((count-1)*stride+block_length-1)*old_type->get_extent()+old_type->ub();
448 if(old_type->flags() & DT_FLAG_DERIVED || stride != block_length){
449 *new_type = new Type_Vector(count * (block_length) * old_type->size(), lb, ub,
450 DT_FLAG_DERIVED, count, block_length, stride, old_type);
453 /* in this situation the data are contiguous thus it's not required to serialize and unserialize it*/
454 *new_type = new Datatype(count * block_length * old_type->size(), 0, ((count -1) * stride + block_length)*
455 old_type->size(), DT_FLAG_CONTIGUOUS);
462 int Datatype::create_hvector(int count, int block_length, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type)
471 ub=((count-1)*stride)+(block_length-1)*old_type->get_extent()+old_type->ub();
473 if(old_type->flags() & DT_FLAG_DERIVED || stride != block_length*old_type->get_extent()){
474 *new_type = new Type_Hvector(count * (block_length) * old_type->size(), lb, ub,
475 DT_FLAG_DERIVED, count, block_length, stride, old_type);
478 /* in this situation the data are contiguous thus it's not required to serialize and unserialize it*/
479 *new_type = new Datatype(count * block_length * old_type->size(), 0, count * block_length * old_type->size(), DT_FLAG_CONTIGUOUS);
485 int Datatype::create_indexed(int count, int* block_lengths, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type){
487 bool contiguous=true;
491 lb=indices[0]*old_type->get_extent();
492 ub=indices[0]*old_type->get_extent() + block_lengths[0]*old_type->ub();
495 for (int i = 0; i < count; i++) {
496 if (block_lengths[i] < 0)
498 size += block_lengths[i];
500 if(indices[i]*old_type->get_extent()+old_type->lb()<lb)
501 lb = indices[i]*old_type->get_extent()+old_type->lb();
502 if(indices[i]*old_type->get_extent()+block_lengths[i]*old_type->ub()>ub)
503 ub = indices[i]*old_type->get_extent()+block_lengths[i]*old_type->ub();
505 if ( (i< count -1) && (indices[i]+block_lengths[i] != indices[i+1]) )
508 if(old_type->flags_ & DT_FLAG_DERIVED)
512 *new_type = new Type_Indexed(size * old_type->size(),lb,ub,
513 DT_FLAG_DERIVED|DT_FLAG_DATA, count, block_lengths, indices, old_type);
515 Datatype::create_contiguous(size, old_type, lb, new_type);
520 int Datatype::create_hindexed(int count, int* block_lengths, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type){
522 bool contiguous=true;
526 lb=indices[0] + old_type->lb();
527 ub=indices[0] + block_lengths[0]*old_type->ub();
529 for (int i = 0; i < count; i++) {
530 if (block_lengths[i] < 0)
532 size += block_lengths[i];
534 if(indices[i]+old_type->lb()<lb)
535 lb = indices[i]+old_type->lb();
536 if(indices[i]+block_lengths[i]*old_type->ub()>ub)
537 ub = indices[i]+block_lengths[i]*old_type->ub();
539 if ( (i< count -1) && (indices[i]+block_lengths[i]*(static_cast<int>(old_type->size())) != indices[i+1]) )
542 if (old_type->flags_ & DT_FLAG_DERIVED || lb!=0)
546 *new_type = new Type_Hindexed(size * old_type->size(),lb,ub,
547 DT_FLAG_DERIVED|DT_FLAG_DATA, count, block_lengths, indices, old_type);
549 Datatype::create_contiguous(size, old_type, lb, new_type);
554 int Datatype::create_struct(int count, int* block_lengths, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type){
556 bool contiguous=true;
561 lb=indices[0] + old_types[0]->lb();
562 ub=indices[0] + block_lengths[0]*old_types[0]->ub();
564 bool forced_lb=false;
565 bool forced_ub=false;
566 for (int i = 0; i < count; i++) {
567 if (block_lengths[i]<0)
569 if (old_types[i]->flags_ & DT_FLAG_DERIVED)
572 size += block_lengths[i]*old_types[i]->size();
573 if (old_types[i]==MPI_LB){
577 if (old_types[i]==MPI_UB){
582 if(!forced_lb && indices[i]+old_types[i]->lb()<lb)
584 if(!forced_ub && indices[i]+block_lengths[i]*old_types[i]->ub()>ub)
585 ub = indices[i]+block_lengths[i]*old_types[i]->ub();
587 if ( (i< count -1) && (indices[i]+block_lengths[i]*static_cast<int>(old_types[i]->size()) != indices[i+1]) )
591 *new_type = new Type_Struct(size, lb,ub, DT_FLAG_DERIVED|DT_FLAG_DATA,
592 count, block_lengths, indices, old_types);
594 Datatype::create_contiguous(size, MPI_CHAR, lb, new_type);