SST  15.1.0
StructuralSimulationToolkit
statoutputtxt.h
1 // Copyright 2009-2025 NTESS. Under the terms
2 // of Contract DE-NA0003525 with NTESS, the U.S.
3 // Government retains certain rights in this software.
4 //
5 // Copyright (c) 2009-2025, NTESS
6 // All rights reserved.
7 //
8 // This file is part of the SST software package. For license
9 // information, see the LICENSE file in the top level directory of the
10 // distribution.
11 
12 #ifndef SST_CORE_STATAPI_STATOUTPUTTXT_H
13 #define SST_CORE_STATAPI_STATOUTPUTTXT_H
14 
15 #include "sst/core/sst_types.h"
16 #include "sst/core/statapi/statoutput.h"
17 
18 #ifdef HAVE_LIBZ
19 #include <zlib.h>
20 #endif
21 
22 #include <cstdio>
23 #include <string>
24 
25 namespace SST::Statistics {
26 
28 {
29 public:
30  /** Construct a StatOutputTxt
31  * @param outputParameters - Parameters used for this Statistic Output
32  */
33  explicit StatisticOutputTextBase(Params& outputParameters);
34 
35  /** This output supports adding statistics during runtime if the header is embedded in the output */
36  virtual bool supportsDynamicRegistration() const override { return m_outputInlineHeader; }
37 
38  void serialize_order(SST::Core::Serialization::serializer& ser) override;
39 
40  ImplementVirtualSerializable(SST::Statistics::StatisticOutputTextBase);
41 
42 protected:
43 
44  /** Perform a check of provided parameters
45  * @return True if all required parameters and options are acceptable
46  */
47  bool checkOutputParameters() override;
48 
49  /** Indicate to Statistic Output that simulation started.
50  * Statistic output may perform any startup code here as necessary.
51  */
52  void startOfSimulation() override;
53 
54  /** Indicate to Statistic Output that simulation ended.
55  * Statistic output may perform any shutdown code here as necessary.
56  */
57  void endOfSimulation() override;
58 
59  /** Implementation function for the start of output.
60  * This will be called by the Statistic Processing Engine to indicate that
61  * a Statistic is about to send data to the Statistic Output for processing.
62  * @param statistic - Pointer to the statistic object than the output can
63  * retrieve data from.
64  */
65  void implStartOutputEntries(StatisticBase* statistic) override;
66 
67  /** Implementation function for the end of output.
68  * This will be called by the Statistic Processing Engine to indicate that
69  * a Statistic is finished sending data to the Statistic Output for processing.
70  * The Statistic Output can perform any output related functions here.
71  */
72  void implStopOutputEntries() override;
73 
74  /** Implementation functions for output.
75  * These will be called by the statistic to provide Statistic defined
76  * data to be output.
77  * @param fieldHandle - The handle to the registered statistic field.
78  * @param data - The data related to the registered field to be output.
79  */
80  void outputField(fieldHandle_t fieldHandle, int32_t data) override;
81  void outputField(fieldHandle_t fieldHandle, uint32_t data) override;
82  void outputField(fieldHandle_t fieldHandle, int64_t data) override;
83  void outputField(fieldHandle_t fieldHandle, uint64_t data) override;
84  void outputField(fieldHandle_t fieldHandle, float data) override;
85  void outputField(fieldHandle_t fieldHandle, double data) override;
86 
87 protected:
89 
90  bool m_outputTopHeader;
91  bool m_outputInlineHeader;
92  bool m_outputSimTime;
93  bool m_outputRank;
94  bool m_useCompression;
95 
96 private:
97  bool openFile();
98  void closeFile();
99  int print(const char* fmt, ...) __attribute__((format(printf, 2, 3)));
100 
101 private:
102 #ifdef HAVE_LIBZ
103  gzFile m_gzFile;
104 #endif
105  FILE* m_hFile;
106  std::string m_outputBuffer;
107  std::string m_FilePath;
108 
109  /**
110  Returns whether or not this outputter outputs to a file
111  */
112  virtual bool outputsToFile() = 0;
113 
114  /**
115  Returns whether or not this outputter supports compression.
116  Only checked if the class also writes to a file
117  */
118  virtual bool supportsCompression() = 0;
119 
120  /**
121  Returns a prefix that will start each new output entry
122  */
123  virtual std::string getStartOutputPrefix() = 0;
124 
125  /**
126  These functions return the default value for the associated
127  flags. These are implemented by the child class so that each
128  final class can control how the defaults work.
129  */
130  virtual bool getOutputTopHeaderDefault() = 0;
131  virtual bool getOutputInlineHeaderDefault() = 0;
132  virtual bool getOutputSimTimeDefault() = 0;
133  virtual bool getOutputRankDefault() = 0;
134 
135  virtual std::string getDefaultFileName() { return ""; }
136 
137  /** True if this StatOutput can handle StatisticGroups */
138  virtual bool acceptsGroups() const override { return true; }
139 };
140 
141 
142 /**
143  \class StatisticOutputTxt
144 
145  The class for statistics output to a text file.
146 */
148 {
149 public:
150  SST_ELI_REGISTER_DERIVED(
153  "sst",
154  "statoutputtxt",
155  SST_ELI_ELEMENT_VERSION(1,0,0),
156  "Output to text file"
157  )
158 
159  SST_ELI_DOCUMENT_PARAMS(
160  { "outputtopheader", "Whether to print a header at the top of the output", "False" },
161  { "outputinlineheader", "Whether to print a field names inline with statistic output", "True" },
162  { "outputsimtime", "Whether to print the simulation time in the output", "True" },
163  { "outputrank", "Whether to print the rank in the output", "True" },
164  { "filepath", "Filepath for the output file", "./StatisticOutput.txt" },
165  { "compressed", "Whether to compress the output file", "False" }
166  )
167 
168  /** Construct a StatOutputTxt
169  * @param outputParameters - Parameters used for this Statistic Output
170  */
171  explicit StatisticOutputTxt(Params& outputParameters);
172 
173  void serialize_order(SST::Core::Serialization::serializer& ser) override;
174  ImplementSerializable(SST::Statistics::StatisticOutputTxt)
175 
176 
177 protected:
178  StatisticOutputTxt() { ; } // For serialization
179 
180 private:
181  /**
182  Returns whether or not this outputter outputs to a file
183  */
184  bool outputsToFile() override { return true; }
185 
186  /**
187  Returns whether or not this outputter supports compression.
188  Only checked if the class also writes to a file
189  */
190  bool supportsCompression() override
191  {
192 #ifdef HAVE_LIBZ
193  return true;
194 #else
195  return false;
196 #endif
197  }
198  /**
199  Returns a prefix that will start each new output entry
200  */
201  std::string getStartOutputPrefix() override { return ""; }
202 
203  /**
204  These functions return the default value for the associated
205  flags. These are implemented by the child class so that each
206  final class can control how the defaults work.
207  */
208  bool getOutputTopHeaderDefault() override { return false; }
209  bool getOutputInlineHeaderDefault() override { return true; }
210  bool getOutputSimTimeDefault() override { return true; }
211  bool getOutputRankDefault() override { return true; }
212 
213  std::string getDefaultFileName() override { return "StatisticOutput.txt"; }
214 };
215 
216 /**
217  \class StatisticOutputConsole
218 
219  The class for statistics output to the console.
220 */
222 {
223 public:
224  SST_ELI_REGISTER_DERIVED(
227  "sst",
228  "statoutputconsole",
229  SST_ELI_ELEMENT_VERSION(1,0,0),
230  "Output to console"
231  )
232 
233  SST_ELI_DOCUMENT_PARAMS(
234  { "outputtopheader", "Whether to print a header at the top of the output", "False" },
235  { "outputinlineheader", "Whether to print a field names inline with statistic output", "True" },
236  { "outputsimtime", "Whether to print the simulation time in the output", "False" },
237  { "outputrank", "Whether to print the rank in the output", "False" },
238  )
239 
240  /** Construct a StatOutputTxt
241  * @param outputParameters - Parameters used for this Statistic Output
242  */
243  explicit StatisticOutputConsole(Params& outputParameters);
244 
245  void serialize_order(SST::Core::Serialization::serializer& ser) override;
246  ImplementSerializable(SST::Statistics::StatisticOutputConsole)
247 
248 protected:
249  StatisticOutputConsole() { ; } // For serialization
250 
251 private:
252  /**
253  Returns whether or not this outputter outputs to a file
254  */
255  bool outputsToFile() override { return false; }
256 
257  /**
258  Returns whether or not this outputter supports compression.
259  Only checked if the class also writes to a file
260  */
261  bool supportsCompression() override { return false; }
262 
263  /**
264  Returns a prefix that will start each new output entry
265  */
266  std::string getStartOutputPrefix() override { return " "; }
267 
268  /**
269  These functions return the default value for the associated
270  flags. These are implemented by the child class so that each
271  final class can control how the defaults work.
272  */
273  bool getOutputTopHeaderDefault() override { return false; }
274  bool getOutputInlineHeaderDefault() override { return true; }
275  bool getOutputSimTimeDefault() override { return false; }
276  bool getOutputRankDefault() override { return false; }
277 };
278 
279 } // namespace SST::Statistics
280 
281 #endif // SST_CORE_STATAPI_STATOUTPUTTXT_H
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:42
void implStopOutputEntries() override
Implementation function for the end of output.
Definition: statoutputtxt.cc:152
Definition: statoutputtxt.h:27
void implStartOutputEntries(StatisticBase *statistic) override
Implementation function for the start of output.
Definition: statoutputtxt.cc:112
Forms the base class for statistics output generation within the SST core.
Definition: statoutput.h:52
Forms the base class for statistics gathering within SST.
Definition: statbase.h:49
virtual bool supportsDynamicRegistration() const override
This output supports adding statistics during runtime if the header is embedded in the output...
Definition: statoutputtxt.h:36
The class for statistics output to a text file.
Definition: statoutputtxt.h:147
void startOfSimulation() override
Indicate to Statistic Output that simulation started.
Definition: statoutputtxt.cc:59
Definition: statoutput.h:170
bool checkOutputParameters() override
Perform a check of provided parameters.
Definition: statoutputtxt.cc:30
void outputField(fieldHandle_t fieldHandle, int32_t data) override
Implementation functions for output.
Definition: statoutputtxt.cc:160
Parameter store.
Definition: params.h:63
StatisticOutputConsole(Params &outputParameters)
Construct a StatOutputTxt.
Definition: statoutputtxt.cc:429
void endOfSimulation() override
Indicate to Statistic Output that simulation ended.
Definition: statoutputtxt.cc:104
Definition: elementinfo.h:44
The class for statistics output to the console.
Definition: statoutputtxt.h:221