gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-252-gf9a3f78
a Global And Modular Bsm Inference Tool
plugin_loader.hpp
Go to the documentation of this file.
1 // GAMBIT: Global and Modular BSM Inference Tool
2 // *********************************************
25 
26 #ifndef __PLUGIN_LOADER_HPP
27 #define __PLUGIN_LOADER_HPP
28 
29 #include <vector>
30 #include <unordered_map>
31 #include <string>
32 #include <type_traits>
33 
38 #include "gambit/cmake/cmake_variables.hpp"
42 
43 namespace Gambit
44 {
46  namespace GMPI { class Comm; }
47 
48  namespace Scanner
49  {
50 
51  namespace Plugins
52  {
55  {
56  std::string plugin;
57  std::string version;
58  std::string path;
59 
60  Proto_Plugin_Details() : plugin(""), version(""), path("") {}
61  };
62 
65  {
69  YAML::Node flags;
70  YAML::Node node;
71 
72  Plugin_Interface_Details(Plugin_Details &details, printer_interface *printer, Priors::BasePrior *prior, const YAML::Node &node)
73  : details(details), printer(printer), prior(prior), flags(details.flags), node(node) {}
74  };
75 
78  {
79  private:
80  std::string path;
81  std::vector<Plugin_Details> plugins;
82  std::map<std::string, std::map<std::string, std::vector<Plugin_Details>>> plugin_map;
83  std::vector<Plugin_Details> excluded_plugins;
84  std::map<std::string, std::map<std::string, std::vector<Plugin_Details>>> excluded_plugin_map;
85  std::vector<Plugin_Details> total_plugins;
86  std::map<std::string, std::map<std::string, std::vector<Plugin_Details>>> total_plugin_map;
87  std::vector<Plugin_Details> loadExcluded(const std::string &);
88  void process(const std::string &, const std::string &, const std::string &, std::vector<Plugin_Details>&);
89 
90  public:
91  Plugin_Loader();
92  const std::vector<Plugin_Details> &getPluginsVec() const {return total_plugins;}
93  const std::map<std::string, std::map<std::string, std::vector<Plugin_Details>>> &getPluginsMap() const {return total_plugin_map;}
94  void loadLibrary (const std::string &, const std::string & = "");
95  std::vector<std::string> print_plugin_names(const std::string & = "") const;
96  std::string print_all (const std::string &plug_type = "") const;
97  int print_all_to_screen (const std::string &plug_type = "") const;
98  std::string print_plugin (const std::string &) const;
99  std::string print_plugin (const std::string &, const std::string &) const;
100  std::vector<std::string> list_prior_groups() const;
101  std::string print_priors (const std::string &prior_group = "priors") const;
102  int print_plugin_to_screen (const std::string &) const;
103  int print_plugin_to_screen (const std::string &, const std::string &) const;
104  int print_plugin_to_screen (const std::vector<std::string> &) const;
105  Plugin_Details &find (const std::string &, const std::string &, const std::string &, const std::string &) const;
106  };
107 
110  {
111  public:
112  virtual void print(std::ofstream &) = 0;
114  };
115 
117  template <typename T>
119  {
120  private:
121  T *data;
122 
123  public:
124  __plugin_resume__(T &data) : data(&data) {}
125 
126  void print(std::ofstream &out)
127  {
128  resume_file_output<T>(out, *data);
129  }
130 
132  };
133 
136  {
137  private:
140  std::map<std::string, std::map<std::string, Proto_Plugin_Details> > selectedPlugins;
142  std::map<std::string, std::vector<__plugin_resume_base__ *>> resume_data;
143  std::map<std::string, std::ifstream *> resume_streams;
147  std::string def_out_path;
148  int MPIrank;
149  #ifdef WITH_MPI
150  GMPI::Comm* scannerComm;
151  bool MPIdata_is_init;
152  #endif
153  bool earlyShutdownInProgress;
155 
156  inline void set_resume(std::vector<__plugin_resume_base__ *> &){}
157 
158  template<typename U, typename... T>
159  void set_resume(std::vector<__plugin_resume_base__ *> &r_data, U& param, T&... params)
160  {
161  r_data.push_back(new __plugin_resume__<typename std::decay<U>::type>(param));
162  set_resume(r_data, params...);
163  }
164 
165  inline void get_resume(std::ifstream &){}
166 
167  template<typename U, typename... T>
168  void get_resume(std::ifstream &in, U& param, T&... params)
169  {
170  resume_file_input(in, param);
171  get_resume(in, params...);
172  }
173 
174  inline size_t get_size_of(){return 0;}
175 
176  template<typename U, typename... T>
177  inline size_t get_size_of(U& param, T&... params)
178  {
179  return resume_size_of(param) + get_size_of(params...);
180  }
181 
182  public:
183  pluginInfo();
184 
186  void iniFile(const Options &);
187  void printer_prior(printer_interface &, Priors::BasePrior &);
188  bool keep_running() const {return keepRunning;}
189  void set_running(bool b){keepRunning = b;}
190  bool func_calculating() const {return funcCalculating;}
191  void set_calculating(bool b){funcCalculating = b;}
192  void set_early_shutdown_in_progress(){earlyShutdownInProgress=true;}
193  bool early_shutdown_in_progress() const {return earlyShutdownInProgress;}
194  bool resume_mode() const { return printer->resume_mode(); }
195  std::string temp_file_path() {return Gambit::Utils::ensure_path_exists(def_out_path + "/temp_files/");}
196 
197  #ifdef WITH_MPI
198  // tags for messages sent via scannerComm
199  static const int MIN_LOGL_MSG = 0;
201  void initMPIdata(GMPI::Comm* newcomm);
202  GMPI::Comm& scanComm();
203  #endif
204  int getRank() { return MPIrank; }
205 
207  template <typename... T>
208  void resume(const std::string &name, T&... data)
209  {
210  if (resume_mode())
211  {
212  if (resume_streams.find(name) == resume_streams.end())
213  {
214  std::string path = Gambit::Utils::ensure_path_exists(def_out_path + "/temp_files/" + name);
215  resume_streams[name] = new std::ifstream((path).c_str(), std::ifstream::binary);
216  }
217 
218  if (resume_streams[name]->is_open())
219  {
220  get_resume(*resume_streams[name], data...);
221  }
222  else
223  {
224  std::cerr << "Could not load resume data." << std::endl;
225  //scan_err << "Could not load resume data." << scan_end;
226  }
227  }
228 
229  set_resume(resume_data[name], data...);
230  }
231 
233  void dump();
234 
236  void dump(const std::string &);
237 
239  void save_alt_min_LogL_state() const;
240 
242  void clear_alt_min_LogL_state() const;
243 
245  bool check_alt_min_LogL_state() const;
246 
248  const Plugin_Loader &operator()() {return plugins;}
249 
251  Plugin_Interface_Details operator()(const std::string &, const std::string &);
252  ~pluginInfo();
253  };
254 
258 
259  }
260 
261  }
262 
263 }
264 
265 #endif
Abstract base class for a prior.
void print(MixMatrix A)
Helper function to print a matrix.
greatScanData data
Definition: great.cpp:38
Abstract base class for priors.
Definition: base_prior.hpp:40
Plugin_Interface_Details(Plugin_Details &details, printer_interface *printer, Priors::BasePrior *prior, const YAML::Node &node)
General small utility macros.
std::map< std::string, std::map< std::string, std::vector< Plugin_Details > > > excluded_plugin_map
container info for a specific plugin
std::map< std::string, std::map< std::string, std::vector< Plugin_Details > > > plugin_map
const std::vector< Plugin_Details > & getPluginsVec() const
const std::map< std::string, std::map< std::string, std::vector< Plugin_Details > > > & getPluginsMap() const
#define EXPORT_SYMBOLS
declaration for scanner module
void set_resume(std::vector< __plugin_resume_base__ *> &)
Container class to store plugin values for resume function.
container class for the actual plugins detected by ScannerBit
START_MODEL b
Definition: demo.hpp:235
Declarations for the YAML options class.
std::vector< Plugin_Details > total_plugins
Plugin info to be given to the interface class.
General small utility functions.
Virtual container base class to store plugin values for resume function.
std::map< std::string, std::vector< __plugin_resume_base__ * > > resume_data
std::vector< Plugin_Details > plugins
Container for all the plugin info from the inifile and Scannerbit.
Class to hold details of scanner plugins and define simple comparison operations on them...
std::map< std::string, std::map< std::string, std::vector< Plugin_Details > > > total_plugin_map
void get_resume(std::ifstream &in, U &param, T &... params)
void resume(const std::string &name, T &... data)
resume function
std::enable_if<!is_container< T >::value &&!is_pair< T >::value, size_t >::type resume_size_of(T &)
Utility Functions for the Gambit Scanner.
bool resume_mode()
Getter for "resume" mode flag.
size_t get_size_of(U &param, T &... params)
std::vector< Plugin_Details > excluded_plugins
EXPORT_SYMBOLS const str & ensure_path_exists(const str &)
Ensure that a path exists (and then return the path, for chaining purposes)
std::enable_if<!is_container< T >::value &&!is_pair< T >::value, void >::type resume_file_input(std::ifstream &in, T &param)
void set_resume(std::vector< __plugin_resume_base__ *> &r_data, U &param, T &... params)
const Plugin_Loader & operator()()
Retrieve plugin data.
std::map< std::string, std::map< std::string, Proto_Plugin_Details > > selectedPlugins
Manager class for creating printer objects.
std::map< std::string, std::ifstream * > resume_streams
EXPORT_SYMBOLS pluginInfo plugin_info
Access Functor for plugin info.
TODO: see if we can use this one:
Definition: Analysis.hpp:33
A small wrapper object for &#39;options&#39; nodes.