NOMAD Source  Version 4.0.0 Beta
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OutputQueue.hpp
Go to the documentation of this file.
1 #ifndef __NOMAD400_OUTPUTQUEUE__
2 #define __NOMAD400_OUTPUTQUEUE__
3 
4 #include <vector>
5 #ifdef _OPENMP
6 // Using OpenMP.
7 // VRM We should do a wrapper like suggested in http://bisqwit.iki.fi/story/howto/openmp/.
8 // This way, the code would compile even if OpenMP is not available.
9 // Plus, it might be clearer.
10 #include <omp.h>
11 #endif // _OPENMP
12 
13 #include "../Param/DisplayParameters.hpp"
14 #include "../Output/OutputInfo.hpp"
15 #include "../Output/StatsInfo.hpp"
16 
17 #include "../nomad_nsbegin.hpp"
18 
19 // Queue of all information that was not output yet. When Flush is called,
20 // the queue is emptied.
22 {
23 private:
24  // Private constructor
25  OutputQueue();
26 
27 public:
28  // Destructor
29  ~OutputQueue();
30 
31  // Access to singleton
32  static std::unique_ptr<OutputQueue>& getInstance();
33 
34  void initParameters(const std::shared_ptr<DisplayParameters>& params);
35 
36  // Add Output info
37  void add(OutputInfo outputInfo);
38  static void Add(OutputInfo outputInfo)
39  {
40  getInstance()->add(std::move(outputInfo));
41  }
42 
43  void add(const std::string& s,
44  OutputLevel outputLevel = OutputLevel::LEVEL_INFO);
45  static void Add(const std::string& s,
47  {
48  getInstance()->add(s, outputLevel);
49  }
50 
51  // Add Output stats
52  void add(const StatsInfo& statsInfo);
53  static void Add(const StatsInfo & statsInfo)
54  {
55  getInstance()->add(statsInfo);
56  }
57 
58  // Print all in the queue and flush.
59  // OutputInfo block start and block end flags will print
60  // _blockStart after the msg, or _blockEnd before.
61  // Example output:
62  // Start step Mads {
63  // Start step Search {
64  // Things happening in Search
65  // More things happening in Search
66  // } End step Search
67  // Start step Poll {
68  // Things happening in Poll
69  // More things happening in Poll
70  // } End step Poll
71  // } End step Mads
72  // If there are more than one line to print, flags for block
73  // start and end are ignored.
74  static void Flush()
75  {
76  getInstance()->flush();
77  }
78 
79 
80  size_t getMaxStepLevel() const { return _maxStepLevel; }
81  void setMaxStepLevel(const size_t maxStepLevel) { _maxStepLevel = maxStepLevel; }
82 
83  int getDisplayDegree() const;
84  void setDisplayDegree(const int displayDegree);
85 
86  void setStatsFileName(const std::string& statsFile) { _statsFile = statsFile; }
87  void initStatsFile();
88  const std::string& getStatsFileName() const { return _statsFile; }
89 
90  void setStatsFileFormat(const DisplayStatsTypeList& statsFileFormat)
91  {
92  _statsFileFormat = statsFileFormat;
93  }
95 
96  // Used by OutputInfo.
97  const ArrayOfDouble& getSolFormat() const
98  {
99  return _params->getAttributeValue<ArrayOfDouble>("SOL_FORMAT");
100  }
101 
102 private:
103 #ifdef _OPENMP
104  // Acquire lock before Add or Flush.
105  // VRM it does not seem relevant for the lock to be static,
106  // because OutputQueue is a singleton anyway. If staticity causes problems,
107  // we could remove the static keyword.
108  static omp_lock_t _s_queue_lock;
109 #endif // _OPENMP
110 
111 
112  static std::unique_ptr<OutputQueue> _single; ///< The singleton
113 
114 
115  // Queue of all the OutputInfo we have to print.
116  std::vector<OutputInfo> _queue;
117  // Parameters
118  std::shared_ptr<DisplayParameters> _params;
119 
120  // Stats file and display stats
121  // File for stats
122  std::string _statsFile;
123  std::ofstream _statsStream;
124  // Format for stats, parameter STATS_FILE
125  // Might include some raw strings, do not convert to DisplayStatsType.
127  // Keep track of the number of lines printed to output (DISPLAY_STATS).
128  // Used to print stats header regularly.
130 
131  // Format width for OBJ and CONS_H. May be enlarged during the run.
132  size_t _objWidth;
133  size_t _hWidth;
134 
135  size_t _maxStepLevel; // Maximum step level we want to print out.
136  OutputLevel _maxOutputLevel; // Output level (~display degree) we want to print out
137  int _indentLevel; // Internal
138 
139  const std::string _blockStart;
140  const std::string _blockEnd;
141 
142  // Private helper functions
143  void startBlock();
144  void endBlock();
145  void flush();
146  void flushBlock(const OutputInfo &outputInfo);
147  void flushStatsToStatsFile(const StatsInfo *statsInfo);
148  void flushStatsToStdout(const StatsInfo *statsInfo);
149  void indent(int level);
150 };
151 
152 #include "../nomad_nsend.hpp"
153 
154 #endif // __NOMAD400_OUTPUTQUEUE__
std::vector< OutputInfo > _queue
void indent(int level)
void initParameters(const std::shared_ptr< DisplayParameters > &params)
void setMaxStepLevel(const size_t maxStepLevel)
Definition: OutputQueue.hpp:81
static void Add(const std::string &s, OutputLevel outputLevel=OutputLevel::LEVEL_INFO)
Definition: OutputQueue.hpp:45
void startBlock()
void setStatsFileFormat(const DisplayStatsTypeList &statsFileFormat)
Definition: OutputQueue.hpp:90
void flushStatsToStdout(const StatsInfo *statsInfo)
size_t _hWidth
void flushStatsToStatsFile(const StatsInfo *statsInfo)
void initStatsFile()
const std::string _blockEnd
std::shared_ptr< DisplayParameters > _params
int getDisplayDegree() const
void setDisplayDegree(const int displayDegree)
const ArrayOfDouble & getSolFormat() const
Definition: OutputQueue.hpp:97
Class for the representation of an array of n values.
size_t _objWidth
void setStatsFileName(const std::string &statsFile)
Definition: OutputQueue.hpp:86
size_t _maxStepLevel
static void Add(const StatsInfo &statsInfo)
Definition: OutputQueue.hpp:53
OutputLevel
Definition: OutputInfo.hpp:12
Class for the representation of an array of n strings.
static std::unique_ptr< OutputQueue > & getInstance()
void flushBlock(const OutputInfo &outputInfo)
Information for stats format (parameters DISPLAY_STATS and STATS_FILE).
Definition: StatsInfo.hpp:63
void endBlock()
void flush()
DisplayStatsTypeList _statsFileFormat
const std::string _blockStart
size_t _statsLineCount
std::ofstream _statsStream
void add(OutputInfo outputInfo)
OutputLevel _maxOutputLevel
std::string _statsFile
const DisplayStatsTypeList & getStatsFileFormat() const
Definition: OutputQueue.hpp:94
static std::unique_ptr< OutputQueue > _single
The singleton.
static void Add(OutputInfo outputInfo)
Definition: OutputQueue.hpp:38
size_t getMaxStepLevel() const
Definition: OutputQueue.hpp:80
static void Flush()
Definition: OutputQueue.hpp:74
const std::string & getStatsFileName() const
Definition: OutputQueue.hpp:88