MAIA bb96820c
Multiphysics at AIA
Loading...
Searching...
No Matches
InfoOutFile Class Reference

Class to create a create an output stream for a writable file, using either MPI I/O or a physical file. More...

#include <infoout.h>

Inheritance diagram for InfoOutFile:
[legend]
Collaboration diagram for InfoOutFile:
[legend]

Public Member Functions

 InfoOutFile ()
 Default constructor creates (virtual) file that cannot yet be used. More...
 
 InfoOutFile (const MString &filename, const MString &projectName, MInt fileType=0, MPI_Comm mpiComm=MPI_COMM_WORLD, MBool rootOnlyHardwired=false)
 Constructor creates InfoOut_mpiFileBuffer buffer and calls ostream constructor with reference to it. More...
 
 ~InfoOutFile ()
 Destructor closes the stream. More...
 
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(...). More...
 
void close (MBool forceClose=false)
 Pass the close call to the respective internal buffer. More...
 
MBool setRootOnly (MBool rootOnly=true)
 Sets interal state of whether only the root domain (rank 0) should write to file. More...
 
MInt setMinFlushSize (MInt minFlushSize)
 Sets the minimum buffer length that has to be reached before the buffer is flushed. More...
 
- Public Member Functions inherited from InfoOut
 InfoOut ()
 
virtual MBool setRootOnly (MBool rootOnly=true)=0
 
MInt addAttribute (std::pair< MString, MString >)
 Adds an attribute to the prefix of the XML string. More...
 
void eraseAttribute (MInt)
 Erases an attribute from the prefix of the XML string. More...
 
void modifyAttribute (MInt, std::pair< MString, MString >)
 Modifies an attribute of the prefix of the XML string. More...
 

Private Attributes

MInt m_fileType
 File type that is opened. More...
 
MBool m_isOpen
 Stores whether a file was already opened or not. More...
 

Additional Inherited Members

- Protected Attributes inherited from InfoOut
InfoOut_bufferm_buffer = nullptr
 

Detailed Description

Author
Michael Schlottke
Date
June 2012

This class can be used to open a file on all processors (alternatively: only on a specified MPI communicator) and write to it using the normal C++ stream syntax (i.e. just like cout or cerr). Internally, it uses a InfoOut_mpiFileBuffer object as the internal buffer, and can thus hide all MPI-related commands from the user.

Alternatively, it is also possible to use regular physical files for each processor, and to write directly to it using a regular ofstream. This mode also allows for the setting that only process 0 within an MPI communicator opens a file to write to.

Definition at line 217 of file infoout.h.

Constructor & Destructor Documentation

◆ InfoOutFile() [1/2]

InfoOutFile::InfoOutFile ( )
Author
Michael Schlottke
Date
April 2012

When this constructor is used, open() needs to be called before the stream can be used.

Definition at line 930 of file infoout.cpp.

930 : m_fileType(0), m_isOpen(false) {
931 // Nothing here
932}
MBool m_isOpen
Stores whether a file was already opened or not.
Definition: infoout.h:220
MInt m_fileType
File type that is opened.
Definition: infoout.h:219

◆ InfoOutFile() [2/2]

InfoOutFile::InfoOutFile ( const MString filename,
const MString projectName,
MInt  fileType = 0,
MPI_Comm  mpiComm = MPI_COMM_WORLD,
MBool  rootOnlyHardwired = false 
)
Author
Michael Schlottke
Date
April 2012

When this constructor is used, the stream is immediately ready to use. For information about the paramters, please have a look at InfoOut_mpiFileBuffer::open.

Parameters
[in]filenameName of the file to open.
[in]mpiCommMPI communicator for which to open the file.
[in]projectNameProjectname given.

Definition at line 945 of file infoout.cpp.

947 : m_fileType(0), m_isOpen(false) {
948 open(filename, projectName, fileType, mpiComm, rootOnlyHardwired);
949}
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

◆ ~InfoOutFile()

InfoOutFile::~InfoOutFile ( )
Author
Michael Schlottke
Date
June 2012

Definition at line 956 of file infoout.cpp.

956{ close(); }
void close(MBool forceClose=false)
Pass the close call to the respective internal buffer.
Definition: infoout.cpp:1011

Member Function Documentation

◆ close()

void InfoOutFile::close ( MBool  forceClose = false)
Author
Michael Schlottke
Date
June 2012

All attempts to write to the stream after closing it will be discarded.

Definition at line 1011 of file infoout.cpp.

1011 {
1012 // Only close file if was already opened
1013 if(m_isOpen) {
1014 // Determine correct cast by evaluating internal file type variable
1015 switch(m_fileType) {
1017 static_cast<InfoOut_simpleFileBuffer*>(m_buffer)->close(forceClose);
1018 break;
1020 static_cast<InfoOut_mpiFileBuffer*>(m_buffer)->close(forceClose);
1021 break;
1022 default: {
1023 mTerm(1, AT_, "Unknown file type");
1024 }
1025 }
1026
1027 // Delete internal buffer to prevent memory leaks
1028 delete m_buffer;
1029
1030 // Set state variable
1031 m_isOpen = false;
1032 }
1033}
Customized buffer to facilitate MPI I/O usage for a single file for all domains within an MPI communi...
Definition: infoout.h:114
Customized buffer to facilitate of a regular physical file for each processor within an MPI communica...
Definition: infoout.h:152
InfoOut_buffer * m_buffer
Definition: infoout.h:82
void mTerm(const MInt errorCode, const MString &location, const MString &message)
Definition: functions.cpp:29
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

◆ open()

void InfoOutFile::open ( const MString filename,
const MString projectName,
MInt  fileType = 0,
MPI_Comm  mpiComm = MPI_COMM_WORLD,
MBool  rootOnlyHardwired = false 
)
Author
Michael Schlottke
Date
April 2012

The parameter fileType can be any of the constants defined in namespace MAIA_INFOOUT_FILETYPES. This method then creates a new internal buffer anbd passes along the parameters.

Parameters
[in]filenameName of the file to open.
[in]fileTypeType of file that should be opened. Can be any of the constants defined in MAIA_INFOOUT_FILETYPES.
[in]mpiCommMPI communicator for which to open the file.
[in]rootOnlyHardwiredIf true, only rank 0 creates a file and writes to it. On all other processors, no file is opened and at each flushing of the buffer, the buffer content is discarded. This parameter makes only sense when using MAIA_INFOOUT_SIMPLE_FILE and is ignored otherwise.
[in]projectNameName of the executed program.

Definition at line 975 of file infoout.cpp.

976 {
977 // Only open file if it was not yet opened
978 if(!m_isOpen) {
979 // Save file type to member variable
980 m_fileType = fileType;
981
982 // Create a new buffer object depending on the specified file type
983 switch(m_fileType) {
985 // Open a simple file
986 m_buffer = new InfoOut_simpleFileBuffer(filename, projectName, mpiComm, rootOnlyHardwired);
987 break;
989 // Open an MPI file
990 default:
991 // This is also the default behavior if no valid fileType was specified (or none at all).
993 m_buffer = new InfoOut_mpiFileBuffer(filename, projectName, mpiComm);
994 break;
995 }
996
997 // Associate the stream with the newly created buffer
998 rdbuf(m_buffer);
999
1000 // Set state variable
1001 m_isOpen = true;
1002 }
1003}

◆ setMinFlushSize()

MInt InfoOutFile::setMinFlushSize ( MInt  minFlushSize)
Author
Michael Schlottke
Date
June 2012

Flushing the buffer means that the contents of the buffer in memory is written to the file. If the file stream was not opened yet, this method just returns 0 and does nothing else.

\params[in] minFlushSize Minimum buffer length.

Returns
The previous value of the minimum flush size.

Definition at line 1055 of file infoout.cpp.

1055 {
1056 if(m_isOpen) {
1057 return m_buffer->setMinFlushSize(minFlushSize);
1058 } else {
1059 return 0;
1060 }
1061}
virtual MInt setMinFlushSize(MInt minFlushSize)
Sets the minimum buffer length that has to be reached before the buffer is flushed.
Definition: infoout.cpp:187

◆ setRootOnly()

MBool InfoOutFile::setRootOnly ( MBool  rootOnly = true)
virtual
Author
Michael Schlottke
Date
June 2012

\params[in] rootOnly If true, only rank 0 of the specified MPI communicator writes to file.

Returns
The previous internal state (may be stored to return to the previous behavior).

Implements InfoOut.

Definition at line 1043 of file infoout.cpp.

1043{ return m_buffer->setRootOnly(rootOnly); }
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

Member Data Documentation

◆ m_fileType

MInt InfoOutFile::m_fileType
private

Definition at line 219 of file infoout.h.

◆ m_isOpen

MBool InfoOutFile::m_isOpen
private

Definition at line 220 of file infoout.h.


The documentation for this class was generated from the following files: