Logo AND Algorithmique Numérique Distribuée

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