Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0ca8ffc0df14ce47ba2e816f009d6c1cac8e23ec
[simgrid.git] / src / smpi / smpi_keyvals.hpp
1 /* Copyright (c) 2010, 2013-2017. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef SMPI_KEYVALS_HPP_INCLUDED
8 #define SMPI_KEYVALS_HPP_INCLUDED
9
10 #include "private.h"
11 #include <unordered_map>
12 #include <xbt/ex.hpp>
13
14 namespace simgrid{
15 namespace smpi{
16
17
18 typedef union smpi_delete_fn{
19   MPI_Comm_delete_attr_function          *comm_delete_fn;
20   MPI_Type_delete_attr_function          *type_delete_fn;
21   MPI_Win_delete_attr_function           *win_delete_fn;
22 } smpi_delete_fn;
23
24 typedef union smpi_copy_fn{
25   MPI_Comm_copy_attr_function          *comm_copy_fn;
26   MPI_Type_copy_attr_function          *type_copy_fn;
27   MPI_Win_copy_attr_function           *win_copy_fn;
28 } smpi_copy_fn;
29
30 typedef struct s_smpi_key_elem {
31   smpi_copy_fn copy_fn;
32   smpi_delete_fn delete_fn;
33   int refcount;
34 } s_smpi_mpi_key_elem_t; 
35 typedef struct s_smpi_key_elem *smpi_key_elem;
36
37 class Keyval{
38   protected:
39     std::unordered_map<int, void*> attributes_;
40   public:
41 // Each subclass should have two members, as we want to separate the ones for Win, Comm, and Datatypes :  
42 //    static std::unordered_map<int, smpi_key_elem> keyvals_;
43 //    static int keyval_id_;
44     template <typename T> static int keyval_create(smpi_copy_fn copy_fn, smpi_delete_fn delete_fn, int* keyval, void* extra_statee);
45     template <typename T> static int keyval_free(int* keyval);
46     template <typename T> int attr_delete(int keyval);
47     template <typename T> int attr_get(int keyval, void* attr_value, int* flag);
48     template <typename T> int attr_put(int keyval, void* attr_value);
49     template <typename T> static int call_deleter(T* obj, smpi_key_elem elem, int keyval, void * value, int* flag);
50     template <typename T> void cleanup_attr();
51 };
52
53 template <typename T> int Keyval::keyval_create(smpi_copy_fn copy_fn, smpi_delete_fn delete_fn, int* keyval, void* extra_state){
54
55   smpi_key_elem value = (smpi_key_elem) xbt_new0(s_smpi_mpi_key_elem_t,1);
56
57   value->copy_fn=copy_fn;
58   value->delete_fn=delete_fn;
59   value->refcount=1;
60
61   *keyval = T::keyval_id_;
62   T::keyvals_.insert({*keyval, value});
63   T::keyval_id_++;
64   return MPI_SUCCESS;
65 }
66
67 template <typename T> int Keyval::keyval_free(int* keyval){
68 /* See MPI-1, 5.7.1.  Freeing the keyval does not remove it if it
69          * is in use in an attribute */
70   smpi_key_elem elem = T::keyvals_.at(*keyval);
71   if(elem==0){
72     return MPI_ERR_ARG;
73   }
74   if(elem->refcount==1){
75     T::keyvals_.erase(*keyval);
76     xbt_free(elem);
77   }else{
78     elem->refcount--;
79   }
80   return MPI_SUCCESS;
81 }
82
83 template <typename T> int Keyval::attr_delete(int keyval){
84   smpi_key_elem elem = T::keyvals_.at(keyval);
85   if(elem==nullptr)
86     return MPI_ERR_ARG;
87   elem->refcount--;
88   void * value = nullptr;
89   int flag=0;
90   if(this->attr_get<T>(keyval, &value, &flag)==MPI_SUCCESS){
91     int ret = call_deleter<T>((T*)this, elem, keyval,value,&flag);
92     if(ret!=MPI_SUCCESS)
93         return ret;
94   }
95   if(attributes_.empty())
96     return MPI_ERR_ARG;
97   attributes_.erase(keyval);
98   return MPI_SUCCESS;
99 }
100
101
102 template <typename T> int Keyval::attr_get(int keyval, void* attr_value, int* flag){
103   smpi_key_elem elem = T::keyvals_.at(keyval);
104   if(elem==nullptr)
105     return MPI_ERR_ARG;
106   if(attributes_.empty()){
107     *flag=0;
108     return MPI_SUCCESS;
109   }
110   try {
111     *static_cast<void**>(attr_value) = attributes_.at(keyval);
112     *flag=1;
113   }
114   catch (const std::out_of_range& oor) {
115     *flag=0;
116   }
117   return MPI_SUCCESS;
118 }
119
120 template <typename T> int Keyval::attr_put(int keyval, void* attr_value){
121   smpi_key_elem elem = T::keyvals_.at(keyval);
122   if(elem==nullptr)
123     return MPI_ERR_ARG;
124   elem->refcount++;
125   void * value = nullptr;
126   int flag=0;
127   this->attr_get<T>(keyval, &value, &flag);
128   if(flag!=0){
129     int ret = call_deleter<T>((T*)this, elem, keyval,value,&flag);
130     if(ret!=MPI_SUCCESS)
131         return ret;
132   }
133   attributes_.insert({keyval, attr_value});
134   return MPI_SUCCESS;
135 }
136
137 template <typename T> void Keyval::cleanup_attr(){
138   if(!attributes_.empty()){
139     int flag=0;
140     for(auto it = attributes_.begin(); it != attributes_.end(); it++){
141       try{
142         smpi_key_elem elem = T::keyvals_.at((*it).first);
143         if(elem != nullptr){
144           call_deleter<T>((T*)this, elem, (*it).first,(*it).second,&flag);
145         }
146       }catch(const std::out_of_range& oor) {
147         //already deleted, not a problem;
148       }
149     }
150   }
151 }
152
153 }
154 }
155
156 #endif