Logo AND Algorithmique Numérique Distribuée

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