Reference documentation for deal.II version GIT c1ddcf411d 2023-11-30 16:30:02+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Public Member Functions | Private Attributes | List of all members
ConditionalOStream Class Reference

#include <deal.II/base/conditional_ostream.h>

Public Member Functions

 ConditionalOStream (std::ostream &stream, const bool active=true)
void set_condition (const bool active)
bool is_active () const
std::ostream & get_stream () const
template<typename T >
const ConditionalOStreamoperator<< (const T &t) const
const ConditionalOStreamoperator<< (std::ostream &(*p)(std::ostream &)) const

Private Attributes

std::ostream & output_stream
bool active_flag

Detailed Description

A class that allows printing to an output stream, e.g. std::cout, depending on the ConditionalOStream object being active (default) or not. The condition of this object can be changed by set_condition() and in the constructor. This class is used in the step-17, step-18, step-32, step-33, and step-35 tutorial programs.

This class is mostly useful in parallel computations. Ordinarily, you would use std::cout to print messages like what the program is presently doing, or the number of degrees of freedom in each step. However, in parallel programs, this means that each of the MPI processes write to the screen, which yields many repetitions of the same text. To avoid it, one would have to have a designated process, say the one with MPI process number zero, do the output, and guard each write statement with an if- condition. This becomes cumbersome and clutters up the code. Rather than doing so, the present class can be used: objects of its type act just like a standard output stream, but they only print something based on a condition that can be set to, for example, mpi_process==0, so that only one process has a true condition and in all other processes writes to this object just disappear in nirvana.

The usual usage of this class is as follows:

// all processes print the following information to standard output
std::cout << "Reading parameter file on process "
<< this_mpi_process << std::endl;
// following is printed by process 0 only
pout << "Solving ..." << std::endl;
pout << "done" << std::endl;
unsigned int this_mpi_process(const MPI_Comm mpi_communicator)

Here, ‘Reading parameter file on process xy’ is printed by each process separately. In contrast to that, ‘Solving ...’ and ‘done’ is printed to standard output only once, namely by process 0.

This class is not derived from ostream. Therefore


is not possible. Instead use the is_active() function for a work- around:

if (pout.is_active())

Definition at line 80 of file conditional_ostream.h.

Constructor & Destructor Documentation

◆ ConditionalOStream()

ConditionalOStream::ConditionalOStream ( std::ostream &  stream,
const bool  active = true 

Constructor. Set the stream to which we want to write, and the condition based on which writes are actually forwarded. Per default the condition of an object is active.

Definition at line 21 of file

Member Function Documentation

◆ set_condition()

void ConditionalOStream::set_condition ( const bool  active)

Depending on the active flag set the condition of this stream to active (true) or non-active (false). An object of this class prints to cout if and only if its condition is active.

Definition at line 28 of file

◆ is_active()

bool ConditionalOStream::is_active ( ) const

Return the condition of the object.

Definition at line 35 of file

◆ get_stream()

std::ostream & ConditionalOStream::get_stream ( ) const

Return a reference to the stream currently in use.

Definition at line 168 of file conditional_ostream.h.

◆ operator<<() [1/2]

template<class T >
const ConditionalOStream & ConditionalOStream::operator<< ( const T &  t) const

Output a constant something through this stream. This function must be const so that member objects of this type can also be used from const member functions of the surrounding class.

Definition at line 147 of file conditional_ostream.h.

◆ operator<<() [2/2]

const ConditionalOStream & ConditionalOStream::operator<< ( std::ostream &(*)(std::ostream &)  p) const

Treat ostream manipulators. This function must be const so that member objects of this type can also be used from const member functions of the surrounding class.

Note that compilers want to see this treated differently from the general template above since functions like std::endl are actually overloaded and can't be bound directly to a template type.

Definition at line 157 of file conditional_ostream.h.

Member Data Documentation

◆ output_stream

std::ostream& ConditionalOStream::output_stream

Reference to the stream we want to write to.

Definition at line 135 of file conditional_ostream.h.

◆ active_flag

bool ConditionalOStream::active_flag

Stores the actual condition the object is in.

Definition at line 140 of file conditional_ostream.h.

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