Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix process_killall. Closes #186.
[simgrid.git] / src / smpi / include / 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   const auto& attribs = attributes();
113   auto attr           = attribs->find(keyval);
114   if (attr != attribs->end()) {
115     *static_cast<void**>(attr_value) = attr->second;
116     *flag=1;
117   } else {
118     *flag=0;
119   }
120   return MPI_SUCCESS;
121 }
122
123 template <typename T> int Keyval::attr_put(int keyval, void* attr_value){
124   smpi_key_elem elem = T::keyvals_.at(keyval);
125   if(elem==nullptr)
126     return MPI_ERR_ARG;
127   elem->refcount++;
128   void * value = nullptr;
129   int flag=0;
130   this->attr_get<T>(keyval, &value, &flag);
131   if(flag!=0){
132     int ret = call_deleter<T>((T*)this, elem, keyval,value,&flag);
133     if(ret!=MPI_SUCCESS)
134         return ret;
135   }
136   attributes()->insert({keyval, attr_value});
137   return MPI_SUCCESS;
138 }
139
140 template <typename T> void Keyval::cleanup_attr(){
141   if (not attributes()->empty()) {
142     int flag=0;
143     for (auto const& it : attributes_) {
144       auto elm = T::keyvals_.find(it.first);
145       if (elm != T::keyvals_.end()) {
146         smpi_key_elem elem = elm->second;
147         if(elem != nullptr){
148           call_deleter<T>((T*)this, elem, it.first,it.second,&flag);
149         }
150       } else {
151         //already deleted, not a problem;
152         flag=0;
153       }
154     }
155   }
156 }
157
158 }
159 }
160
161 #endif