/* 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. */
#include "src/smpi/include/smpi_actor.hpp"
#include <algorithm>
+#include <array>
#include <functional>
#include <string>
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)
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)
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)
// 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{
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_));
}
// 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});
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);
}
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<Datatype>();
delete contents_;
- xbt_free(name_);
}
int Datatype::copy_attrs(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()
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<int>(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*)
/* 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<int, 3> 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;
}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<int, 2> 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;
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;
}
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<int, 3> blocks = {{1, 1, 1}};
+ const std::array<MPI_Aint, 3> disps = {{lb, 0, lb + extent}};
+ const std::array<MPI_Datatype, 3> 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;
{
return static_cast<Datatype*>(F2C::f2c(id));
}
+
} // namespace smpi
} // namespace simgrid