MueLu Version of the Day
Loading...
Searching...
No Matches
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
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"
21#include "MueLu_Exceptions.hpp"
22
23namespace 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
53
55 verbLevel_ = verbLevel;
56}
57
59 return procRank_;
60}
61
62int VerboseObject::SetProcRankVerbose(int procRank) const {
63 int oldRank = procRank_;
64 procRank_ = procRank;
65 return oldRank;
66}
67
68bool VerboseObject::IsPrint(MsgType type, int thisProcRankOnly) const {
69 return ((type & GetVerbLevel()) && (thisProcRankOnly < 0 || procRank_ == thisProcRankOnly));
70}
71
72Teuchos::FancyOStream& VerboseObject::GetOStream(MsgType type, int thisProcRankOnly) const {
73 if (!IsPrint(type, thisProcRankOnly))
74 return *blackHole_;
75
76 Teuchos::FancyOStream& os = *GetMueLuOStream();
77 if (!(type & ((Extreme | Test) ^ Warnings)))
78 os << "\n******* WARNING *******" << std::endl;
79
80 return os;
81}
82
83Teuchos::FancyOStream& VerboseObject::GetBlackHole() const {
84 return *blackHole_;
85}
86
87void VerboseObject::SetDefaultVerbLevel(const VerbLevel defaultVerbLevel) {
88 TEUCHOS_TEST_FOR_EXCEPTION(defaultVerbLevel == NotSpecified, Exceptions::RuntimeError,
89 "MueLu::VerboseObject::GetVerbLevel(): global verbose level cannot be 'NotSpecified'.");
90 globalVerbLevel_ = defaultVerbLevel;
91}
92
96
97void VerboseObject::SetMueLuOStream(const Teuchos::RCP<Teuchos::FancyOStream>& mueluOStream) {
98 mueluOStream->setOutputToRootOnly(-1);
99 mueluOutputStream_ = mueluOStream;
100}
101
102void 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
119Teuchos::RCP<Teuchos::FancyOStream> VerboseObject::GetMueLuOStream() {
120 if (mueluOutputStream_.get() == NULL) {
121 mueluOutputStream_ = fancyOStream(rcpFromRef(std::cout));
122 mueluOutputStream_->setOutputToRootOnly(-1);
123 }
124 return mueluOutputStream_;
125}
126
127VerbLevel VerboseObject::globalVerbLevel_ = High; // Default global verbose level.
128
129RCP<Teuchos::FancyOStream> VerboseObject::blackHole_ = Teuchos::getFancyOStream(rcp(new Teuchos::oblackholestream()));
130
131RCP<Teuchos::FancyOStream> VerboseObject::mueluOutputStream_ = Teuchos::null;
132
133} // namespace MueLu
Exception throws to report errors in the internal logical of the program.
Teuchos::FancyOStream & GetOStream(MsgType type, int thisProcRankOnly=0) const
Get an output stream for outputting the input message type.
VerbLevel GetVerbLevel() const
Get the verbosity level.
int SetProcRankVerbose(int procRank) const
Set proc rank used for printing.
VerbLevel verbLevel_
Verbose level specific to 'this'.
static Teuchos::RCP< Teuchos::FancyOStream > blackHole_
static VerbLevel GetDefaultVerbLevel()
Get the default (global) verbosity level.
static Teuchos::RCP< Teuchos::FancyOStream > GetMueLuOStream()
void SetVerbLevel(const VerbLevel verbLevel)
Set the verbosity level of this object.
bool IsPrint(MsgType type, int thisProcRankOnly=-1) const
Find out whether we need to print out information for a specific message type.
static Teuchos::RCP< Teuchos::FancyOStream > mueluOutputStream_
static void SetMueLuOStream(const Teuchos::RCP< Teuchos::FancyOStream > &mueluOStream)
static VerbLevel globalVerbLevel_
Global verbose level. This verbose level is used when the verbose level of the object is not specifie...
Teuchos::FancyOStream & GetBlackHole() const
static void SetDefaultVerbLevel(const VerbLevel defaultVerbLevel)
Set the default (global) verbosity level.
virtual ~VerboseObject()
Destructor.
static void SetMueLuOFileStream(const std::string &filename)
int GetProcRankVerbose() const
Get proc rank used for printing. Do not use this information for any other purpose.
Namespace for MueLu classes and methods.
@ Warnings
Print all warning messages.