X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/4d140762b2b9c8746cdd3b0680a7167867ba0ad9..5ed37babb2fa9097abe82df299c0aa259ed84d5a:/src/smpi/include/smpi_keyvals.hpp diff --git a/src/smpi/include/smpi_keyvals.hpp b/src/smpi/include/smpi_keyvals.hpp index bc3e2af943..768bc51553 100644 --- a/src/smpi/include/smpi_keyvals.hpp +++ b/src/smpi/include/smpi_keyvals.hpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2010-2017. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2010-2023. 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. */ @@ -7,153 +7,161 @@ #define SMPI_KEYVALS_HPP_INCLUDED #include "smpi/smpi.h" -#include "xbt/ex.hpp" +#include "xbt/asserts.h" #include -typedef struct smpi_delete_fn{ +struct smpi_delete_fn { MPI_Comm_delete_attr_function *comm_delete_fn; MPI_Type_delete_attr_function *type_delete_fn; MPI_Win_delete_attr_function *win_delete_fn; -} smpi_delete_fn; + MPI_Comm_delete_attr_function_fort *comm_delete_fn_fort; + MPI_Type_delete_attr_function_fort *type_delete_fn_fort; + MPI_Win_delete_attr_function_fort *win_delete_fn_fort; +}; -typedef struct smpi_copy_fn{ +struct smpi_copy_fn { MPI_Comm_copy_attr_function *comm_copy_fn; MPI_Type_copy_attr_function *type_copy_fn; MPI_Win_copy_attr_function *win_copy_fn; -} smpi_copy_fn; + MPI_Comm_copy_attr_function_fort *comm_copy_fn_fort; + MPI_Type_copy_attr_function_fort *type_copy_fn_fort; + MPI_Win_copy_attr_function_fort *win_copy_fn_fort; +}; -typedef struct s_smpi_key_elem { +struct smpi_key_elem { smpi_copy_fn copy_fn; smpi_delete_fn delete_fn; + void* extra_state; int refcount; -} s_smpi_mpi_key_elem_t; - -typedef struct s_smpi_key_elem *smpi_key_elem; + bool deleted; + bool delete_attr; // if true, xbt_free(attr) on delete: used by Fortran bindings +}; -namespace simgrid{ -namespace smpi{ +namespace simgrid::smpi { class Keyval{ private: std::unordered_map attributes_; protected: - std::unordered_map* attributes(); + std::unordered_map& attributes() { return attributes_; } + public: // Each subclass should have two members, as we want to separate the ones for Win, Comm, and Datatypes : // static std::unordered_map keyvals_; // static int keyval_id_; - template static int keyval_create(smpi_copy_fn copy_fn, smpi_delete_fn delete_fn, int* keyval, void* extra_statee); + template + static int keyval_create(const smpi_copy_fn& copy_fn, const smpi_delete_fn& delete_fn, int* keyval, + void* extra_state, bool delete_attr = false); template static int keyval_free(int* keyval); template int attr_delete(int keyval); template int attr_get(int keyval, void* attr_value, int* flag); template int attr_put(int keyval, void* attr_value); - template static int call_deleter(T* obj, smpi_key_elem elem, int keyval, void * value, int* flag); + template + static int call_deleter(T* obj, const smpi_key_elem& elem, int keyval, void* value, int* flag); template void cleanup_attr(); }; -template int Keyval::keyval_create(smpi_copy_fn copy_fn, smpi_delete_fn delete_fn, int* keyval, void* extra_state){ - - smpi_key_elem value = (smpi_key_elem) xbt_new0(s_smpi_mpi_key_elem_t,1); - - value->copy_fn=copy_fn; - value->delete_fn=delete_fn; - value->refcount=1; +template +int Keyval::keyval_create(const smpi_copy_fn& copy_fn, const smpi_delete_fn& delete_fn, int* keyval, void* extra_state, + bool delete_attr) +{ + smpi_key_elem value; + value.copy_fn = copy_fn; + value.delete_fn = delete_fn; + value.extra_state = extra_state; + value.refcount = 0; + value.deleted = false; + value.delete_attr = delete_attr; *keyval = T::keyval_id_; - T::keyvals_.insert({*keyval, value}); + T::keyvals_.emplace(*keyval, std::move(value)); T::keyval_id_++; return MPI_SUCCESS; } template int Keyval::keyval_free(int* keyval){ -/* See MPI-1, 5.7.1. Freeing the keyval does not remove it if it - * is in use in an attribute */ - smpi_key_elem elem = T::keyvals_.at(*keyval); - if(elem==0){ + /* See MPI-1, 5.7.1. Freeing the keyval does not remove it if it is in use in an attribute */ + auto elem_it = T::keyvals_.find(*keyval); + if (elem_it == T::keyvals_.end()) return MPI_ERR_ARG; - } - if(elem->refcount==1){ - T::keyvals_.erase(*keyval); - xbt_free(elem); - }else{ - elem->refcount--; - } + + smpi_key_elem& elem = elem_it->second; + elem.deleted = true; + if (elem.refcount == 0) + T::keyvals_.erase(elem_it); + *keyval = MPI_KEYVAL_INVALID; return MPI_SUCCESS; } template int Keyval::attr_delete(int keyval){ - smpi_key_elem elem = T::keyvals_.at(keyval); - if(elem==nullptr) + auto elem_it = T::keyvals_.find(keyval); + if (elem_it == T::keyvals_.end()) return MPI_ERR_ARG; - elem->refcount--; - void * value = nullptr; - int flag=0; - if(this->attr_get(keyval, &value, &flag)==MPI_SUCCESS){ - int ret = call_deleter((T*)this, elem, keyval,value,&flag); - if(ret!=MPI_SUCCESS) - return ret; - } - if(attributes()->empty()) + + auto attr = attributes().find(keyval); + if (attr == attributes().end()) return MPI_ERR_ARG; - attributes()->erase(keyval); + + smpi_key_elem& elem = elem_it->second; + int flag = 0; + if (int ret = call_deleter((T*)this, elem, keyval, attr->second, &flag); ret != MPI_SUCCESS) + return ret; + + elem.refcount--; + if (elem.deleted && elem.refcount == 0) + T::keyvals_.erase(elem_it); + attributes().erase(attr); return MPI_SUCCESS; } template int Keyval::attr_get(int keyval, void* attr_value, int* flag){ - smpi_key_elem elem = T::keyvals_.at(keyval); - if(elem==nullptr) + if (auto elem_it = T::keyvals_.find(keyval); elem_it == T::keyvals_.end() || elem_it->second.deleted) return MPI_ERR_ARG; - if(attributes()->empty()){ - *flag=0; - return MPI_SUCCESS; - } - try { - *static_cast(attr_value) = attributes()->at(keyval); + + if (auto attr = attributes().find(keyval); attr != attributes().end()) { + *static_cast(attr_value) = attr->second; *flag=1; - } - catch (const std::out_of_range& oor) { + } else { *flag=0; } return MPI_SUCCESS; } template int Keyval::attr_put(int keyval, void* attr_value){ - smpi_key_elem elem = T::keyvals_.at(keyval); - if(elem==nullptr) + auto elem_it = T::keyvals_.find(keyval); + if (elem_it == T::keyvals_.end() || elem_it->second.deleted) return MPI_ERR_ARG; - elem->refcount++; - void * value = nullptr; - int flag=0; - this->attr_get(keyval, &value, &flag); - if(flag!=0){ - int ret = call_deleter((T*)this, elem, keyval,value,&flag); + + smpi_key_elem& elem = elem_it->second; + if (auto [attr, inserted] = attributes().try_emplace(keyval, attr_value); inserted) { + elem.refcount++; + } else { + int flag = 0; + int ret = call_deleter((T*)this, elem, keyval, attr->second, &flag); + // overwrite previous value + attr->second = attr_value; if(ret!=MPI_SUCCESS) - return ret; + return ret; } - attributes()->insert({keyval, attr_value}); return MPI_SUCCESS; } template void Keyval::cleanup_attr(){ - if (not attributes()->empty()) { - int flag=0; - for(auto it : attributes_){ - try{ - smpi_key_elem elem = T::keyvals_.at(it.first); - if(elem != nullptr){ - call_deleter((T*)this, elem, it.first,it.second,&flag); - } - }catch(const std::out_of_range& oor) { - //already deleted, not a problem; - flag=0; - } - } + for (auto const& [key, value] : attributes()) { + auto elem_it = T::keyvals_.find(key); + xbt_assert(elem_it != T::keyvals_.end()); + smpi_key_elem& elem = elem_it->second; + int flag = 0; + call_deleter((T*)this, elem, key, value, &flag); + elem.refcount--; + if (elem.deleted && elem.refcount == 0) + T::keyvals_.erase(elem_it); } + attributes().clear(); } -} -} +} // namespace simgrid::smpi #endif