gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-252-gf9a3f78
a Global And Modular Bsm Inference Tool
plugin_factory.hpp
Go to the documentation of this file.
1 // GAMBIT: Global and Modular BSM Inference Tool
2 // *********************************************
16 
17 #ifndef __plugin_factory_hpp__
18 #define __plugin_factory_hpp__
19 
20 #include <map>
21 #include <vector>
22 #include <string>
23 #include <unordered_map>
24 #include <sstream>
25 #include <unordered_map>
26 #include <typeinfo>
27 #include <utility>
28 #include <limits>
29 #include <type_traits>
30 
39 
40 #define LOAD_FUNC_TEMPLATE(name, ...) REGISTER_ELEM(__functions__, typeid(__VA_ARGS__), name<__VA_ARGS__>)
41 #define LOAD_MULTI_FUNC_TEMPLATE(name, ...) REGISTER_ELEM(__multi_functions__, typeid(__VA_ARGS__), name<__VA_ARGS__>)
42 
43 namespace Gambit
44 {
45  namespace Scanner
46  {
47  using Gambit::type_index;
48 
50  {
51  typedef void* func_type(const std::vector<std::string> &, const std::string &);
52  typedef void* multi_func_type(const std::map<std::string, std::vector<std::string>> &, const std::vector<std::pair<std::string, std::string>> &);
53  std::unordered_map<type_index, func_type *, Gambit::type_hasher, Gambit::type_equal_to> __functions__;
54  std::unordered_map<type_index, multi_func_type *> __multi_functions__;
55  }
56 
57  template <typename T>
59 
60  template <typename T>
62 
64  template <typename ret, typename... args>
65  class Scanner_Plugin_Function<ret (args...)> : public Plugins::Plugin_Interface<ret (args...)>, public Function_Base<ret (args...)>
66  {
67  private:
68  //std::vector<double> &params;
69 
70  public:
71  Scanner_Plugin_Function(const std::vector<std::string> &params, const std::string &name)
72  : Plugins::Plugin_Interface<ret (args...)>("objective", name, params)
73  {
74  }
75 
76  ret main(const args&... in)
77  {
78  // Check for signals to abort run
79  if(signaldata().check_if_shutdown_begun())
80  {
81  Function_Base<ret (args...)>::tell_scanner_early_shutdown_in_progress(); // e.g. sets 'quit' flag in Diver
82  }
83 
84  if(signaldata().shutdown_begun() and not Function_Base<ret (args...)>::scanner_can_quit())
85  {
87  //lnlike = alt_min_valid_lnlike;
88  //point_invalidated = true;
89  return scanner_plugin_def_ret<ret>();
90  }
91  else
92  return this->Plugins::Plugin_Interface<ret (args...)>::operator()(in...);
93  }
94  };
95 
97  template <typename ret, typename... args>
98  class Multi_Scanner_Plugin_Function <ret (args...)> : public Function_Base<ret (args...)>
99  {
100  private:
101  std::vector< Scanner_Plugin_Function<ret (args...)> > functions;
102 
103  public:
104  Multi_Scanner_Plugin_Function(const std::map< std::string, std::vector<std::string> > &params, const std::vector<std::pair<std::string, std::string>> &names)
105  {
106  for (auto it = names.begin(), end = names.end(); it != end; it++)
107  {
108  functions.emplace_back(params.at(it->second), it->first);
109  }
110  }
111 
112  ret main(const args&... in)
113  {
114  // Check for signals to abort run
115  if(signaldata().check_if_shutdown_begun())
116  {
117  Function_Base<ret (args...)>::tell_scanner_early_shutdown_in_progress(); // e.g. sets 'quit' flag in Diver
118  }
119 
120  if(signaldata().shutdown_begun() and not Function_Base<ret (args...)>::scanner_can_quit())
121  {
123  //lnlike = alt_min_valid_lnlike;
124  //point_invalidated = true;
125  return scanner_plugin_def_ret<ret>();
126  }
127  else
128  {
129  ret retval = 0.0;
130  for (auto& it : functions) retval += it.main(in...);
131  return retval;
132  }
133  }
134  };
135 
136  LOAD_FUNC_TEMPLATE(Scanner_Plugin_Function, double (std::unordered_map<std::string, double> &));
137  LOAD_FUNC_TEMPLATE(Scanner_Plugin_Function, void (const std::vector<double> &, std::unordered_map<std::string, double> &));
138  LOAD_FUNC_TEMPLATE(Scanner_Plugin_Function, std::vector<double> (std::unordered_map<std::string, double> &));
139  LOAD_MULTI_FUNC_TEMPLATE(Multi_Scanner_Plugin_Function, double(std::unordered_map<std::string, double> &));
140 
143  {
144  private:
145  std::map< std::string, std::vector<std::pair<std::string, std::string> > > names;
146  std::map< std::string, std::vector<std::string> > parameters;
147  std::unordered_map<std::string, Gambit::type_index> purpose_index;
148 
149  public:
150  Plugin_Function_Factory(const std::vector<std::string> &keys, std::map< std::string, std::vector<std::pair<std::string, std::string>> > &names)
151  : names(names)
152  {
153  parameters = convert_to_map(keys);
154  purpose_index["Likelihood"] = typeid(double (std::unordered_map<std::string, double> &));
155  purpose_index["LogLike"] = typeid(double (std::unordered_map<std::string, double> &));
156  purpose_index["Observable"] = typeid(std::vector<double> (std::unordered_map<std::string, double> &));
157  purpose_index["Prior"] = typeid(void (const std::vector<double> &, std::unordered_map<std::string, double> &));
158  }
159 
160  void * operator() (const std::string &purpose) const
161  {
162  auto it = names.find(purpose);
163  std::string purpose_i = purpose;
164 
165  if (purpose_index.find(purpose) == purpose_index.end())
166  {
167  purpose_i = "Likelihood";
168  }
169 
170  if (it == names.end())
171  {
172  return NULL;
173  }
174  else if (it->second.size() == 1)
175  {
176  return __functions__.at(purpose_index.at(purpose_i))(parameters.at(it->second.at(0).second), it->second.at(0).first);
177  }
178  else if (it->second.size() > 1)
179  {
180  return __multi_functions__.at(purpose_index.at(purpose_i))(parameters, it->second);
181  }
182  else
183  {
184  return NULL;
185  }
186  }
187 
189  {
190  }
191  };
192  }
193 }
194 
195 #endif
DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry DecayTable::Entry double
Interface details for scanner plugins.
Plugin_Function_Factory(const std::vector< std::string > &keys, std::map< std::string, std::vector< std::pair< std::string, std::string >> > &names)
Variadic utilty functions.
Factory class to make objectives using objective plugins.
Helper functions for dealing with POSIX signals.
std::map< std::string, std::vector< std::string > > parameters
std::unordered_map< std::string, Gambit::type_index > purpose_index
Pure Base class of a plugin Factory function.
void attempt_soft_shutdown()
Perform soft shutdown if processes can be synchronised.
std::map< std::string, std::vector< std::pair< std::string, std::string > > > names
Declarations for the YAML options class.
GAMBIT signal handling functions.
Scanner_Plugin_Function(const std::vector< std::string > &params, const std::string &name)
std::unordered_map< type_index, multi_func_type * > __multi_functions__
std::vector< Scanner_Plugin_Function< ret(args...)> > functions
std::unordered_map< type_index, func_type *, Gambit::type_hasher, Gambit::type_equal_to > __functions__
EXPORT_SYMBOLS SignalData & signaldata()
Retrieve global instance of signal handler options struct.
std::map< std::string, std::vector< std::string > > convert_to_map(const std::vector< std::string > &vec)
Turns a vector with enters [model::parameter, ...] into a map with [{model, parameter}, ...].
Utility Functions for the Gambit Scanner.
void * multi_func_type(const std::map< std::string, std::vector< std::string >> &, const std::vector< std::pair< std::string, std::string >> &)
Scanner method implementations.
Interface for a ScannerBit plugin.
Generic function base used by the scanner. Can be Likelihood, observables, etc.
Multi_Scanner_Plugin_Function(const std::map< std::string, std::vector< std::string > > &params, const std::vector< std::pair< std::string, std::string >> &names)
hb_ModelParameters void
declaration for scanner module
LOAD_MULTI_FUNC_TEMPLATE(Multi_Scanner_Plugin_Function, double(std::unordered_map< std::string, double > &))
LOAD_FUNC_TEMPLATE(Scanner_Plugin_Function, double(std::unordered_map< std::string, double > &))
TODO: see if we can use this one:
Definition: Analysis.hpp:33