MFEM  v3.4
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 /** @brief Construct a string of the form "<prefix><myid><suffix>" where the
73  integer @a myid is padded with leading zeros to be at least @a width digits
74  long. */
75 /** This is a convenience function, e.g. to redirect mfem::out to individual
76  files for each rank, one can use:
77  \code
78  std::ofstream out_file(MakeParFilename("app_out.", myid).c_str());
79  mfem::out.SetStream(out_file);
80  \endcode
81 */
82 std::string MakeParFilename(const std::string &prefix, const int myid,
83  const std::string suffix = "", const int width = 6);
84 
85 
86 #ifdef MFEM_USE_MPI
87 
88 /** @name MFEM "global" communicator functions.
89 
90  Functions for getting and setting the MPI communicator used by the library
91  as the "global" communicator.
92 
93  Currently, the MFEM "global" communicator is used only by the function
94  mfem_error(), invoked when an error is detected - the "global" communicator
95  is used as a parameter to MPI_Abort() to terminate all "global" tasks. */
96 ///@{
97 
98 /// Get MFEM's "global" MPI communicator.
99 MPI_Comm GetGlobalMPI_Comm();
100 
101 /// Set MFEM's "global" MPI communicator.
102 void SetGlobalMPI_Comm(MPI_Comm comm);
103 
104 ///@}
105 
106 #endif
107 
108 } // namespace mfem
109 
110 #endif
std::ostream * m_tie
Definition: globals.hpp:35
std::string MakeParFilename(const std::string &prefix, const int myid, const std::string suffix, const int width)
Construct a string of the form &quot;&lt;prefix&gt;&lt;myid&gt;&lt;suffix&gt;&quot; where the integer myid is padded with leading...
Definition: globals.cpp:26
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:44
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:39