Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
SMPI : add leak detection.
[simgrid.git] / src / smpi / mpi / smpi_datatype.cpp
index 85d16a6c3ed1849ef653b1dbfb592844c17b329b..fa2c0dc6b3611601c5defcced910228573157dba 100644 (file)
@@ -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. */
@@ -37,10 +37,14 @@ static std::unordered_map<std::string, simgrid::smpi::Datatype*> 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)
@@ -48,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)
@@ -81,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)
@@ -99,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{
@@ -113,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_));
@@ -120,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});
@@ -133,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);
 }
 
@@ -142,13 +144,14 @@ 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<Datatype>();
   delete contents_;
 }
@@ -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,7 +263,7 @@ int Datatype::extent(MPI_Aint* lb, MPI_Aint* extent) const
 
 void Datatype::get_name(char* name, int* length) const
 {
-  *length = name_.length();
+  *length = static_cast<int>(name_.length());
   if (not name_.empty()) {
     name_.copy(name, *length);
     name[*length] = '\0';
@@ -645,5 +648,6 @@ Datatype* Datatype::f2c(int id)
 {
   return static_cast<Datatype*>(F2C::f2c(id));
 }
+
 } // namespace smpi
 } // namespace simgrid