SST/macro
driver.h
Go to the documentation of this file.
1 #ifndef sstmac_main_DRIVER_H
2 #define sstmac_main_DRIVER_H
3 
6 #include <sstmac/main/sstmac.h>
8 #include <list>
9 #include <iostream>
10 #include <sstmac/libraries/uq/uq.h>
11 
12 #ifdef SSTMAC_MPI_DRIVER
13 #include <mpi.h>
14 #endif
15 
16 namespace sstmac {
17 
18 typedef int pipe_t[2];
19 
21 {
22  friend class SimulationQueue;
23 
24  public:
26  complete_(false),
27  results_(nullptr),
28  allocated_results_(false)
29  {
30  }
31 
32  ~Simulation();
33 
34  double
35  wallTime() const {
36  return stats_.wallTime;
37  }
38 
39  double
40  simulatedTime() const {
41  return stats_.simulatedTime;
42  }
43 
44  sim_stats*
45  stats() {
46  return &stats_;
47  }
48 
49  void finalize();
50 
51  void waitFork();
52 
53  pid_t
54  pid() const {
55  return pid_;
56  }
57 
58  void
59  setResults(double* results, int numResults){
61  delete[] results_;
62 
63  results_ = results;
65  allocated_results_ = false;
66  }
67 
68  void
69  allocateResults(int nresults){
70  if (allocated_results_ && stats_.numResults >= nresults){
71  //do nothing - good already
72  } else {
73  allocated_results_ = true;
74  results_ = new double[nresults];
75  }
76  stats_.numResults = nresults;
77  }
78 
79  void
81  stats_ = stats;
82  }
83 
84  double*
85  results() const {
86  return results_;
87  }
88 
89 
90  bool
91  complete() const {
92  return complete_;
93  }
94 
95  void
96  setComplete(bool flag){
97  complete_ = flag;
98  }
99 
100  int
101  numResults() const {
102  return stats_.numResults;
103  }
104 
105  void
106  setLabel(int idx){
107  idx_ = idx;
108  }
109 
110  int
111  label() const {
112  return idx_;
113  }
114 
115  private:
116  void
117  setPid(pid_t pid){
118  pid_ = pid;
119  }
120 
121  void
122  setPipe(pipe_t p){
123  pfd_[0] = p[0];
124  pfd_[1] = p[1];
125  }
126 
127  int
128  readPipe() const {
129  return pfd_[0];
130  }
131 
132  int
133  writePipe() const {
134  return pfd_[1];
135  }
136 
137  void
138  setParameters(sprockit::sim_parameters* params);
139 
140  sprockit::sim_parameters params_;
142  char label_[256];
144  pid_t pid_;
145  pipe_t pfd_;
146  double* results_;
147  bool complete_;
148  int idx_;
150 
151 
152  public:
153  void waitMPIScan();
154 
155 #if SSTMAC_MPI_DRIVER
156  public:
157  MPI_Request*
158  initSendRequest() {
159  return mpi_requests_;
160  }
161 
162  MPI_Request*
163  recvResultsRequest() {
164  return &mpi_requests_[2];
165  }
166 
167  MPI_Request*
168  recvStatsRequest() {
169  return &mpi_requests_[1];
170  }
171 
172  private:
173  MPI_Request mpi_requests_[3];
174 #endif
175 
176 };
177 
178 
180 {
181  public:
182  SimulationQueue();
183 
184  ~SimulationQueue();
185 
186  Simulation*
187  fork(sprockit::sim_parameters& params,
188  int nresults = 0,
189  double* resultPtr = nullptr){
190  return fork(&params, nresults, resultPtr);
191  }
192 
193  bool
194  runJobsOnMaster() const {
195  return nproc_ <= 4;
196  }
197 
198  int
200  if (runJobsOnMaster()) return nproc_;
201  else return nproc_ - 1;
202  }
203 
204  void
206  next_worker_ = (next_worker_ + 1) % nproc_;
207  }
208 
209  void teardown();
210 
211  void buildUp(){
212  built_up_ = true;
213  }
214 
215  void init(int argc, char** argv);
216 
217  void finalize();
218 
219  Simulation*
220  fork(sprockit::sim_parameters* params,
221  int nresults = 0,
222  double* resultPtr = nullptr);
223 
224  Simulation*
225  waitForForked();
226 
227  void
228  clear(Simulation* sim);
229 
230  void
231  run(sprockit::sim_parameters* params, sim_stats& stats);
232 
233  static double*
234  allocateResults(int nresults);
235 
236  static void
238 
239  static void
240  delete_statics();
241 
242  Simulation*
243  sendScanPoint(int bufferSize, char* bufferPtr, int nresults, double* resultPtr = nullptr);
244 
245  sprockit::sim_parameters*
247  return &template_params_;
248  }
249 
250  void
251  rerun(sprockit::sim_parameters* params, sim_stats& stats);
252 
253  void busyLoopMPI();
254 
255  void runScanPoint(char* buffer, sim_stats& stats);
256 
257  int workerID() const {
258  return me_;
259  }
260 
261  Simulation**
262  allocateSims(int max_nthread);
263 
264  char*
265  allocateTmpBuffer(size_t buf_size);
266 
267  double**
268  allocateResults(int njobs, int nresults);
269 
270  double**
271  allocateParams(int njobs, int nparams);
272 
273  uq_param_t**
274  allocateParamStructs(int njobs, int nparams);
275 
276 
277  private:
278  bool built_up_;
279  std::list<Simulation*> pending_;
281  sprockit::sim_parameters template_params_;
283  static double* results_;
284  static int num_results_;
286  int nsims_;
287  char* tmp_buffer_;
289 
290  std::pair<int,int> result_buf_size_;
291  std::pair<int,int> param_buf_size_;
292  std::pair<int,int> struct_buf_size_;
293 
294  double** tmp_results_;
295  double** tmp_params_;
297 
298  private:
299  int nproc_;
300  int me_;
303 };
304 
305 }
306 
307 
308 #endif // DRIVER_H
309 
int workerID() const
Definition: driver.h:257
sprockit::sim_parameters * template_params()
Definition: driver.h:246
sprockit::sim_parameters params_
Definition: driver.h:140
double simulatedTime() const
Definition: driver.h:40
int pipe_t[2]
Definition: driver.h:18
int numResults
Definition: sstmac.h:54
Definition: sstmac.h:24
std::pair< int, int > struct_buf_size_
Definition: driver.h:292
std::pair< int, int > param_buf_size_
Definition: driver.h:291
void setLabel(int idx)
Definition: driver.h:106
uq_param_t ** tmp_structs_
Definition: driver.h:296
void setPipe(pipe_t p)
Definition: driver.h:122
bool allocated_results_
Definition: driver.h:149
int writePipe() const
Definition: driver.h:133
void setResults(double *results, int numResults)
Definition: driver.h:59
std::list< Simulation * > pending_
Definition: driver.h:279
Simulation ** sims_
Definition: driver.h:285
bool runJobsOnMaster() const
Definition: driver.h:194
double * results() const
Definition: driver.h:85
double * results_
Definition: driver.h:146
Simulation * fork(sprockit::sim_parameters &params, int nresults=0, double *resultPtr=nullptr)
Definition: driver.h:187
double wallTime
Definition: sstmac.h:52
bool complete() const
Definition: driver.h:91
sim_stats * stats()
Definition: driver.h:45
parallel_runtime * init()
SUMI = Simulator unified messagine interface It is also the name for a solid ink in Japanese - i...
static double * results_
Definition: driver.h:283
int readPipe() const
Definition: driver.h:128
static void publishResults()
Definition: driver.h:237
int maxParallelWorkers() const
Definition: driver.h:199
double simulatedTime
Definition: sstmac.h:53
friend class SimulationQueue
Definition: driver.h:22
double ** tmp_params_
Definition: driver.h:295
std::pair< int, int > result_buf_size_
Definition: driver.h:290
void allocateResults(int nresults)
Definition: driver.h:69
sim_stats stats_
Definition: driver.h:141
Definition: uq.h:23
int numResults() const
Definition: driver.h:101
char label_[256]
Definition: driver.h:142
parallel_runtime * rt_
Definition: driver.h:280
void setStats(const sim_stats &stats)
Definition: driver.h:80
void run(opts &oo, sstmac::parallel_runtime *rt, sprockit::sim_parameters *params, sim_stats &stats)
static int num_results_
Definition: driver.h:284
sprockit::sim_parameters template_params_
Definition: driver.h:281
void setPid(pid_t pid)
Definition: driver.h:117
double ** tmp_results_
Definition: driver.h:294
void setParameters(sprockit::sim_parameters *params)
pid_t pid() const
Definition: driver.h:54
void setComplete(bool flag)
Definition: driver.h:96
int label() const
Definition: driver.h:111
double wallTime() const
Definition: driver.h:35