Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
oops, wasn't supposed to change that
[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 };
51
52 template <typename T> int Keyval::keyval_create(smpi_copy_fn copy_fn, smpi_delete_fn delete_fn, int* keyval, void* extra_state){
53
54   smpi_key_elem value = (smpi_key_elem) xbt_new0(s_smpi_mpi_key_elem_t,1);
55
56   value->copy_fn=copy_fn;
57   value->delete_fn=delete_fn;
58   value->refcount=1;
59
60   *keyval = T::keyval_id_;
61   T::keyvals_.insert({*keyval, value});
62   T::keyval_id_++;
63   return MPI_SUCCESS;
64 }
65
66 template <typename T> int Keyval::keyval_free(int* keyval){
67 /* See MPI-1, 5.7.1.  Freeing the keyval does not remove it if it
68          * is in use in an attribute */
69   smpi_key_elem elem = T::keyvals_.at(*keyval);
70   if(elem==0){
71     return MPI_ERR_ARG;
72   }
73   if(elem->refcount==1){
74     T::keyvals_.erase(*keyval);
75     xbt_free(elem);
76   }else{
77     elem->refcount--;
78   }
79   return MPI_SUCCESS;
80 }
81
82 //specialized in smpi_keyvals.cpp
83 template <typename T> int Keyval::call_deleter(T* obj, smpi_key_elem elem, int keyval, void * value, int* flag){
84   return MPI_SUCCESS;
85 }
86
87 template <typename T> int Keyval::attr_delete(int keyval){
88   smpi_key_elem elem = T::keyvals_.at(keyval);
89   if(elem==nullptr)
90     return MPI_ERR_ARG;
91   elem->refcount--;
92   void * value = nullptr;
93   int flag;
94   if(this->attr_get<T>(keyval, &value, &flag)==MPI_SUCCESS){
95     int ret = call_deleter<T>((T*)this, elem, keyval,value,&flag);
96     if(ret!=MPI_SUCCESS)
97         return ret;
98   }
99   if(attributes_.empty())
100     return MPI_ERR_ARG;
101   attributes_.erase(keyval);
102   return MPI_SUCCESS;
103 }
104
105
106 template <typename T> int Keyval::attr_get(int keyval, void* attr_value, int* flag){
107   smpi_key_elem elem = T::keyvals_.at(keyval);
108   if(elem==nullptr)
109     return MPI_ERR_ARG;
110   if(attributes_.empty()){
111     *flag=0;
112     return MPI_SUCCESS;
113   }
114   try {
115     *static_cast<void**>(attr_value) = attributes_.at(keyval);
116     *flag=1;
117   }
118   catch (const std::out_of_range& oor) {
119     *flag=0;
120   }
121   return MPI_SUCCESS;
122 }
123
124 template <typename T> int Keyval::attr_put(int keyval, void* attr_value){
125   smpi_key_elem elem = T::keyvals_.at(keyval);
126   if(elem==nullptr)
127     return MPI_ERR_ARG;
128   elem->refcount++;
129   void * value = nullptr;
130   int flag;
131   this->attr_get<T>(keyval, &value, &flag);
132   if(flag!=0){
133     int ret = call_deleter<T>((T*)this, elem, keyval,value,&flag);
134     if(ret!=MPI_SUCCESS)
135         return ret;
136   }
137   attributes_.insert({keyval, attr_value});
138   return MPI_SUCCESS;
139 }
140
141 }
142 }
143
144 #endif