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
10 #include <unordered_map>
12 #include "smpi/smpi.h"
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()){
113 *static_cast<void**>(attr_value) = attributes()->at(keyval);
116 catch (const std::out_of_range& oor) {
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(!attributes()->empty()){
142 for(auto it : attributes_){
144 smpi_key_elem elem = T::keyvals_.at(it.first);
146 call_deleter<T>((T*)this, elem, it.first,it.second,&flag);
148 }catch(const std::out_of_range& oor) {
149 //already deleted, not a problem;