Reference documentation for deal.II version 9.2.0
|
#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 ConditionalOStream & | operator<< (const T &t) const |
const ConditionalOStream & | operator<< (std::ostream &(*p)(std::ostream &)) const |
Private Attributes | |
std::ostream & | output_stream |
bool | active_flag |
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:
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:
Definition at line 81 of file conditional_ostream.h.
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 conditional_ostream.cc.
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 conditional_ostream.cc.
bool ConditionalOStream::is_active | ( | ) | const |
Return the condition of the object.
Definition at line 35 of file conditional_ostream.cc.
|
inline |
Return a reference to the stream currently in use.
Definition at line 169 of file conditional_ostream.h.
|
inline |
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 149 of file conditional_ostream.h.
|
inline |
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 159 of file conditional_ostream.h.
|
private |
Reference to the stream we want to write to.
Definition at line 136 of file conditional_ostream.h.
|
private |
Stores the actual condition the object is in.
Definition at line 141 of file conditional_ostream.h.