NOMAD Source  Version 4.0.0 Beta
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Step.hpp
Go to the documentation of this file.
1 
2 #ifndef __NOMAD400_STEP__
3 #define __NOMAD400_STEP__
4 
5 #include <stdexcept>
6 
7 #include "../Algos/AllStopReasons.hpp"
8 #include "../Algos/MeshBase.hpp"
9 #include "../Eval/Barrier.hpp"
10 #include "../Eval/EvaluatorControl.hpp"
11 #include "../Output/OutputInfo.hpp"
12 #include "../Output/OutputQueue.hpp"
13 #include "../Param/RunParameters.hpp"
14 #include "../Type/CallbackType.hpp"
15 
16 #include "../nomad_nsbegin.hpp"
17 
18 class Step;
19 
20 typedef std::function<void(const Step& step, bool &stop)> StepEndCbFunc; ///< Type definitions for callback functions at the end of a step.
21 typedef std::function<void(std::vector<std::string>& paramLines)> HotRestartCbFunc; ///< Type definitions for callback functions for hot restart.
22 
23 /// Base class of all types of steps (Iteration, Termination, Initialization, Poll, Mads,...).
24 class Step
25 {
26 
27 protected:
28  static bool _userInterrupt; ///< Interrupt NOMAD if Ctrl-C is pressed.
29  static bool _userTerminate; ///< Terminate NOMAD if Ctrl-C is pressed again.
30 
31  const Step* _parentStep; ///< The parent of this step.
32  std::string _name; ///< The name of this step.
33 
34  std::shared_ptr<AllStopReasons> _stopReasons; ///< The stop reasons of an algorithm.
35 
36 
37  std::shared_ptr<RunParameters> _runParams; ///< The run parameters that control a step.
38  std::shared_ptr<PbParameters> _pbParams; ///< The problem parameters that control a step.
39 
40  // Callbacks that may be re-implemented by the user
44 
45 
46 public:
47 
48  /// Constructor #1 for MainStep (no parent)
49  /**
50  */
51  explicit Step()
52  : _parentStep( nullptr ),
53  _name("Main Step"),
54  _stopReasons( nullptr ),
55  _runParams( nullptr ),
56  _pbParams( nullptr )
57  {
58  init();
59  }
60 
61 
62  /// Constructor #2 for child step of a parent sharing the same stopReason
63  /**
64  \param parentStep The parent of this step (cannot be nullptr).
65  \param runParams The run parameters that control this step (null by default).
66  \param pbParams The problem parameters that control this step (null by default).
67  */
68  explicit Step(const Step* parentStep,
69  const std::shared_ptr<RunParameters> &runParams = nullptr,
70  const std::shared_ptr<PbParameters> &pbParams = nullptr )
71  : _parentStep(parentStep),
72  _name("Step"),
73  _runParams (runParams),
74  _pbParams(pbParams)
75  {
76  if ( _parentStep == nullptr )
77  {
78  throw Exception( __FILE__ , __LINE__ ,"Parent step is NULL. This constructor is for child steps having a parent only.");
79  }
80  else
81  {
82  _name = "Child step";
83  _stopReasons = parentStep->getAllStopReasons();
84  }
85  init();
86  }
87 
88  /// Constructor #3: for a child Step with a provided stopReason (such as an algorithm)
89  /**
90  \param parentStep The parent of this step (can be nullptr if child of a MainStep).
91  \param stopReasons The stop reasons for all the steps of an algo (cannot be nullptr)
92  \param runParams The run parameters that control this step (null by default).
93  \param pbParams The problem parameters that control this step (null by default).
94  */
95  explicit Step(const Step* parentStep,
96  std::shared_ptr<AllStopReasons> stopReasons ,
97  const std::shared_ptr<RunParameters> &runParams = nullptr,
98  const std::shared_ptr<PbParameters> &pbParams = nullptr )
99  : _parentStep(parentStep),
100  _name("Step"),
101  _stopReasons(stopReasons),
102  _runParams (runParams),
103  _pbParams(pbParams)
104  {
105  if ( _stopReasons == nullptr )
106  {
107  throw Exception( __FILE__ , __LINE__ ,"StopReason is NULL. Must be provided for this child step.");
108  }
109 
110  init();
111  }
112 
113 
114  /// Destructor
115  /**
116  Upon destruction of a step the output queue is flushed. Time to print.
117  */
118  virtual ~Step();
119 
120  // Get / Set
121 
122  /// Interruption call by user.
123  /**
124  Called by pressing Ctrl-C.
125  */
126  static bool getUserTerminate() { return _userTerminate; }
127 
128  /// Interruption requested
129  static void setUserTerminate() { _userTerminate = true; }
130 
131  /// Get the parent step.
132  /**
133  * There is no setParentStep(). We should not change parent step externally.
134 
135  \return The parent step of this step.
136  */
137  const Step* getParentStep() const { return _parentStep; }
138 
139  /// Get the name of this step
140  /**
141  \return A /c string containing the name of this step.
142  */
143  virtual const std::string getName() const { return _name; }
144 
145  /// Set the name of this step
146  /**
147  \param name The name is provided as a \c string -- \b IN.
148  */
149  void setName(const std::string& name) { _name = name; }
150 
151  std::shared_ptr<AllStopReasons> getAllStopReasons() const { return _stopReasons ; }
152 
153  /// Interruption call by user with a signal value
154  static void userInterrupt(int signalValue);
155 
156  static bool getUserInterrupt() { return _userInterrupt; }
157 
158  /// \brief Set user callback
159  void addCallback(const NOMAD::CallbackType& callbackType,
160  const NOMAD::StepEndCbFunc& stepEndCbFunc);
161  void addCallback(const NOMAD::CallbackType& callbackType,
162  const NOMAD::HotRestartCbFunc& hotRestartCbFunc);
163 
164  /// \brief Run user callback
165  static void runCallback(NOMAD::CallbackType callbackType,
166  const NOMAD::Step& step,
167  bool &stop);
168  static void runCallback(NOMAD::CallbackType callbackType,
169  std::vector<std::string>& paramLines);
170 
171 
172  /// \brief display output
173  void AddOutputInfo(const std::string& s, bool isBlockStart, bool isBlockEnd) const;
174  void AddOutputInfo(const std::string& s, OutputLevel outputLevel = OutputLevel::LEVEL_INFO) const;
175  void AddOutputError(const std::string& s) const;
176  void AddOutputWarning(const std::string& s) const;
177  void AddOutputVeryHigh(const std::string& s) const;
178  void AddOutputHigh(const std::string& s) const;
179  void AddOutputDebug(const std::string& s) const;
180  void AddOutputInfo(OutputInfo outputInfo) const;
181 
182  /// Template function to get the parent of given type.
183  /**
184  * Starting with parent of current Step, and going through ancestors,
185  get first Step that is of type T.
186  * By default, stop if an Algorithm is found. Returned Step could be
187  irrelevant otherwise. To go further up than an Algorithm, set optional
188  parameter stopAtAlgo to false.
189  */
190  template<typename T>
191  const Step* getParentOfType(const bool stopAtAlgo = true) const
192  {
193  Step* retStep = nullptr;
194 
195  Step* step = const_cast<Step*>(_parentStep);
196  while (step)
197  {
198  if (nullptr != dynamic_cast<T>(step))
199  {
200  retStep = step;
201  break;
202  }
203  else if (stopAtAlgo && step->isAnAlgorithm())
204  {
205  break;
206  }
207  step = const_cast<Step*>(step->getParentStep());
208  }
209 
210  return retStep;
211  }
212 
213 // CT maybe needed
214 // template<typename T>
215 // const Step* getStepOfType() const
216 // {
217 // Step* step = const_cast<Step*>(this);
218 // if (nullptr != dynamic_cast<T>(step))
219 // {
220 // return step;
221 // }
222 // else
223 // {
224 // return step->getParentOfType<T>();
225 // }
226 // }
227 
228  bool isAnAlgorithm() const;
229 
230  /**
231  \return The MeshBase for the first Iteration ancestor of this Step.
232  */
233  const std::shared_ptr<MeshBase> getIterationMesh() const;
234 
235  /**
236  /return The frameCenter for the first Iteration ancestor of this Step.
237  */
238  const std::shared_ptr<EvalPoint> getIterationFrameCenter() const;
239 
240  /**
241  /return The Barrier for the main MegaIteration ancestor of this Step.
242  */
243  const std::shared_ptr<Barrier> getMegaIterationBarrier() const;
244 
245  /**
246  /return The fixedVariable Point for the first Subproblem ancestor.
247  If no subproblem is available, return a default Point (of size 0).
248  */
249  Point getSubFixedVariable() const;
250 
251 
252  /// What a step does.
253  /**
254  \todo Consider the us
255  */
256  virtual void start() ;
257 
258  /// What a step does.
259  virtual bool run() ;
260 
261  /// What a step does.
262  virtual void end();
263 
264  /// Helper for hot restart functionalities
265  virtual void hotRestartOnUserInterrupt();
266 
267 protected:
268  /// Helper for constructors.
269  /**
270  Throw Exception when not verified.
271  */
272  void verifyParentNotNull();
273 
274  /// Helper for validating steps depending on parameter GENERATE_ALL_POINTS_BEFORE_EVAL
275  void verifyGenerateAllPointsBeforeEval(const std::string& method, const bool expected) const;
276 
277  /// Helper for constructors
278  void init();
279 
280  void defaultStart();
281  bool defaultRun();
282  void defaultEnd();
283 
284 
285  /// Helpers for hot restart, to be called at the start and end of any override.
286  void hotRestartBeginHelper();
287  /// Helpers for hot restart, to be called at the start and end of any override.
288  void hotRestartEndHelper();
289 
290 
291 private:
292  // Default callbacks. They do nothing.
293  static void defaultStepEnd(const Step& step __attribute__((unused)), bool &stop) { stop = false; }
294  static void defaultHotRestart(std::vector<std::string>& paramLines __attribute__((unused))) {};
295 
296 };
297 
298 #include "../nomad_nsend.hpp"
299 
300 #endif // __NOMAD400_STEP__
Exception utility.
virtual ~Step()
Destructor.
void defaultStart()
void AddOutputDebug(const std::string &s) const
Step()
Constructor #1 for MainStep (no parent)
Definition: Step.hpp:51
const Step * _parentStep
The parent of this step.
Definition: Step.hpp:31
void AddOutputVeryHigh(const std::string &s) const
Step(const Step *parentStep, std::shared_ptr< AllStopReasons > stopReasons, const std::shared_ptr< RunParameters > &runParams=nullptr, const std::shared_ptr< PbParameters > &pbParams=nullptr)
Constructor #3: for a child Step with a provided stopReason (such as an algorithm) ...
Definition: Step.hpp:95
static bool _userTerminate
Terminate NOMAD if Ctrl-C is pressed again.
Definition: Step.hpp:29
const std::shared_ptr< EvalPoint > getIterationFrameCenter() const
static void setUserTerminate()
Interruption requested.
Definition: Step.hpp:129
void init()
Helper for constructors.
virtual void end()
What a step does.
static void userInterrupt(int signalValue)
Interruption call by user with a signal value.
void verifyGenerateAllPointsBeforeEval(const std::string &method, const bool expected) const
Helper for validating steps depending on parameter GENERATE_ALL_POINTS_BEFORE_EVAL.
static bool getUserInterrupt()
Definition: Step.hpp:156
const Step * getParentOfType(const bool stopAtAlgo=true) const
Template function to get the parent of given type.
Definition: Step.hpp:191
std::string _name
The name of this step.
Definition: Step.hpp:32
std::shared_ptr< AllStopReasons > _stopReasons
The stop reasons of an algorithm.
Definition: Step.hpp:34
virtual void hotRestartOnUserInterrupt()
Helper for hot restart functionalities.
static bool _userInterrupt
Interrupt NOMAD if Ctrl-C is pressed.
Definition: Step.hpp:28
void AddOutputHigh(const std::string &s) const
void addCallback(const NOMAD::CallbackType &callbackType, const NOMAD::StepEndCbFunc &stepEndCbFunc)
Set user callback.
void verifyParentNotNull()
Helper for constructors.
CallbackType
static void runCallback(NOMAD::CallbackType callbackType, const NOMAD::Step &step, bool &stop)
Run user callback.
Point getSubFixedVariable() const
OutputLevel
Definition: OutputInfo.hpp:12
virtual const std::string getName() const
Get the name of this step.
Definition: Step.hpp:143
std::shared_ptr< PbParameters > _pbParams
The problem parameters that control a step.
Definition: Step.hpp:38
void AddOutputError(const std::string &s) const
const Step * getParentStep() const
Get the parent step.
Definition: Step.hpp:137
std::function< void(const Step &step, bool &stop)> StepEndCbFunc
Type definitions for callback functions at the end of a step.
Definition: Step.hpp:18
static void defaultStepEnd(const Step &step __attribute__((unused)), bool &stop)
Definition: Step.hpp:293
static bool getUserTerminate()
Interruption call by user.
Definition: Step.hpp:126
bool isAnAlgorithm() const
const std::shared_ptr< Barrier > getMegaIterationBarrier() const
void AddOutputWarning(const std::string &s) const
const std::shared_ptr< MeshBase > getIterationMesh() const
void setName(const std::string &name)
Set the name of this step.
Definition: Step.hpp:149
static NOMAD::StepEndCbFunc _cbIterationEnd
Definition: Step.hpp:41
std::shared_ptr< AllStopReasons > getAllStopReasons() const
Definition: Step.hpp:151
Base class of all types of steps (Iteration, Termination, Initialization, Poll, Mads,...).
Definition: Step.hpp:24
virtual void start()
What a step does.
static NOMAD::HotRestartCbFunc _cbHotRestart
Definition: Step.hpp:43
std::function< void(std::vector< std::string > &paramLines)> HotRestartCbFunc
Type definitions for callback functions for hot restart.
Definition: Step.hpp:21
void defaultEnd()
virtual bool run()
What a step does.
static void defaultHotRestart(std::vector< std::string > &paramLines __attribute__((unused)))
Definition: Step.hpp:294
bool defaultRun()
void hotRestartEndHelper()
Helpers for hot restart, to be called at the start and end of any override.
Class for the representation of a point.
Definition: Point.hpp:23
void hotRestartBeginHelper()
Helpers for hot restart, to be called at the start and end of any override.
void AddOutputInfo(const std::string &s, bool isBlockStart, bool isBlockEnd) const
display output
Step(const Step *parentStep, const std::shared_ptr< RunParameters > &runParams=nullptr, const std::shared_ptr< PbParameters > &pbParams=nullptr)
Constructor #2 for child step of a parent sharing the same stopReason.
Definition: Step.hpp:68
static NOMAD::StepEndCbFunc _cbMegaIterationEnd
Definition: Step.hpp:42
std::shared_ptr< RunParameters > _runParams
The run parameters that control a step.
Definition: Step.hpp:37