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 #
29 ###############################################################################
30 */
31 
32 
38 #ifndef EGS_RUN_CONTROL_
39 #define EGS_RUN_CONTROL_
40 
41 #include "egs_libconfig.h"
42 #include "egs_timer.h"
43 
44 #include <iostream>
45 using namespace std;
46 
47 class EGS_Application;
48 class EGS_Input;
49 
95 
96 public:
97 
105 
107  virtual ~EGS_RunControl();
108 
110  void setNcase(EGS_I64 n) {
111  if (n > 0) {
112  ncase = n;
113  }
114  };
115 
117  void setNbatch(int n) {
118  if (n > 0) {
119  nbatch = n;
120  }
121  };
122 
124  void setMaxTime(EGS_Float t) {
125  maxt = t;
126  };
127 
129  void setRequiredUncertainty(EGS_Float a) {
130  accu = a;
131  };
132 
134  EGS_I64 getNcase() const {
135  return ncase;
136  };
137 
139  int getNbatch() const {
140  return nbatch;
141  };
142 
144  int getNchunk() const {
145  return nchunk;
146  };
147 
155  virtual int startSimulation();
156 
166  virtual EGS_I64 getNextChunk() {
167  return getNcase() - ndone;
168  };
169 
180  virtual int finishSimulation();
181 
188  virtual bool startBatch(int,EGS_I64);
189 
196  virtual bool finishBatch();
197 
198  virtual void describeRCO();
199  virtual bool storeState(ostream &data);
200  virtual bool setState(istream &data);
201  virtual bool addState(istream &data);
202  virtual void resetCounter();
203  virtual bool getCombinedResult(double &, double &) const {
204  return false;
205  };
206 
207  virtual EGS_I64 getNdone() const {
208  return ndone;
209  };
210 
211  virtual void setNdone(EGS_I64 Ndone) {
212  ndone = Ndone;
213  };
214 
215  virtual void incrementNdone() {
216  ++ndone;
217  };
218 
219  virtual EGS_Float getCPUTime() const {
220  return cpu_time+previous_cpu_time;
221  };
222 
224  enum RCOType {
227  balanced
228  };
229 
230  static EGS_RunControl *getRunControlObject(EGS_Application *);
231 
232  int geomErrorCount, geomErrorMax;
233 
234 protected:
235 
236  EGS_Application *app;
237  EGS_Input *input;
238 
239  EGS_I64 ncase; // number of histories.
240  EGS_I64 ndone; // histories done so far.
241  EGS_Float maxt; // maximum time to run in hours.
242  EGS_Float accu; // statistical uncertainty sought.
243  int nbatch; // number of batches.
244  int restart;// =0 => fresh calculation
245  // =1 => restart calculation
246  // =2 => analyze results
247  // =3 => combine parallel run
248  int nchunk; // number of simulation "chunks"
249 
251 
252  EGS_Timer timer;
253  EGS_Float cpu_time;
254  EGS_Float previous_cpu_time;
255 
256 };
257 
258 class EGS_FileLocking;
259 
278 
279 public:
280 
281  EGS_JCFControl(EGS_Application *, int Nbuf=1024);
282  ~EGS_JCFControl();
283  void setNchunkForParallel(int n) {
284  if (n > 0) {
285  nchunk = n;
286  }
287  };
288  int startSimulation();
289  EGS_I64 getNextChunk();
290  int finishSimulation();
291  bool getCombinedResult(double &, double &) const;
292 
293 protected:
294 
295  EGS_I64 nleft, ntot;
296  double tsum, tsum2, tcount, norm;
297  double last_sum, last_sum2, last_count;
298  unsigned long start_time;
299  int njob;
300  int npar;
301  int ipar;
302  int ifirst;
303  bool first_time;
304  bool removed_jcf;
305  int nbuf;
306  char *buf;
307 
308  EGS_FileLocking *p;
309 
310  bool createControlFile();
311  bool openControlFile();
312  bool closeControlFile();
313  bool lockControlFile();
314  bool unlockControlFile();
315  bool rewindControlFile();
316  bool readControlFile();
317  bool writeControlFile();
318  virtual bool writeControlString();
319  virtual bool readControlString();
320 
321 };
322 
366 
367 public:
368 
370  ~EGS_UniformRunControl() {};
371 
372  void describeRCO();
373 
374  int startSimulation();
375 
384  int finishSimulation();
385 
386 protected:
387 
388  int milliseconds; // time interval for checking
389  // if all jobs finished (default 1000 ms)
390 
391  int check_intervals;// Number of intervals to check
392  // if all jobs done (default 5)
393 
394  int njob;
395  int npar;
396  int ipar;
397  int ifirst;
398  bool check_egsdat;// If true, and a 'watcher' job, produce intermediate results
399  bool watcher_job; // If true, job is a 'watcher'
400 };
401 
402 #endif
#define EGS_EXPORT
Export symbols from the egspp library.
Definition: egs_libconfig.h:90
virtual EGS_I64 getNextChunk()
Returns the number of histories to run in the next simulation chunk.
int getNchunk() const
Returns the number of simulation chunks.
A simple run control object for advanced EGSnrc C++ applications.
EGS_I64 getNcase() const
Returns the total number of particles to be simulated.
void setMaxTime(EGS_Float t)
Set the maximum CPU time for the simulation to t.
RCOType rco_type
RCO type to use.
int getNbatch() const
Returns the number of batches per simulation chunk.
RCOType
Define RCO types.
parallel jobs with same numbe of histories
A simple class for measuring CPU time.
Definition: egs_timer.h:57
single job or multiple independent jobs
A job control object for homogeneous computing environments (HCE).
A class for storing information in a tree-like structure of key-value pairs. This class is used throu...
Definition: egs_input.h:182
void setNbatch(int n)
Set the number of batches to n.
void setNcase(EGS_I64 n)
Set the number of particles to be simulated to n.
Defines the EGS_EXPORT and EGS_LOCAL macros.
A &#39;job control file&#39; (JCF) RCO.
EGS_Timer class header file.
Base class for advanced EGSnrc C++ applications.
void setRequiredUncertainty(EGS_Float a)
Set the required statistical uncertainty to a.