NOMAD Source  Version 4.0.0 Beta
OutputQueue.hpp
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------------*/
2 /* NOMAD - Nonlinear Optimization by Mesh Adaptive Direct Search - */
3 /* */
4 /* NOMAD - Version 4.0.0 has been created by */
5 /* Viviane Rochon Montplaisir - Polytechnique Montreal */
6 /* Christophe Tribes - Polytechnique Montreal */
7 /* */
8 /* The copyright of NOMAD - version 4.0.0 is owned by */
9 /* Charles Audet - Polytechnique Montreal */
10 /* Sebastien Le Digabel - Polytechnique Montreal */
11 /* Viviane Rochon Montplaisir - Polytechnique Montreal */
12 /* Christophe Tribes - Polytechnique Montreal */
13 /* */
14 /* NOMAD v4 has been funded by Rio Tinto, Hydro-Québec, NSERC (Natural */
15 /* Sciences and Engineering Research Council of Canada), InnovÉÉ (Innovation */
16 /* en Énergie Électrique) and IVADO (The Institute for Data Valorization) */
17 /* */
18 /* NOMAD v3 was created and developed by Charles Audet, Sebastien Le Digabel, */
19 /* Christophe Tribes and Viviane Rochon Montplaisir and was funded by AFOSR */
20 /* and Exxon Mobil. */
21 /* */
22 /* NOMAD v1 and v2 were created and developed by Mark Abramson, Charles Audet, */
23 /* Gilles Couture, and John E. Dennis Jr., and were funded by AFOSR and */
24 /* Exxon Mobil. */
25 /* */
26 /* Contact information: */
27 /* Polytechnique Montreal - GERAD */
28 /* C.P. 6079, Succ. Centre-ville, Montreal (Quebec) H3C 3A7 Canada */
29 /* e-mail: nomad@gerad.ca */
30 /* */
31 /* This program is free software: you can redistribute it and/or modify it */
32 /* under the terms of the GNU Lesser General Public License as published by */
33 /* the Free Software Foundation, either version 3 of the License, or (at your */
34 /* option) any later version. */
35 /* */
36 /* This program is distributed in the hope that it will be useful, but WITHOUT */
37 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
38 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License */
39 /* for more details. */
40 /* */
41 /* You should have received a copy of the GNU Lesser General Public License */
42 /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
43 /* */
44 /* You can find information on the NOMAD software at www.gerad.ca/nomad */
45 /*---------------------------------------------------------------------------------*/
46 #ifndef __NOMAD400_OUTPUTQUEUE__
47 #define __NOMAD400_OUTPUTQUEUE__
48 
49 #include <vector>
50 #ifdef _OPENMP
51 // Using OpenMP.
52 // NOTE We would do a wrapper like suggested in http://bisqwit.iki.fi/story/howto/openmp/.
53 // This way, the code would compile even if OpenMP is not available.
54 // Plus, it might be clearer.
55 #include <omp.h>
56 #endif // _OPENMP
57 
58 #include "../Param/DisplayParameters.hpp"
59 #include "../Output/OutputInfo.hpp"
60 #include "../Output/StatsInfo.hpp"
61 
62 #include "../nomad_nsbegin.hpp"
63 
64 /// Queue of all information that was not output yet.
65 /**
66  The output queue is a singleton. Some OutputInfo can be added to the queue. The output information is displayed when calling OutputQueue::Flush and queue is emptied. \n
67  The information can be send to the standard display and to a stats file. \n
68  The display is formatted with indendation of blocks of information. The display parameters (DisplayParameters) are attributes of the class provided by calling OutputQueue::initParameters. \n
69 
70  The display can be limited to a maximum block/step level. (OutputQueue::_maxStepLevel). \n
71 
72  \todo Replace calls to std::cout by something more general.
73 
74  */
76 {
77 private:
78  /// Private constructor
79  OutputQueue();
80 
81 public:
82  // Destructor
83  virtual ~OutputQueue();
84 
85  /// Access to singleton
86  static std::unique_ptr<OutputQueue>& getInstance();
87 
88  void initParameters(const std::shared_ptr<DisplayParameters>& params);
89 
90  /// Flush and close stats file (called by initParameters) if OutputQueue has been already initialized
91  void reset();
92 
93  void add(OutputInfo outputInfo);
94  static void Add(OutputInfo outputInfo)
95  {
96  getInstance()->add(std::move(outputInfo));
97  }
98 
99  void add(const std::string& s,
100  OutputLevel outputLevel = OutputLevel::LEVEL_INFO);
101  static void Add(const std::string& s,
102  OutputLevel outputLevel = OutputLevel::LEVEL_INFO)
103  {
104  getInstance()->add(s, outputLevel);
105  }
106 
107  void add(const StatsInfo& statsInfo);
108  static void Add(const StatsInfo & statsInfo)
109  {
110  getInstance()->add(statsInfo);
111  }
112 
113  /// Print all in the queue and flush.
114  /**
115  OutputInfo block start and block end flags will print
116  _blockStart after the msg, or _blockEnd before.
117  \note Example output: \n
118  Start step MADS { \n
119  _______Start step SEARCH { \n
120  _____________Things happening in SEARCH \n
121  _____________More things happening in SEARCH \n
122  _______} End step SEARCH \n
123  _______Start step POLL { \n
124  _____________Things happening in POLL \n
125  _____________More things happening in POLL \n
126  _______} End step POLL \n
127  } End step MADS \n
128  If there are more than one line to print, flags for block
129  start and end are ignored.
130  */
131  static void Flush()
132  {
133  getInstance()->flush();
134  }
135 
136 
137  size_t getMaxStepLevel() const { return _maxStepLevel; }
138  void setMaxStepLevel(const size_t maxStepLevel) { _maxStepLevel = maxStepLevel; }
139 
140  bool goodLevel(const OutputLevel& outputLevel) const;
141  static bool GoodLevel(const OutputLevel& outputLevel)
142  {
143  return getInstance()->goodLevel(outputLevel);
144  }
145 
146  // Macros for output
147 #define OUTPUT_STATS_START if (OutputQueue::GoodLevel(OutputLevel::LEVEL_STATS)) {
148 #define OUTPUT_INFO_START if (OutputQueue::GoodLevel(OutputLevel::LEVEL_INFO)) {
149 #define OUTPUT_DEBUG_START if (OutputQueue::GoodLevel(OutputLevel::LEVEL_DEBUG)) {
150 #define OUTPUT_STATS_END }
151 #define OUTPUT_INFO_END }
152 #define OUTPUT_DEBUG_END }
153 
154  void setDisplayDegree(const int displayDegree);
155 
156  void setStatsFileName(const std::string& statsFile) { _statsFile = statsFile; }
157  void initStatsFile();
158  const std::string& getStatsFileName() const { return _statsFile; }
159 
160  void setStatsFileFormat(const DisplayStatsTypeList& statsFileFormat)
161  {
162  _statsFileFormat = statsFileFormat;
163  }
165 
166  // Used by OutputInfo.
168  {
169  return _params->getAttributeValue<ArrayOfDouble>("SOL_FORMAT");
170  }
171 
172 private:
173 #ifdef _OPENMP
174  // Acquire lock before Add or Flush.
175  // NOTE It does not seem relevant for the lock to be static,
176  // because OutputQueue is a singleton anyway. If staticity causes problems,
177  // we could remove the static keyword.
178  static omp_lock_t _s_queue_lock;
179 #endif // _OPENMP
180 
181  static bool _hasBeenInitialized; ///< Flag for initialization (initialization cannot be performed more than once).
182 
183  static std::unique_ptr<OutputQueue> _single; ///< The singleton
184 
185  /// Queue of all the OutputInfo we have to print.
186  std::vector<OutputInfo> _queue;
187 
188  /// Display parameters
189  std::shared_ptr<DisplayParameters> _params;
190 
191  std::string _statsFile;
192  std::ofstream _statsStream;
193 
194  /**
195  Format for stats in a file (parameter STATS_FILE).
196  Might include some raw strings, do not convert to DisplayStatsType.
197  */
199 
200  /**
201  Keep track of the number of lines printed to output (DISPLAY_STATS).
202  Used to print stats header regularly.
203  */
205 
206  /**
207  Format width for OBJ and CONS_H. May be enlarged during the run.
208  */
209  size_t _objWidth;
210 
211  size_t _hWidth;
212 
213  size_t _maxStepLevel; ///< Maximum step level we want to print out.
214  OutputLevel _maxOutputLevel; ///< Output level (~display degree) we want to print out
215  int _indentLevel; ///< Internal indentation level
216 
217  const std::string _blockStart; ///< Symbol for a block start.
218  const std::string _blockEnd; ///< Symbol for an end block.
219 
220  void startBlock();
221  void endBlock();
222  void flush();
223  void flushBlock(const OutputInfo &outputInfo);
224  void flushStatsToStatsFile(const StatsInfo *statsInfo);
225  void flushStatsToStdout(const StatsInfo *statsInfo);
226  void indent(int level);
227 };
228 
229 #include "../nomad_nsend.hpp"
230 
231 #endif // __NOMAD400_OUTPUTQUEUE__
OutputLevel
OutputLevel
Level of output AKA Display degree.
Definition: OutputInfo.hpp:55
OutputQueue::_hasBeenInitialized
static bool _hasBeenInitialized
Flag for initialization (initialization cannot be performed more than once).
Definition: OutputQueue.hpp:181
OutputQueue::OutputQueue
OutputQueue()
Private constructor.
OutputQueue::Flush
static void Flush()
Print all in the queue and flush.
Definition: OutputQueue.hpp:131
OutputQueue::indent
void indent(int level)
OutputQueue::setStatsFileFormat
void setStatsFileFormat(const DisplayStatsTypeList &statsFileFormat)
Definition: OutputQueue.hpp:160
OutputQueue::_blockStart
const std::string _blockStart
Symbol for a block start.
Definition: OutputQueue.hpp:217
OutputQueue::~OutputQueue
virtual ~OutputQueue()
OutputQueue::getMaxStepLevel
size_t getMaxStepLevel() const
Definition: OutputQueue.hpp:137
OutputQueue::_statsFile
std::string _statsFile
Definition: OutputQueue.hpp:191
OutputQueue::GoodLevel
static bool GoodLevel(const OutputLevel &outputLevel)
Definition: OutputQueue.hpp:141
OutputQueue::Add
static void Add(const StatsInfo &statsInfo)
Definition: OutputQueue.hpp:108
OutputQueue::Add
static void Add(OutputInfo outputInfo)
Definition: OutputQueue.hpp:94
OutputQueue::_objWidth
size_t _objWidth
Definition: OutputQueue.hpp:209
OutputQueue::_queue
std::vector< OutputInfo > _queue
Queue of all the OutputInfo we have to print.
Definition: OutputQueue.hpp:186
OutputQueue::setMaxStepLevel
void setMaxStepLevel(const size_t maxStepLevel)
Definition: OutputQueue.hpp:138
ArrayOfString
Class for the representation of an array of n strings.
Definition: ArrayOfString.hpp:63
OutputQueue::_hWidth
size_t _hWidth
Definition: OutputQueue.hpp:211
OutputQueue::getSolFormat
const ArrayOfDouble & getSolFormat() const
Definition: OutputQueue.hpp:167
OutputQueue::add
void add(OutputInfo outputInfo)
OutputQueue::flushStatsToStdout
void flushStatsToStdout(const StatsInfo *statsInfo)
StatsInfo
Information for stats format (parameters DISPLAY_STATS and STATS_FILE).
Definition: StatsInfo.hpp:119
OutputQueue::initStatsFile
void initStatsFile()
OutputQueue::setDisplayDegree
void setDisplayDegree(const int displayDegree)
OutputQueue::_statsStream
std::ofstream _statsStream
Definition: OutputQueue.hpp:192
OutputQueue::_maxOutputLevel
OutputLevel _maxOutputLevel
Output level (~display degree) we want to print out.
Definition: OutputQueue.hpp:214
OutputQueue::reset
void reset()
Flush and close stats file (called by initParameters) if OutputQueue has been already initialized.
OutputQueue::_indentLevel
int _indentLevel
Internal indentation level.
Definition: OutputQueue.hpp:215
OutputQueue::flush
void flush()
OutputQueue::_blockEnd
const std::string _blockEnd
Symbol for an end block.
Definition: OutputQueue.hpp:218
OutputQueue::startBlock
void startBlock()
OutputQueue::initParameters
void initParameters(const std::shared_ptr< DisplayParameters > &params)
ArrayOfDouble
Class for the representation of an array of n values.
Definition: ArrayOfDouble.hpp:67
OutputQueue::endBlock
void endBlock()
OutputQueue::Add
static void Add(const std::string &s, OutputLevel outputLevel=OutputLevel::LEVEL_INFO)
Definition: OutputQueue.hpp:101
OutputQueue::_maxStepLevel
size_t _maxStepLevel
Maximum step level we want to print out.
Definition: OutputQueue.hpp:213
OutputQueue::flushBlock
void flushBlock(const OutputInfo &outputInfo)
OutputInfo
Definition: OutputInfo.hpp:75
OutputQueue::_statsFileFormat
DisplayStatsTypeList _statsFileFormat
Definition: OutputQueue.hpp:198
OutputQueue::flushStatsToStatsFile
void flushStatsToStatsFile(const StatsInfo *statsInfo)
OutputQueue::getStatsFileName
const std::string & getStatsFileName() const
Definition: OutputQueue.hpp:158
OutputQueue::getInstance
static std::unique_ptr< OutputQueue > & getInstance()
Access to singleton.
OutputLevel::LEVEL_INFO
@ LEVEL_INFO
Lots of information.
OutputQueue::goodLevel
bool goodLevel(const OutputLevel &outputLevel) const
OutputQueue::_statsLineCount
size_t _statsLineCount
Definition: OutputQueue.hpp:204
OutputQueue::_single
static std::unique_ptr< OutputQueue > _single
The singleton.
Definition: OutputQueue.hpp:183
OutputQueue::_params
std::shared_ptr< DisplayParameters > _params
Display parameters.
Definition: OutputQueue.hpp:189
OutputQueue
Queue of all information that was not output yet.
Definition: OutputQueue.hpp:75
OutputQueue::getStatsFileFormat
const DisplayStatsTypeList & getStatsFileFormat() const
Definition: OutputQueue.hpp:164
OutputQueue::setStatsFileName
void setStatsFileName(const std::string &statsFile)
Definition: OutputQueue.hpp:156