gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-2191-ga4742ac
a Global And Modular Bsm Inference Tool
safety_bucket.hpp
Go to the documentation of this file.
1 // GAMBIT: Global and Modular BSM Inference Tool
2 // *********************************************
20 
21 #ifndef __safety_bucket_hpp__
22 #define __safety_bucket_hpp__
23 
24 #include <iostream>
25 #include <omp.h>
26 
30 
31 
32 namespace Gambit
33 {
34 
37  {
38 
39  public:
40 
42  safety_bucket_base(str myinfo) : whoami(myinfo) {}
43 
46  {
47  if (not _initialized) dieGracefully();
48  return _functor_base_ptr->name();
49  }
50 
53  {
54  if (not _initialized) dieGracefully();
55  return _functor_base_ptr->origin();
56  }
57 
58 
59  protected:
60 
62 
64 
65  const str whoami;
66 
68  void dieGracefully() const
69  {
70  str errmsg = "You just just tried to access the dependency or backend requirement\n"
71  + whoami + "\n"
72  "Unfortunately this does not yet point to anything, because the dependency\n"
73  "or backend requirement has not been satistified yet. If using GAMBIT in \n"
74  "full, please check your rollcall declaration of this module function, and\n"
75  "its source code. This error often occurs because you have written \n"
76  " using namespace Pipes::x \n"
77  "where x is not actually the function being executed. Don't steal pipes!!\n\n"
78  "Alternatively, if you are writing a standalone executable using some \n"
79  "GAMBIT modules, please check that you have correctly filled this dep/req.";
80  utils_error().raise(LOCAL_INFO,errmsg);
81  }
82  };
83 
84 
85 
87  template <typename TYPE>
89  {
90 
91  public:
92 
94  dep_bucket(str mym, str myf, str me, module_functor<TYPE> * functor_ptr_in = NULL, module_functor_common* dependent_functor_ptr_in = NULL)
95  : safety_bucket_base(mym+"::Pipes::"+myf+"::Dep::"+me)
96  {
97  initialize(functor_ptr_in, dependent_functor_ptr_in);
98  }
99 
101  void initialize(module_functor<TYPE> * functor_ptr_in, module_functor_common* dependent_functor_ptr_in)
102  {
103  _functor_ptr = functor_ptr_in;
104  _functor_base_ptr = functor_ptr_in;
105  _dependent_functor_ptr = dependent_functor_ptr_in;
106 
107  // Extract pointer to dependency from functor and store as a safe_ptr.
108  if (functor_ptr_in == NULL)
109  {
110  _sptr.set(NULL);
111  _initialized = false;
112  }
113  else
114  {
115  _sptr = _functor_ptr->valuePtr();
116  _initialized = true;
117  }
118  }
119 
122  {
123  return origin();
124  }
125 
128  {
129  return (f1->loopManagerCapability() != "none" and
131  f1->loopManagerName() == f2->loopManagerName() and
132  f1->loopManagerOrigin() == f2->loopManagerOrigin() );
133  }
134 
136  const TYPE& operator *() const
137  {
138  if (not _initialized) dieGracefully();
139  //Choose the index of the thread if the dependency and the dependent functor are running inside the same loop. If not, just access the first element.
140  int index = use_thread_index(_functor_ptr, _dependent_functor_ptr) ? omp_get_thread_num() : 0;
141  return _sptr[index];
142  }
143 
144 
146  const TYPE* operator->() const
147  {
148  if (not _initialized) this->dieGracefully();
149  //Choose the index of the thread if the dependency and the dependent functor are running inside the same loop. If not, just choose the first element.
150  int index = use_thread_index(_functor_ptr, _dependent_functor_ptr) ? omp_get_thread_num() : 0;
151  return _sptr.operator->() + index; //Call a const member function of the indexth element of the array pointed to by the safe pointer.
152  }
153 
156  {
157  if (not _initialized) dieGracefully();
158  return _sptr;
159  }
160 
161 
162  protected:
163 
167 
168  };
169 
170 
171 
174  {
175 
176  public:
177 
179  BE_bucket_base(str mym, str myf, str me)
180  : safety_bucket_base(mym+"::Pipes::"+myf+"::BEreq::"+me) {}
181 
184  {
185  return origin();
186  }
187 
190  {
191  if (not _initialized) dieGracefully();
192  return _functor_base_ptr->version();
193  }
194 
195  };
196 
197 
199  template <typename TYPE>
201  {
202 
203  protected:
204 
207 
208  public:
209 
211  BEvariable_bucket(str mym, str myf, str me, backend_functor<TYPE*(*)(),TYPE*> * functor_ptr_in = NULL)
212  : BE_bucket_base(mym, myf, me)
213  {
214  initialize(functor_ptr_in);
215  }
216 
217 
219  void initialize(backend_functor<TYPE*(*)(),TYPE*> * functor_ptr_in)
220  {
221  _functor_ptr = functor_ptr_in;
222  _functor_base_ptr = functor_ptr_in;
223 
224  if (functor_ptr_in == NULL)
225  {
226  _svptr.set(NULL);
227  _initialized = false;
228  }
229  else
230  {
231  // Extract variable pointer from functor and store as a safe_variable_ptr
232  _svptr.set( (*_functor_ptr)() );
233  _initialized = true;
234  }
235  }
236 
238  TYPE& operator *()
239  {
240  if (not _initialized) dieGracefully();
241  return *_svptr;
242  }
243 
245  TYPE* operator->()
246  {
247  return _svptr.operator->();
248  }
249 
251  TYPE * pointer()
252  {
253  if (not _initialized) dieGracefully();
254  return _svptr.get();
255  }
256 
259  {
260  if (not _initialized) dieGracefully();
261  return _svptr;
262  }
263 
264  };
265 
266 
268  template <typename PTR_TYPE, typename TYPE, typename... ARGS>
270  {
271  public:
272 
275  : BE_bucket_base(mym, myf, me)
276  {
277  initialize(functor_ptr_in);
278  }
279 
282  {
283  _functor_ptr = functor_ptr_in;
284  _functor_base_ptr = functor_ptr_in;
285 
286  if (functor_ptr_in == NULL)
287  {
288  _initialized = false;
289  }
290  else
291  {
292  _initialized = true;
293  }
294  }
295 
297  PTR_TYPE pointer()
298  {
299  if (not _initialized) dieGracefully();
300  return _functor_ptr->handoutFunctionPointer();
301  }
302 
303 
304  protected:
305 
306  backend_functor<PTR_TYPE, TYPE, ARGS...>* _functor_ptr;
307 
308  };
309 
310 
312  template <typename PTR_TYPE, typename TYPE, typename... ARGS> class BEfunction_bucket;
313 
315  template <typename TYPE, typename... ARGS>
316  class BEfunction_bucket<TYPE(*)(ARGS...),TYPE,ARGS...> : public BEfunction_bucket_common<TYPE(*)(ARGS...),TYPE,ARGS...>
317  {
318 
319  public:
320 
322  BEfunction_bucket(str mym, str myf, str me, backend_functor<TYPE(*)(ARGS...), TYPE, ARGS...>* functor_ptr_in = NULL)
323  : BEfunction_bucket_common<TYPE(*)(ARGS...),TYPE,ARGS...>(mym, myf, me, functor_ptr_in) {}
324 
326  TYPE operator ()(ARGS&& ...args)
327  {
328  if (not this->_initialized) this->dieGracefully();
329  return (*(this->_functor_ptr))(std::forward<ARGS>(args)...);
330  }
331 
332  };
333 
335  template <typename TYPE, typename... ARGS>
336  class BEfunction_bucket<typename variadic_ptr<TYPE,ARGS...>::type,TYPE,ARGS...>
337  : public BEfunction_bucket_common<typename variadic_ptr<TYPE,ARGS...>::type,TYPE,ARGS...>
338  {
339 
340  public:
341 
343  BEfunction_bucket(str mym, str myf, str me, backend_functor<typename variadic_ptr<TYPE,ARGS...>::type, TYPE, ARGS...>* functor_ptr_in = NULL)
344  : BEfunction_bucket_common<typename variadic_ptr<TYPE,ARGS...>::type,TYPE,ARGS...>(mym, myf, me, functor_ptr_in) {}
345 
347  template <typename... VARARGS>
348  TYPE operator ()(VARARGS&& ...varargs)
349  {
350  if (not this->_initialized) this->dieGracefully();
351  return (*(this->_functor_ptr))(std::forward<VARARGS>(varargs)...);
352  }
353 
354  };
355 
356 }
357 
358 #endif // defined __safety_bucket_hpp__
359 
str version() const
Getter for the version of the wrapped function&#39;s origin (module or backend)
Definition: functors.cpp:123
str module()
Get module name.
safe_variable_ptr< TYPE > _svptr
safety_bucket_base(str myinfo)
Master constructor.
void initialize(module_functor< TYPE > *functor_ptr_in, module_functor_common *dependent_functor_ptr_in)
Initialize this bucket with a depedency functor pointer.
PTR_TYPE pointer()
Return the underlying function pointer.
EXPORT_SYMBOLS error & utils_error()
Utility errors.
virtual str loopManagerOrigin()
Getter for revealing the module of the wrapped function&#39;s assigned loop manager.
Definition: functors.cpp:927
A safe pointer that throws an informative error if you try to dereference it when nullified...
Definition: util_types.hpp:175
Actual module functor type for all but TYPE=void.
Definition: functors.hpp:745
virtual str loopManagerCapability()
Getter for revealing the required capability of the wrapped function&#39;s loop manager.
Definition: functors.cpp:921
void dieGracefully() const
Failure message invoked when the user tries to access the object before it is initialized.
void set(TYPE *in_ptr)
Set pointer.
Definition: util_types.hpp:265
#define LOCAL_INFO
Definition: local_info.hpp:34
virtual str loopManagerName()
Getter for revealing the name of the wrapped function&#39;s assigned loop manager.
Definition: functors.cpp:925
Function wrapper (functor) base class.
Definition: functors.hpp:87
P2 operator*(const P2 &a, double f)
General small utility classes, typedefs, etc.
The actual usable form of the interface class to backend functions.
str name()
Get capability name.
str origin() const
Getter for the wrapped function&#39;s origin (module or backend name)
Definition: functors.cpp:121
TYPE * get()
Get pointer.
Definition: util_types.hpp:268
Functor derived class for module functions.
Definition: functors.hpp:399
str name() const
Getter for the wrapped function&#39;s name.
Definition: functors.cpp:115
Functor class definitions.
BE_bucket_base(str mym, str myf, str me)
Constructor for BE_bucket_base.
BEfunction_bucket(str mym, str myf, str me, backend_functor< TYPE(*)(ARGS...), TYPE, ARGS... > *functor_ptr_in=NULL)
Constructor for non-variadic BEfunction_bucket.
static bool use_thread_index(module_functor< TYPE > *f1, module_functor_common *f2)
Check if the thread index needs to be used to access the functor&#39;s result.
A safe variable pointer that throws an informative error if you try to dereference it when nullified...
Definition: util_types.hpp:252
str origin()
Get name of origin (module/backend).
A base class for BEvariable_bucket and BEfunction_bucket.
An interface class for module dependencies.
dep_bucket(str mym, str myf, str me, module_functor< TYPE > *functor_ptr_in=NULL, module_functor_common *dependent_functor_ptr_in=NULL)
Constructor for dep_bucket.
backend_functor< PTR_TYPE, TYPE, ARGS... > * _functor_ptr
void initialize(backend_functor< PTR_TYPE, TYPE, ARGS... > *functor_ptr_in)
Initialize this bucket with a functor pointer.
Actual backend functor type.
Definition: functors.hpp:880
std::string str
Shorthand for a standard string.
Definition: Analysis.hpp:35
void initialize(backend_functor< TYPE *(*)(), TYPE *> *functor_ptr_in)
Initialize this bucket with a functor pointer.
backend_functor< TYPE *(*)(), TYPE * > * _functor_ptr
safe_variable_ptr< TYPE > & safe_pointer()
Get the safe_variable_ptr.
str version()
Get version information.
TYPE * operator->()
Access member functions.
Base class for the interface classes &#39;dep_bucket&#39;, &#39;BEvariable_bucket&#39; and &#39;BEfunction_bucket&#39;.
BEfunction_bucket(str mym, str myf, str me, backend_functor< typename variadic_ptr< TYPE, ARGS... >::type, TYPE, ARGS... > *functor_ptr_in=NULL)
Constructor for variadic BEfunction_bucket.
module_functor_common * _dependent_functor_ptr
Type redefinition to get around icc compiler bugs.
Definition: functors.hpp:73
module_functor< TYPE > * _functor_ptr
An interface class for backend functions.
TYPE * pointer()
Get the underlying variable pointer.
str backend()
Get backend name.
Exception objects required for standalone compilation.
const TYPE * operator->() const
Access is allowed to const member functions only.
BEvariable_bucket(str mym, str myf, str me, backend_functor< TYPE *(*)(), TYPE *> *functor_ptr_in=NULL)
Constructor for BEvariable_bucket.
BEfunction_bucket_common(str mym, str myf, str me, backend_functor< PTR_TYPE, TYPE, ARGS... > *functor_ptr_in=NULL)
Constructor for BEfunction_bucket_common.
An interface class for backend variables.
safe_ptr< TYPE > _sptr
safe_ptr< TYPE > & safe_pointer()
Get the safe_ptr.
TODO: see if we can use this one:
Definition: Analysis.hpp:33