EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_run_control.h
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ run control headers
5 # Copyright (C) 2015 National Research Council Canada
6 #
7 # This file is part of EGSnrc.
8 #
9 # EGSnrc is free software: you can redistribute it and/or modify it under
10 # the terms of the GNU Affero General Public License as published by the
11 # Free Software Foundation, either version 3 of the License, or (at your
12 # option) any later version.
13 #
14 # EGSnrc is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
17 # more details.
18 #
19 # You should have received a copy of the GNU Affero General Public License
20 # along with EGSnrc. If not, see <http://www.gnu.org/licenses/>.
21 #
22 ###############################################################################
23 #
24 # Author: Iwan Kawrakow, 2005
25 #
26 # Contributors: Frederic Tessier
27 # Ernesto Mainegra-Hing
28 # Blake Walters
29 # Reid Townson
30 #
31 ###############################################################################
32 */
33 
34 
40 #ifndef EGS_RUN_CONTROL_
41 #define EGS_RUN_CONTROL_
42 
43 #include "egs_libconfig.h"
44 #include "egs_timer.h"
45 #include "egs_input_struct.h"
46 
47 #include <iostream>
48 using namespace std;
49 
50 class EGS_Application;
51 class EGS_Input;
52 
53 inline void addRunControlBlock(shared_ptr<EGS_InputStruct> blockPtr) {
54  shared_ptr<EGS_BlockInput> runBlock = blockPtr->addBlockInput("run control");
55  runBlock->addSingleInput("ncase", true, "The number of histories to simulate.");
56  runBlock->addSingleInput("nbatch", false, "The number of batches to divide the simulation into. After each batch, a checkpoint is created to allow resuming the simulation. For simulations with very large intermediate data (like a high resolution dose grid), reducing nbatch to 1 can speed up the simulation. Defaults to 10.");
57  runBlock->addSingleInput("nchunk", false, "For parallel runs, each job splits its portion of the simulation into nchunk serial chunks, and each of those chunks is divided into nbatch batches. After each chunk, the parallel job grabs a new portion of the simulation to work on. Defaults to 10.");
58  runBlock->addSingleInput("max cpu hours allowed", false, "The number hours after which the simulation will be haulted. Defaults to -1, which is no limit.");
59  runBlock->addSingleInput("statistical accuracy sought", false, "The statistical uncertainty for a particular quantity of interest, below which the simulation will be haulted. Note that the quantity must be defined by the application (e.g. the cavity dose in egs_chamber), and in general is undefined (e.g. this input does nothing for egs_app).");
60  runBlock->addSingleInput("geometry error limit", false, "The number of geometry errors that will be allowed to occur, before haulting the simulation. Defaults to 0.");
61  runBlock->addSingleInput("calculation", false, "The calculation type: first (default, runs a new simulation), resume (continue a crashed simulation), analyze (prints results), combine (combines results from a parallel run). Defaults to 'first'.", {"first", "resume", "analyze", "combine"});
62 }
63 
64 inline string addRunControlExample() {
65  string example = {
66  R"(
67 :start run control:
68  ncase = 1e4
69  nbatch = 10 #[optional]
70  nchunk = 10 #[optional]
71  statistical accuracy sought = 5 #[optional]
72  max cpu hours allowed = 0.5 #[optional]
73  calculation = first #[optional]
74  geometry error limit = 2 #[optional]
75 :stop run control:
76 )"};
77  return example;
78 }
79 
125 
126 public:
127 
135 
137  virtual ~EGS_RunControl();
138 
140  void setNcase(EGS_I64 n) {
141  if (n > 0) {
142  ncase = n;
143  }
144  };
145 
147  void setNbatch(int n) {
148  if (n > 0) {
149  nbatch = n;
150  }
151  };
152 
154  void setMaxTime(EGS_Float t) {
155  maxt = t;
156  };
157 
159  void setRequiredUncertainty(EGS_Float a) {
160  accu = a;
161  };
162 
164  EGS_I64 getNcase() const {
165  return ncase;
166  };
167 
169  int getNbatch() const {
170  return nbatch;
171  };
172 
174  int getNchunk() const {
175  return nchunk;
176  };
177 
185  virtual int startSimulation();
186 
196  virtual EGS_I64 getNextChunk() {
197  return getNcase() - ndone;
198  };
199 
210  virtual int finishSimulation();
211 
218  virtual bool startBatch(int,EGS_I64);
219 
226  virtual bool finishBatch();
227 
228  virtual void describeRCO();
229  virtual bool storeState(ostream &data);
230  virtual bool setState(istream &data);
231  virtual bool addState(istream &data);
232  virtual void resetCounter();
233  virtual bool getCombinedResult(double &, double &) const {
234  return false;
235  };
236 
237  virtual EGS_I64 getNdone() const {
238  return ndone;
239  };
240 
241  virtual void setNdone(EGS_I64 Ndone) {
242  ndone = Ndone;
243  };
244 
245  virtual void incrementNdone() {
246  ++ndone;
247  };
248 
249  virtual EGS_Float getCPUTime() const {
250  return cpu_time+previous_cpu_time;
251  };
252 
254  enum RCOType {
255  simple,
256  uniform,
257  balanced
258  };
259 
260  static EGS_RunControl *getRunControlObject(EGS_Application *);
261 
262  int geomErrorCount, geomErrorMax;
263 
264 protected:
265 
266  EGS_Application *app;
267  EGS_Input *input;
268 
269  EGS_I64 ncase; // number of histories.
270  EGS_I64 ndone; // histories done so far.
271  EGS_Float maxt; // maximum time to run in hours.
272  EGS_Float accu; // statistical uncertainty sought.
273  int nbatch; // number of batches.
274  int resume;// =0 => fresh calculation
275  // =1 => resume calculation
276  // =2 => analyze results
277  // =3 => combine parallel run
278  int nchunk; // number of simulation "chunks"
279 
280  RCOType rco_type;
281 
282  EGS_Timer timer;
283  EGS_Float cpu_time;
284  EGS_Float previous_cpu_time;
285 
286 };
287 
288 class EGS_FileLocking;
289 
308 
309 public:
310 
311  EGS_JCFControl(EGS_Application *, int Nbuf=1024);
312  ~EGS_JCFControl();
313  void setNchunkForParallel(int n) {
314  if (n > 0) {
315  nchunk = n;
316  }
317  };
318  int startSimulation();
319  EGS_I64 getNextChunk();
320  int finishSimulation();
321  bool getCombinedResult(double &, double &) const;
322 
323 protected:
324 
325  EGS_I64 nleft, ntot;
326  double tsum, tsum2, tcount, norm;
327  double last_sum, last_sum2, last_count;
328  unsigned long start_time;
329  int njob;
330  int npar;
331  int ipar;
332  int ifirst;
333  bool first_time;
334  bool removed_jcf;
335  int nbuf;
336  char *buf;
337 
338  EGS_FileLocking *p;
339 
340  bool createControlFile();
341  bool openControlFile();
342  bool closeControlFile();
343  bool lockControlFile();
344  bool unlockControlFile();
345  bool rewindControlFile();
346  bool readControlFile();
347  bool writeControlFile();
348  virtual bool writeControlString();
349  virtual bool readControlString();
350 
351 };
352 
396 
397 public:
398 
400  ~EGS_UniformRunControl() {};
401 
402  void describeRCO();
403 
404  int startSimulation();
405 
414  int finishSimulation();
415 
416 protected:
417 
418  int milliseconds; // time interval for checking
419  // if all jobs finished (default 1000 ms)
420 
421  int check_intervals;// Number of intervals to check
422  // if all jobs done (default 5)
423 
424  int njob;
425  int npar;
426  int ipar;
427  int ifirst;
428  bool check_egsdat;// If true, and a 'watcher' job, produce intermediate results
429  bool watcher_job; // If true, job is a 'watcher'
430 };
431 
432 #endif
Base class for advanced EGSnrc C++ applications.
A class for storing information in a tree-like structure of key-value pairs. This class is used throu...
Definition: egs_input.h:182
A 'job control file' (JCF) RCO.
A simple run control object for advanced EGSnrc C++ applications.
RCOType
Define RCO types.
A simple class for measuring CPU time.
Definition: egs_timer.h:57
A job control object for homogeneous computing environments (HCE).
The input struct header file.
Defines the EGS_EXPORT and EGS_LOCAL macros.
#define EGS_EXPORT
Export symbols from the egspp library.
Definition: egs_libconfig.h:90
EGS_Timer class header file.