MFEM v4.7.0
Finite element discretization library
Loading...
Searching...
No Matches
globals.hpp
Go to the documentation of this file.
1// Copyright (c) 2010-2024, Lawrence Livermore National Security, LLC. Produced
2// at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3// LICENSE and NOTICE for details. LLNL-CODE-806117.
4//
5// This file is part of the MFEM library. For more information and source code
6// availability visit https://mfem.org.
7//
8// MFEM is free software; you can redistribute it and/or modify it under the
9// terms of the BSD-3 license. We welcome feedback and contributions, see file
10// CONTRIBUTING.md for details.
11
12#ifndef MFEM_GLOBALS_HPP
13#define MFEM_GLOBALS_HPP
14
15#include "../config/config.hpp"
16#include <iostream>
17
18#ifdef MFEM_USE_MPI
19#include <mpi.h>
20#endif
21
22namespace mfem
23{
24
25/// Simple extension of std::ostream.
26/** This class adds the ability to enable and disable the stream. The associated
27 std::streambuf and tied std::ostream can be replaced with that of any
28 std::ostream. */
29class OutStream : public std::ostream
30{
31protected:
32 // Pointer that stores the associated streambuf when output is disabled.
33 std::streambuf *m_rdbuf;
34 // Pointer that stores the tied ostream when output is disabled.
35 std::ostream *m_tie;
36
37 void Init();
38
39public:
40 /** @brief Construct an OutStream from the given stream @a os, by using its
41 `rdbuf()`. */
42 OutStream(std::ostream &os) : std::ostream(NULL) { SetStream(os); }
43
44 /** @brief Replace the `rdbuf()` and `tie()` of the OutStream with that of
45 @a os, enabling output. */
46 void SetStream(std::ostream &os)
47 {
48 rdbuf(m_rdbuf = os.rdbuf()); tie(m_tie = os.tie()); Init();
49 }
50
51 /// Enable output.
52 void Enable() { if (!IsEnabled()) { rdbuf(m_rdbuf); tie(m_tie); } }
53 /// Disable output.
54 void Disable()
55 {
56 if (IsEnabled()) { m_rdbuf = rdbuf(NULL); m_tie = tie(NULL); }
57 }
58 /// Check if output is enabled.
59 bool IsEnabled() const { return (rdbuf() != NULL); }
60};
61
62
63/** @brief Global stream used by the library for standard output. Initially it
64 uses the same std::streambuf as std::cout, however that can be changed.
65 @sa OutStream. */
66extern MFEM_EXPORT OutStream out;
67/** @brief Global stream used by the library for standard error output.
68 Initially it uses the same std::streambuf as std::cerr, however that can be
69 changed.
70 @sa OutStream. */
71extern MFEM_EXPORT OutStream err;
72
73
74/** @brief Construct a string of the form "<prefix><myid><suffix>" where the
75 integer @a myid is padded with leading zeros to be at least @a width digits
76 long. */
77/** This is a convenience function, e.g. to redirect mfem::out to individual
78 files for each rank, one can use:
79 \code
80 std::ofstream out_file(MakeParFilename("app_out.", myid).c_str());
81 mfem::out.SetStream(out_file);
82 \endcode
83*/
84std::string MakeParFilename(const std::string &prefix, const int myid,
85 const std::string suffix = "", const int width = 6);
86
87
88#ifdef MFEM_USE_MPI
89
90/** @name MFEM "global" communicator functions.
91
92 Functions for getting and setting the MPI communicator used by the library
93 as the "global" communicator.
94
95 This "global" communicator is used for example in the function mfem_error(),
96 which is invoked when an error is detected - the "global" communicator is
97 used as a parameter to MPI_Abort() to terminate all "global" tasks. */
98///@{
99
100/// Get MFEM's "global" MPI communicator.
101MPI_Comm GetGlobalMPI_Comm();
102
103/// Set MFEM's "global" MPI communicator.
104void SetGlobalMPI_Comm(MPI_Comm comm);
105
106///@}
107
108#endif
109
110} // namespace mfem
111
112#endif
Simple extension of std::ostream.
Definition globals.hpp:30
void SetStream(std::ostream &os)
Replace the rdbuf() and tie() of the OutStream with that of os, enabling output.
Definition globals.hpp:46
void Enable()
Enable output.
Definition globals.hpp:52
OutStream(std::ostream &os)
Construct an OutStream from the given stream os, by using its rdbuf().
Definition globals.hpp:42
std::ostream * m_tie
Definition globals.hpp:35
void Disable()
Disable output.
Definition globals.hpp:54
bool IsEnabled() const
Check if output is enabled.
Definition globals.hpp:59
std::streambuf * m_rdbuf
Definition globals.hpp:33
std::string MakeParFilename(const std::string &prefix, const int myid, const std::string suffix, const int width)
Construct a string of the form "<prefix><myid><suffix>" where the integer myid is padded with leading...
Definition globals.cpp:43
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
Definition globals.hpp:66
OutStream err(std::cerr)
Global stream used by the library for standard error output. Initially it uses the same std::streambu...
Definition globals.hpp:71
void SetGlobalMPI_Comm(MPI_Comm comm)
Set MFEM's "global" MPI communicator.
Definition globals.cpp:67
MPI_Comm GetGlobalMPI_Comm()
Get MFEM's "global" MPI communicator.
Definition globals.cpp:62