11 #include "Teko_DiagnosticPreconditionerFactory.hpp"
13 #include "Teko_PreconditionerInverseFactory.hpp"
16 #include "Teuchos_TimeMonitor.hpp"
22 : outputStream_(Teko::getOutputStream()),
23 invFactory_(Teuchos::null),
24 diagString_(
"<label me!>"),
25 printResidual_(false) {}
31 const Teuchos::RCP<Teko::InverseFactory>& invFactory,
const std::string& label,
32 const Teuchos::RCP<std::ostream>& os,
bool printResidual)
33 : outputStream_(Teko::getOutputStream()),
34 invFactory_(invFactory),
35 precFactory_(Teuchos::null),
37 printResidual_(printResidual) {
38 initTimers(diagString_);
40 if (os != Teuchos::null) outputStream_ = os;
47 const Teuchos::RCP<Teko::InverseFactory>& invFactory,
48 const Teuchos::RCP<Teko::InverseFactory>& precFactory,
const std::string& label,
49 const Teuchos::RCP<std::ostream>& os,
bool printResidual)
50 : outputStream_(Teko::getOutputStream()),
51 invFactory_(invFactory),
52 precFactory_(precFactory),
54 printResidual_(printResidual) {
55 initTimers(diagString_);
57 if (os != Teuchos::null) outputStream_ = os;
60 double DiagnosticPreconditionerFactory::totalInitialBuildTime()
const {
61 return buildTimer_->totalElapsedTime() + precBuildTimer_->totalElapsedTime();
64 double DiagnosticPreconditionerFactory::totalRebuildTime()
const {
65 return rebuildTimer_->totalElapsedTime() + precRebuildTimer_->totalElapsedTime();
70 if (buildTimer_ == Teuchos::null || rebuildTimer_ == Teuchos::null) {
77 double initBuildTime = totalInitialBuildTime();
78 int initBuilds = numInitialBuilds();
80 double initRebuildTime = totalRebuildTime();
81 int initRebuilds = numRebuilds();
83 (*outputStream_) <<
"DiagnosticPreconditionerFactory \"" << diagString_ <<
"\":\n";
86 (*outputStream_) <<
" build elapsed = " << initBuildTime <<
", "
87 <<
"num builds = " << initBuilds <<
", ";
89 (*outputStream_) <<
"timer/app = " << initBuildTime / double(initBuilds) <<
"\n";
91 (*outputStream_) <<
"timer/app = "
96 (*outputStream_) <<
" rebuild elapsed = " << initRebuildTime <<
", "
97 <<
"num rebuilds = " << initRebuilds <<
", ";
99 (*outputStream_) <<
"timer/app = " << initRebuildTime / double(initRebuilds) <<
"\n";
101 (*outputStream_) <<
"timer/app = "
106 (*outputStream_) <<
" total elapsed = " << initRebuildTime + initBuildTime <<
", "
107 <<
"num rebuilds = " << initRebuilds + initBuilds <<
", ";
108 if (initBuilds + initRebuilds > 0)
109 (*outputStream_) <<
"timer/app = "
110 << (initRebuildTime + initBuildTime) /
double(initRebuilds + initBuilds)
113 (*outputStream_) <<
"timer/app = "
114 <<
"none" << std::endl;
123 using Teuchos::rcp_dynamic_cast;
125 TEUCHOS_TEST_FOR_EXCEPTION(
126 invFactory_ == Teuchos::null, std::runtime_error,
127 "ERROR: Teko::DiagnosticPreconditionerFactory::buildPreconditionerOperator requires that an "
128 <<
"inverse factory has been set. Currently it is null!");
130 TEUCHOS_TEST_FOR_EXCEPTION(
131 buildTimer_ == Teuchos::null || rebuildTimer_ == Teuchos::null, std::runtime_error,
132 "ERROR: Teko::DiagnosticPreconditionerFactory::buildPreconditionerOperator requires that "
133 <<
"the timers be initialized. Currently they are null! (label = \"" << diagString_
137 ModifiableLinearOp& diagOp_ptr = state.
getModifiableOp(
"diagnosticOp");
138 ModifiableLinearOp& diagOp_prec_ptr = state.
getModifiableOp(
"prec_diagnosticOp");
140 if (precFactory_ != Teuchos::null) {
141 if (diagOp_prec_ptr == Teuchos::null) {
144 Teuchos::TimeMonitor monitor(*precBuildTimer_,
false);
146 diagOp_prec_ptr = precFactory_->buildInverse(lo);
151 Teuchos::TimeMonitor monitor(*precRebuildTimer_,
false);
156 if (diagOp_ptr == Teuchos::null) {
157 ModifiableLinearOp invOp;
160 Teuchos::TimeMonitor monitor(*buildTimer_,
false);
162 if (diagOp_prec_ptr.is_null())
170 diagOp_ptr = createDiagnosticLinearOp(outputStream_, lo, invOp, diagString_);
172 diagOp_ptr = createDiagnosticLinearOp(outputStream_, invOp, diagString_);
177 if (printResidual_) diagOp->setForwardOp(lo);
179 ModifiableLinearOp invOp = diagOp->getModifiableOp();
182 Teuchos::TimeMonitor monitor(*rebuildTimer_,
false);
184 if (diagOp_prec_ptr.is_null())
191 return diagOp_ptr.getConst();
198 const Teuchos::ParameterList& settings) {
199 TEUCHOS_TEST_FOR_EXCEPTION(
200 not settings.isParameter(
"Inverse Factory"), std::runtime_error,
201 "Parameter \"Inverse Factory\" is required by a Teko::DiagnosticPreconditionerFactory");
202 TEUCHOS_TEST_FOR_EXCEPTION(
203 not settings.isParameter(
"Descriptive Label"), std::runtime_error,
204 "Parameter \"Descriptive Label\" is required by a Teko::DiagnosticPreconditionerFactory");
207 std::string invName = settings.get<std::string>(
"Inverse Factory");
208 std::string precName =
"";
209 if (settings.isParameter(
"Preconditioner Factory"))
210 precName = settings.get<std::string>(
"Preconditioner Factory");
211 diagString_ = settings.get<std::string>(
"Descriptive Label");
215 invFactory_ = il->getInverseFactory(invName);
216 if (precName !=
"") precFactory_ = il->getInverseFactory(precName);
217 TEUCHOS_TEST_FOR_EXCEPTION(invFactory_ == Teuchos::null, std::runtime_error,
218 "ERROR: \"Inverse Factory\" = " << invName <<
" could not be found");
220 if (settings.isParameter(
"Print Residual")) printResidual_ = settings.get<
bool>(
"Print Residual");
223 initTimers(diagString_);
231 TEUCHOS_TEST_FOR_EXCEPTION(
232 invFactory_ == Teuchos::null, std::runtime_error,
233 "ERROR: Teko::DiagnosticPreconditionerFactory::getRequestedParameters requires that a "
234 <<
"preconditioner factory has been set. Currently it is null!");
236 return invFactory_->getRequestedParameters();
242 TEUCHOS_TEST_FOR_EXCEPTION(
243 invFactory_ == Teuchos::null, std::runtime_error,
244 "ERROR: Teko::DiagnosticPreconditionerFactory::updateRequestedParameters requires that a "
245 <<
"preconditioner factory has been set. Currently it is null!");
248 success &= invFactory_->updateRequestedParameters(pl);
249 if (precFactory_) success &= precFactory_->updateRequestedParameters(pl);
254 void DiagnosticPreconditionerFactory::initTimers(
const std::string& str) {
255 buildTimer_ = Teuchos::rcp(
new Teuchos::Time(str +
" buildTimer"));
256 rebuildTimer_ = Teuchos::rcp(
new Teuchos::Time(str +
" rebuildTimer"));
257 precBuildTimer_ = Teuchos::rcp(
new Teuchos::Time(str +
" precBuildTimer"));
258 precRebuildTimer_ = Teuchos::rcp(
new Teuchos::Time(str +
" precRebuildTimer"));
void rebuildInverse(const InverseFactory &factory, const LinearOp &A, InverseLinearOp &invA)
virtual Teuchos::RCP< Teuchos::ParameterList > getRequestedParameters() const
Request the additional parameters this preconditioner factory needs.
virtual ~DiagnosticPreconditionerFactory()
default destructor: prints out diagnostic string
DiagnosticPreconditionerFactory()
Default constructor, for use with the AutoClone class.
virtual bool updateRequestedParameters(const Teuchos::ParameterList &pl)
Update this object with the fields from a parameter list.
This linear operator prints diagnostics about operator application and creation times. It is useful for debugging problems and determining bottle necks.
virtual void addModifiableOp(const std::string &name, const Teko::ModifiableLinearOp &mlo)
Add a named operator to the state object.
InverseLinearOp buildInverse(const InverseFactory &factory, const LinearOp &A)
Build an inverse operator using a factory and a linear operator.
Teuchos::RCP< const InverseLibrary > getInverseLibrary() const
Get the inverse library used by this preconditioner factory.
virtual void initializeFromParameterList(const Teuchos::ParameterList &settings)
This function builds the internals of the preconditioner factory from a parameter list...
An implementation of a state object preconditioners.
virtual LinearOp buildPreconditionerOperator(LinearOp &lo, PreconditionerState &state) const
Function that is called to build the preconditioner for the linear operator that is passed in...
virtual Teko::ModifiableLinearOp & getModifiableOp(const std::string &name)
Add a named operator to the state object.