Pakman
SweepController.cc
1 #include <string>
2 #include <vector>
3 #include <stdexcept>
4 #include <iostream>
5 
6 #include <assert.h>
7 
8 #include "core/utils.h"
9 #include "core/OutputStreamHandler.h"
10 #include "system/system_call.h"
11 #include "interface/output.h"
12 #include "interface/protocols.h"
13 #include "master/AbstractMaster.h"
14 
15 #include "SweepController.h"
16 
18  m_parameter_names(input_obj.parameter_names),
19  m_simulator(input_obj.simulator)
20 {
21  // Read output from generator
22  std::string generator_output = system_call(input_obj.generator);
23 
24  // Decompose into individual parameters
25  m_prmtr_list = parse_generator_output(generator_output);
26 
27  // Sanity check: at least one parameter should have been generated
28  if (m_prmtr_list.size() == 0)
29  {
30  std::runtime_error e("generator did not output any parameters");
31  throw e;
32  }
33 }
34 
36 {
37  // This function should never be called recursively
38  assert(!m_entered);
39  m_entered = true;
40 
41  // If in the first iteration
42  if (m_first_iteration)
43  {
44  // Unset flag
45  m_first_iteration = false;
46 
47  // Push all parameters
48  for (auto it = m_prmtr_list.begin(); it != m_prmtr_list.end(); it++)
49  {
50  std::string input(it->str());
51  input += '\n';
52  m_p_master->pushPendingTask(std::move(input));
53  }
54 
55  // Return
56  m_entered = false;
57  return;
58  }
59 
60  // Check if there are any new finished parameters
61  while (!m_p_master->finishedTasksEmpty())
62  {
63  // Increment counter
64  m_num_finished++;
65 
66  // Get reference to front finished task
67  TaskHandler& task = m_p_master->frontFinishedTask();
68 
69  // Throw error if task finished with error and we are not ignoring
70  // task errors
71  if (!g_ignore_errors && task.didErrorOccur())
72  {
73  std::runtime_error e("Task finished with error!");
74  throw e;
75  }
76 
77  // Pop finished parameters
78  m_p_master->popFinishedTask();
79  }
80 
81  // If all parameters have finished, then write parameters and terminate
82  // Master
83  if (m_num_finished == m_prmtr_list.size())
84  {
85  // Print finished parameters
87  m_parameter_names, m_prmtr_list);
88 
89  // Terminate Master
90  m_p_master->terminate();
91  m_entered = false;
92  return;
93  }
94 
95  m_entered = false;
96 }
97 
99 {
100  return m_simulator;
101 }
virtual void iterate() override
SweepController(const Input &input_obj)
virtual Command getSimulator() const override
static OutputStreamHandler * instance()
void write_parameters(std::ostream &ostrm, const std::vector< ParameterName > &parameter_names, const std::vector< Parameter > &parameters)
Definition: output.cc:11
std::vector< Parameter > parse_generator_output(const std::string &generator_output)
Definition: protocols.cc:253
bool didErrorOccur() const
Definition: TaskHandler.cc:37
std::shared_ptr< AbstractMaster > m_p_master
bool g_ignore_errors
Definition: main.cc:28