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);
52 template <typename T> int Keyval::keyval_create(smpi_copy_fn copy_fn, smpi_delete_fn delete_fn, int* keyval, void* extra_state){
54 smpi_key_elem value = (smpi_key_elem) xbt_new0(s_smpi_mpi_key_elem_t,1);
56 value->copy_fn=copy_fn;
57 value->delete_fn=delete_fn;
60 *keyval = T::keyval_id_;
61 T::keyvals_.insert({*keyval, value});
66 template <typename T> int Keyval::keyval_free(int* keyval){
67 /* See MPI-1, 5.7.1. Freeing the keyval does not remove it if it
68 * is in use in an attribute */
69 smpi_key_elem elem = T::keyvals_.at(*keyval);
73 if(elem->refcount==1){
74 T::keyvals_.erase(*keyval);
82 //specialized in smpi_keyvals.cpp
83 template <typename T> int Keyval::call_deleter(T* obj, smpi_key_elem elem, int keyval, void * value, int* flag){
87 template <typename T> int Keyval::attr_delete(int keyval){
88 smpi_key_elem elem = T::keyvals_.at(keyval);
92 void * value = nullptr;
94 if(this->attr_get<T>(keyval, &value, &flag)==MPI_SUCCESS){
95 int ret = call_deleter<T>((T*)this, elem, keyval,value,&flag);
99 if(attributes_.empty())
101 attributes_.erase(keyval);
106 template <typename T> int Keyval::attr_get(int keyval, void* attr_value, int* flag){
107 smpi_key_elem elem = T::keyvals_.at(keyval);
110 if(attributes_.empty()){
115 *static_cast<void**>(attr_value) = attributes_.at(keyval);
118 catch (const std::out_of_range& oor) {
124 template <typename T> int Keyval::attr_put(int keyval, void* attr_value){
125 smpi_key_elem elem = T::keyvals_.at(keyval);
129 void * value = nullptr;
131 this->attr_get<T>(keyval, &value, &flag);
133 int ret = call_deleter<T>((T*)this, elem, keyval,value,&flag);
137 attributes_.insert({keyval, attr_value});