gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-252-gf9a3f78
a Global And Modular Bsm Inference Tool
VertexBufferBase.hpp
Go to the documentation of this file.
1 // GAMBIT: Global and Modular BSM Inference Tool
2 // *********************************************
20 
21 #ifndef __VertexBufferBase_hpp__
22 #define __VertexBufferBase_hpp__
23 
24 #include <sstream>
25 #include <iostream>
27 
28 namespace Gambit {
29 
30  namespace Printers {
31 
34  {
35  private:
36  // flag to indicate whether an append or skip_append has been done for a given point
37  bool donethispoint = false;
38 
39  // Metadata
40  std::string label;
41  int vertexID;
42  uint index; // discriminator in case of multiple output streams from one vertex
43 
44  // buffer index to which "append" is targeted.
45  unsigned int head_position = 0;
46 
49 
51  bool silenced;
52 
55  bool resume;
56 
60  bool MPImode;
61 
63  char access;
64 
65  protected:
67  bool sync_buffer_full = false;
68  bool sync_buffer_empty = true;
69 
70  public:
72  : label("None (Bug!)")
73  , vertexID(0)
74  , index(0)
75  , synchronised(true)
76  , silenced(false)
77  , resume(false)
78  , MPImode(false)
79  {
80  #ifdef HDF5_DEBUG
81  std::cout<<"Default constructing buffer name='"<<label<<"', synchronised="<<synchronised<<std::endl;
82  #endif
83  }
84 
85  VertexBufferBase(const std::string& l, const int vID, const uint i, const bool sync, const bool sil, const bool r, const bool mode, const char a)
86  : label(l)
87  , vertexID(vID)
88  , index(i)
89  , synchronised(sync)
90  , silenced(sil)
91  , resume(r)
92  , MPImode(mode)
93  , access(a)
94  {
95  #ifdef HDF5_DEBUG
96  std::cout<<"Constructing buffer name='"<<label<<"', synchronised="<<synchronised<<std::endl;
97  #endif
98  }
99 
101  {
102  #ifdef HDF5_DEBUG
103  std::cout<<"Destructing buffer name='"<<label<<"'"<<std::endl;
104  #endif
105  }
106 
107  // Metadata getters
108  int get_vertexID() const { return vertexID; }
109  uint get_index() const { return index; }
110  std::string get_label() const { return label; }
111 
113  char access_mode() const { return access; }
114  bool sync_buffer_is_full() const { return sync_buffer_full; }
115  bool sync_buffer_is_empty() const { return sync_buffer_empty; }
116  bool is_synchronised() const { return synchronised; }
117  bool is_silenced() const { return silenced; }
118  bool resume_mode() const { return resume; }
119  bool MPI_mode() const { return MPImode; }
120  unsigned int get_head_position() const { return head_position; }
122 
125  void MPImode_only(std::string local_info)
126  {
127  if(not MPImode)
128  {
129  std::ostringstream errmsg;
130  errmsg << "Error! Attempted to use forbidden function in buffer "<<this->get_label()<<". This function is flagged as usable only if MPImode=true, however currently it is the case that MPImode=false.";
131  printer_error().raise(local_info, errmsg.str());
132  }
133  }
134 
135  // Get the current head position in the output dataset
136  virtual unsigned long dset_head_pos() = 0;
137 
138  // Print to std::cout a report on the sync status of this buffer
139  virtual void sync_report() = 0;
140 
141  // Trigger MPI send of sync buffer to master node, or write to disk
142  virtual void flush() = 0;
143 
144  // Trigger MPI send of random-access buffer queue, or write to disk
145  // Have to provide a map from PPIDpairs to dataset indices, so that buffers
146  // know where in the output datasets they are supposed to write.
147  virtual void RA_flush(const std::map<PPIDpair, ulong>& PPID_to_dsetindex) = 0;
148 
149  // Finalise writing to underlying output. Do not do any more writing after this!
150  virtual void finalise() = 0;
151 
152  // For debugging purposes only
153  virtual std::size_t postponed_RA_queue_length() = 0;
154  virtual uint get_RA_queue_length() = 0;
155 
156  // // Perform write to disk of sync buffer
157  // virtual void write_to_disk() = 0;
158 
159  // // Perform write to disk of random-access buffer
160  // virtual void RA_write_to_disk() = 0;
161 
162  // Resets buffer and signals to printer to empty out the contents of the output
163  // dataset in preparation of new writing.
164  virtual void reset(bool force=false) = 0;
165 
166  // Needed to externally inform buffer of a skipped iteration (when no data to write)
167  virtual void skip_append() = 0;
168 
173  virtual void N_skip_append(ulong N) = 0;
174 
175  // Needed for checking that dataset sizes on disk are consistent
176  virtual ulong get_dataset_length() = 0;
177 
178  // #ifdef WITH_MPI
179  // // Probe for a sync buffer MPI message from a process
180  // virtual bool probe_sync_mpi_message(uint,int*) = 0;
181 
182  // // Probe for a RA buffer MPI message from a process
183  // virtual bool probe_RA_mpi_message(uint) = 0;
184 
185  // // Retrieve sync buffer data from an MPI message from a known process rank
186  // // Should only be triggered if a valid message is known to exist to be retrieved!
187  // virtual void get_sync_mpi_message(uint,int) = 0;
188 
189  // // Retrieve RA buffer data from an MPI message from a known process rank
190  // // Should only be triggered if a valid message is known to exist to be retrieved!
191  // virtual void get_RA_mpi_message(uint, const std::map<PPIDpair, ulong>&) = 0;
192 
193  // // Update MPI tags with valid values
194  // virtual void update_myTags(uint) = 0;
195  // #endif
196 
197  // getter for donethispoint
198  bool donepoint() {return donethispoint;}
199 
200  // setter for donethispoint
201  void set_donepoint(bool flag) {donethispoint=flag;}
202 
203  // Move buffer write head to next position
205  {
206  head_position++;
207  //std::cout<<"Advanced head of buffer "<<get_label()<<" to pos. "<<head_position<<std::endl;
208  }
209 
210  // Force move buffer write head to specified position
211  // (intended for initialising "late-comer" buffers)
212  void fast_forward(long target_pos)
213  {
214  long needed_steps = target_pos - dset_head_pos();
215  if(needed_steps<0)
216  {
217  std::ostringstream errmsg;
218  errmsg << "Error while attempted to fast_forward VertexBuffer '"<<get_label()<<"'! Number of iterations required to teach target position ("<<target_pos<<") from current position ("<<dset_head_pos()<<") is negative, i.e. we would need to move backwards. This function does not allow reverse movement of the buffer write head.";
219  printer_error().raise(LOCAL_INFO, errmsg.str());
220  }
221  for(long i=0; i<needed_steps; i++)
222  {
223  skip_append();
224  if(sync_buffer_is_full()) flush();
225  }
226  }
227 
228  // Rewind buffer head to start of buffer
229  void reset_head() { head_position = 0; }
230 
231  // Error thrower for when append is attempted with point already set to "done"
233  {
234  if(donethispoint)
235  {
236  std::ostringstream errmsg;
237  errmsg << "Error! VertexBuffer set to 'done'! Append may have been attempted without moving the buffer write head forward (.";
238  printer_error().raise(LOCAL_INFO, errmsg.str());
239  }
240  }
241 
242  // Ensure dataset "write head" (i.e. next append) is prepared to
243  // write to the supplied absolute dataset index (e.g. by inserting
244  // blank entries if need)
245  // HDF5 version (at least) cannot move backwards, and expects to
246  // be moved ahead only one index at a time. But there is some freedom
247  // for different behaviour by other writers.
248  virtual void synchronise_output_to_position(const unsigned long i) = 0;
249  };
250 
251  }
252 }
253 
254 #endif
255 
bool resume
flag to indicate that GAMBIT is attempting to resume a run, so we need to hook into existing output s...
EXPORT_SYMBOLS error & printer_error()
Printer errors.
#define LOCAL_INFO
Definition: local_info.hpp:34
virtual void RA_flush(const std::map< PPIDpair, ulong > &PPID_to_dsetindex)=0
bool silenced
flag to disable any writing (turns this into a null buffer)
virtual std::size_t postponed_RA_queue_length()=0
bool synchronised
flag to trigger synchronised buffer writing
virtual ulong get_dataset_length()=0
START_MODEL dNur_CMB r
virtual void reset(bool force=false)=0
void MPImode_only(std::string local_info)
MPI mode error Put in functions which should not run if MPImode=false.
char access
flag to indicate the buffer access mode (i.e. read/write)
unsigned long int ulong
virtual void synchronise_output_to_position(const unsigned long i)=0
virtual unsigned long dset_head_pos()=0
VertexBuffer abstract interface base class.
Exception objects required for standalone compilation.
virtual void N_skip_append(ulong N)=0
Skip several/many positions NOTE! This is meant for initialising new buffers to the correct position...
bool MPImode
flag to indicate whether full buffers should be written to disk, or whether they should be send to th...
bool sync_buffer_full
flag to indicate if the sync buffer is full (and ready for sending/dumping)
TODO: see if we can use this one:
Definition: Analysis.hpp:33
char access_mode() const
Buffer status getters.
VertexBufferBase(const std::string &l, const int vID, const uint i, const bool sync, const bool sil, const bool r, const bool mode, const char a)