gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-2191-ga4742ac
a Global And Modular Bsm Inference Tool
plugin_defs.hpp
Go to the documentation of this file.
1 // GAMBIT: Global and Modular BSM Inference Tool
2 // *********************************************
21 
22 #ifndef GAMBIT_PLUGIN_DEFS_HPP
23 #define GAMBIT_PLUGIN_DEFS_HPP
24 
25 #include <vector>
26 #include <string>
27 #include <map>
28 #include <set>
29 #include <algorithm>
30 #include <typeinfo>
31 #include <unistd.h>
32 
33 #ifdef WITH_MPI
35 #include <mpi.h>
37 #endif
38 
43 
44 namespace Gambit
45 {
46 
47  namespace Scanner
48  {
50 
53  {
54  private:
55  std::string name;
56  bool resume;
57  int rank;
58  int numtasks;
59 
60  public:
61  resume_params_func(const std::string &name_in) : resume(false)
62  {
63 #ifdef WITH_MPI
64  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
65  MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
66 #else
67  rank = 0;
68  numtasks = 1;
69 #endif
70  std::stringstream ss;
71  ss << rank;
72  name = name_in + "_" + ss.str();
73  }
74 
75  std::string get_name() const { return name; }
76 
77  void set_resume_mode(const bool &mode)
78  {
79  resume = mode;
80  }
81 
82  bool resume_mode() const {return resume;}
83 
84  std::string get_temp_file_name(const std::string &temp_file)
85  {
86  std::stringstream ss;
88  ss << name;
89  ss << "_";
90  ss << temp_file;
91  ss << "_";
92  ss << rank;
93 
94  return ss.str();
95  }
96 
97  void dump()
98  {
100  }
101 
102  int Rank() const {return rank;}
103  int NumTasks() const {return numtasks;}
104 
105  template <typename... T>
106  void operator ()(T&... params)
107  {
109  }
110  };
111 
112  namespace Plugins
113  {
114  using Gambit::type_index;
115 
119  {
120  public:
121  virtual void *operator()() = 0;
122  virtual ~factoryBase() {}
123  };
124 
125  template <typename T>
126  class funcFactory : public factoryBase
127  {
128  private:
129  T *func;
130 
131  public:
132  funcFactory (T *in) : func(in) {}
133  void *operator()(){return *(void**)&func;}
135  };
136 
137  template <typename T>
138  class classFactory : public factoryBase
139  {
140  private:
141  std::vector<T *> ptrs;
142 
143  public:
144  void *operator()()
145  {
146  T *ptr = new T;
147  ptrs.push_back(ptr);
148  return (void*) ptr;
149  }
150 
152  {
153  for (auto it = ptrs.begin(), end = ptrs.end(); it != end; it++)
154  delete *it;
155  }
156  };
158 
160  struct pluginData
161  {
162  std::string name;
163  std::string type;
164  std::string version;
165  std::string tag;
166  YAML::Node node;
168  prior_interface *prior;
169  std::vector <void *> inputData;
170  std::vector <void (*)(pluginData &)> inits;
171  std::map<std::string, factoryBase *> outputFuncs;
172  std::map<type_index, void *> plugin_mains;
173  void (*deconstructor)();
174  bool loaded;
175 
176  pluginData(const std::string &name, const std::string &type, const std::string &version_in)
177  : name(name), type(type), version(version_in), deconstructor(NULL), loaded(false)
178  {
179  std::string::size_type posLast = version.find("_");
180  std::string major_version = version.substr(0, posLast);
181  std::string::size_type posMid = version.find("_", posLast + 1);
182  std::string minor_version = version.substr(posLast + 1, posMid - posLast - 1);
183  posLast = version.find("_", posMid + 1);
184  std::string patch_version = version.substr(posMid + 1, posLast - posMid - 1);
185  std::string release_version = version.substr(posLast + 1);
186  version = major_version + "." + minor_version + "." + patch_version;
187  if (release_version != "")
188  version += "-" + release_version;
189  }
190 
191  std::string print()
192  {
193  std::stringstream ss;
194  ss << "plugin name: " << name << std::endl;
195  ss << "plugin type: " << type << std::endl;
196  ss << "plugin version: " << version << std::endl;
197 
198  return ss.str();
199  }
200 
202  {
203  if (deconstructor != NULL && loaded == true)
204  deconstructor();
205 
206  for (auto it = outputFuncs.begin(), end = outputFuncs.end(); it != end; it++)
207  {
208  delete it->second;
209  }
210 
211  loaded = false;
212  }
213  };
214 
215  }
216 
217  }
218 
219 }
220 
221 #endif
std::vector< void(*)(pluginData &)> inits
Abstract base class for a prior.
Structure that holds all the data provided by plugins about themselves.
Abstract base class for priors.
Definition: base_prior.hpp:40
These classes are used by the plugins to load and save data.
Variadic utilty functions.
std::string get_temp_file_name(const std::string &temp_file)
Definition: plugin_defs.hpp:84
declaration for scanner module
class to interface with the plugin manager resume functions.
Definition: plugin_defs.hpp:52
pluginData(const std::string &name, const std::string &type, const std::string &version_in)
std::map< std::string, factoryBase * > outputFuncs
resume_params_func(const std::string &name_in)
Definition: plugin_defs.hpp:61
Priors::BasePrior prior_interface
Definition: plugin_defs.hpp:49
void resume(const std::string &name, T &... data)
resume function
void set_resume_mode(const bool &mode)
Definition: plugin_defs.hpp:77
Simple header file for turning compiler warnings back on after having included one of the begin_ignor...
hb_ModelParameters void
Pragma directives to suppress compiler warnings coming from including MPI library headers...
std::map< type_index, void * > plugin_mains
Manager class for creating printer objects.
void dump()
Dump contents for resume.
Loader singleton class for scanner plugins.
EXPORT_SYMBOLS pluginInfo plugin_info
Access Functor for plugin info.
TODO: see if we can use this one:
Definition: Analysis.hpp:33