1 /* smpi_datatype.cpp -- MPI primitives to handle datatypes */
2 /* Copyright (c) 2009-2017. 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"
16 #include <unordered_map>
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_datatype, smpi, "Logging specific to SMPI (datatype)");
21 #define CREATE_MPI_DATATYPE(name, type) \
22 static simgrid::smpi::Datatype mpi_##name ( \
24 sizeof(type), /* size */ \
26 sizeof(type), /* ub = lb + size */ \
27 DT_FLAG_BASIC /* flags */ \
29 const MPI_Datatype name = &mpi_##name;
31 #define CREATE_MPI_DATATYPE_NULL(name) \
32 static simgrid::smpi::Datatype mpi_##name ( \
36 0, /* ub = lb + size */ \
37 DT_FLAG_BASIC /* flags */ \
39 const MPI_Datatype name = &mpi_##name;
41 // Predefined data types
42 CREATE_MPI_DATATYPE(MPI_CHAR, char);
43 CREATE_MPI_DATATYPE(MPI_SHORT, short);
44 CREATE_MPI_DATATYPE(MPI_INT, int);
45 CREATE_MPI_DATATYPE(MPI_LONG, long);
46 CREATE_MPI_DATATYPE(MPI_LONG_LONG, long long);
47 CREATE_MPI_DATATYPE(MPI_SIGNED_CHAR, signed char);
48 CREATE_MPI_DATATYPE(MPI_UNSIGNED_CHAR, unsigned char);
49 CREATE_MPI_DATATYPE(MPI_UNSIGNED_SHORT, unsigned short);
50 CREATE_MPI_DATATYPE(MPI_UNSIGNED, unsigned int);
51 CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG, unsigned long);
52 CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG_LONG, unsigned long long);
53 CREATE_MPI_DATATYPE(MPI_FLOAT, float);
54 CREATE_MPI_DATATYPE(MPI_DOUBLE, double);
55 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE, long double);
56 CREATE_MPI_DATATYPE(MPI_WCHAR, wchar_t);
57 CREATE_MPI_DATATYPE(MPI_C_BOOL, bool);
58 CREATE_MPI_DATATYPE(MPI_BYTE, int8_t);
59 CREATE_MPI_DATATYPE(MPI_INT8_T, int8_t);
60 CREATE_MPI_DATATYPE(MPI_INT16_T, int16_t);
61 CREATE_MPI_DATATYPE(MPI_INT32_T, int32_t);
62 CREATE_MPI_DATATYPE(MPI_INT64_T, int64_t);
63 CREATE_MPI_DATATYPE(MPI_UINT8_T, uint8_t);
64 CREATE_MPI_DATATYPE(MPI_UINT16_T, uint16_t);
65 CREATE_MPI_DATATYPE(MPI_UINT32_T, uint32_t);
66 CREATE_MPI_DATATYPE(MPI_UINT64_T, uint64_t);
67 CREATE_MPI_DATATYPE(MPI_C_FLOAT_COMPLEX, float _Complex);
68 CREATE_MPI_DATATYPE(MPI_C_DOUBLE_COMPLEX, double _Complex);
69 CREATE_MPI_DATATYPE(MPI_C_LONG_DOUBLE_COMPLEX, long double _Complex);
70 CREATE_MPI_DATATYPE(MPI_AINT, MPI_Aint);
71 CREATE_MPI_DATATYPE(MPI_OFFSET, MPI_Offset);
73 CREATE_MPI_DATATYPE(MPI_FLOAT_INT, float_int);
74 CREATE_MPI_DATATYPE(MPI_LONG_INT, long_int);
75 CREATE_MPI_DATATYPE(MPI_DOUBLE_INT, double_int);
76 CREATE_MPI_DATATYPE(MPI_SHORT_INT, short_int);
77 CREATE_MPI_DATATYPE(MPI_2INT, int_int);
78 CREATE_MPI_DATATYPE(MPI_2FLOAT, float_float);
79 CREATE_MPI_DATATYPE(MPI_2DOUBLE, double_double);
80 CREATE_MPI_DATATYPE(MPI_2LONG, long_long);
82 CREATE_MPI_DATATYPE(MPI_REAL, float);
83 CREATE_MPI_DATATYPE(MPI_REAL4, float);
84 CREATE_MPI_DATATYPE(MPI_REAL8, float);
85 CREATE_MPI_DATATYPE(MPI_REAL16, double);
86 CREATE_MPI_DATATYPE_NULL(MPI_COMPLEX8);
87 CREATE_MPI_DATATYPE_NULL(MPI_COMPLEX16);
88 CREATE_MPI_DATATYPE_NULL(MPI_COMPLEX32);
89 CREATE_MPI_DATATYPE(MPI_INTEGER1, int);
90 CREATE_MPI_DATATYPE(MPI_INTEGER2, int16_t);
91 CREATE_MPI_DATATYPE(MPI_INTEGER4, int32_t);
92 CREATE_MPI_DATATYPE(MPI_INTEGER8, int64_t);
93 CREATE_MPI_DATATYPE(MPI_INTEGER16, integer128_t);
95 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE_INT, long_double_int);
97 CREATE_MPI_DATATYPE_NULL(MPI_UB);
98 CREATE_MPI_DATATYPE_NULL(MPI_LB);
99 CREATE_MPI_DATATYPE(MPI_PACKED, char);
101 CREATE_MPI_DATATYPE(MPI_PTR, void*);
106 std::unordered_map<int, smpi_key_elem> Datatype::keyvals_;
107 int Datatype::keyval_id_=0;
109 Datatype::Datatype(int size,MPI_Aint lb, MPI_Aint ub, int flags) : name_(nullptr), size_(size), lb_(lb), ub_(ub), flags_(flags), refcount_(1){
112 MC_ignore(&(refcount_), sizeof(refcount_));
116 //for predefined types, so in_use = 0.
117 Datatype::Datatype(char* name, int size,MPI_Aint lb, MPI_Aint ub, int flags) : name_(name), size_(size), lb_(lb), ub_(ub), flags_(flags), refcount_(0){
120 MC_ignore(&(refcount_), sizeof(refcount_));
124 Datatype::Datatype(Datatype *datatype, int* ret) : name_(nullptr), lb_(datatype->lb_), ub_(datatype->ub_), flags_(datatype->flags_), refcount_(1)
126 flags_ &= ~DT_FLAG_PREDEFINED;
129 name_ = xbt_strdup(datatype->name_);
131 if(!(datatype->attributes()->empty())){
134 for(auto it = datatype->attributes()->begin(); it != datatype->attributes()->end(); it++){
135 smpi_key_elem elem = keyvals_.at((*it).first);
137 if (elem != nullptr && elem->copy_fn.type_copy_fn != MPI_NULL_COPY_FN) {
138 *ret = elem->copy_fn.type_copy_fn(datatype, (*it).first, nullptr, (*it).second, &value_out, &flag);
139 if (*ret != MPI_SUCCESS) {
144 attributes()->insert({(*it).first, value_out});
151 Datatype::~Datatype(){
152 xbt_assert(refcount_ >= 0);
154 if(flags_ & DT_FLAG_PREDEFINED)
157 //if still used, mark for deletion
159 flags_ |=DT_FLAG_DESTROYED;
163 cleanup_attr<Datatype>();
169 void Datatype::ref(){
175 MC_ignore(&(refcount_), sizeof(refcount_));
179 void Datatype::unref(MPI_Datatype datatype)
181 if (datatype->refcount_ > 0)
182 datatype->refcount_--;
184 if (datatype->refcount_ == 0 && !(datatype->flags_ & DT_FLAG_PREDEFINED))
189 MC_ignore(&(datatype->refcount_), sizeof(datatype->refcount_));
193 void Datatype::commit()
195 flags_ |= DT_FLAG_COMMITED;
199 bool Datatype::is_valid(){
200 return (flags_ & DT_FLAG_COMMITED);
203 size_t Datatype::size(){
207 int Datatype::flags(){
211 int Datatype::refcount(){
215 void Datatype::addflag(int flag){
219 MPI_Aint Datatype::lb(){
223 MPI_Aint Datatype::ub(){
227 char* Datatype::name(){
232 int Datatype::extent(MPI_Aint * lb, MPI_Aint * extent){
238 MPI_Aint Datatype::get_extent(){
242 void Datatype::get_name(char* name, int* length){
243 *length = strlen(name_);
244 strncpy(name, name_, *length+1);
247 void Datatype::set_name(char* name){
248 if(name_!=nullptr && (flags_ & DT_FLAG_PREDEFINED) == 0)
250 name_ = xbt_strdup(name);
253 int Datatype::pack(void* inbuf, int incount, void* outbuf, int outcount, int* position,MPI_Comm comm){
254 if (outcount - *position < incount*static_cast<int>(size_))
255 return MPI_ERR_BUFFER;
256 Datatype::copy(inbuf, incount, this, static_cast<char*>(outbuf) + *position, outcount, MPI_CHAR);
257 *position += incount * size_;
261 int Datatype::unpack(void* inbuf, int insize, int* position, void* outbuf, int outcount,MPI_Comm comm){
262 if (outcount*static_cast<int>(size_)> insize)
263 return MPI_ERR_BUFFER;
264 Datatype::copy(static_cast<char*>(inbuf) + *position, insize, MPI_CHAR, outbuf, outcount, this);
265 *position += outcount * size_;
270 int Datatype::copy(void *sendbuf, int sendcount, MPI_Datatype sendtype,
271 void *recvbuf, int recvcount, MPI_Datatype recvtype){
274 // FIXME Handle the case of a partial shared malloc.
276 if(smpi_is_shared(sendbuf)){
277 XBT_DEBUG("Copy input buf %p is shared. Let's ignore it.", sendbuf);
278 }else if(smpi_is_shared(recvbuf)){
279 XBT_DEBUG("Copy output buf %p is shared. Let's ignore it.", recvbuf);
283 if(smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP){
284 smpi_switch_data_segment(smpi_process()->index());
286 /* First check if we really have something to do */
287 if (recvcount > 0 && recvbuf != sendbuf) {
288 sendcount *= sendtype->size();
289 recvcount *= recvtype->size();
290 count = sendcount < recvcount ? sendcount : recvcount;
292 if(!(sendtype->flags() & DT_FLAG_DERIVED) && !(recvtype->flags() & DT_FLAG_DERIVED)) {
293 if(!smpi_process()->replaying())
294 memcpy(recvbuf, sendbuf, count);
296 else if (!(sendtype->flags() & DT_FLAG_DERIVED))
298 recvtype->unserialize( sendbuf, recvbuf, recvcount/recvtype->size(), MPI_REPLACE);
300 else if (!(recvtype->flags() & DT_FLAG_DERIVED))
302 sendtype->serialize(sendbuf, recvbuf, sendcount/sendtype->size());
305 void * buf_tmp = xbt_malloc(count);
307 sendtype->serialize( sendbuf, buf_tmp,count/sendtype->size());
308 recvtype->unserialize( buf_tmp, recvbuf,count/recvtype->size(), MPI_REPLACE);
314 return sendcount > recvcount ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
317 //Default serialization method : memcpy.
318 void Datatype::serialize( void* noncontiguous_buf, void *contiguous_buf, int count){
319 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
320 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+lb_;
321 memcpy(contiguous_buf_char, noncontiguous_buf_char, count*size_);
325 void Datatype::unserialize( void* contiguous_buf, void *noncontiguous_buf, int count, MPI_Op op){
326 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
327 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+lb_;
330 op->apply( contiguous_buf_char, noncontiguous_buf_char, &n, this);
333 int Datatype::create_contiguous(int count, MPI_Datatype old_type, MPI_Aint lb, MPI_Datatype* new_type){
334 if(old_type->flags_ & DT_FLAG_DERIVED){
335 //handle this case as a hvector with stride equals to the extent of the datatype
336 return create_hvector(count, 1, old_type->get_extent(), old_type, new_type);
339 *new_type = new Type_Contiguous(count * old_type->size(), lb, lb + count * old_type->size(),
340 DT_FLAG_DERIVED, count, old_type);
342 *new_type = new Datatype(count * old_type->size(), lb, lb + count * old_type->size(),0);
346 int Datatype::create_vector(int count, int block_length, int stride, MPI_Datatype old_type, MPI_Datatype* new_type)
355 ub=((count-1)*stride+block_length-1)*old_type->get_extent()+old_type->ub();
357 if(old_type->flags() & DT_FLAG_DERIVED || stride != block_length){
358 *new_type = new Type_Vector(count * (block_length) * old_type->size(), lb, ub,
359 DT_FLAG_DERIVED, count, block_length, stride, old_type);
362 /* in this situation the data are contiguous thus it's not required to serialize and unserialize it*/
363 *new_type = new Datatype(count * block_length * old_type->size(), 0, ((count -1) * stride + block_length)*
364 old_type->size(), DT_FLAG_CONTIGUOUS);
371 int Datatype::create_hvector(int count, int block_length, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type)
380 ub=((count-1)*stride)+(block_length-1)*old_type->get_extent()+old_type->ub();
382 if(old_type->flags() & DT_FLAG_DERIVED || stride != block_length*old_type->get_extent()){
383 *new_type = new Type_Hvector(count * (block_length) * old_type->size(), lb, ub,
384 DT_FLAG_DERIVED, count, block_length, stride, old_type);
387 /* in this situation the data are contiguous thus it's not required to serialize and unserialize it*/
388 *new_type = new Datatype(count * block_length * old_type->size(), 0, count * block_length * old_type->size(), DT_FLAG_CONTIGUOUS);
394 int Datatype::create_indexed(int count, int* block_lengths, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type){
396 bool contiguous=true;
400 lb=indices[0]*old_type->get_extent();
401 ub=indices[0]*old_type->get_extent() + block_lengths[0]*old_type->ub();
404 for (int i = 0; i < count; i++) {
405 if (block_lengths[i] < 0)
407 size += block_lengths[i];
409 if(indices[i]*old_type->get_extent()+old_type->lb()<lb)
410 lb = indices[i]*old_type->get_extent()+old_type->lb();
411 if(indices[i]*old_type->get_extent()+block_lengths[i]*old_type->ub()>ub)
412 ub = indices[i]*old_type->get_extent()+block_lengths[i]*old_type->ub();
414 if ( (i< count -1) && (indices[i]+block_lengths[i] != indices[i+1]) )
417 if(old_type->flags_ & DT_FLAG_DERIVED)
421 *new_type = new Type_Indexed(size * old_type->size(),lb,ub,
422 DT_FLAG_DERIVED|DT_FLAG_DATA, count, block_lengths, indices, old_type);
424 Datatype::create_contiguous(size, old_type, lb, new_type);
429 int Datatype::create_hindexed(int count, int* block_lengths, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type){
431 bool contiguous=true;
435 lb=indices[0] + old_type->lb();
436 ub=indices[0] + block_lengths[0]*old_type->ub();
438 for (int i = 0; i < count; i++) {
439 if (block_lengths[i] < 0)
441 size += block_lengths[i];
443 if(indices[i]+old_type->lb()<lb)
444 lb = indices[i]+old_type->lb();
445 if(indices[i]+block_lengths[i]*old_type->ub()>ub)
446 ub = indices[i]+block_lengths[i]*old_type->ub();
448 if ( (i< count -1) && (indices[i]+block_lengths[i]*(static_cast<int>(old_type->size())) != indices[i+1]) )
451 if (old_type->flags_ & DT_FLAG_DERIVED || lb!=0)
455 *new_type = new Type_Hindexed(size * old_type->size(),lb,ub,
456 DT_FLAG_DERIVED|DT_FLAG_DATA, count, block_lengths, indices, old_type);
458 Datatype::create_contiguous(size, old_type, lb, new_type);
463 int Datatype::create_struct(int count, int* block_lengths, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type){
465 bool contiguous=true;
470 lb=indices[0] + old_types[0]->lb();
471 ub=indices[0] + block_lengths[0]*old_types[0]->ub();
473 bool forced_lb=false;
474 bool forced_ub=false;
475 for (int i = 0; i < count; i++) {
476 if (block_lengths[i]<0)
478 if (old_types[i]->flags_ & DT_FLAG_DERIVED)
481 size += block_lengths[i]*old_types[i]->size();
482 if (old_types[i]==MPI_LB){
486 if (old_types[i]==MPI_UB){
491 if(!forced_lb && indices[i]+old_types[i]->lb()<lb)
493 if(!forced_ub && indices[i]+block_lengths[i]*old_types[i]->ub()>ub)
494 ub = indices[i]+block_lengths[i]*old_types[i]->ub();
496 if ( (i< count -1) && (indices[i]+block_lengths[i]*static_cast<int>(old_types[i]->size()) != indices[i+1]) )
500 *new_type = new Type_Struct(size, lb,ub, DT_FLAG_DERIVED|DT_FLAG_DATA,
501 count, block_lengths, indices, old_types);
503 Datatype::create_contiguous(size, MPI_CHAR, lb, new_type);
508 Datatype* Datatype::f2c(int id){
509 return static_cast<Datatype*>(F2C::f2c(id));