X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/1746e0d39563b9a808ae7d76baaf0ad344d33ee6..8f29d9411160cd90ee8d2541c741daafb3f396c6:/src/smpi/mpi/smpi_datatype.cpp diff --git a/src/smpi/mpi/smpi_datatype.cpp b/src/smpi/mpi/smpi_datatype.cpp index a7ea791711..fa2c0dc6b3 100644 --- a/src/smpi/mpi/smpi_datatype.cpp +++ b/src/smpi/mpi/smpi_datatype.cpp @@ -1,5 +1,5 @@ /* smpi_datatype.cpp -- MPI primitives to handle datatypes */ -/* Copyright (c) 2009-2020. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2009-2021. The SimGrid Team. All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ @@ -12,6 +12,7 @@ #include "src/smpi/include/smpi_actor.hpp" #include +#include #include #include @@ -36,10 +37,14 @@ static std::unordered_map id2type_lookup; const MPI_Datatype name = &_XBT_CONCAT(mpi_, name); // Predefined data types +CREATE_MPI_DATATYPE_NULL(MPI_DATATYPE_NULL, -1) +CREATE_MPI_DATATYPE(MPI_DOUBLE, 0, double) +CREATE_MPI_DATATYPE(MPI_INT, 1, int) CREATE_MPI_DATATYPE(MPI_CHAR, 2, char) CREATE_MPI_DATATYPE(MPI_SHORT, 3, short) -CREATE_MPI_DATATYPE(MPI_INT, 1, int) CREATE_MPI_DATATYPE(MPI_LONG, 4, long) +CREATE_MPI_DATATYPE(MPI_FLOAT, 5, float) +CREATE_MPI_DATATYPE(MPI_BYTE, 6, int8_t) CREATE_MPI_DATATYPE(MPI_LONG_LONG, 7, long long) CREATE_MPI_DATATYPE(MPI_SIGNED_CHAR, 8, signed char) CREATE_MPI_DATATYPE(MPI_UNSIGNED_CHAR, 9, unsigned char) @@ -47,12 +52,9 @@ CREATE_MPI_DATATYPE(MPI_UNSIGNED_SHORT, 10, unsigned short) CREATE_MPI_DATATYPE(MPI_UNSIGNED, 11, unsigned int) CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG, 12, unsigned long) CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG_LONG, 13, unsigned long long) -CREATE_MPI_DATATYPE(MPI_FLOAT, 5, float) -CREATE_MPI_DATATYPE(MPI_DOUBLE, 0, double) CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE, 14, long double) CREATE_MPI_DATATYPE(MPI_WCHAR, 15, wchar_t) CREATE_MPI_DATATYPE(MPI_C_BOOL, 16, bool) -CREATE_MPI_DATATYPE(MPI_BYTE, 6, int8_t) CREATE_MPI_DATATYPE(MPI_INT8_T, 17, int8_t) CREATE_MPI_DATATYPE(MPI_INT16_T, 18, int16_t) CREATE_MPI_DATATYPE(MPI_INT32_T, 19, int32_t) @@ -80,7 +82,6 @@ CREATE_MPI_DATATYPE(MPI_REAL, 38, float) CREATE_MPI_DATATYPE(MPI_REAL4, 39, float) CREATE_MPI_DATATYPE(MPI_REAL8, 40, double) CREATE_MPI_DATATYPE(MPI_REAL16, 41, long double) -CREATE_MPI_DATATYPE_NULL(MPI_DATATYPE_NULL, -1) CREATE_MPI_DATATYPE(MPI_COMPLEX8, 42, float_float) CREATE_MPI_DATATYPE(MPI_COMPLEX16, 43, double_double) CREATE_MPI_DATATYPE(MPI_COMPLEX32, 44, double_double) @@ -98,7 +99,7 @@ CREATE_MPI_DATATYPE(MPI_PACKED, 53, char) // Internal use only CREATE_MPI_DATATYPE(MPI_PTR, 54, void*) CREATE_MPI_DATATYPE(MPI_COUNT, 55, long long) - +#define NUM_BASIC_DATATYPES 57 namespace simgrid{ namespace smpi{ @@ -112,6 +113,7 @@ Datatype::Datatype(int ident, int size, MPI_Aint lb, MPI_Aint ub, int flags) : D Datatype::Datatype(int size, MPI_Aint lb, MPI_Aint ub, int flags) : size_(size), lb_(lb), ub_(ub), flags_(flags) { + this->add_f(); #if SIMGRID_HAVE_MC if(MC_is_active()) MC_ignore(&(refcount_), sizeof(refcount_)); @@ -119,7 +121,7 @@ Datatype::Datatype(int size, MPI_Aint lb, MPI_Aint ub, int flags) : size_(size), } // for predefined types, so refcount_ = 0. -Datatype::Datatype(char* name, int ident, int size, MPI_Aint lb, MPI_Aint ub, int flags) +Datatype::Datatype(const char* name, int ident, int size, MPI_Aint lb, MPI_Aint ub, int flags) : name_(name), id(std::to_string(ident)), size_(size), lb_(lb), ub_(ub), flags_(flags), refcount_(0) { id2type_lookup.insert({id, this}); @@ -132,6 +134,7 @@ Datatype::Datatype(char* name, int ident, int size, MPI_Aint lb, MPI_Aint ub, in Datatype::Datatype(Datatype* datatype, int* ret) : size_(datatype->size_), lb_(datatype->lb_), ub_(datatype->ub_), flags_(datatype->flags_) { + this->add_f(); *ret = this->copy_attrs(datatype); } @@ -141,16 +144,16 @@ Datatype::~Datatype() if(flags_ & DT_FLAG_PREDEFINED) return; - + //prevent further usage + flags_ &= ~ DT_FLAG_COMMITED; + F2C::free_f(this->c2f()); //if still used, mark for deletion if(refcount_!=0){ flags_ |=DT_FLAG_DESTROYED; return; } - cleanup_attr(); delete contents_; - xbt_free(name_); } int Datatype::copy_attrs(Datatype* datatype){ @@ -211,13 +214,13 @@ void Datatype::unref(MPI_Datatype datatype) if (datatype->refcount_ > 0) datatype->refcount_--; - if (datatype->refcount_ == 0 && not(datatype->flags_ & DT_FLAG_PREDEFINED)) - delete datatype; - #if SIMGRID_HAVE_MC if(MC_is_active()) MC_ignore(&(datatype->refcount_), sizeof(datatype->refcount_)); #endif + + if (datatype->refcount_ == 0 && not(datatype->flags_ & DT_FLAG_PREDEFINED)) + delete datatype; } void Datatype::commit() @@ -260,18 +263,16 @@ int Datatype::extent(MPI_Aint* lb, MPI_Aint* extent) const void Datatype::get_name(char* name, int* length) const { - if(name_!=nullptr){ - *length = strlen(name_); - strncpy(name, name_, *length+1); - }else{ - *length = 0; + *length = static_cast(name_.length()); + if (not name_.empty()) { + name_.copy(name, *length); + name[*length] = '\0'; } } -void Datatype::set_name(const char* name){ - if(name_!=nullptr && (flags_ & DT_FLAG_PREDEFINED) == 0) - xbt_free(name_); - name_ = xbt_strdup(name); +void Datatype::set_name(const char* name) +{ + name_ = name; } int Datatype::pack(const void* inbuf, int incount, void* outbuf, int outcount, int* position, const Comm*) @@ -423,8 +424,8 @@ int Datatype::create_vector(int count, int block_length, int stride, MPI_Datatyp /* in this situation the data are contiguous thus it's not required to serialize and unserialize it*/ *new_type = new Datatype(count * block_length * old_type->size(), 0, ((count -1) * stride + block_length)* old_type->size(), DT_FLAG_CONTIGUOUS); - int ints[3] = {count, block_length, stride}; - (*new_type)->contents_ = new Datatype_contents(MPI_COMBINER_VECTOR, 3, ints, 0, nullptr, 1, &old_type); + const std::array ints = {{count, block_length, stride}}; + (*new_type)->contents_ = new Datatype_contents(MPI_COMBINER_VECTOR, 3, ints.data(), 0, nullptr, 1, &old_type); retval=MPI_SUCCESS; } return retval; @@ -449,8 +450,8 @@ int Datatype::create_hvector(int count, int block_length, MPI_Aint stride, MPI_D }else{ /* in this situation the data are contiguous thus it's not required to serialize and unserialize it*/ *new_type = new Datatype(count * block_length * old_type->size(), 0, count * block_length * old_type->size(), DT_FLAG_CONTIGUOUS); - int ints[2] = {count, block_length}; - (*new_type)->contents_ = new Datatype_contents(MPI_COMBINER_HVECTOR, 2, ints, 1, &stride, 1, &old_type); + const std::array ints = {{count, block_length}}; + (*new_type)->contents_ = new Datatype_contents(MPI_COMBINER_HVECTOR, 2, ints.data(), 1, &stride, 1, &old_type); retval=MPI_SUCCESS; } return retval; @@ -618,10 +619,10 @@ int Datatype::create_subarray(int ndims, const int* array_of_sizes, tmp = *newtype; } - MPI_Aint lbs[1] = {lb * extent}; - int sizes [1]={1}; + const MPI_Aint lbs = lb * extent; + const int sizes = 1; //handle LB and UB with a resized call - create_hindexed( 1, sizes, lbs, tmp, newtype); + create_hindexed(1, &sizes, &lbs, tmp, newtype); unref(tmp); tmp = *newtype; @@ -632,11 +633,12 @@ int Datatype::create_subarray(int ndims, const int* array_of_sizes, } int Datatype::create_resized(MPI_Datatype oldtype,MPI_Aint lb, MPI_Aint extent, MPI_Datatype *newtype){ - int blocks[3] = {1, 1, 1}; - MPI_Aint disps[3] = {lb, 0, lb + extent}; - MPI_Datatype types[3] = {MPI_LB, oldtype, MPI_UB}; + const std::array blocks = {{1, 1, 1}}; + const std::array disps = {{lb, 0, lb + extent}}; + const std::array types = {{MPI_LB, oldtype, MPI_UB}}; - *newtype = new simgrid::smpi::Type_Struct(oldtype->size(), lb, lb + extent, DT_FLAG_DERIVED, 3, blocks, disps, types); + *newtype = new simgrid::smpi::Type_Struct(oldtype->size(), lb, lb + extent, DT_FLAG_DERIVED, 3, blocks.data(), + disps.data(), types.data()); (*newtype)->addflag(~DT_FLAG_COMMITED); return MPI_SUCCESS; @@ -646,5 +648,6 @@ Datatype* Datatype::f2c(int id) { return static_cast(F2C::f2c(id)); } + } // namespace smpi } // namespace simgrid