Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[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 //specialized in smpi_keyvals.cpp
84 template <typename T> int Keyval::call_deleter(T* obj, smpi_key_elem elem, int keyval, void * value, int* flag){
85   return MPI_SUCCESS;
86 }
87
88 template <typename T> int Keyval::attr_delete(int keyval){
89   smpi_key_elem elem = T::keyvals_.at(keyval);
90   if(elem==nullptr)
91     return MPI_ERR_ARG;
92   elem->refcount--;
93   void * value = nullptr;
94   int flag;
95   if(this->attr_get<T>(keyval, &value, &flag)==MPI_SUCCESS){
96     int ret = call_deleter<T>((T*)this, elem, keyval,value,&flag);
97     if(ret!=MPI_SUCCESS)
98         return ret;
99   }
100   if(attributes_.empty())
101     return MPI_ERR_ARG;
102   attributes_.erase(keyval);
103   return MPI_SUCCESS;
104 }
105
106
107 template <typename T> int Keyval::attr_get(int keyval, void* attr_value, int* flag){
108   smpi_key_elem elem = T::keyvals_.at(keyval);
109   if(elem==nullptr)
110     return MPI_ERR_ARG;
111   if(attributes_.empty()){
112     *flag=0;
113     return MPI_SUCCESS;
114   }
115   try {
116     *static_cast<void**>(attr_value) = attributes_.at(keyval);
117     *flag=1;
118   }
119   catch (const std::out_of_range& oor) {
120     *flag=0;
121   }
122   return MPI_SUCCESS;
123 }
124
125 template <typename T> int Keyval::attr_put(int keyval, void* attr_value){
126   smpi_key_elem elem = T::keyvals_.at(keyval);
127   if(elem==nullptr)
128     return MPI_ERR_ARG;
129   elem->refcount++;
130   void * value = nullptr;
131   int flag;
132   this->attr_get<T>(keyval, &value, &flag);
133   if(flag!=0){
134     int ret = call_deleter<T>((T*)this, elem, keyval,value,&flag);
135     if(ret!=MPI_SUCCESS)
136         return ret;
137   }
138   attributes_.insert({keyval, attr_value});
139   return MPI_SUCCESS;
140 }
141
142 template <typename T> void Keyval::cleanup_attr(){
143   if(!attributes_.empty()){
144     int flag;
145     for(auto it = attributes_.begin(); it != attributes_.end(); it++){
146       try{
147         smpi_key_elem elem = T::keyvals_.at((*it).first);
148         if(elem != nullptr){
149           call_deleter<T>((T*)this, elem, (*it).first,(*it).second,&flag);
150         }
151       }catch(const std::out_of_range& oor) {
152         //already deleted, not a problem;
153       }
154     }
155   }
156 }
157
158 }
159 }
160
161 #endif