1 /* Copyright (c) 2010, 2013-2017. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #ifndef SMPI_KEYVALS_HPP_INCLUDED
8 #define SMPI_KEYVALS_HPP_INCLUDED
11 #include <unordered_map>
18 typedef union smpi_delete_fn{
19 MPI_Comm_delete_attr_function *comm_delete_fn;
20 MPI_Type_delete_attr_function *type_delete_fn;
21 MPI_Win_delete_attr_function *win_delete_fn;
24 typedef union smpi_copy_fn{
25 MPI_Comm_copy_attr_function *comm_copy_fn;
26 MPI_Type_copy_attr_function *type_copy_fn;
27 MPI_Win_copy_attr_function *win_copy_fn;
30 typedef struct s_smpi_key_elem {
32 smpi_delete_fn delete_fn;
34 } s_smpi_mpi_key_elem_t;
35 typedef struct s_smpi_key_elem *smpi_key_elem;
39 std::unordered_map<int, void*> attributes_;
41 // Each subclass should have two members, as we want to separate the ones for Win, Comm, and Datatypes :
42 // static std::unordered_map<int, smpi_key_elem> keyvals_;
43 // static int keyval_id_;
44 template <typename T> static int keyval_create(smpi_copy_fn copy_fn, smpi_delete_fn delete_fn, int* keyval, void* extra_statee);
45 template <typename T> static int keyval_free(int* keyval);
46 template <typename T> int attr_delete(int keyval);
47 template <typename T> int attr_get(int keyval, void* attr_value, int* flag);
48 template <typename T> int attr_put(int keyval, void* attr_value);
49 template <typename T> static int call_deleter(T* obj, smpi_key_elem elem, int keyval, void * value, int* flag);
50 template <typename T> void cleanup_attr();
53 template <typename T> int Keyval::keyval_create(smpi_copy_fn copy_fn, smpi_delete_fn delete_fn, int* keyval, void* extra_state){
55 smpi_key_elem value = (smpi_key_elem) xbt_new0(s_smpi_mpi_key_elem_t,1);
57 value->copy_fn=copy_fn;
58 value->delete_fn=delete_fn;
61 *keyval = T::keyval_id_;
62 T::keyvals_.insert({*keyval, value});
67 template <typename T> int Keyval::keyval_free(int* keyval){
68 /* See MPI-1, 5.7.1. Freeing the keyval does not remove it if it
69 * is in use in an attribute */
70 smpi_key_elem elem = T::keyvals_.at(*keyval);
74 if(elem->refcount==1){
75 T::keyvals_.erase(*keyval);
83 template <typename T> int Keyval::attr_delete(int keyval){
84 smpi_key_elem elem = T::keyvals_.at(keyval);
88 void * value = nullptr;
90 if(this->attr_get<T>(keyval, &value, &flag)==MPI_SUCCESS){
91 int ret = call_deleter<T>((T*)this, elem, keyval,value,&flag);
95 if(attributes_.empty())
97 attributes_.erase(keyval);
102 template <typename T> int Keyval::attr_get(int keyval, void* attr_value, int* flag){
103 smpi_key_elem elem = T::keyvals_.at(keyval);
106 if(attributes_.empty()){
111 *static_cast<void**>(attr_value) = attributes_.at(keyval);
114 catch (const std::out_of_range& oor) {
120 template <typename T> int Keyval::attr_put(int keyval, void* attr_value){
121 smpi_key_elem elem = T::keyvals_.at(keyval);
125 void * value = nullptr;
127 this->attr_get<T>(keyval, &value, &flag);
129 int ret = call_deleter<T>((T*)this, elem, keyval,value,&flag);
133 attributes_.insert({keyval, attr_value});
137 template <typename T> void Keyval::cleanup_attr(){
138 if(!attributes_.empty()){
140 for(auto it = attributes_.begin(); it != attributes_.end(); it++){
142 smpi_key_elem elem = T::keyvals_.at((*it).first);
144 call_deleter<T>((T*)this, elem, (*it).first,(*it).second,&flag);
146 }catch(const std::out_of_range& oor) {
147 //already deleted, not a problem;