1 /* Copyright (c) 2010-2022. 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
10 #include "xbt/asserts.h"
12 #include <unordered_map>
14 struct smpi_delete_fn {
15 MPI_Comm_delete_attr_function *comm_delete_fn;
16 MPI_Type_delete_attr_function *type_delete_fn;
17 MPI_Win_delete_attr_function *win_delete_fn;
18 MPI_Comm_delete_attr_function_fort *comm_delete_fn_fort;
19 MPI_Type_delete_attr_function_fort *type_delete_fn_fort;
20 MPI_Win_delete_attr_function_fort *win_delete_fn_fort;
24 MPI_Comm_copy_attr_function *comm_copy_fn;
25 MPI_Type_copy_attr_function *type_copy_fn;
26 MPI_Win_copy_attr_function *win_copy_fn;
27 MPI_Comm_copy_attr_function_fort *comm_copy_fn_fort;
28 MPI_Type_copy_attr_function_fort *type_copy_fn_fort;
29 MPI_Win_copy_attr_function_fort *win_copy_fn_fort;
32 struct smpi_key_elem {
34 smpi_delete_fn delete_fn;
38 bool delete_attr; // if true, xbt_free(attr) on delete: used by Fortran bindings
46 std::unordered_map<int, void*> attributes_;
48 std::unordered_map<int, void*>& attributes() { return attributes_; }
51 // Each subclass should have two members, as we want to separate the ones for Win, Comm, and Datatypes :
52 // static std::unordered_map<int, smpi_key_elem> keyvals_;
53 // static int keyval_id_;
55 static int keyval_create(const smpi_copy_fn& copy_fn, const smpi_delete_fn& delete_fn, int* keyval,
56 void* extra_state, bool delete_attr = false);
57 template <typename T> static int keyval_free(int* keyval);
58 template <typename T> int attr_delete(int keyval);
59 template <typename T> int attr_get(int keyval, void* attr_value, int* flag);
60 template <typename T> int attr_put(int keyval, void* attr_value);
62 static int call_deleter(T* obj, const smpi_key_elem& elem, int keyval, void* value, int* flag);
63 template <typename T> void cleanup_attr();
67 int Keyval::keyval_create(const smpi_copy_fn& copy_fn, const smpi_delete_fn& delete_fn, int* keyval, void* extra_state,
71 value.copy_fn = copy_fn;
72 value.delete_fn = delete_fn;
73 value.extra_state = extra_state;
75 value.deleted = false;
76 value.delete_attr = delete_attr;
78 *keyval = T::keyval_id_;
79 T::keyvals_.emplace(*keyval, std::move(value));
84 template <typename T> int Keyval::keyval_free(int* keyval){
85 /* See MPI-1, 5.7.1. Freeing the keyval does not remove it if it is in use in an attribute */
86 auto elem_it = T::keyvals_.find(*keyval);
87 if (elem_it == T::keyvals_.end())
90 smpi_key_elem& elem = elem_it->second;
92 if (elem.refcount == 0)
93 T::keyvals_.erase(elem_it);
94 *keyval = MPI_KEYVAL_INVALID;
98 template <typename T> int Keyval::attr_delete(int keyval){
99 auto elem_it = T::keyvals_.find(keyval);
100 if (elem_it == T::keyvals_.end())
103 auto attr = attributes().find(keyval);
104 if (attr == attributes().end())
107 smpi_key_elem& elem = elem_it->second;
109 int ret = call_deleter<T>((T*)this, elem, keyval, attr->second, &flag);
110 if (ret != MPI_SUCCESS)
114 if (elem.deleted && elem.refcount == 0)
115 T::keyvals_.erase(elem_it);
116 attributes().erase(attr);
121 template <typename T> int Keyval::attr_get(int keyval, void* attr_value, int* flag){
122 auto elem_it = T::keyvals_.find(keyval);
123 if (elem_it == T::keyvals_.end() || elem_it->second.deleted)
126 auto attr = attributes().find(keyval);
127 if (attr != attributes().end()) {
128 *static_cast<void**>(attr_value) = attr->second;
136 template <typename T> int Keyval::attr_put(int keyval, void* attr_value){
137 auto elem_it = T::keyvals_.find(keyval);
138 if (elem_it == T::keyvals_.end() || elem_it->second.deleted)
141 smpi_key_elem& elem = elem_it->second;
143 auto p = attributes().emplace(keyval, attr_value);
147 int ret = call_deleter<T>((T*)this, elem, keyval,p.first->second,&flag);
148 // overwrite previous value
149 p.first->second = attr_value;
156 template <typename T> void Keyval::cleanup_attr(){
157 for (auto const& it : attributes()) {
158 auto elem_it = T::keyvals_.find(it.first);
159 xbt_assert(elem_it != T::keyvals_.end());
160 smpi_key_elem& elem = elem_it->second;
162 call_deleter<T>((T*)this, elem, it.first, it.second, &flag);
164 if (elem.deleted && elem.refcount == 0)
165 T::keyvals_.erase(elem_it);
167 attributes().clear();