gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-252-gf9a3f78
a Global And Modular Bsm Inference Tool
basebaseprinter.hpp
Go to the documentation of this file.
1 // GAMBIT: Global and Modular BSM Inference Tool
2 // *********************************************
31 
32 #ifndef __base_base_printer_hpp__
33 #define __base_base_printer_hpp__
34 
35 // Standard libraries
36 #include <string>
37 #include <sstream>
38 #include <vector>
39 
40 // Boost
41 #include <boost/preprocessor/seq/for_each.hpp>
42 
43 // Gambit
48 
49 namespace Gambit
50 {
51  // Forward declarations needed for some of the _print/_retrieve functions
53  typedef std::map<std::string,double> map_str_dbl; // can't have commas in macro input
54 
55 
56  namespace Printers
57  {
58  // Convenienece typedefs for printers
59  typedef unsigned int uint;
60  typedef unsigned long int ulong;
61  typedef long long int longlong;
62  typedef unsigned long long int ulonglong;
63 
66  template<class T>
67  std::size_t getTypeID(void)
68  {
69  std::ostringstream err;
70  err << "getTypeID failed! No typeID known for requested type! (with compiler name: "<<typeid(T).name()<<")";
71  printer_error().raise(LOCAL_INFO,err.str());
72  return 0;
73  }
74 
76  {
77  private:
78  int rank;
79  std::set<std::string> print_list; // List of names of data that may be printed (i.e. with printme=true)
80  bool resume; // Flag to query to determine if printer is appending points to existing output
81  bool printUnitcube; // Flag whether unitCubeParameters should be printed.
82 
83  public:
84  BaseBasePrinter(): rank(0), printUnitcube(false), printer_enabled(true), printer_cooldown(-1) {}
85  virtual ~BaseBasePrinter() {}
87  //virtual void flush() {}; // TODO: needed?
88 
90  virtual void reset(bool force=false) = 0;
91 
95  virtual void flush() = 0;
96 
98  int getRank() {return rank;}
99  void setRank(int r) {rank = r;}
100 
101  // Retrieve and set the state of the 'printUnitcube' flag
102  bool& get_printUnitcube() { return printUnitcube; }
103  void set_printUnitcube(const bool& rflag) { printUnitcube = rflag; }
104 
107  std::set<std::string> getPrintList() {return print_list;}
108  void setPrintList(const std::set<std::string>& in) {print_list = in;}
109  void addToPrintList(const std::string& in) {print_list.insert(in);}
110 
111  // Get options required to construct a reader object that can read
112  // the previous output of this printer.
113  virtual Options resume_reader_options() = 0;
114 
115  bool get_resume() { return resume; }
116  void set_resume(bool rflag) { resume = rflag; }
117 
119  virtual void finalise(bool abnormal=false) = 0;
120 
123  void disable(int n=-1)
124  {
125  if(printer_enabled)
126  printer_cooldown = n;
127  printer_enabled = false;
128  }
129 
132  void enable() { printer_enabled = true; printer_cooldown = 0;}
133 
134  // Printer dispatch function. If a virtual function override exists for
135  // the print type, info is passed on, otherwise the function call is resolved
136  // to a default function which raises an informative runtime error explaining
137  // that the type is not printable.
138  template<typename T>
139  void print(T const& in, const std::string& label,
140  const int vertexID, const uint rank,
141  const ulong pointID)
142  {
143  if(printer_enabled) _print(in, label, vertexID, rank, pointID);
144  if(printer_cooldown > 0) printer_cooldown--; // if there's a cooldown, reduce it afer printing
145  if(!printer_cooldown) printer_enabled = true; // if cooldown has ended, re-enable printer
146  }
147 
148  // Overload which automatically determines a unique ID code
149  // based on the label.
150  template<typename T>
151  void print(T const& in, const std::string& label,
152  const uint rank,
153  const ulong pointID)
154  {
155  if(printer_enabled) _print(in, label, rank, pointID);
156  if(printer_cooldown > 0) printer_cooldown--; // if there's a cooldown, reduce it afer printing
157  if(!printer_cooldown) printer_enabled = true; // if cooldown has ended, re-enable printer
158  }
159 
160  protected:
163 
166 
170  template<typename T>
171  void _print(T const&, const std::string& label,
172  const int vertexID, const uint,
173  const ulong)
174  {
175  std::ostringstream err;
176 
177  err << "Attempted to print a functor whose return type "
178  << "is not registered as being printable. "
179  << "If you really want to print this functor, you must "
180  << "add its return type to the PRINTABLE_TYPES sequence "
181  << "in \"gambit/Elements/printable_types.hpp\". You will then have "
182  << "to define a print function for it in whatever printer "
183  << "you are using (see documentation for GAMBIT printers)."
184  << "\n Available info for this print attempt..."
185  << "\n Label : " << label
186  << "\n vertexID : " << vertexID
187  << "\n Type : " << STRINGIFY(T);
188  printer_error().raise(LOCAL_INFO,err.str());
189  }
190 
192  template<typename T>
193  void _print(T const& in, const std::string& label,
194  const uint rank,
195  const ulong pointID)
196  {
197  _print(in,label,rank,pointID);
198  }
199 
200 
201  // Virtual print methods for base printer classes
202  #define VPRINT(r,data,elem) \
203  virtual void _print(elem const&, const std::string& label, \
204  const int vertexID, const uint /*rank*/, \
205  const ulong /*pointID*/) \
206  { \
207  std::ostringstream err; \
208  \
209  err << "No print function override has been " \
210  << "\ndefined for this type (for whatever printer" \
211  << "\nclass the current printer comes from)" \
212  << "\n Dumping available info..." \
213  << "\n Label : " << label \
214  << "\n vertexID : " << vertexID \
215  << "\n Type : " << STRINGIFY(elem); \
216  printer_error().raise(LOCAL_INFO,err.str()); \
217  } \
218  \
219  /* version that assigns vertexID automatically */ \
220  virtual void _print(elem const& in, const std::string& label, \
221  const uint rank, \
222  const ulong pointID) \
223  { \
224  _print(in,label,get_param_id(label),rank,pointID); \
225  }
226 
227  #define ADD_VIRTUAL_PRINTS(TYPES) BOOST_PP_SEQ_FOR_EACH(VPRINT, , TYPES)
228 
229  // Add the base virtual functions for registered printable and
230  // retrievable types, to be overloaded in each printer.
232 
233  };
234 
255 
276 
278  {
279  public:
280  virtual ~BaseBaseReader() {};
281 
282  virtual void reset() = 0; // Reset 'read head' position to first entry
283  virtual ulong get_dataset_length() = 0; // Get length of input dataset (used e.g. by postprocessor to divide post-processing workload via MPI)
284  virtual PPIDpair get_current_point() = 0; // Get current rank/ptID pair (i.e. whatever get_next_point() last output)
285  virtual ulong get_current_index() = 0; // Get a linear index which corresponds to the current rank/ptID pair in the iterative sense
286  virtual PPIDpair get_next_point() = 0; // Get next rank/ptID pair in data file
287  virtual bool eoi() = 0; // Check if 'current point' is past the end of the data file (and thus invalid!)
288 
306  template<typename T>
307  bool retrieve(T& out, const std::string& label, const uint rank, const ulong pointID)
308  {
309  return _retrieve(out, label, rank, pointID);
310  }
311 
313  template<typename T>
314  bool retrieve(T& out, const std::string& label)
315  {
316  bool valid;
317  PPIDpair pt = get_current_point();
318  // Need to check if the current rank/pointID pair is valid before trying to retrieve from it
319  if(pt==nullpoint)
320  {
321  valid = false;
322  }
323  else
324  {
325  valid = retrieve(out, label, pt.rank, pt.pointID);
326  }
327  return valid;
328  }
329 
331  bool retrieve_and_print(const std::string& label, BaseBasePrinter& printer, const uint rank, const ulong pointID)
332  {
334  return retrieve_and_print(label, label, printer, rank, pointID);
335  }
336 
339  virtual bool retrieve_and_print(const std::string& in_label, const std::string& out_label, BaseBasePrinter& printer, const uint rank, const ulong pointID) = 0;
340 
342  bool retrieve_and_print(const std::string& label, BaseBasePrinter& printer)
343  {
345  return retrieve_and_print(label, label, printer);
346  }
347 
349  bool retrieve_and_print(const std::string& in_label, const std::string& out_label, BaseBasePrinter& printer)
350  {
351  bool valid;
352  PPIDpair pt = get_current_point();
353  // Need to check if the current rank/pointID pair is valid before trying to retrieve from it
354  if(pt==nullpoint)
355  {
356  valid = false;
357  }
358  else
359  {
360  valid = retrieve_and_print(in_label, out_label, printer, pt.rank, pt.pointID);
361  }
362  return valid;
363  }
364 
370  virtual std::size_t get_type(const std::string& label) = 0;
371 
374  virtual std::set<std::string> get_all_labels() = 0;
375 
376  protected:
380  template<typename T>
381  bool _retrieve(T&, const std::string& label, const uint, const ulong)
382  {
383  std::ostringstream err;
384 
385  err << "Attempted to retrieve a print result as an unregistered type!"
386  << "If you really want to retrieve a result as this type, you must "
387  << "add the type to the RETRIEVABLE_TYPES sequence "
388  << "in \"gambit/Elements/printable_types.hpp\". You will then have "
389  << "to define a retrieve function for it in whatever printer "
390  << "you are using (see documentation for GAMBIT printers)."
391  << "\n Available info for this retrieve attempt..."
392  << "\n Label : " << label
393  << "\n Type : " << STRINGIFY(T);
394  printer_error().raise(LOCAL_INFO,err.str());
395  return false;
396  }
397 
398  // Virtual retrieval methods for base printer classes
399  #define VRETRIEVE(r,data,elem) \
400  virtual bool _retrieve(elem& /*output*/, \
401  const std::string& label, \
402  const uint /*rank*/, \
403  const ulong /*pointID*/ \
404  ) \
405  { \
406  std::ostringstream err; \
407  \
408  err << "No retrieve function override has been " \
409  << "\ndefined for this retrieval type (for whatever printer" \
410  << "\nclass the current printer comes from)" \
411  << "\n Dumping available info..." \
412  << "\n Label : " << label \
413  << "\n Type : " << STRINGIFY(elem); \
414  printer_warning().raise(LOCAL_INFO,err.str()); \
415  return false; \
416  }
417 
418  #define ADD_VIRTUAL_RETRIEVALS(TYPES) BOOST_PP_SEQ_FOR_EACH(VRETRIEVE, , TYPES)
419 
420  // Add the base virtual functions for registered printable and
421  // retrievable types, to be overloaded in each printer.
423  };
424 
425  } //end namespace Printers
426 } // end namespace Gambit
427 
428 
429 #endif //ifndef __base_base_printer_hpp__
#define STRINGIFY(X)
Definition: stringify.hpp:29
void print(T const &in, const std::string &label, const int vertexID, const uint rank, const ulong pointID)
bool _retrieve(T &, const std::string &label, const uint, const ulong)
Default _retrieve function.
unsigned long long int pointID
EXPORT_SYMBOLS error & printer_error()
Printer errors.
bool retrieve_and_print(const std::string &label, BaseBasePrinter &printer)
Overload for &#39;retrieve_and_print&#39; that uses the current point as the input for rank/pointID.
void setPrintList(const std::set< std::string > &in)
bool retrieve(T &out, const std::string &label)
Overload for &#39;retrieve&#39; that uses the current point as the input for rank/pointID.
#define LOCAL_INFO
Definition: local_info.hpp:34
#define SCANNER_RETRIEVABLE_TYPES
Definitions of new MPI datatypes needed by printers.
unsigned long long int ulonglong
bool printer_enabled
Flag to check if print functions are enabled or disabled.
virtual Options resume_reader_options()=0
#define ADD_VIRTUAL_RETRIEVALS(TYPES)
std::set< std::string > getPrintList()
Retrieve/Set print list for this printer Required by e.g.
void enable()
"Turn on" printer; print calls will work as normal.
void _print(T const &in, const std::string &label, const uint rank, const ulong pointID)
Same for overloaded function.
void print(T const &in, const std::string &label, const uint rank, const ulong pointID)
int getRank()
Retrieve/Set MPI rank (setting is useful for e.g. the postprocessor to re-print points from other ran...
#define ADD_VIRTUAL_PRINTS(TYPES)
virtual void reset(bool force=false)=0
Function to signal to the printer to write buffer contents to disk.
void disable(int n=-1)
"Turn off" printer; i.e.
void addToPrintList(const std::string &in)
Printers::BaseBasePrinter printer
Type of the printer objects.
START_MODEL dNur_CMB r
bool retrieve(T &out, const std::string &label, const uint rank, const ulong pointID)
Printer-retrieve dispatch function.
Tools for accessing printers.
#define SCANNER_PRINTABLE_TYPES
unsigned long int ulong
Exception objects required for standalone compilation.
void _print(T const &, const std::string &label, const int vertexID, const uint, const ulong)
Default _print function.
Preprocessor sequence of functor types that should be allowed to print when using ScannerBit in stand...
std::map< std::string, double > map_str_dbl
Shorthand for a string-to-double map.
void set_printUnitcube(const bool &rflag)
EXPORT_SYMBOLS const PPIDpair nullpoint
Define &#39;nullpoint&#39; const.
std::set< std::string > print_list
std::size_t getTypeID(void)
Helper template functions to retrieve type IDs for a type.
int printer_cooldown
Counter for printer cooldown. If non-zero printer can be disabled for a fixed number of print calls...
bool retrieve_and_print(const std::string &label, BaseBasePrinter &printer, const uint rank, const ulong pointID)
Retrieve and directly print data to new output.
virtual void flush()=0
Signal printer to flush data in buffers to disk Printers should do this automatically as needed...
pointID / process number pair Used to identify a single parameter space point
TODO: see if we can use this one:
Definition: Analysis.hpp:33
A small wrapper object for &#39;options&#39; nodes.
virtual void finalise(bool abnormal=false)=0
Signal printer that scan is finished, and final output needs to be performed.
long long int longlong
bool retrieve_and_print(const std::string &in_label, const std::string &out_label, BaseBasePrinter &printer)
As above, but allows for different input/output labels.