1 /* Copyright (c) 2010-2019. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #ifndef SMPI_KEYVALS_HPP_INCLUDED
7 #define SMPI_KEYVALS_HPP_INCLUDED
11 #include <unordered_map>
13 struct smpi_delete_fn {
14 MPI_Comm_delete_attr_function *comm_delete_fn;
15 MPI_Type_delete_attr_function *type_delete_fn;
16 MPI_Win_delete_attr_function *win_delete_fn;
17 MPI_Comm_delete_attr_function_fort *comm_delete_fn_fort;
18 MPI_Type_delete_attr_function_fort *type_delete_fn_fort;
19 MPI_Win_delete_attr_function_fort *win_delete_fn_fort;
23 MPI_Comm_copy_attr_function *comm_copy_fn;
24 MPI_Type_copy_attr_function *type_copy_fn;
25 MPI_Win_copy_attr_function *win_copy_fn;
26 MPI_Comm_copy_attr_function_fort *comm_copy_fn_fort;
27 MPI_Type_copy_attr_function_fort *type_copy_fn_fort;
28 MPI_Win_copy_attr_function_fort *win_copy_fn_fort;
31 struct s_smpi_key_elem_t {
33 smpi_delete_fn delete_fn;
38 typedef s_smpi_key_elem_t* smpi_key_elem;
45 std::unordered_map<int, void*> attributes_;
47 std::unordered_map<int, void*>* attributes();
49 // Each subclass should have two members, as we want to separate the ones for Win, Comm, and Datatypes :
50 // static std::unordered_map<int, smpi_key_elem> keyvals_;
51 // static int keyval_id_;
53 static int keyval_create(const smpi_copy_fn& copy_fn, const smpi_delete_fn& delete_fn, int* keyval,
55 template <typename T> static int keyval_free(int* keyval);
56 template <typename T> int attr_delete(int keyval);
57 template <typename T> int attr_get(int keyval, void* attr_value, int* flag);
58 template <typename T> int attr_put(int keyval, void* attr_value);
59 template <typename T> static int call_deleter(T* obj, smpi_key_elem elem, int keyval, void * value, int* flag);
60 template <typename T> void cleanup_attr();
64 int Keyval::keyval_create(const smpi_copy_fn& copy_fn, const smpi_delete_fn& delete_fn, int* keyval, void* extra_state)
66 smpi_key_elem value = new s_smpi_key_elem_t;
68 value->copy_fn=copy_fn;
69 value->delete_fn=delete_fn;
70 value->extra_state=extra_state;
73 *keyval = T::keyval_id_;
74 T::keyvals_.insert({*keyval, value});
79 template <typename T> int Keyval::keyval_free(int* keyval){
80 /* See MPI-1, 5.7.1. Freeing the keyval does not remove it if it
81 * is in use in an attribute */
82 smpi_key_elem elem = T::keyvals_.at(*keyval);
86 if(elem->refcount==1){
87 T::keyvals_.erase(*keyval);
95 template <typename T> int Keyval::attr_delete(int keyval){
96 smpi_key_elem elem = T::keyvals_.at(keyval);
100 void * value = nullptr;
102 if(this->attr_get<T>(keyval, &value, &flag)==MPI_SUCCESS){
103 int ret = call_deleter<T>((T*)this, elem, keyval,value,&flag);
107 if(attributes()->empty())
109 attributes()->erase(keyval);
114 template <typename T> int Keyval::attr_get(int keyval, void* attr_value, int* flag){
115 smpi_key_elem elem = T::keyvals_.at(keyval);
118 if(attributes()->empty()){
122 const auto& attribs = attributes();
123 auto attr = attribs->find(keyval);
124 if (attr != attribs->end()) {
125 *static_cast<void**>(attr_value) = attr->second;
133 template <typename T> int Keyval::attr_put(int keyval, void* attr_value){
134 smpi_key_elem elem = T::keyvals_.at(keyval);
139 auto p = attributes()->insert({keyval, attr_value});
141 int ret = call_deleter<T>((T*)this, elem, keyval,p.first->second,&flag);
142 // overwrite previous value
143 p.first->second = attr_value;
150 template <typename T> void Keyval::cleanup_attr(){
151 if (not attributes()->empty()) {
153 for (auto const& it : attributes_) {
154 auto elm = T::keyvals_.find(it.first);
155 if (elm != T::keyvals_.end()) {
156 smpi_key_elem elem = elm->second;
158 call_deleter<T>((T*)this, elem, it.first,it.second,&flag);
161 // already deleted, not a problem