| 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 |
|
| 25 |
VerboseObject::VerboseObject() |
| 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 |
|
| 48 |
VerboseObject::~VerboseObject() {} |
| 49 |
|
| 50 |
VerbLevel VerboseObject::GetVerbLevel() const { |
| 51 |
return (verbLevel_ != NotSpecified ? verbLevel_ : globalVerbLevel_); |
| 52 |
} |
| 53 |
|
| 54 |
void VerboseObject::SetVerbLevel(const VerbLevel verbLevel) { |
| 55 |
verbLevel_ = verbLevel; |
| 56 |
} |
| 57 |
|
| 58 |
int VerboseObject::GetProcRankVerbose() const { |
| 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 |
|
| 76 |
Teuchos::FancyOStream& os = *GetMueLuOStream(); |
| 77 |
if (!(type & ((Extreme | Test) ^ Warnings))) |
| 78 |
os << "\n******* WARNING *******" << std::endl; |
| 79 |
|
| 80 |
return os; |
| 81 |
} |
| 82 |
|
| 83 |
Teuchos::FancyOStream& VerboseObject::GetBlackHole() const { |
| 84 |
return *blackHole_; |
| 85 |
} |
| 86 |
|
| 87 |
void 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 |
|
| 93 |
VerbLevel VerboseObject::GetDefaultVerbLevel() { |
| 94 |
return globalVerbLevel_; |
| 95 |
} |
| 96 |
|
| 97 |
void VerboseObject::SetMueLuOStream(const Teuchos::RCP<Teuchos::FancyOStream>& mueluOStream) { |
| 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 |
|
| 119 |
Teuchos::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 |
|
| 127 |
VerbLevel VerboseObject::globalVerbLevel_ = High; // Default global verbose level. |
| 128 |
|
| 129 |
RCP<Teuchos::FancyOStream> VerboseObject::blackHole_ = Teuchos::getFancyOStream(rcp(new Teuchos::oblackholestream())); |
| 130 |
|
| 131 |
RCP<Teuchos::FancyOStream> VerboseObject::mueluOutputStream_ = Teuchos::null; |
| 132 |
|
| 133 |
} // namespace MueLu |