Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Prefer using emplace() to insert() for smpi keyvals/attributes.
[simgrid.git] / src / smpi / include / smpi_keyvals.hpp
1 /* Copyright (c) 2010-2021. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #ifndef SMPI_KEYVALS_HPP_INCLUDED
7 #define SMPI_KEYVALS_HPP_INCLUDED
8
9 #include "smpi/smpi.h"
10
11 #include <unordered_map>
12
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;
17   MPI_Comm_delete_attr_function_fort     *comm_delete_fn_fort;
18   MPI_Type_delete_attr_function_fort     *type_delete_fn_fort;
19   MPI_Win_delete_attr_function_fort      *win_delete_fn_fort;
20 };
21
22 struct smpi_copy_fn {
23   MPI_Comm_copy_attr_function          *comm_copy_fn;
24   MPI_Type_copy_attr_function          *type_copy_fn;
25   MPI_Win_copy_attr_function           *win_copy_fn;
26   MPI_Comm_copy_attr_function_fort     *comm_copy_fn_fort;
27   MPI_Type_copy_attr_function_fort     *type_copy_fn_fort;
28   MPI_Win_copy_attr_function_fort      *win_copy_fn_fort;
29 };
30
31 struct s_smpi_key_elem_t {
32   smpi_copy_fn copy_fn;
33   smpi_delete_fn delete_fn;
34   void* extra_state;
35   int refcount;
36 };
37
38 using smpi_key_elem = s_smpi_key_elem_t*;
39
40 namespace simgrid{
41 namespace smpi{
42
43 class Keyval{
44   private:
45     std::unordered_map<int, void*> attributes_;
46   protected:
47     std::unordered_map<int, void*>& attributes() { return attributes_; }
48
49   public:
50 // Each subclass should have two members, as we want to separate the ones for Win, Comm, and Datatypes :
51 //    static std::unordered_map<int, smpi_key_elem> keyvals_;
52 //    static int keyval_id_;
53     template <typename T>
54     static int keyval_create(const smpi_copy_fn& copy_fn, const smpi_delete_fn& delete_fn, int* keyval,
55                              void* extra_state);
56     template <typename T> static int keyval_free(int* keyval);
57     template <typename T> int attr_delete(int keyval);
58     template <typename T> int attr_get(int keyval, void* attr_value, int* flag);
59     template <typename T> int attr_put(int keyval, void* attr_value);
60     template <typename T>
61     static int call_deleter(T* obj, const s_smpi_key_elem_t* elem, int keyval, void* value, int* flag);
62     template <typename T> void cleanup_attr();
63 };
64
65 template <typename T>
66 int Keyval::keyval_create(const smpi_copy_fn& copy_fn, const smpi_delete_fn& delete_fn, int* keyval, void* extra_state)
67 {
68   auto* value = new s_smpi_key_elem_t;
69
70   value->copy_fn=copy_fn;
71   value->delete_fn=delete_fn;
72   value->extra_state=extra_state;
73   value->refcount=1;
74
75   *keyval = T::keyval_id_;
76   T::keyvals_.emplace(*keyval, value);
77   T::keyval_id_++;
78   return MPI_SUCCESS;
79 }
80
81 template <typename T> int Keyval::keyval_free(int* keyval){
82 /* See MPI-1, 5.7.1.  Freeing the keyval does not remove it if it
83          * is in use in an attribute */
84   smpi_key_elem elem = T::keyvals_.at(*keyval);
85   if (elem == nullptr) {
86     return MPI_ERR_ARG;
87   }
88   if(elem->refcount==1){
89     T::keyvals_.erase(*keyval);
90     delete elem;
91   }else{
92     elem->refcount--;
93   }
94   *keyval = MPI_KEYVAL_INVALID;
95   return MPI_SUCCESS;
96 }
97
98 template <typename T> int Keyval::attr_delete(int keyval){
99   smpi_key_elem elem = T::keyvals_.at(keyval);
100   if(elem==nullptr)
101     return MPI_ERR_ARG;
102   elem->refcount--;
103   void * value = nullptr;
104   int flag=0;
105   if(this->attr_get<T>(keyval, &value, &flag)==MPI_SUCCESS){
106     int ret = call_deleter<T>((T*)this, elem, keyval,value,&flag);
107     if(ret!=MPI_SUCCESS)
108         return ret;
109   }
110   if (attributes().empty())
111     return MPI_ERR_ARG;
112   attributes().erase(keyval);
113   return MPI_SUCCESS;
114 }
115
116
117 template <typename T> int Keyval::attr_get(int keyval, void* attr_value, int* flag){
118   const s_smpi_key_elem_t* elem = T::keyvals_.at(keyval);
119   if(elem==nullptr)
120     return MPI_ERR_ARG;
121
122   auto attr = attributes().find(keyval);
123   if (attr != attributes().end()) {
124     *static_cast<void**>(attr_value) = attr->second;
125     *flag=1;
126   } else {
127     *flag=0;
128   }
129   return MPI_SUCCESS;
130 }
131
132 template <typename T> int Keyval::attr_put(int keyval, void* attr_value){
133   smpi_key_elem elem = T::keyvals_.at(keyval);
134   if(elem==nullptr)
135     return MPI_ERR_ARG;
136   elem->refcount++;
137   int flag=0;
138   auto p  = attributes().emplace(keyval, attr_value);
139   if (not p.second) {
140     int ret = call_deleter<T>((T*)this, elem, keyval,p.first->second,&flag);
141     // overwrite previous value
142     p.first->second = attr_value;
143     if(ret!=MPI_SUCCESS)
144       return ret;
145   }
146   return MPI_SUCCESS;
147 }
148
149 template <typename T> void Keyval::cleanup_attr(){
150   int flag = 0;
151   for (auto const& it : attributes()) {
152     auto elm = T::keyvals_.find(it.first);
153     if (elm != T::keyvals_.end()) {
154       smpi_key_elem elem = elm->second;
155       if (elem != nullptr) {
156         call_deleter<T>((T*)this, elem, it.first, it.second, &flag);
157       }
158     } else {
159       // already deleted, not a problem
160       flag = 0;
161     }
162   }
163 }
164
165 }
166 }
167
168 #endif