1 /* Copyright (c) 2010-2021. 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 smpi_key_elem {
33 smpi_delete_fn delete_fn;
43 std::unordered_map<int, void*> attributes_;
45 std::unordered_map<int, void*>& attributes() { return attributes_; }
48 // Each subclass should have two members, as we want to separate the ones for Win, Comm, and Datatypes :
49 // static std::unordered_map<int, smpi_key_elem> keyvals_;
50 // static int keyval_id_;
52 static int keyval_create(const smpi_copy_fn& copy_fn, const smpi_delete_fn& delete_fn, int* keyval,
54 template <typename T> static int keyval_free(int* keyval);
55 template <typename T> int attr_delete(int keyval);
56 template <typename T> int attr_get(int keyval, void* attr_value, int* flag);
57 template <typename T> int attr_put(int keyval, void* attr_value);
59 static int call_deleter(T* obj, const 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)
67 value.copy_fn = copy_fn;
68 value.delete_fn = delete_fn;
69 value.extra_state = extra_state;
72 *keyval = T::keyval_id_;
73 T::keyvals_.emplace(*keyval, std::move(value));
78 template <typename T> int Keyval::keyval_free(int* keyval){
79 /* See MPI-1, 5.7.1. Freeing the keyval does not remove it if it is in use in an attribute */
80 auto elem_it = T::keyvals_.find(*keyval);
81 if (elem_it == T::keyvals_.end())
84 smpi_key_elem& elem = elem_it->second;
86 if (elem.refcount == 0)
87 T::keyvals_.erase(elem_it);
88 *keyval = MPI_KEYVAL_INVALID;
92 template <typename T> int Keyval::attr_delete(int keyval){
93 auto elem_it = T::keyvals_.find(keyval);
94 if (elem_it == T::keyvals_.end())
97 smpi_key_elem& elem = elem_it->second;
99 void * value = nullptr;
101 if(this->attr_get<T>(keyval, &value, &flag)==MPI_SUCCESS){
102 int ret = call_deleter<T>((T*)this, elem, keyval,value,&flag);
106 if (attributes().empty())
108 attributes().erase(keyval);
113 template <typename T> int Keyval::attr_get(int keyval, void* attr_value, int* flag){
114 auto elem_it = T::keyvals_.find(keyval);
115 if (elem_it == T::keyvals_.end())
118 auto attr = attributes().find(keyval);
119 if (attr != attributes().end()) {
120 *static_cast<void**>(attr_value) = attr->second;
128 template <typename T> int Keyval::attr_put(int keyval, void* attr_value){
129 auto elem_it = T::keyvals_.find(keyval);
130 if (elem_it == T::keyvals_.end())
133 smpi_key_elem& elem = elem_it->second;
135 auto p = attributes().emplace(keyval, attr_value);
139 int ret = call_deleter<T>((T*)this, elem, keyval,p.first->second,&flag);
140 // overwrite previous value
141 p.first->second = attr_value;
148 template <typename T> void Keyval::cleanup_attr(){
150 for (auto const& it : attributes()) {
151 auto elem_it = T::keyvals_.find(it.first);
152 if (elem_it != T::keyvals_.end()) {
153 smpi_key_elem& elem = elem_it->second;
154 call_deleter<T>((T*)this, elem, it.first, it.second, &flag);
156 // already deleted, not a problem