Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / smpi / include / smpi_keyvals.hpp
1 /* Copyright (c) 2010-2023. 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::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, bool delete_attr = false);
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 smpi_key_elem& 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                           bool delete_attr)
68 {
69   smpi_key_elem value;
70   value.copy_fn     = copy_fn;
71   value.delete_fn   = delete_fn;
72   value.extra_state = extra_state;
73   value.refcount    = 0;
74   value.deleted     = false;
75   value.delete_attr = delete_attr;
76
77   *keyval = T::keyval_id_;
78   T::keyvals_.emplace(*keyval, std::move(value));
79   T::keyval_id_++;
80   return MPI_SUCCESS;
81 }
82
83 template <typename T> int Keyval::keyval_free(int* keyval){
84   /* See MPI-1, 5.7.1.  Freeing the keyval does not remove it if it is in use in an attribute */
85   auto elem_it = T::keyvals_.find(*keyval);
86   if (elem_it == T::keyvals_.end())
87     return MPI_ERR_ARG;
88
89   smpi_key_elem& elem = elem_it->second;
90   elem.deleted        = true;
91   if (elem.refcount == 0)
92     T::keyvals_.erase(elem_it);
93   *keyval = MPI_KEYVAL_INVALID;
94   return MPI_SUCCESS;
95 }
96
97 template <typename T> int Keyval::attr_delete(int keyval){
98   auto elem_it = T::keyvals_.find(keyval);
99   if (elem_it == T::keyvals_.end())
100     return MPI_ERR_ARG;
101
102   auto attr = attributes().find(keyval);
103   if (attr == attributes().end())
104     return MPI_ERR_ARG;
105
106   smpi_key_elem& elem = elem_it->second;
107   int flag            = 0;
108   if (int ret = call_deleter<T>((T*)this, elem, keyval, attr->second, &flag); ret != MPI_SUCCESS)
109     return ret;
110
111   elem.refcount--;
112   if (elem.deleted && elem.refcount == 0)
113     T::keyvals_.erase(elem_it);
114   attributes().erase(attr);
115   return MPI_SUCCESS;
116 }
117
118
119 template <typename T> int Keyval::attr_get(int keyval, void* attr_value, int* flag){
120   if (auto elem_it = T::keyvals_.find(keyval); elem_it == T::keyvals_.end() || elem_it->second.deleted)
121     return MPI_ERR_ARG;
122
123   if (auto attr = attributes().find(keyval); 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   auto elem_it = T::keyvals_.find(keyval);
134   if (elem_it == T::keyvals_.end() || elem_it->second.deleted)
135     return MPI_ERR_ARG;
136
137   smpi_key_elem& elem = elem_it->second;
138   if (auto [attr, inserted] = attributes().try_emplace(keyval, attr_value); inserted) {
139     elem.refcount++;
140   } else {
141     int flag = 0;
142     int ret  = call_deleter<T>((T*)this, elem, keyval, attr->second, &flag);
143     // overwrite previous value
144     attr->second = attr_value;
145     if(ret!=MPI_SUCCESS)
146       return ret;
147   }
148   return MPI_SUCCESS;
149 }
150
151 template <typename T> void Keyval::cleanup_attr(){
152   for (auto const& [key, value] : attributes()) {
153     auto elem_it = T::keyvals_.find(key);
154     xbt_assert(elem_it != T::keyvals_.end());
155     smpi_key_elem& elem = elem_it->second;
156     int flag            = 0;
157     call_deleter<T>((T*)this, elem, key, value, &flag);
158     elem.refcount--;
159     if (elem.deleted && elem.refcount == 0)
160       T::keyvals_.erase(elem_it);
161   }
162   attributes().clear();
163 }
164
165 } // namespace simgrid::smpi
166
167 #endif