MFEM  v3.3.2
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
globals.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
3 // reserved. See file COPYRIGHT for details.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability see http://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License (as published by the Free
10 // Software Foundation) version 2.1 dated February 1999.
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 
22 namespace 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. */
29 class OutStream : public std::ostream
30 {
31 protected:
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 public:
38  /** @brief Construct an OutStream from the given stream @a out, by using its
39  `rdbuf()`. */
40  OutStream(std::ostream &out) : std::ostream(NULL) { SetStream(out); }
41 
42  /** @brief Replace the `rdbuf()` and `tie()` of the OutStream with that of
43  @a out, enabling output. */
44  void SetStream(std::ostream &out)
45  {
46  rdbuf(m_rdbuf = out.rdbuf()); tie(m_tie = out.tie());
47  }
48 
49  /// Enable output.
50  void Enable() { if (!IsEnabled()) { rdbuf(m_rdbuf); tie(m_tie); } }
51  /// Disable output.
52  void Disable()
53  {
54  if (IsEnabled()) { m_rdbuf = rdbuf(NULL); m_tie = tie(NULL); }
55  }
56  /// Check if output is enabled.
57  bool IsEnabled() const { return (rdbuf() != NULL); }
58 };
59 
60 
61 /** @brief Global stream used by the library for standard output. Initially it
62  uses the same std::streambuf as std::cout, however that can be changed.
63  @sa OutStream. */
64 extern OutStream out;
65 /** @brief Global stream used by the library for standard error output.
66  Initially it uses the same std::streambuf as std::cerr, however that can be
67  changed.
68  @sa OutStream. */
69 extern OutStream err;
70 
71 
72 #ifdef MFEM_USE_MPI
73 
74 /** @name MFEM "global" communicator functions.
75 
76  Functions for getting and setting the MPI communicator used by the library
77  as the "global" communicator.
78 
79  Currently, the MFEM "global" communicator is used only by the function
80  mfem_error(), invoked when an error is detected - the "global" communicator
81  is used as a parameter to MPI_Abort() to terminate all "global" tasks. */
82 ///@{
83 
84 /// Get MFEM's "global" MPI communicator.
85 MPI_Comm GetGlobalMPI_Comm();
86 
87 /// Set MFEM's "global" MPI communicator.
88 void SetGlobalMPI_Comm(MPI_Comm comm);
89 
90 ///@}
91 
92 #endif
93 
94 } // namespace mfem
95 
96 #endif
std::ostream * m_tie
Definition: globals.hpp:35
Simple extension of std::ostream.
Definition: globals.hpp:29
void SetStream(std::ostream &out)
Replace the rdbuf() and tie() of the OutStream with that of out, enabling output. ...
Definition: globals.hpp:44
std::streambuf * m_rdbuf
Definition: globals.hpp:33
bool IsEnabled() const
Check if output is enabled.
Definition: globals.hpp:57
OutStream err(std::cerr)
Global stream used by the library for standard error output. Initially it uses the same std::streambu...
Definition: globals.hpp:69
void Disable()
Disable output.
Definition: globals.hpp:52
void Enable()
Enable output.
Definition: globals.hpp:50
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:64
void SetGlobalMPI_Comm(MPI_Comm comm)
Set MFEM&#39;s &quot;global&quot; MPI communicator.
Definition: globals.cpp:32
OutStream(std::ostream &out)
Construct an OutStream from the given stream out, by using its rdbuf().
Definition: globals.hpp:40
MPI_Comm GetGlobalMPI_Comm()
Get MFEM&#39;s &quot;global&quot; MPI communicator.
Definition: globals.cpp:27