MAIA bb96820c
Multiphysics at AIA
Loading...
Searching...
No Matches
infoout.h
Go to the documentation of this file.
1// Copyright (C) 2024 The m-AIA AUTHORS
2//
3// This file is part of m-AIA (https://git.rwth-aachen.de/aia/m-AIA/m-AIA)
4//
5// SPDX-License-Identifier: LGPL-3.0-only
6
7#ifndef INFOOUT_H
8#define INFOOUT_H
9
10#include <fstream> // Needed for ofstream
11#include <sstream> // Needed for ostringstream
12#include <vector>
13#include "COMM/mpioverride.h" // Needed for MPI functionality
14#include "INCLUDE/maiatypes.h"
15
16#ifdef _SX
17#include <sys/socket.h>
18#endif
19
20
29} // namespace MAIA_INFOOUT_FILETYPES
30
39class InfoOut;
40class InfoOut_buffer : public std::stringbuf {
41 friend class InfoOut;
42
43 protected:
44 static const MInt m_fileFormatVersion = 1;
52 std::ostringstream m_tmpBuffer;
54
55 std::vector<std::pair<MString, MString>> m_prefixAttributes;
56
57 virtual MString encodeXml(const std::string& str);
58 virtual MString getXmlHeader();
59 virtual MString getXmlFooter();
60 virtual void createPrefixMessage();
61 virtual void createSuffixMessage();
62 virtual void flushBuffer() = 0;
63
64 public:
66 virtual MBool setRootOnly(MBool rootOnly = true);
67 virtual MInt setMinFlushSize(MInt minFlushSize);
68};
69
78class InfoOut : public std::ostream {
79 friend class InfoOut_buffer;
80
81 protected:
83
84 public:
85// Disable clang warning for ostream(m_buffer), although this is probably a compiler error
86#if defined(MAIA_CLANG_COMPILER)
87#pragma clang diagnostic push
88#pragma clang diagnostic ignored "-Wuninitialized"
89#endif
90 InfoOut() : std::ostream(m_buffer){};
91#if defined(MAIA_CLANG_COMPILER)
92#pragma clang diagnostic pop
93#endif
94 virtual MBool setRootOnly(MBool rootOnly = true) = 0;
95 MInt addAttribute(std::pair<MString, MString>);
96 void eraseAttribute(MInt);
97 void modifyAttribute(MInt, std::pair<MString, MString>);
98};
99
115 private:
116 static const MInt m_maxStringLength = 8192;
122 MPI_Comm m_mpiComm;
124 MPI_Request m_mpiRequest;
125
126
127 MInt setMpiWriteBuffer(MInt newBufferSize);
128
129 protected:
130 virtual MInt sync();
131 virtual void flushBuffer();
132
133 public:
135 InfoOut_mpiFileBuffer(const MString& filename, const MString& projectName, MPI_Comm mpiComm = MPI_COMM_WORLD);
137 void open(const MString& filename, const MString& projectName, MPI_Comm mpiComm = MPI_COMM_WORLD);
138 void close(MBool forceClose = false);
139 MInt setMinFlushSize(MInt minFlushSize);
140};
141
153 private:
157 std::ofstream m_file;
158 MPI_Comm m_mpiComm;
159
160 protected:
161 virtual MInt sync();
162 virtual void flushBuffer();
163
164 public:
166 InfoOut_simpleFileBuffer(const MString& filename, const MString& projectName, MPI_Comm mpiComm = MPI_COMM_WORLD,
167 MBool rootOnlyHardwired = false);
169 void open(const MString& filename, const MString& projectName, MPI_Comm mpiComm = MPI_COMM_WORLD,
170 MBool rootOnlyHardwired = false);
171 void close(MBool forceClose = false);
172};
173
174
186 private:
189 std::ostream* m_output;
190 MPI_Comm m_mpiComm;
192
193 protected:
194 virtual MInt sync();
195 virtual void flushBuffer();
196 virtual MString addPrefix(const MString& str);
197
198 public:
200 InfoOut_streamBuffer(std::ostream* os, MPI_Comm mpiComm = MPI_COMM_WORLD, MBool printDomainId = true);
201 void initialize(std::ostream* os, MPI_Comm mpiComm = MPI_COMM_WORLD, MBool printDomainId = true);
202};
203
217class InfoOutFile : public InfoOut {
218 private:
221
222 public:
223 InfoOutFile();
224 InfoOutFile(const MString& filename, const MString& projectName, MInt fileType = 0, MPI_Comm mpiComm = MPI_COMM_WORLD,
225 MBool rootOnlyHardwired = false);
226 ~InfoOutFile();
227 void open(const MString& filename, const MString& projectName, MInt fileType = 0, MPI_Comm mpiComm = MPI_COMM_WORLD,
228 MBool rootOnlyHardwired = false);
229 void close(MBool forceClose = false);
230 MBool setRootOnly(MBool rootOnly = true);
231 MInt setMinFlushSize(MInt minFlushSize);
232};
233
243class InfoOutStream : public InfoOut {
244 private:
246
247 public:
249 InfoOutStream(std::ostream* os, MPI_Comm mpiComm = MPI_COMM_WORLD, MBool printDomainId = true);
251 void initialize(std::ostream* os, MPI_Comm mpiComm = MPI_COMM_WORLD, MBool printDomainId = true);
252 MBool setRootOnly(MBool rootOnly = true);
253};
254
255#endif /* ifndef INFOOUT_H */
virtual void flushBuffer()=0
MInt m_minFlushSize
Minimum length of the internal buffer before flushing.
Definition: infoout.h:49
MInt m_domainId
Contains the MPI rank (= domain id) of this process.
Definition: infoout.h:47
virtual MInt setMinFlushSize(MInt minFlushSize)
Sets the minimum buffer length that has to be reached before the buffer is flushed.
Definition: infoout.cpp:187
InfoOut_buffer()
Generic constructor is used when no information is provided during declaration.
Definition: infoout.cpp:72
MString m_prefixMessage
Stores the prefix that is prepended to each output.
Definition: infoout.h:50
MString m_suffixMessage
Stores the suffix that is apended to each output.
Definition: infoout.h:51
std::ostringstream m_tmpBuffer
Temporary buffer to hold string until flushing.
Definition: infoout.h:52
virtual void createSuffixMessage()
Creates an XML suffix that is appended to each message.
Definition: infoout.cpp:162
std::vector< std::pair< MString, MString > > m_prefixAttributes
Definition: infoout.h:55
virtual MBool setRootOnly(MBool rootOnly=true)
Sets interal state of whether only the root domain (rank 0) should write to file.
Definition: infoout.cpp:173
virtual MString getXmlHeader()
Return an XML header that should written at the beginning of each log file.
Definition: infoout.cpp:199
virtual void createPrefixMessage()
Creates an XML prefix using the domain id that is prepended to each message.
Definition: infoout.cpp:138
MInt m_noDomains
Contains the MPI rank count (= number of domains)
Definition: infoout.h:48
MString m_projectName
Name of the current Project.
Definition: infoout.h:53
MBool m_rootOnly
Stores whether only the root domain writes a log file.
Definition: infoout.h:46
virtual MString encodeXml(const std::string &str)
Parses the string input and returns the string with XML entities escaped.
Definition: infoout.cpp:95
static const MInt m_fileFormatVersion
Definition: infoout.h:44
virtual MString getXmlFooter()
Return an XML footer that should written at the end of each log file.
Definition: infoout.cpp:299
Customized buffer to facilitate MPI I/O usage for a single file for all domains within an MPI communi...
Definition: infoout.h:114
void close(MBool forceClose=false)
Closes the MPI file.
Definition: infoout.cpp:629
MChar * m_mpiWriteBuffer
MPI write buffer.
Definition: infoout.h:119
virtual void flushBuffer()
Flushes the buffer by writing the contents to the MPI file.
Definition: infoout.cpp:714
MBool m_isOpen
Stores whether the MPI file was already opened.
Definition: infoout.h:120
MInt setMpiWriteBuffer(MInt newBufferSize)
Delete the current MPI write buffer and allocate a new one.
Definition: infoout.cpp:765
MInt m_mpiWriteBufferSize
Size of the MPI write buffer.
Definition: infoout.h:118
void open(const MString &filename, const MString &projectName, MPI_Comm mpiComm=MPI_COMM_WORLD)
Initialization of the MPI I/O environment.
Definition: infoout.cpp:566
MPI_Request m_mpiRequest
MPI request object (nonblocking I/O)
Definition: infoout.h:124
MInt m_maxMessageLength
Maximum message length (excluding formatting)
Definition: infoout.h:117
MString m_filename
Filename on disk.
Definition: infoout.h:121
MPI_File m_mpiFileHandle
MPI file handle.
Definition: infoout.h:123
virtual MInt sync()
Flushes the buffer if flushing conditions are met.
Definition: infoout.cpp:677
static const MInt m_maxStringLength
Maximum string length (including formatting, default: 4096)
Definition: infoout.h:116
MInt setMinFlushSize(MInt minFlushSize)
Sets the minimum buffer length that has to be reached before the buffer is flushed.
Definition: infoout.cpp:742
InfoOut_mpiFileBuffer()
Generic constructor is used when no information is provided during declaration.
Definition: infoout.cpp:504
~InfoOut_mpiFileBuffer()
Destructor calls close() to close the MPI file (if opened) and deletes the MPI write buffer.
Definition: infoout.cpp:545
MPI_Comm m_mpiComm
MPI communicator group.
Definition: infoout.h:122
Customized buffer to facilitate of a regular physical file for each processor within an MPI communica...
Definition: infoout.h:152
~InfoOut_simpleFileBuffer()
Destructor calls close() to close the file.
Definition: infoout.cpp:359
MString m_filename
Filename on disk.
Definition: infoout.h:156
void close(MBool forceClose=false)
Closes the file.
Definition: infoout.cpp:424
virtual void flushBuffer()
Flushes the buffer by writing the contents to the file.
Definition: infoout.cpp:487
MBool m_isOpen
Stores whether the file(s) were already opened.
Definition: infoout.h:154
virtual MInt sync()
Flushes the buffer by writing the contents to the file.
Definition: infoout.cpp:454
MBool m_rootOnlyHardwired
If true, only domain 0 opens and uses a file.
Definition: infoout.h:155
void open(const MString &filename, const MString &projectName, MPI_Comm mpiComm=MPI_COMM_WORLD, MBool rootOnlyHardwired=false)
Initialization of the file I/O environment.
Definition: infoout.cpp:376
InfoOut_simpleFileBuffer()
Generic constructor is used when no information is provided during declaration.
Definition: infoout.cpp:331
MPI_Comm m_mpiComm
MPI communicator group.
Definition: infoout.h:158
std::ofstream m_file
File stream tied to physical file on disk.
Definition: infoout.h:157
Customized string buffer to prepend cout/cerr with the domain id.
Definition: infoout.h:185
virtual MString addPrefix(const MString &str)
Definition: infoout.cpp:867
virtual void flushBuffer()
This function does nothing for this class, since as of now there is no intermediate buffering.
Definition: infoout.cpp:919
virtual MInt sync()
Flushes the buffer by writing the contents to the asssociated output stream.
Definition: infoout.cpp:894
std::ostream * m_output
Stores the output stream (usually cout or cerr) that will be modified.
Definition: infoout.h:189
InfoOut_streamBuffer()
Default constructor does basically nothing.
Definition: infoout.cpp:789
void initialize(std::ostream *os, MPI_Comm mpiComm=MPI_COMM_WORLD, MBool printDomainId=true)
Initializes the buffer to make it ready for use.
Definition: infoout.cpp:820
MPI_Comm m_mpiComm
MPI communicator group.
Definition: infoout.h:190
MBool m_isInitialized
Stores whether a stream was already associated with this buffer.
Definition: infoout.h:187
MBool m_isDisabled
Stores wether output in streamBuffer should be written.
Definition: infoout.h:191
MBool m_printDomainId
Stores whether the domain id should be prepended to each output.
Definition: infoout.h:188
Class to create a create an output stream for a writable file, using either MPI I/O or a physical fil...
Definition: infoout.h:217
MBool m_isOpen
Stores whether a file was already opened or not.
Definition: infoout.h:220
InfoOutFile()
Default constructor creates (virtual) file that cannot yet be used.
Definition: infoout.cpp:930
~InfoOutFile()
Destructor closes the stream.
Definition: infoout.cpp:956
MInt m_fileType
File type that is opened.
Definition: infoout.h:219
void open(const MString &filename, const MString &projectName, MInt fileType=0, MPI_Comm mpiComm=MPI_COMM_WORLD, MBool rootOnlyHardwired=false)
Opens a file by passing the parameters to InfoOut_<xyz>FileBuffer::open(...).
Definition: infoout.cpp:975
void close(MBool forceClose=false)
Pass the close call to the respective internal buffer.
Definition: infoout.cpp:1011
MBool setRootOnly(MBool rootOnly=true)
Sets interal state of whether only the root domain (rank 0) should write to file.
Definition: infoout.cpp:1043
MInt setMinFlushSize(MInt minFlushSize)
Sets the minimum buffer length that has to be reached before the buffer is flushed.
Definition: infoout.cpp:1055
Base class for all InfoOut<xyz> classes.
Definition: infoout.h:78
void modifyAttribute(MInt, std::pair< MString, MString >)
Modifies an attribute of the prefix of the XML string.
Definition: infoout.cpp:62
void eraseAttribute(MInt)
Erases an attribute from the prefix of the XML string.
Definition: infoout.cpp:50
InfoOut()
Definition: infoout.h:90
virtual MBool setRootOnly(MBool rootOnly=true)=0
InfoOut_buffer * m_buffer
Definition: infoout.h:82
MInt addAttribute(std::pair< MString, MString >)
Adds an attribute to the prefix of the XML string.
Definition: infoout.cpp:38
Class to create an output stream that writes to cout or cerr but prepends each line with the MPI rank...
Definition: infoout.h:243
InfoOutStream()
Default construtor creates InfoOutStream that is not (yet) usable.
Definition: infoout.cpp:1070
MBool setRootOnly(MBool rootOnly=true)
Sets interal state of whether only the root domain (rank 0) should write to file. Author Michael Schl...
Definition: infoout.cpp:1132
MBool m_isInitialized
Stores whether a stream was already opened or not.
Definition: infoout.h:245
void initialize(std::ostream *os, MPI_Comm mpiComm=MPI_COMM_WORLD, MBool printDomainId=true)
Passes the parameters to a call of InfoOut_streamBuffer::initialize().
Definition: infoout.cpp:1112
~InfoOutStream()
Destructor deletes the internal buffer object.
Definition: infoout.cpp:1095
int32_t MInt
Definition: maiatypes.h:62
std::basic_string< char > MString
Definition: maiatypes.h:55
bool MBool
Definition: maiatypes.h:58
char MChar
Definition: maiatypes.h:56
Namespace to hold all supported filetypes within the InfoOutFile.
Definition: infoout.h:26
const MInt MAIA_INFOOUT_SIMPLE_FILE
Use a physical file for each domain.
Definition: infoout.h:28
const MInt MAIA_INFOOUT_MPI_FILE
Use a single file for all domains (MPI I/O)
Definition: infoout.h:27