Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Change a bit F2C handling
authorAugustin Degomme <adegomme@gmail.com>
Fri, 26 Jul 2019 08:09:57 +0000 (10:09 +0200)
committerAugustin Degomme <adegomme@gmail.com>
Fri, 26 Jul 2019 08:09:57 +0000 (10:09 +0200)
Store the f2c id in the objects, instead of stupidly browsing the entire map to get it each time
In some cases we had issues with old pointers not being removed from the map, and being reused for other objects, which could cause mismatches.

src/smpi/include/smpi_comm.hpp
src/smpi/include/smpi_f2c.hpp
src/smpi/include/smpi_request.hpp
src/smpi/mpi/smpi_comm.cpp
src/smpi/mpi/smpi_f2c.cpp
src/smpi/mpi/smpi_request.cpp

index ae30f65..27d71bd 100644 (file)
@@ -67,7 +67,6 @@ public:
   static void destroy(MPI_Comm comm);
   void init_smp();
 
-  int add_f() override;
   static void free_f(int id);
   static Comm* f2c(int);
 
index f28c8fb..1d965e9 100644 (file)
@@ -27,19 +27,20 @@ class F2C {
     static void set_f2c_lookup(std::unordered_map<std::string, F2C*>* map);
     static int f2c_id();
     static void f2c_id_increment();
+    int my_f2c_id_;
   public:
+    char* get_my_key(char* key);
     static char* get_key(char* key, int id);
-    static char* get_key_id(char* key, int id);
     static void delete_lookup();
     static std::unordered_map<std::string, F2C*>* lookup();
-
+    F2C() : my_f2c_id_(-1){}
     virtual ~F2C() = default;
 
     //Override these to handle specific values.
     virtual int add_f();
     static void free_f(int id);
     virtual int c2f();
-
+    static void print_f2c_lookup();
     // This method should be overridden in all subclasses to avoid casting the result when calling it.
     // For the default one, the MPI_*_NULL returned is assumed to be NULL.
     static F2C* f2c(int id);
index f8fe782..d16e050 100644 (file)
@@ -115,7 +115,6 @@ public:
   static int grequest_complete( MPI_Request request);
   static int get_status(MPI_Request req, int* flag, MPI_Status * status);
 
-  int add_f() override;
   static void free_f(int id);
   static Request* f2c(int);
 };
index 380c314..b03c340 100644 (file)
@@ -28,7 +28,7 @@ namespace smpi{
 std::unordered_map<int, smpi_key_elem> Comm::keyvals_;
 int Comm::keyval_id_=0;
 
-Comm::Comm(MPI_Group group, MPI_Topology topo, int smp, int id) : group_(group), topo_(topo),is_smp_comm_(smp), id_(id)
+Comm::Comm(MPI_Group group, MPI_Topology topo, int smp, int in_id) : group_(group), topo_(topo),is_smp_comm_(smp), id_(in_id)
 {
   refcount_        = 1;
   topoType_        = MPI_INVALID_TOPO;
@@ -41,7 +41,7 @@ Comm::Comm(MPI_Group group, MPI_Topology topo, int smp, int id) : group_(group),
   info_            = MPI_INFO_NULL;
   static int global_id_=0;
   //First creation of comm is done before SIMIX_run, so only do comms for others
-  if(id==MPI_UNDEFINED && smp==0 && this->rank()!=MPI_UNDEFINED ){
+  if(in_id==MPI_UNDEFINED && smp==0 && this->rank()!=MPI_UNDEFINED ){
     int id;
     if(this->rank()==0){
       id=global_id_;
@@ -505,7 +505,7 @@ MPI_Comm Comm::f2c(int id) {
   } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
     char key[KEY_SIZE];
     const auto& lookup = F2C::f2c_lookup();
-    auto comm          = lookup->find(get_key_id(key, id));
+    auto comm          = lookup->find(get_key(key, id));
     return comm == lookup->end() ? MPI_COMM_NULL : static_cast<MPI_Comm>(comm->second);
   } else {
     return MPI_COMM_NULL;
@@ -514,17 +514,7 @@ MPI_Comm Comm::f2c(int id) {
 
 void Comm::free_f(int id) {
   char key[KEY_SIZE];
-  F2C::f2c_lookup()->erase(id == 0 ? get_key(key, id) : get_key_id(key, id));
-}
-
-int Comm::add_f() {
-  if(F2C::f2c_lookup()==nullptr){
-    F2C::set_f2c_lookup(new std::unordered_map<std::string, F2C*>);
-  }
-  char key[KEY_SIZE];
-  (*(F2C::f2c_lookup()))[this == MPI_COMM_WORLD ? get_key(key, F2C::f2c_id()) : get_key_id(key, F2C::f2c_id())] = this;
-  f2c_id_increment();
-  return F2C::f2c_id()-1;
+  F2C::f2c_lookup()->erase(get_key(key, id));
 }
 
 void Comm::add_rma_win(MPI_Win win){
index 868644c..5fd2657 100644 (file)
@@ -38,13 +38,13 @@ int F2C::f2c_id(){
   return f2c_id_;
 };
 
-char* F2C::get_key(char* key, int id) {
-  std::snprintf(key, KEY_SIZE, "%u", static_cast<unsigned>(id));
+char* F2C::get_my_key(char* key) {
+  std::snprintf(key, KEY_SIZE, "%d", my_f2c_id_);
   return key;
 }
 
-char* F2C::get_key_id(char* key, int id) {
-  std::snprintf(key, KEY_SIZE, "%u_%ld", static_cast<unsigned>(id), simgrid::s4u::this_actor::get_pid());
+char* F2C::get_key(char* key, int id) {
+  std::snprintf(key, KEY_SIZE, "%d", id);
   return key;
 }
 
@@ -60,7 +60,7 @@ std::unordered_map<std::string, F2C*>* F2C::lookup()
 void F2C::free_f(int id)
 {
   char key[KEY_SIZE];
-  f2c_lookup_->erase(get_key(key, id));
+  f2c_lookup_->erase(get_key(key,id));
 }
 
 int F2C::add_f()
@@ -69,9 +69,10 @@ int F2C::add_f()
     f2c_lookup_ = new std::unordered_map<std::string, F2C*>;
 
   char key[KEY_SIZE];
-  (*f2c_lookup_)[get_key(key, f2c_id_)] = this;
+  my_f2c_id_=f2c_id_;
+  (*f2c_lookup_)[get_my_key(key)] = this;
   f2c_id_increment();
-  return f2c_id_-1;
+  return my_f2c_id_;
 }
 
 int F2C::c2f()
@@ -80,12 +81,11 @@ int F2C::c2f()
     f2c_lookup_ = new std::unordered_map<std::string, F2C*>;
   }
 
-  for (auto const& elm : *f2c_lookup_)
-    if (elm.second == this)
-      return std::stoi(elm.first);
-
-  /* this function wasn't found, add it */
-  return this->add_f();
+  if(my_f2c_id_==-1)
+    /* this function wasn't found, add it */
+    return this->add_f();
+  else
+    return my_f2c_id_;
 }
 
 F2C* F2C::f2c(int id)
@@ -95,7 +95,7 @@ F2C* F2C::f2c(int id)
 
   if(id >= 0){
     char key[KEY_SIZE];
-    auto comm = f2c_lookup_->find(get_key(key, id));
+    auto comm = f2c_lookup_->find(get_key(key,id));
     return comm == f2c_lookup_->end() ? nullptr : comm->second;
   }else
     return nullptr;
index 08003ce..f8fa611 100644 (file)
@@ -1089,29 +1089,17 @@ MPI_Request Request::f2c(int id) {
   char key[KEY_SIZE];
   if(id==MPI_FORTRAN_REQUEST_NULL)
     return static_cast<MPI_Request>(MPI_REQUEST_NULL);
-  return static_cast<MPI_Request>(F2C::f2c_lookup()->at(get_key_id(key, id)));
-}
-
-int Request::add_f()
-{
-  if (F2C::f2c_lookup() == nullptr) {
-    F2C::set_f2c_lookup(new std::unordered_map<std::string, F2C*>);
-  }
-  char key[KEY_SIZE];
-  (*(F2C::f2c_lookup()))[get_key_id(key, F2C::f2c_id())] = this;
-  F2C::f2c_id_increment();
-  return F2C::f2c_id()-1;
+  return static_cast<MPI_Request>(F2C::f2c_lookup()->at(get_key(key,id)));
 }
 
 void Request::free_f(int id)
 {
   if (id != MPI_FORTRAN_REQUEST_NULL) {
     char key[KEY_SIZE];
-    F2C::f2c_lookup()->erase(get_key_id(key, id));
+    F2C::f2c_lookup()->erase(get_key(key, id));
   }
 }
 
-
 int Request::get_status(MPI_Request req, int* flag, MPI_Status * status){
   *flag=0;