Logo AND Algorithmique Numérique Distribuée

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