Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ab076557b7b7410e21c54a1d18e5904198d2ac79
[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 smpi_key_elem {
32   smpi_copy_fn copy_fn;
33   smpi_delete_fn delete_fn;
34   void* extra_state;
35   int refcount;
36 };
37
38 namespace simgrid{
39 namespace smpi{
40
41 class Keyval{
42   private:
43     std::unordered_map<int, void*> attributes_;
44   protected:
45     std::unordered_map<int, void*>& attributes() { return attributes_; }
46
47   public:
48 // Each subclass should have two members, as we want to separate the ones for Win, Comm, and Datatypes :
49 //    static std::unordered_map<int, smpi_key_elem> keyvals_;
50 //    static int keyval_id_;
51     template <typename T>
52     static int keyval_create(const smpi_copy_fn& copy_fn, const smpi_delete_fn& delete_fn, int* keyval,
53                              void* extra_state);
54     template <typename T> static int keyval_free(int* keyval);
55     template <typename T> int attr_delete(int keyval);
56     template <typename T> int attr_get(int keyval, void* attr_value, int* flag);
57     template <typename T> int attr_put(int keyval, void* attr_value);
58     template <typename T>
59     static int call_deleter(T* obj, const smpi_key_elem& elem, int keyval, void* value, int* flag);
60     template <typename T> void cleanup_attr();
61 };
62
63 template <typename T>
64 int Keyval::keyval_create(const smpi_copy_fn& copy_fn, const smpi_delete_fn& delete_fn, int* keyval, void* extra_state)
65 {
66   smpi_key_elem value;
67   value.copy_fn     = copy_fn;
68   value.delete_fn   = delete_fn;
69   value.extra_state = extra_state;
70   value.refcount    = 1;
71
72   *keyval = T::keyval_id_;
73   T::keyvals_.emplace(*keyval, std::move(value));
74   T::keyval_id_++;
75   return MPI_SUCCESS;
76 }
77
78 template <typename T> int Keyval::keyval_free(int* keyval){
79   /* See MPI-1, 5.7.1.  Freeing the keyval does not remove it if it is in use in an attribute */
80   auto elem_it = T::keyvals_.find(*keyval);
81   if (elem_it == T::keyvals_.end())
82     return MPI_ERR_ARG;
83
84   smpi_key_elem& elem = elem_it->second;
85   elem.refcount--;
86   if (elem.refcount == 0)
87     T::keyvals_.erase(elem_it);
88   *keyval = MPI_KEYVAL_INVALID;
89   return MPI_SUCCESS;
90 }
91
92 template <typename T> int Keyval::attr_delete(int keyval){
93   auto elem_it = T::keyvals_.find(keyval);
94   if (elem_it == T::keyvals_.end())
95     return MPI_ERR_ARG;
96
97   smpi_key_elem& elem = elem_it->second;
98   elem.refcount--;
99   void * value = nullptr;
100   int flag=0;
101   if(this->attr_get<T>(keyval, &value, &flag)==MPI_SUCCESS){
102     int ret = call_deleter<T>((T*)this, elem, keyval,value,&flag);
103     if(ret!=MPI_SUCCESS)
104         return ret;
105   }
106   if (attributes().empty())
107     return MPI_ERR_ARG;
108   attributes().erase(keyval);
109   return MPI_SUCCESS;
110 }
111
112
113 template <typename T> int Keyval::attr_get(int keyval, void* attr_value, int* flag){
114   auto elem_it = T::keyvals_.find(keyval);
115   if (elem_it == T::keyvals_.end())
116     return MPI_ERR_ARG;
117
118   auto attr = attributes().find(keyval);
119   if (attr != attributes().end()) {
120     *static_cast<void**>(attr_value) = attr->second;
121     *flag=1;
122   } else {
123     *flag=0;
124   }
125   return MPI_SUCCESS;
126 }
127
128 template <typename T> int Keyval::attr_put(int keyval, void* attr_value){
129   auto elem_it = T::keyvals_.find(keyval);
130   if (elem_it == T::keyvals_.end())
131     return MPI_ERR_ARG;
132
133   smpi_key_elem& elem = elem_it->second;
134   int flag=0;
135   auto p  = attributes().emplace(keyval, attr_value);
136   if (p.second) {
137     elem.refcount++;
138   } else {
139     int ret = call_deleter<T>((T*)this, elem, keyval,p.first->second,&flag);
140     // overwrite previous value
141     p.first->second = attr_value;
142     if(ret!=MPI_SUCCESS)
143       return ret;
144   }
145   return MPI_SUCCESS;
146 }
147
148 template <typename T> void Keyval::cleanup_attr(){
149   int flag = 0;
150   for (auto const& it : attributes()) {
151     auto elem_it = T::keyvals_.find(it.first);
152     if (elem_it != T::keyvals_.end()) {
153       smpi_key_elem& elem = elem_it->second;
154       call_deleter<T>((T*)this, elem, it.first, it.second, &flag);
155     } else {
156       // already deleted, not a problem
157       flag = 0;
158     }
159   }
160 }
161
162 }
163 }
164
165 #endif