1 /* smpi_datatype.cpp -- 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. */
7 #include "simgrid/modelchecker.h"
10 #include "smpi_datatype_derived.hpp"
11 #include "smpi_op.hpp"
12 #include "smpi_process.hpp"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_datatype, smpi, "Logging specific to SMPI (datatype)");
16 #define CREATE_MPI_DATATYPE(name, type) \
17 static simgrid::smpi::Datatype mpi_##name ( \
19 sizeof(type), /* size */ \
21 sizeof(type), /* ub = lb + size */ \
22 DT_FLAG_BASIC /* flags */ \
24 const MPI_Datatype name = &mpi_##name;
26 #define CREATE_MPI_DATATYPE_NULL(name) \
27 static simgrid::smpi::Datatype mpi_##name ( \
31 0, /* ub = lb + size */ \
32 DT_FLAG_BASIC /* flags */ \
34 const MPI_Datatype name = &mpi_##name;
36 // Predefined data types
37 CREATE_MPI_DATATYPE(MPI_CHAR, char);
38 CREATE_MPI_DATATYPE(MPI_SHORT, short);
39 CREATE_MPI_DATATYPE(MPI_INT, int);
40 CREATE_MPI_DATATYPE(MPI_LONG, long);
41 CREATE_MPI_DATATYPE(MPI_LONG_LONG, long long);
42 CREATE_MPI_DATATYPE(MPI_SIGNED_CHAR, signed char);
43 CREATE_MPI_DATATYPE(MPI_UNSIGNED_CHAR, unsigned char);
44 CREATE_MPI_DATATYPE(MPI_UNSIGNED_SHORT, unsigned short);
45 CREATE_MPI_DATATYPE(MPI_UNSIGNED, unsigned int);
46 CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG, unsigned long);
47 CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG_LONG, unsigned long long);
48 CREATE_MPI_DATATYPE(MPI_FLOAT, float);
49 CREATE_MPI_DATATYPE(MPI_DOUBLE, double);
50 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE, long double);
51 CREATE_MPI_DATATYPE(MPI_WCHAR, wchar_t);
52 CREATE_MPI_DATATYPE(MPI_C_BOOL, bool);
53 CREATE_MPI_DATATYPE(MPI_BYTE, int8_t);
54 CREATE_MPI_DATATYPE(MPI_INT8_T, int8_t);
55 CREATE_MPI_DATATYPE(MPI_INT16_T, int16_t);
56 CREATE_MPI_DATATYPE(MPI_INT32_T, int32_t);
57 CREATE_MPI_DATATYPE(MPI_INT64_T, int64_t);
58 CREATE_MPI_DATATYPE(MPI_UINT8_T, uint8_t);
59 CREATE_MPI_DATATYPE(MPI_UINT16_T, uint16_t);
60 CREATE_MPI_DATATYPE(MPI_UINT32_T, uint32_t);
61 CREATE_MPI_DATATYPE(MPI_UINT64_T, uint64_t);
62 CREATE_MPI_DATATYPE(MPI_C_FLOAT_COMPLEX, float _Complex);
63 CREATE_MPI_DATATYPE(MPI_C_DOUBLE_COMPLEX, double _Complex);
64 CREATE_MPI_DATATYPE(MPI_C_LONG_DOUBLE_COMPLEX, long double _Complex);
65 CREATE_MPI_DATATYPE(MPI_AINT, MPI_Aint);
66 CREATE_MPI_DATATYPE(MPI_OFFSET, MPI_Offset);
68 CREATE_MPI_DATATYPE(MPI_FLOAT_INT, float_int);
69 CREATE_MPI_DATATYPE(MPI_LONG_INT, long_int);
70 CREATE_MPI_DATATYPE(MPI_DOUBLE_INT, double_int);
71 CREATE_MPI_DATATYPE(MPI_SHORT_INT, short_int);
72 CREATE_MPI_DATATYPE(MPI_2INT, int_int);
73 CREATE_MPI_DATATYPE(MPI_2FLOAT, float_float);
74 CREATE_MPI_DATATYPE(MPI_2DOUBLE, double_double);
75 CREATE_MPI_DATATYPE(MPI_2LONG, long_long);
77 CREATE_MPI_DATATYPE(MPI_REAL, float);
78 CREATE_MPI_DATATYPE(MPI_REAL4, float);
79 CREATE_MPI_DATATYPE(MPI_REAL8, float);
80 CREATE_MPI_DATATYPE(MPI_REAL16, double);
81 CREATE_MPI_DATATYPE_NULL(MPI_COMPLEX8);
82 CREATE_MPI_DATATYPE_NULL(MPI_COMPLEX16);
83 CREATE_MPI_DATATYPE_NULL(MPI_COMPLEX32);
84 CREATE_MPI_DATATYPE(MPI_INTEGER1, int);
85 CREATE_MPI_DATATYPE(MPI_INTEGER2, int16_t);
86 CREATE_MPI_DATATYPE(MPI_INTEGER4, int32_t);
87 CREATE_MPI_DATATYPE(MPI_INTEGER8, int64_t);
88 CREATE_MPI_DATATYPE(MPI_INTEGER16, integer128_t);
90 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE_INT, long_double_int);
92 CREATE_MPI_DATATYPE_NULL(MPI_UB);
93 CREATE_MPI_DATATYPE_NULL(MPI_LB);
94 CREATE_MPI_DATATYPE(MPI_PACKED, char);
96 CREATE_MPI_DATATYPE(MPI_PTR, void*);
101 std::unordered_map<int, smpi_key_elem> Datatype::keyvals_;
102 int Datatype::keyval_id_=0;
104 Datatype::Datatype(int size,MPI_Aint lb, MPI_Aint ub, int flags) : name_(nullptr), size_(size), lb_(lb), ub_(ub), flags_(flags), refcount_(1){
107 MC_ignore(&(refcount_), sizeof(refcount_));
111 //for predefined types, so in_use = 0.
112 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){
115 MC_ignore(&(refcount_), sizeof(refcount_));
119 Datatype::Datatype(Datatype *datatype, int* ret) : name_(nullptr), lb_(datatype->lb_), ub_(datatype->ub_), flags_(datatype->flags_), refcount_(1)
121 flags_ &= ~DT_FLAG_PREDEFINED;
124 name_ = xbt_strdup(datatype->name_);
126 if (not datatype->attributes()->empty()) {
129 for(auto it = datatype->attributes()->begin(); it != datatype->attributes()->end(); it++){
130 smpi_key_elem elem = keyvals_.at((*it).first);
132 if (elem != nullptr && elem->copy_fn.type_copy_fn != MPI_NULL_COPY_FN) {
133 *ret = elem->copy_fn.type_copy_fn(datatype, (*it).first, nullptr, (*it).second, &value_out, &flag);
134 if (*ret != MPI_SUCCESS) {
139 attributes()->insert({(*it).first, value_out});
146 Datatype::~Datatype(){
147 xbt_assert(refcount_ >= 0);
149 if(flags_ & DT_FLAG_PREDEFINED)
152 //if still used, mark for deletion
154 flags_ |=DT_FLAG_DESTROYED;
158 cleanup_attr<Datatype>();
164 void Datatype::ref(){
170 MC_ignore(&(refcount_), sizeof(refcount_));
174 void Datatype::unref(MPI_Datatype datatype)
176 if (datatype->refcount_ > 0)
177 datatype->refcount_--;
179 if (datatype->refcount_ == 0 && not(datatype->flags_ & DT_FLAG_PREDEFINED))
184 MC_ignore(&(datatype->refcount_), sizeof(datatype->refcount_));
188 void Datatype::commit()
190 flags_ |= DT_FLAG_COMMITED;
194 bool Datatype::is_valid(){
195 return (flags_ & DT_FLAG_COMMITED);
198 size_t Datatype::size(){
202 int Datatype::flags(){
206 int Datatype::refcount(){
210 void Datatype::addflag(int flag){
214 MPI_Aint Datatype::lb(){
218 MPI_Aint Datatype::ub(){
222 char* Datatype::name(){
227 int Datatype::extent(MPI_Aint * lb, MPI_Aint * extent){
233 MPI_Aint Datatype::get_extent(){
237 void Datatype::get_name(char* name, int* length){
238 *length = strlen(name_);
239 strncpy(name, name_, *length+1);
242 void Datatype::set_name(char* name){
243 if(name_!=nullptr && (flags_ & DT_FLAG_PREDEFINED) == 0)
245 name_ = xbt_strdup(name);
248 int Datatype::pack(void* inbuf, int incount, void* outbuf, int outcount, int* position,MPI_Comm comm){
249 if (outcount - *position < incount*static_cast<int>(size_))
250 return MPI_ERR_BUFFER;
251 Datatype::copy(inbuf, incount, this, static_cast<char*>(outbuf) + *position, outcount, MPI_CHAR);
252 *position += incount * size_;
256 int Datatype::unpack(void* inbuf, int insize, int* position, void* outbuf, int outcount,MPI_Comm comm){
257 if (outcount*static_cast<int>(size_)> insize)
258 return MPI_ERR_BUFFER;
259 Datatype::copy(static_cast<char*>(inbuf) + *position, insize, MPI_CHAR, outbuf, outcount, this);
260 *position += outcount * size_;
265 int Datatype::copy(void *sendbuf, int sendcount, MPI_Datatype sendtype,
266 void *recvbuf, int recvcount, MPI_Datatype recvtype){
268 // FIXME Handle the case of a partial shared malloc.
270 if(smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP){
271 smpi_switch_data_segment(smpi_process()->index());
273 /* First check if we really have something to do */
274 if (recvcount > 0 && recvbuf != sendbuf) {
275 sendcount *= sendtype->size();
276 recvcount *= recvtype->size();
277 int count = sendcount < recvcount ? sendcount : recvcount;
279 if (not(sendtype->flags() & DT_FLAG_DERIVED) && not(recvtype->flags() & DT_FLAG_DERIVED)) {
280 if (not smpi_process()->replaying())
281 memcpy(recvbuf, sendbuf, count);
282 } else if (not(sendtype->flags() & DT_FLAG_DERIVED)) {
283 recvtype->unserialize( sendbuf, recvbuf, recvcount/recvtype->size(), MPI_REPLACE);
284 } else if (not(recvtype->flags() & DT_FLAG_DERIVED)) {
285 sendtype->serialize(sendbuf, recvbuf, sendcount/sendtype->size());
288 void * buf_tmp = xbt_malloc(count);
290 sendtype->serialize( sendbuf, buf_tmp,count/sendtype->size());
291 recvtype->unserialize( buf_tmp, recvbuf,count/recvtype->size(), MPI_REPLACE);
297 return sendcount > recvcount ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
300 //Default serialization method : memcpy.
301 void Datatype::serialize( void* noncontiguous_buf, void *contiguous_buf, int count){
302 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
303 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+lb_;
304 memcpy(contiguous_buf_char, noncontiguous_buf_char, count*size_);
308 void Datatype::unserialize( void* contiguous_buf, void *noncontiguous_buf, int count, MPI_Op op){
309 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
310 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+lb_;
313 op->apply( contiguous_buf_char, noncontiguous_buf_char, &n, this);
316 int Datatype::create_contiguous(int count, MPI_Datatype old_type, MPI_Aint lb, MPI_Datatype* new_type){
317 if(old_type->flags_ & DT_FLAG_DERIVED){
318 //handle this case as a hvector with stride equals to the extent of the datatype
319 return create_hvector(count, 1, old_type->get_extent(), old_type, new_type);
322 *new_type = new Type_Contiguous(count * old_type->size(), lb, lb + count * old_type->size(),
323 DT_FLAG_DERIVED, count, old_type);
325 *new_type = new Datatype(count * old_type->size(), lb, lb + count * old_type->size(),0);
329 int Datatype::create_vector(int count, int block_length, int stride, MPI_Datatype old_type, MPI_Datatype* new_type)
338 ub=((count-1)*stride+block_length-1)*old_type->get_extent()+old_type->ub();
340 if(old_type->flags() & DT_FLAG_DERIVED || stride != block_length){
341 *new_type = new Type_Vector(count * (block_length) * old_type->size(), lb, ub,
342 DT_FLAG_DERIVED, count, block_length, stride, old_type);
345 /* in this situation the data are contiguous thus it's not required to serialize and unserialize it*/
346 *new_type = new Datatype(count * block_length * old_type->size(), 0, ((count -1) * stride + block_length)*
347 old_type->size(), DT_FLAG_CONTIGUOUS);
354 int Datatype::create_hvector(int count, int block_length, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type)
363 ub=((count-1)*stride)+(block_length-1)*old_type->get_extent()+old_type->ub();
365 if(old_type->flags() & DT_FLAG_DERIVED || stride != block_length*old_type->get_extent()){
366 *new_type = new Type_Hvector(count * (block_length) * old_type->size(), lb, ub,
367 DT_FLAG_DERIVED, count, block_length, stride, old_type);
370 /* in this situation the data are contiguous thus it's not required to serialize and unserialize it*/
371 *new_type = new Datatype(count * block_length * old_type->size(), 0, count * block_length * old_type->size(), DT_FLAG_CONTIGUOUS);
377 int Datatype::create_indexed(int count, int* block_lengths, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type){
379 bool contiguous=true;
383 lb=indices[0]*old_type->get_extent();
384 ub=indices[0]*old_type->get_extent() + block_lengths[0]*old_type->ub();
387 for (int i = 0; i < count; i++) {
388 if (block_lengths[i] < 0)
390 size += block_lengths[i];
392 if(indices[i]*old_type->get_extent()+old_type->lb()<lb)
393 lb = indices[i]*old_type->get_extent()+old_type->lb();
394 if(indices[i]*old_type->get_extent()+block_lengths[i]*old_type->ub()>ub)
395 ub = indices[i]*old_type->get_extent()+block_lengths[i]*old_type->ub();
397 if ( (i< count -1) && (indices[i]+block_lengths[i] != indices[i+1]) )
400 if(old_type->flags_ & DT_FLAG_DERIVED)
403 if (not contiguous) {
404 *new_type = new Type_Indexed(size * old_type->size(),lb,ub,
405 DT_FLAG_DERIVED|DT_FLAG_DATA, count, block_lengths, indices, old_type);
407 Datatype::create_contiguous(size, old_type, lb, new_type);
412 int Datatype::create_hindexed(int count, int* block_lengths, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type){
414 bool contiguous=true;
418 lb=indices[0] + old_type->lb();
419 ub=indices[0] + block_lengths[0]*old_type->ub();
421 for (int i = 0; i < count; i++) {
422 if (block_lengths[i] < 0)
424 size += block_lengths[i];
426 if(indices[i]+old_type->lb()<lb)
427 lb = indices[i]+old_type->lb();
428 if(indices[i]+block_lengths[i]*old_type->ub()>ub)
429 ub = indices[i]+block_lengths[i]*old_type->ub();
431 if ( (i< count -1) && (indices[i]+block_lengths[i]*(static_cast<int>(old_type->size())) != indices[i+1]) )
434 if (old_type->flags_ & DT_FLAG_DERIVED || lb!=0)
437 if (not contiguous) {
438 *new_type = new Type_Hindexed(size * old_type->size(),lb,ub,
439 DT_FLAG_DERIVED|DT_FLAG_DATA, count, block_lengths, indices, old_type);
441 Datatype::create_contiguous(size, old_type, lb, new_type);
446 int Datatype::create_struct(int count, int* block_lengths, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type){
448 bool contiguous=true;
453 lb=indices[0] + old_types[0]->lb();
454 ub=indices[0] + block_lengths[0]*old_types[0]->ub();
456 bool forced_lb=false;
457 bool forced_ub=false;
458 for (int i = 0; i < count; i++) {
459 if (block_lengths[i]<0)
461 if (old_types[i]->flags_ & DT_FLAG_DERIVED)
464 size += block_lengths[i]*old_types[i]->size();
465 if (old_types[i]==MPI_LB){
469 if (old_types[i]==MPI_UB){
474 if (not forced_lb && indices[i] + old_types[i]->lb() < lb)
476 if (not forced_ub && indices[i] + block_lengths[i] * old_types[i]->ub() > ub)
477 ub = indices[i]+block_lengths[i]*old_types[i]->ub();
479 if ( (i< count -1) && (indices[i]+block_lengths[i]*static_cast<int>(old_types[i]->size()) != indices[i+1]) )
482 if (not contiguous) {
483 *new_type = new Type_Struct(size, lb,ub, DT_FLAG_DERIVED|DT_FLAG_DATA,
484 count, block_lengths, indices, old_types);
486 Datatype::create_contiguous(size, MPI_CHAR, lb, new_type);
491 Datatype* Datatype::f2c(int id){
492 return static_cast<Datatype*>(F2C::f2c(id));