MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_VerboseObject.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // MueLu: A package for multigrid based preconditioning
4 //
5 // Copyright 2012 NTESS and the MueLu contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include "MueLu_VerboseObject.hpp"
11 
12 #include <fstream>
13 #ifdef HAVE_MPI
14 #include <mpi.h>
15 #endif
16 
17 #include <Teuchos_VerboseObject.hpp>
18 
19 #include "MueLu_ConfigDefs.hpp"
20 #include "MueLu_VerbosityLevel.hpp"
21 #include "MueLu_Exceptions.hpp"
22 
23 namespace MueLu {
24 
26  : verbLevel_(NotSpecified)
27  , // = use global verbose level by default
28  numProcs_(0) {
29  // Note: using MPI_COMM_RANK is bad idea (because a subcommunicator may be used to run MueLu)
30  // Belos have the same problem in the class BelosOutputManager.
31  //
32  // How to fix this: the FancyOStream provides setProcRankAndSize() to change the proc rank info used by setOutputToRootOnly().
33  // Adding a method FancyOStream::getProcRank() would be enough. And it makes sense to use the info that come from the stream configuration.
34  //
35  // Documentation: after the patch, it migh be nice to add to the documentation (Teuchos and MueLu)
36  // that users of subcommunicators have to setup the output stream (of Teuchos::VerboseObject) separately.
37  procRank_ = 0;
38 #ifdef HAVE_MPI
39  int mpiStarted = 0;
40  MPI_Initialized(&mpiStarted);
41  if (mpiStarted) {
42  MPI_Comm_rank(MPI_COMM_WORLD, &procRank_);
43  MPI_Comm_size(MPI_COMM_WORLD, &numProcs_);
44  }
45 #endif
46 }
47 
49 
52 }
53 
54 void VerboseObject::SetVerbLevel(const VerbLevel verbLevel) {
55  verbLevel_ = verbLevel;
56 }
57 
59  return procRank_;
60 }
61 
62 int VerboseObject::SetProcRankVerbose(int procRank) const {
63  int oldRank = procRank_;
64  procRank_ = procRank;
65  return oldRank;
66 }
67 
68 bool VerboseObject::IsPrint(MsgType type, int thisProcRankOnly) const {
69  return ((type & GetVerbLevel()) && (thisProcRankOnly < 0 || procRank_ == thisProcRankOnly));
70 }
71 
72 Teuchos::FancyOStream& VerboseObject::GetOStream(MsgType type, int thisProcRankOnly) const {
73  if (!IsPrint(type, thisProcRankOnly))
74  return *blackHole_;
75 
77  if (!(type & ((Extreme | Test) ^ Warnings)))
78  os << "\n******* WARNING *******" << std::endl;
79 
80  return os;
81 }
82 
84  return *blackHole_;
85 }
86 
87 void VerboseObject::SetDefaultVerbLevel(const VerbLevel defaultVerbLevel) {
89  "MueLu::VerboseObject::GetVerbLevel(): global verbose level cannot be 'NotSpecified'.");
90  globalVerbLevel_ = defaultVerbLevel;
91 }
92 
94  return globalVerbLevel_;
95 }
96 
98  mueluOStream->setOutputToRootOnly(-1);
99  mueluOutputStream_ = mueluOStream;
100 }
101 
102 void VerboseObject::SetMueLuOFileStream(const std::string& filename) {
103  std::string fn;
104 #ifdef HAVE_MPI
105  int mpiStarted = 0;
106  MPI_Initialized(&mpiStarted);
107  if (mpiStarted) {
108  int procRank;
109  MPI_Comm_rank(MPI_COMM_WORLD, &procRank);
110  fn = filename + "." + std::to_string(procRank);
111  } else
112 #endif
113  fn = filename;
114  RCP<std::ofstream> outFile(new std::ofstream(fn));
115  Teuchos::RCP<Teuchos::FancyOStream> fancyOutFile = Teuchos::fancyOStream(Teuchos::rcp_implicit_cast<std::ostream>(outFile));
116  SetMueLuOStream(fancyOutFile);
117 }
118 
120  if (mueluOutputStream_.get() == NULL) {
121  mueluOutputStream_ = fancyOStream(rcpFromRef(std::cout));
123  }
124  return mueluOutputStream_;
125 }
126 
127 VerbLevel VerboseObject::globalVerbLevel_ = High; // Default global verbose level.
128 
130 
132 
133 } // namespace MueLu
static VerbLevel GetDefaultVerbLevel()
Get the default (global) verbosity level.
Teuchos::FancyOStream & GetOStream(MsgType type, int thisProcRankOnly=0) const
Get an output stream for outputting the input message type.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Teuchos::FancyOStream & GetBlackHole() const
T * get() const
static Teuchos::RCP< Teuchos::FancyOStream > mueluOutputStream_
static Teuchos::RCP< Teuchos::FancyOStream > GetMueLuOStream()
static void SetMueLuOFileStream(const std::string &filename)
static VerbLevel globalVerbLevel_
Global verbose level. This verbose level is used when the verbose level of the object is not specifie...
static void SetDefaultVerbLevel(const VerbLevel defaultVerbLevel)
Set the default (global) verbosity level.
static Teuchos::RCP< Teuchos::FancyOStream > blackHole_
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
int GetProcRankVerbose() const
Get proc rank used for printing. Do not use this information for any other purpose.
bool IsPrint(MsgType type, int thisProcRankOnly=-1) const
Find out whether we need to print out information for a specific message type.
VerbLevel GetVerbLevel() const
Get the verbosity level.
basic_FancyOStream & setOutputToRootOnly(const int rootRank)
virtual ~VerboseObject()
Destructor.
VerbLevel verbLevel_
Verbose level specific to &#39;this&#39;.
int SetProcRankVerbose(int procRank) const
Set proc rank used for printing.
void SetVerbLevel(const VerbLevel verbLevel)
Set the verbosity level of this object.
Exception throws to report errors in the internal logical of the program.
Print all warning messages.
static void SetMueLuOStream(const Teuchos::RCP< Teuchos::FancyOStream > &mueluOStream)