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
12 #include <unordered_map>
14 typedef 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;
20 typedef struct smpi_copy_fn{
21 MPI_Comm_copy_attr_function *comm_copy_fn;
22 MPI_Type_copy_attr_function *type_copy_fn;
23 MPI_Win_copy_attr_function *win_copy_fn;
26 typedef struct s_smpi_key_elem {
28 smpi_delete_fn delete_fn;
30 } s_smpi_mpi_key_elem_t;
32 typedef struct s_smpi_key_elem *smpi_key_elem;
39 std::unordered_map<int, void*> attributes_;
41 std::unordered_map<int, void*>* attributes();
43 // Each subclass should have two members, as we want to separate the ones for Win, Comm, and Datatypes :
44 // static std::unordered_map<int, smpi_key_elem> keyvals_;
45 // static int keyval_id_;
46 template <typename T> static int keyval_create(smpi_copy_fn copy_fn, smpi_delete_fn delete_fn, int* keyval, void* extra_statee);
47 template <typename T> static int keyval_free(int* keyval);
48 template <typename T> int attr_delete(int keyval);
49 template <typename T> int attr_get(int keyval, void* attr_value, int* flag);
50 template <typename T> int attr_put(int keyval, void* attr_value);
51 template <typename T> static int call_deleter(T* obj, smpi_key_elem elem, int keyval, void * value, int* flag);
52 template <typename T> void cleanup_attr();
55 template <typename T> int Keyval::keyval_create(smpi_copy_fn copy_fn, smpi_delete_fn delete_fn, int* keyval, void* extra_state){
57 smpi_key_elem value = (smpi_key_elem) xbt_new0(s_smpi_mpi_key_elem_t,1);
59 value->copy_fn=copy_fn;
60 value->delete_fn=delete_fn;
63 *keyval = T::keyval_id_;
64 T::keyvals_.insert({*keyval, value});
69 template <typename T> int Keyval::keyval_free(int* keyval){
70 /* See MPI-1, 5.7.1. Freeing the keyval does not remove it if it
71 * is in use in an attribute */
72 smpi_key_elem elem = T::keyvals_.at(*keyval);
76 if(elem->refcount==1){
77 T::keyvals_.erase(*keyval);
85 template <typename T> int Keyval::attr_delete(int keyval){
86 smpi_key_elem elem = T::keyvals_.at(keyval);
90 void * value = nullptr;
92 if(this->attr_get<T>(keyval, &value, &flag)==MPI_SUCCESS){
93 int ret = call_deleter<T>((T*)this, elem, keyval,value,&flag);
97 if(attributes()->empty())
99 attributes()->erase(keyval);
104 template <typename T> int Keyval::attr_get(int keyval, void* attr_value, int* flag){
105 smpi_key_elem elem = T::keyvals_.at(keyval);
108 if(attributes()->empty()){
112 const auto& attribs = attributes();
113 auto attr = attribs->find(keyval);
114 if (attr != attribs->end()) {
115 *static_cast<void**>(attr_value) = attr->second;
123 template <typename T> int Keyval::attr_put(int keyval, void* attr_value){
124 smpi_key_elem elem = T::keyvals_.at(keyval);
128 void * value = nullptr;
130 this->attr_get<T>(keyval, &value, &flag);
132 int ret = call_deleter<T>((T*)this, elem, keyval,value,&flag);
136 attributes()->insert({keyval, attr_value});
140 template <typename T> void Keyval::cleanup_attr(){
141 if (not attributes()->empty()) {
143 for (auto const& it : attributes_) {
144 auto elm = T::keyvals_.find(it.first);
145 if (elm != T::keyvals_.end()) {
146 smpi_key_elem elem = elm->second;
148 call_deleter<T>((T*)this, elem, it.first,it.second,&flag);
151 //already deleted, not a problem;