Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Please sonar. Hopefully a lot.
[simgrid.git] / src / smpi / smpi_comm.cpp
index 7d67ca1..a92eb41 100644 (file)
@@ -60,7 +60,6 @@ Comm::Comm(MPI_Group group, MPI_Topology topo) : group_(group), topo_(topo)
   non_uniform_map_ = nullptr;
   leaders_map_ = nullptr;
   is_blocked_=0;
-  attributes_=nullptr;
 }
 
 void Comm::destroy(Comm* comm)
@@ -81,25 +80,22 @@ int Comm::dup(MPI_Comm* newcomm){
   (*newcomm) = new  Comm(cp, this->topo());
   int ret = MPI_SUCCESS;
 
-  if(attributes_ !=nullptr){
-    (*newcomm)->attributes_   = xbt_dict_new_homogeneous(nullptr);
-    xbt_dict_cursor_t cursor = nullptr;
-    char* key;
+  if(!attributes()->empty()){
     int flag;
-    void* value_in;
     void* value_out;
-    xbt_dict_foreach (attributes_, cursor, key, value_in) {
-      smpi_key_elem elem = keyvals_.at(*key);
+    for(auto it : *attributes()){
+      smpi_key_elem elem = keyvals_.at(it.first);
       if (elem != nullptr && elem->copy_fn.comm_copy_fn != MPI_NULL_COPY_FN) {
-        ret = elem->copy_fn.comm_copy_fn(this, *key, nullptr, value_in, &value_out, &flag);
+        ret = elem->copy_fn.comm_copy_fn(this, it.first, nullptr, it.second, &value_out, &flag);
         if (ret != MPI_SUCCESS) {
           Comm::destroy(*newcomm);
           *newcomm = MPI_COMM_NULL;
-          xbt_dict_cursor_free(&cursor);
           return ret;
         }
-        if (flag)
-          xbt_dict_set_ext((*newcomm)->attributes_, key, sizeof(int), value_out, nullptr);
+        if (flag){
+          elem->refcount++;
+          (*newcomm)->attributes()->insert({it.first, value_out});
+        }
       }
       }
     }
@@ -254,10 +250,9 @@ MPI_Comm Comm::split(int color, int key)
             reqs++;
           }
         }
-        if(i != 0) {
-          if(group_out != MPI_COMM_WORLD->group() && group_out != MPI_GROUP_EMPTY)
-            Group::unref(group_out);
-        }
+        if(i != 0 && group_out != MPI_COMM_WORLD->group() && group_out != MPI_GROUP_EMPTY)
+          Group::unref(group_out);
+        
         Request::waitall(reqs, requests, MPI_STATUS_IGNORE);
         xbt_free(requests);
       }
@@ -283,25 +278,6 @@ void Comm::ref(){
   refcount_++;
 }
 
-void Comm::cleanup_attributes(){
-  if(attributes_ !=nullptr){
-    xbt_dict_cursor_t cursor = nullptr;
-    char* key;
-    void* value;
-    int flag;
-    xbt_dict_foreach (attributes_, cursor, key, value) {
-      try{
-        smpi_key_elem elem = keyvals_.at(*key);
-        if (elem != nullptr && elem->delete_fn.comm_delete_fn != nullptr)
-          elem->delete_fn.comm_delete_fn(this, *key, value, &flag);
-      }catch(const std::out_of_range& oor) {
-        //already deleted, not a problem;
-      }
-    }
-    xbt_dict_free(&attributes_);
-  }
-}
-
 void Comm::cleanup_smp(){
   if (intra_comm_ != MPI_COMM_NULL)
     Comm::unref(intra_comm_);
@@ -323,7 +299,7 @@ void Comm::unref(Comm* comm){
 
   if(comm->refcount_==0){
     comm->cleanup_smp();
-    comm->cleanup_attributes();
+    comm->cleanup_attr<Comm>();
     delete comm;
   }
 }
@@ -499,72 +475,14 @@ void Comm::init_smp(){
     smpi_process_set_replaying(true); 
 }
 
-int Comm::attr_delete(int keyval){
-  smpi_key_elem elem = keyvals_.at(keyval);
-  if(elem==nullptr)
-    return MPI_ERR_ARG;
-  if(elem->delete_fn.comm_delete_fn!=MPI_NULL_DELETE_FN){
-    void* value = nullptr;
-    int flag;
-    if(this->attr_get(keyval, &value, &flag)==MPI_SUCCESS){
-      int ret = elem->delete_fn.comm_delete_fn(this, keyval, value, &flag);
-      if(ret!=MPI_SUCCESS) 
-        return ret;
-    }
-  }
-  if(attributes_==nullptr)
-    return MPI_ERR_ARG;
-
-  xbt_dict_remove_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int));
-  return MPI_SUCCESS;
-}
-
-int Comm::attr_get(int keyval, void* attr_value, int* flag){
-  smpi_key_elem elem = keyvals_.at(keyval);
-  if(elem==nullptr)
-    return MPI_ERR_ARG;
-  if(attributes_==nullptr){
-    *flag=0;
-    return MPI_SUCCESS;
-  }
-  try {
-    *static_cast<void**>(attr_value) =
-        xbt_dict_get_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int));
-    *flag=1;
-  }
-  catch (xbt_ex& ex) {
-    *flag=0;
-  }
-  return MPI_SUCCESS;
-}
-
-int Comm::attr_put(int keyval, void* attr_value){
-  smpi_key_elem elem = keyvals_.at(keyval);
-  if(elem==nullptr)
-    return MPI_ERR_ARG;
-  int flag;
-  void* value = nullptr;
-  this->attr_get(keyval, &value, &flag);
-  if(flag!=0 && elem->delete_fn.comm_delete_fn!=MPI_NULL_DELETE_FN){
-    int ret = elem->delete_fn.comm_delete_fn(this, keyval, value, &flag);
-    if(ret!=MPI_SUCCESS) 
-      return ret;
-  }
-  if(attributes_==nullptr)
-    attributes_ = xbt_dict_new_homogeneous(nullptr);
-
-  xbt_dict_set_ext(attributes_,  reinterpret_cast<const char*>(&keyval), sizeof(int), attr_value, nullptr);
-  return MPI_SUCCESS;
-}
-
 MPI_Comm Comm::f2c(int id) {
   if(id == -2) {
     return MPI_COMM_SELF;
   } else if(id==0){
     return MPI_COMM_WORLD;
-  } else if(F2C::f2c_lookup_ != nullptr && id >= 0) {
+  } 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)));
+      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 ;
   } else {
     return MPI_COMM_NULL;
@@ -573,17 +491,17 @@ 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));
+  xbt_dict_remove(F2C::f2c_lookup(), id==0? get_key(key, id) : get_key_id(key, id));
 }
 
 int Comm::add_f() {
-  if(F2C::f2c_lookup_==nullptr){
-    F2C::f2c_lookup_=xbt_dict_new_homogeneous(nullptr);
+  if(F2C::f2c_lookup()==nullptr){
+    F2C::set_f2c_lookup(xbt_dict_new_homogeneous(nullptr));
   }
   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_id_++;
-  return F2C::f2c_id_-1;
+  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_id_increment();
+  return F2C::f2c_id()-1;
 }