Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
use a map for F2C lookup.
authorFrederic Suter <frederic.suter@cc.in2p3.fr>
Tue, 1 Aug 2017 08:48:12 +0000 (10:48 +0200)
committerFrederic Suter <frederic.suter@cc.in2p3.fr>
Tue, 1 Aug 2017 08:48:12 +0000 (10:48 +0200)
This class could be further improved

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

index 49f7828..207c699 100644 (file)
@@ -9,7 +9,7 @@
 #ifndef SMPI_F2C_HPP_INCLUDED
 #define SMPI_F2C_HPP_INCLUDED
 
-#include "xbt/dict.h"
+#include <unordered_map>
 
 #define KEY_SIZE (sizeof(int) * 2 + 1)
 
@@ -20,18 +20,18 @@ class F2C {
   private:
     // We use a single lookup table for every type.
     // Beware of collisions if id in mpif.h is not unique
-    static xbt_dict_t f2c_lookup_;
+    static std::unordered_map<std::string, F2C*>* f2c_lookup_;
     static int f2c_id_;
   protected:
-    static xbt_dict_t f2c_lookup();
-    static void set_f2c_lookup(xbt_dict_t dict);
+    static std::unordered_map<std::string, F2C*>* f2c_lookup();
+    static void set_f2c_lookup(std::unordered_map<std::string, F2C*>* map);
     static int f2c_id();
     static void f2c_id_increment();
   public:
     static char* get_key(char* key, int id);
     static char* get_key_id(char* key, int id);
     static void delete_lookup();
-    static xbt_dict_t lookup();
+    static std::unordered_map<std::string, F2C*>* lookup();
 
     //Override these to handle specific values.
     int add_f();
index b3dd93f..23a4bd6 100644 (file)
@@ -20,7 +20,7 @@
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_comm, smpi, "Logging specific to SMPI (comm)");
 
- simgrid::smpi::Comm mpi_MPI_COMM_UNINITIALIZED;
+simgrid::smpi::Comm mpi_MPI_COMM_UNINITIALIZED;
 MPI_Comm MPI_COMM_UNINITIALIZED=&mpi_MPI_COMM_UNINITIALIZED;
 
 /* Support for cartesian topology was added, but there are 2 other types of topology, graph et dist graph. In order to
@@ -56,8 +56,8 @@ void Comm::destroy(Comm* comm)
 
 int Comm::dup(MPI_Comm* newcomm){
   if(smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP){ //we need to switch as the called function may silently touch global variables
-     smpi_switch_data_segment(smpi_process()->index());
-   }
+    smpi_switch_data_segment(smpi_process()->index());
+  }
   MPI_Group cp = new  Group(this->group());
   (*newcomm) = new  Comm(cp, this->topo());
   int ret = MPI_SUCCESS;
@@ -79,8 +79,8 @@ int Comm::dup(MPI_Comm* newcomm){
           (*newcomm)->attributes()->insert({it.first, value_out});
         }
       }
-      }
     }
+  }
   return ret;
 }
 
@@ -294,29 +294,29 @@ void Comm::init_smp(){
   // tell SimGrid we are not in replay for a while, because we need the buffers to be copied for the following calls
   bool replaying = false; //cache data to set it back again after
   if(smpi_process()->replaying()){
-   replaying=true;
-   smpi_process()->set_replaying(false);
+    replaying = true;
+    smpi_process()->set_replaying(false);
   }
 
   if(smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP){ //we need to switch as the called function may silently touch global variables
-     smpi_switch_data_segment(smpi_process()->index());
-   }
+    smpi_switch_data_segment(smpi_process()->index());
+  }
   //identify neighbours in comm
   //get the indexes of all processes sharing the same simix host
-   xbt_swag_t process_list = sg_host_self()->extension<simgrid::simix::Host>()->process_list;
-   int intra_comm_size     = 0;
-   int min_index           = INT_MAX; // the minimum index will be the leader
-   smx_actor_t actor       = nullptr;
-   xbt_swag_foreach(actor, process_list)
-   {
-     int index = actor->pid - 1;
-
-     if (this->group()->rank(index) != MPI_UNDEFINED) {
-       intra_comm_size++;
-       // the process is in the comm
-       if (index < min_index)
-         min_index = index;
-     }
+  xbt_swag_t process_list = sg_host_self()->extension<simgrid::simix::Host>()->process_list;
+  int intra_comm_size     = 0;
+  int min_index           = INT_MAX; // the minimum index will be the leader
+  smx_actor_t actor       = nullptr;
+  xbt_swag_foreach(actor, process_list)
+  {
+    int index = actor->pid - 1;
+
+    if (this->group()->rank(index) != MPI_UNDEFINED) {
+      intra_comm_size++;
+      // the process is in the comm
+      if (index < min_index)
+        min_index = index;
+    }
   }
   XBT_DEBUG("number of processes deployed on my node : %d", intra_comm_size);
   MPI_Group group_intra = new  Group(intra_comm_size);
@@ -336,14 +336,14 @@ void Comm::init_smp(){
   int * leaders_map= static_cast<int*>(xbt_malloc0(sizeof(int)*comm_size));
   int * leader_list= static_cast<int*>(xbt_malloc0(sizeof(int)*comm_size));
   for(i=0; i<comm_size; i++){
-      leader_list[i]=-1;
+    leader_list[i] = -1;
   }
 
   Coll_allgather_mpich::allgather(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, this);
 
   if(smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP){ //we need to switch as the called function may silently touch global variables
-     smpi_switch_data_segment(smpi_process()->index());
-   }
+    smpi_switch_data_segment(smpi_process()->index());
+  }
 
   if(leaders_map_==nullptr){
     leaders_map_= leaders_map;
@@ -353,16 +353,16 @@ void Comm::init_smp(){
   int j=0;
   int leader_group_size = 0;
   for(i=0; i<comm_size; i++){
-      int already_done=0;
-      for(j=0;j<leader_group_size; j++){
-        if(leaders_map_[i]==leader_list[j]){
-            already_done=1;
-        }
-      }
-      if(already_done==0){
-        leader_list[leader_group_size]=leaders_map_[i];
-        leader_group_size++;
+    int already_done = 0;
+    for (j = 0; j < leader_group_size; j++) {
+      if (leaders_map_[i] == leader_list[j]) {
+        already_done = 1;
       }
+    }
+    if (already_done == 0) {
+      leader_list[leader_group_size] = leaders_map_[i];
+      leader_group_size++;
+    }
   }
   std::sort(leader_list, leader_list + leader_group_size);
 
@@ -377,7 +377,7 @@ void Comm::init_smp(){
     this->set_leaders_comm(leader_comm);
     this->set_intra_comm(comm_intra);
 
-   //create intracommunicator
+    // create intracommunicator
   }else{
     for (i=0; i< leader_group_size;i++)
       leaders_group->set_mapping(leader_list[i], i);
@@ -406,27 +406,27 @@ void Comm::init_smp(){
       }
     }
     if(is_uniform==0 && this->is_uniform()!=0){
-        non_uniform_map_= non_uniform_map;
+      non_uniform_map_ = non_uniform_map;
     }else{
-        xbt_free(non_uniform_map);
+      xbt_free(non_uniform_map);
     }
     is_uniform_=is_uniform;
   }
   Coll_bcast_mpich::bcast(&(is_uniform_),1, MPI_INT, 0, comm_intra );
 
   if(smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP){ //we need to switch as the called function may silently touch global variables
-     smpi_switch_data_segment(smpi_process()->index());
-   }
+    smpi_switch_data_segment(smpi_process()->index());
+  }
   // Are the ranks blocked ? = allocated contiguously on the SMP nodes
   int is_blocked=1;
   int prev=this->group()->rank(comm_intra->group()->index(0));
-    for (i=1; i<my_local_size; i++){
-      int that=this->group()->rank(comm_intra->group()->index(i));
-      if(that!=prev+1){
-        is_blocked=0;
-        break;
-      }
-      prev = that;
+  for (i = 1; i < my_local_size; i++) {
+    int that = this->group()->rank(comm_intra->group()->index(i));
+    if (that != prev + 1) {
+      is_blocked = 0;
+      break;
+    }
+    prev = that;
   }
 
   int global_blocked;
@@ -434,7 +434,7 @@ void Comm::init_smp(){
 
   if(MPI_COMM_WORLD==MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD){
     if(this->rank()==0){
-        is_blocked_=global_blocked;
+      is_blocked_ = global_blocked;
     }
   }else{
     is_blocked_=global_blocked;
@@ -451,9 +451,12 @@ MPI_Comm Comm::f2c(int id) {
   } else if(id==0){
     return MPI_COMM_WORLD;
   } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
-      char key[KEY_SIZE];
-      MPI_Comm tmp =  static_cast<MPI_Comm>(xbt_dict_get_or_null(F2C::f2c_lookup(),get_key_id(key, id)));
-      return tmp != nullptr ? tmp : MPI_COMM_NULL ;
+    char key[KEY_SIZE];
+    try {
+      return static_cast<MPI_Comm>(F2C::f2c_lookup()->at(get_key_id(key, id)));
+    } catch (std::out_of_range& unfound) {
+      return MPI_COMM_NULL;
+    }
   } else {
     return MPI_COMM_NULL;
   }
@@ -461,20 +464,19 @@ MPI_Comm Comm::f2c(int id) {
 
 void Comm::free_f(int id) {
   char key[KEY_SIZE];
-  xbt_dict_remove(F2C::f2c_lookup(), id==0? get_key(key, id) : get_key_id(key, id));
+  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(xbt_dict_new_homogeneous(nullptr));
+    F2C::set_f2c_lookup(new std::unordered_map<std::string, F2C*>);
   }
   char key[KEY_SIZE];
-  xbt_dict_set(F2C::f2c_lookup(), this==MPI_COMM_WORLD? get_key(key, F2C::f2c_id()) : get_key_id(key,F2C::f2c_id()), this, nullptr);
+  (*(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;
 }
 
-
 void Comm::add_rma_win(MPI_Win win){
   rma_wins_.push_back(win);
 }
@@ -492,7 +494,6 @@ void Comm::finish_rma_calls(){
   }
 }
 
-
 }
 }
 
index fc2a5c6..ffd795f 100644 (file)
 namespace simgrid{
 namespace smpi{
 
-xbt_dict_t F2C::f2c_lookup_=nullptr;
-int F2C::f2c_id_=0;
+std::unordered_map<std::string, F2C*>* F2C::f2c_lookup_ = nullptr;
+int F2C::f2c_id_ = 0;
 
-xbt_dict_t F2C::f2c_lookup(){
+std::unordered_map<std::string, F2C*>* F2C::f2c_lookup()
+{
   return f2c_lookup_;
 }
 
-void F2C::set_f2c_lookup(xbt_dict_t dict){
-  f2c_lookup_=dict;
+void F2C::set_f2c_lookup(std::unordered_map<std::string, F2C*>* map)
+{
+  f2c_lookup_ = map;
 }
 
 void F2C::f2c_id_increment(){
@@ -32,59 +34,69 @@ int F2C::f2c_id(){
 };
 
 char* F2C::get_key(char* key, int id) {
-  std::snprintf(key, KEY_SIZE, "%x", (unsigned)id);
+  std::snprintf(key, KEY_SIZE, "%x", static_cast<unsigned>(id));
   return key;
 }
 
 char* F2C::get_key_id(char* key, int id) {
-  std::snprintf(key, KEY_SIZE, "%x_%d", (unsigned)id, smpi_process()->index());
+  std::snprintf(key, KEY_SIZE, "%x_%d", static_cast<unsigned>(id), smpi_process()->index());
   return key;
 }
 
 void F2C::delete_lookup(){
-  xbt_dict_free(&f2c_lookup_);
+  delete f2c_lookup_;
 }
 
-xbt_dict_t F2C::lookup(){
+std::unordered_map<std::string, F2C*>* F2C::lookup()
+{
   return f2c_lookup_;
 }
 
-void F2C::free_f(int id){
+void F2C::free_f(int id)
+{
   char key[KEY_SIZE];
-  xbt_dict_remove(f2c_lookup_, get_key(key, id));
+  f2c_lookup_->erase(get_key(key, id));
 }
 
-int F2C::add_f(){
-  if(f2c_lookup_==nullptr){
-    f2c_lookup_=xbt_dict_new_homogeneous(nullptr);
-  }
+int F2C::add_f()
+{
+  if (f2c_lookup_ == nullptr)
+    f2c_lookup_ = new std::unordered_map<std::string, F2C*>;
+
   char key[KEY_SIZE];
-  xbt_dict_set(f2c_lookup_, get_key(key, f2c_id_), this, nullptr);
+  (*f2c_lookup_)[get_key(key, f2c_id_)] = this;
   f2c_id_increment();
   return f2c_id_-1;
 }
 
-int F2C::c2f(){
-  if(f2c_lookup_==nullptr){
-    f2c_lookup_=xbt_dict_new_homogeneous(nullptr);
+int F2C::c2f()
+{
+  if (f2c_lookup_ == nullptr) {
+    f2c_lookup_ = new std::unordered_map<std::string, F2C*>;
   }
 
-  char* existing_key = xbt_dict_get_key(f2c_lookup_, this);
-  if(existing_key!=nullptr){
-    return atoi(existing_key);}
-  else{
-    return this->add_f();}
+  for (auto elm : *f2c_lookup_)
+    if (elm.second == this)
+      return std::stoi(elm.first);
+
+  /* this function wasn't found, add it */
+  return this->add_f();
 }
 
-F2C* F2C::f2c(int id){
-  if(f2c_lookup_==nullptr){
-    f2c_lookup_=xbt_dict_new_homogeneous(nullptr);
-  }
+F2C* F2C::f2c(int id)
+{
+  if (f2c_lookup_ == nullptr)
+    f2c_lookup_ = new std::unordered_map<std::string, F2C*>;
+
   if(id >= 0){
     char key[KEY_SIZE];
-    return static_cast<F2C*>(xbt_dict_get_or_null(f2c_lookup_, get_key(key, id)));
+    try {
+      return f2c_lookup_->at(get_key(key, id));
+    } catch (std::out_of_range& unfound) {
+      return nullptr;
+    }
   }else
-    return NULL;
+    return nullptr;
 }
 
 }
index bce418b..8cc21c3 100644 (file)
@@ -355,7 +355,7 @@ MPI_Group Group::f2c(int id) {
     return MPI_GROUP_EMPTY;
   } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
     char key[KEY_SIZE];
-    return static_cast<MPI_Group>(xbt_dict_get_or_null(F2C::f2c_lookup(), get_key(key, id)));
+    return static_cast<MPI_Group>(F2C::f2c_lookup()->at(get_key(key, id)));
   } else {
     return static_cast<MPI_Group>(MPI_GROUP_NULL);
   }
index 0d846a1..7007143 100644 (file)
@@ -897,28 +897,27 @@ 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>(xbt_dict_get(F2C::f2c_lookup(), get_key_id(key, id)));
+  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(xbt_dict_new_homogeneous(nullptr));
+int Request::add_f()
+{
+  if (F2C::f2c_lookup() == nullptr) {
+    F2C::set_f2c_lookup(new std::unordered_map<std::string, F2C*>);
   }
   char key[KEY_SIZE];
-  xbt_dict_set(F2C::f2c_lookup(), get_key_id(key, F2C::f2c_id()), this, nullptr);
+  (*(F2C::f2c_lookup()))[get_key_id(key, F2C::f2c_id())] = this;
   F2C::f2c_id_increment();
   return F2C::f2c_id()-1;
 }
 
-void Request::free_f(int id) {
+void Request::free_f(int id)
+{
   if (id != MPI_FORTRAN_REQUEST_NULL) {
     char key[KEY_SIZE];
-    xbt_dict_remove(F2C::f2c_lookup(), get_key_id(key, id));
+    F2C::f2c_lookup()->erase(get_key_id(key, id));
   }
 }
 
 }
 }
-
-
-