Logo AND Algorithmique Numérique Distribuée

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