1 /* Copyright (c) 2010-2017. 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;
20 MPI_Comm_copy_attr_function *comm_copy_fn;
21 MPI_Type_copy_attr_function *type_copy_fn;
22 MPI_Win_copy_attr_function *win_copy_fn;
25 struct s_smpi_key_elem_t {
27 smpi_delete_fn delete_fn;
31 typedef s_smpi_key_elem_t* smpi_key_elem;
38 std::unordered_map<int, void*> attributes_;
40 std::unordered_map<int, void*>* attributes();
42 // Each subclass should have two members, as we want to separate the ones for Win, Comm, and Datatypes :
43 // static std::unordered_map<int, smpi_key_elem> keyvals_;
44 // static int keyval_id_;
45 template <typename T> static int keyval_create(smpi_copy_fn copy_fn, smpi_delete_fn delete_fn, int* keyval, void* extra_statee);
46 template <typename T> static int keyval_free(int* keyval);
47 template <typename T> int attr_delete(int keyval);
48 template <typename T> int attr_get(int keyval, void* attr_value, int* flag);
49 template <typename T> int attr_put(int keyval, void* attr_value);
50 template <typename T> static int call_deleter(T* obj, smpi_key_elem elem, int keyval, void * value, int* flag);
51 template <typename T> void cleanup_attr();
54 template <typename T> int Keyval::keyval_create(smpi_copy_fn copy_fn, smpi_delete_fn delete_fn, int* keyval, void* extra_state){
56 smpi_key_elem value = new s_smpi_key_elem_t;
58 value->copy_fn=copy_fn;
59 value->delete_fn=delete_fn;
62 *keyval = T::keyval_id_;
63 T::keyvals_.insert({*keyval, value});
68 template <typename T> int Keyval::keyval_free(int* keyval){
69 /* See MPI-1, 5.7.1. Freeing the keyval does not remove it if it
70 * is in use in an attribute */
71 smpi_key_elem elem = T::keyvals_.at(*keyval);
75 if(elem->refcount==1){
76 T::keyvals_.erase(*keyval);
84 template <typename T> int Keyval::attr_delete(int keyval){
85 smpi_key_elem elem = T::keyvals_.at(keyval);
89 void * value = nullptr;
91 if(this->attr_get<T>(keyval, &value, &flag)==MPI_SUCCESS){
92 int ret = call_deleter<T>((T*)this, elem, keyval,value,&flag);
96 if(attributes()->empty())
98 attributes()->erase(keyval);
103 template <typename T> int Keyval::attr_get(int keyval, void* attr_value, int* flag){
104 smpi_key_elem elem = T::keyvals_.at(keyval);
107 if(attributes()->empty()){
111 const auto& attribs = attributes();
112 auto attr = attribs->find(keyval);
113 if (attr != attribs->end()) {
114 *static_cast<void**>(attr_value) = attr->second;
122 template <typename T> int Keyval::attr_put(int keyval, void* attr_value){
123 smpi_key_elem elem = T::keyvals_.at(keyval);
127 void * value = nullptr;
129 this->attr_get<T>(keyval, &value, &flag);
131 int ret = call_deleter<T>((T*)this, elem, keyval,value,&flag);
135 attributes()->insert({keyval, attr_value});
139 template <typename T> void Keyval::cleanup_attr(){
140 if (not attributes()->empty()) {
142 for (auto const& it : attributes_) {
143 auto elm = T::keyvals_.find(it.first);
144 if (elm != T::keyvals_.end()) {
145 smpi_key_elem elem = elm->second;
147 call_deleter<T>((T*)this, elem, it.first,it.second,&flag);
150 //already deleted, not a problem;