1 #include "Teko_StratimikosFactory.hpp" 3 #include "Teuchos_Time.hpp" 4 #include "Teuchos_AbstractFactoryStd.hpp" 6 #include "Thyra_EpetraOperatorViewExtractorStd.hpp" 7 #include "Thyra_EpetraLinearOp.hpp" 8 #include "Thyra_DefaultPreconditioner.hpp" 10 #include "Teko_InverseLibrary.hpp" 11 #include "Teko_Preconditioner.hpp" 12 #include "Teko_InverseFactoryOperator.hpp" 14 #include "Teko_InverseLibrary.hpp" 15 #include "Teko_StridedEpetraOperator.hpp" 16 #include "Teko_BlockedEpetraOperator.hpp" 17 #include "Teko_ReorderedLinearOp.hpp" 19 #include "EpetraExt_RowMatrixOut.h" 24 using Teuchos::ParameterList;
29 class StratimikosFactoryPreconditioner :
public Thyra::DefaultPreconditioner<double>{
31 StratimikosFactoryPreconditioner() : iter_(0) {}
33 inline void incrIter() { iter_++; }
34 inline std::size_t getIter() {
return iter_; }
37 StratimikosFactoryPreconditioner(
const StratimikosFactoryPreconditioner &);
44 class TekoFactoryBuilder
45 :
public Teuchos::AbstractFactory<Thyra::PreconditionerFactoryBase<double> > {
47 TekoFactoryBuilder(
const Teuchos::RCP<Teko::RequestHandler> & rh) : requestHandler_(rh) {}
48 Teuchos::RCP<Thyra::PreconditionerFactoryBase<double> > create()
const 49 {
return Teuchos::rcp(
new StratimikosFactory(requestHandler_)); }
52 Teuchos::RCP<Teko::RequestHandler> requestHandler_;
58 :epetraFwdOpViewExtractor_(
Teuchos::rcp(new
Thyra::EpetraOperatorViewExtractorStd()))
63 :epetraFwdOpViewExtractor_(Teuchos::rcp(
new Thyra::EpetraOperatorViewExtractorStd()))
72 using Teuchos::outArg;
74 TEUCHOS_ASSERT(
false);
76 Teuchos::RCP<const Epetra_Operator> epetraFwdOp;
77 Thyra::EOpTransp epetraFwdOpTransp;
78 Thyra::EApplyEpetraOpAs epetraFwdOpApplyAs;
79 Thyra::EAdjointEpetraOp epetraFwdOpAdjointSupport;
80 double epetraFwdOpScalar;
83 Teuchos::RCP<const LinearOpBase<double> > fwdOp = fwdOpSrc.getOp();
84 epetraFwdOpViewExtractor_->getEpetraOpView(
86 outArg(epetraFwdOp),outArg(epetraFwdOpTransp),
87 outArg(epetraFwdOpApplyAs),
88 outArg(epetraFwdOpAdjointSupport),
89 outArg(epetraFwdOpScalar));
91 if( !dynamic_cast<const Epetra_CrsMatrix*>(&*epetraFwdOp) )
110 Teuchos::RCP<Thyra::PreconditionerBase<double> >
113 return Teuchos::rcp(
new StratimikosFactoryPreconditioner());
118 const Teuchos::RCP<
const Thyra::LinearOpSourceBase<double> > &fwdOpSrc,
119 Thyra::PreconditionerBase<double> *prec,
120 const Thyra::ESupportSolveUse supportSolveUse
123 Teuchos::RCP<const LinearOpBase<double> > fwdOp = fwdOpSrc->getOp();
125 if(epetraFwdOpViewExtractor_->isCompatible(*fwdOp))
132 const Teuchos::RCP<
const Thyra::LinearOpSourceBase<double> > &fwdOpSrc,
133 Thyra::PreconditionerBase<double> *prec,
134 const Thyra::ESupportSolveUse supportSolveUse
139 using Teuchos::rcp_dynamic_cast;
140 using Teuchos::implicit_cast;
141 using Thyra::LinearOpBase;
143 Teuchos::Time totalTimer(
""), timer(
"");
144 totalTimer.start(
true);
146 const RCP<Teuchos::FancyOStream> out = this->getOStream();
147 const Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
149 bool mediumVerbosity = (out.get() && implicit_cast<
int>(verbLevel) > implicit_cast<int>(Teuchos::VERB_LOW));
151 Teuchos::OSTab tab(out);
153 *out <<
"\nEntering Teko::StratimikosFactory::initializePrec_Thyra(...) ...\n";
155 Teuchos::RCP<const LinearOpBase<double> > fwdOp = fwdOpSrc->getOp();
158 StratimikosFactoryPreconditioner & defaultPrec
159 = Teuchos::dyn_cast<StratimikosFactoryPreconditioner>(*prec);
160 Teuchos::RCP<LinearOpBase<double> > prec_Op = defaultPrec.getNonconstUnspecifiedPrecOp();
163 const bool startingOver = (prec_Op == Teuchos::null);
166 invLib_ = Teuchos::null;
167 invFactory_ = Teuchos::null;
170 *out <<
"\nCreating the initial Teko Operator object...\n";
175 invLib_ = Teko::InverseLibrary::buildFromParameterList(paramList_->sublist(
"Inverse Factory Library"));
176 invLib_->setRequestHandler(reqHandler_);
179 invFactory_ = invLib_->getInverseFactory(paramList_->get<std::string>(
"Inverse Type"));
183 Teuchos::OSTab(out).o() <<
"> Creation time = "<<timer.totalElapsedTime()<<
" sec\n";
187 *out <<
"\nComputing the preconditioner ...\n";
190 std::string reorderType = paramList_->get<std::string>(
"Reorder Type");
191 if(reorderType!=
"") {
193 Teuchos::RCP<const Thyra::BlockedLinearOpBase<double> > blkFwdOp =
194 Teuchos::rcp_dynamic_cast<
const Thyra::BlockedLinearOpBase<double> >(fwdOp,
true);
196 Teko::LinearOp blockedFwdOp = Teko::buildReorderedLinearOp(*brm,blkFwdOp);
198 if(prec_Op==Teuchos::null) {
199 Teko::ModifiableLinearOp reorderedPrec =
Teko::buildInverse(*invFactory_,blockedFwdOp);
203 Teko::ModifiableLinearOp reorderedPrec = Teuchos::rcp_dynamic_cast<
ReorderedLinearOp>(prec_Op,
true)->getBlockedOp();
209 if(prec_Op==Teuchos::null)
219 Teuchos::OSTab(out).o() <<
"=> Preconditioner construction time = "<<timer.totalElapsedTime()<<
" sec\n";
223 defaultPrec.initializeUnspecified( prec_Op);
224 defaultPrec.incrIter();
227 if(out.get() && implicit_cast<
int>(verbLevel) >= implicit_cast<int>(Teuchos::VERB_LOW))
228 *out <<
"\nTotal time in Teko::StratimikosFactory = "<<totalTimer.totalElapsedTime()<<
" sec\n";
230 *out <<
"\nLeaving Teko::StratimikosFactory::initializePrec_Thyra(...) ...\n";
234 const Teuchos::RCP<
const Thyra::LinearOpSourceBase<double> > &fwdOpSrc,
235 Thyra::PreconditionerBase<double> *prec,
236 const Thyra::ESupportSolveUse supportSolveUse
239 using Teuchos::outArg;
242 using Teuchos::rcp_dynamic_cast;
243 using Teuchos::implicit_cast;
245 Teuchos::Time totalTimer(
""), timer(
"");
246 totalTimer.start(
true);
248 const RCP<Teuchos::FancyOStream> out = this->getOStream();
249 const Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
251 bool mediumVerbosity = (out.get() && implicit_cast<
int>(verbLevel) > implicit_cast<int>(Teuchos::VERB_LOW));
253 Teuchos::OSTab tab(out);
255 *out <<
"\nEntering Teko::StratimikosFactory::initializePrec_Epetra(...) ...\n";
257 Teuchos::RCP<const LinearOpBase<double> > fwdOp = fwdOpSrc->getOp();
259 TEUCHOS_TEST_FOR_EXCEPT(fwdOp.get()==NULL);
260 TEUCHOS_TEST_FOR_EXCEPT(prec==NULL);
266 Teuchos::RCP<const Epetra_Operator> epetraFwdOp;
267 Thyra::EOpTransp epetraFwdOpTransp;
268 Thyra::EApplyEpetraOpAs epetraFwdOpApplyAs;
269 Thyra::EAdjointEpetraOp epetraFwdOpAdjointSupport;
270 double epetraFwdOpScalar;
271 epetraFwdOpViewExtractor_->getEpetraOpView(
272 fwdOp,outArg(epetraFwdOp),outArg(epetraFwdOpTransp),outArg(epetraFwdOpApplyAs),
273 outArg(epetraFwdOpAdjointSupport),outArg(epetraFwdOpScalar)
276 StratimikosFactoryPreconditioner & defaultPrec
277 = Teuchos::dyn_cast<StratimikosFactoryPreconditioner>(*prec);
280 RCP<Thyra::EpetraLinearOp> epetra_precOp
281 = rcp_dynamic_cast<Thyra::EpetraLinearOp>(defaultPrec.getNonconstUnspecifiedPrecOp(),
true);
284 Teuchos::RCP<Teko::Epetra::InverseFactoryOperator> teko_precOp;
285 if(epetra_precOp!=Teuchos::null)
289 const bool startingOver = (teko_precOp == Teuchos::null);
292 invLib_ = Teuchos::null;
293 invFactory_ = Teuchos::null;
297 *out <<
"\nCreating the initial Teko::Epetra::InverseFactoryOperator object...\n";
301 std::stringstream ss;
302 ss << paramList_->get<std::string>(
"Strided Blocking");
305 while(not ss.eof()) {
309 TEUCHOS_ASSERT(num>0);
311 decomp_.push_back(num);
315 invLib_ = Teko::InverseLibrary::buildFromParameterList(paramList_->sublist(
"Inverse Factory Library"));
316 invLib_->setRequestHandler(reqHandler_);
319 invFactory_ = invLib_->getInverseFactory(paramList_->get<std::string>(
"Inverse Type"));
326 Teuchos::OSTab(out).o() <<
"> Creation time = "<<timer.totalElapsedTime()<<
" sec\n";
330 *out <<
"\nComputing the preconditioner ...\n";
334 bool writeBlockOps = paramList_->get<
bool>(
"Write Block Operator");
336 teko_precOp->initInverse();
338 if(decomp_.size()==1) {
339 teko_precOp->rebuildInverseOperator(epetraFwdOp);
343 std::stringstream ss;
344 ss <<
"block-" << defaultPrec.getIter() <<
"_00.mm";
345 EpetraExt::RowMatrixToMatrixMarketFile(ss.str().c_str(),*rcp_dynamic_cast<
const Epetra_CrsMatrix>(epetraFwdOp));
349 Teuchos::RCP<Epetra_Operator> wrappedFwdOp
350 = buildWrappedEpetraOperator(epetraFwdOp,teko_precOp->getNonconstForwardOp(),*out);
354 std::stringstream ss;
355 ss <<
"block-" << defaultPrec.getIter();
358 Teuchos::RCP<Teko::Epetra::BlockedEpetraOperator> stridedJac
360 if(stridedJac!=Teuchos::null) {
364 else TEUCHOS_ASSERT(
false);
367 teko_precOp->rebuildInverseOperator(wrappedFwdOp);
374 Teuchos::OSTab(out).o() <<
"=> Preconditioner construction time = "<<timer.totalElapsedTime()<<
" sec\n";
378 epetra_precOp = rcp(
new Thyra::EpetraLinearOp);
380 epetra_precOp->initialize(teko_precOp,epetraFwdOpTransp
381 ,Thyra::EPETRA_OP_APPLY_APPLY_INVERSE
382 ,Thyra::EPETRA_OP_ADJOINT_UNSUPPORTED);
385 defaultPrec.initializeUnspecified( Teuchos::rcp_implicit_cast<LinearOpBase<double> >(epetra_precOp));
386 defaultPrec.incrIter();
389 if(out.get() && implicit_cast<
int>(verbLevel) >= implicit_cast<int>(Teuchos::VERB_LOW))
390 *out <<
"\nTotal time in Teko::StratimikosFactory = "<<totalTimer.totalElapsedTime()<<
" sec\n";
392 *out <<
"\nLeaving Teko::StratimikosFactory::initializePrec(...) ...\n";
397 Thyra::PreconditionerBase<double> *prec,
398 Teuchos::RCP<
const Thyra::LinearOpSourceBase<double> > *fwdOp,
399 Thyra::ESupportSolveUse *supportSolveUse
402 TEUCHOS_TEST_FOR_EXCEPT(
true);
410 Teuchos::RCP<Teuchos::ParameterList>
const& paramList
413 TEUCHOS_TEST_FOR_EXCEPT(paramList.get()==NULL);
416 paramList_ = paramList;
421 Teuchos::RCP<Teuchos::ParameterList>
428 Teuchos::RCP<Teuchos::ParameterList>
431 Teuchos::RCP<ParameterList> _paramList = paramList_;
432 paramList_ = Teuchos::null;
437 Teuchos::RCP<const Teuchos::ParameterList>
444 Teuchos::RCP<const Teuchos::ParameterList>
449 using Teuchos::tuple;
450 using Teuchos::implicit_cast;
451 using Teuchos::rcp_implicit_cast;
452 typedef Teuchos::ParameterEntryValidator PEV;
454 static RCP<const ParameterList> validPL;
456 if(is_null(validPL)) {
458 Teuchos::RCP<Teuchos::ParameterList> pl = rcp(
new Teuchos::ParameterList());
460 pl->set(
"Test Block Operator",
false,
461 "If Stratiikos/Teko is used to break an operator into its parts,\n" 462 "then setting this parameter to true will compare applications of the\n" 463 "segregated operator to the original operator.");
464 pl->set(
"Write Block Operator",
false,
465 "Write out the segregated operator to disk with the name \"block-?_xx\"");
466 pl->set(
"Strided Blocking",
"1",
467 "Assuming that the user wants Strided blocking, break the operating into\n" 468 "blocks. The syntax can be thought to be associated with the solution\n" 469 "vector. For example if your variables are [u v w p T], and we want [u v w]\n" 470 "blocked together, and p and T seperate then the relevant string is \"3 1 1\".\n" 471 "Meaning put the first 3 unknowns per node together and sperate the v and w\n" 473 pl->set(
"Reorder Type",
"",
474 "This specifies how the blocks are reordered for use in the preconditioner.\n" 475 "For example, assume the linear system is generated from 3D Navier-Stokes\n" 476 "with an energy equation, yielding the unknowns [u v w p T]. If the\n" 477 "\"Strided Blocking\" string is \"3 1 1\", then setting this parameter to\n" 478 "\"[2 [0 1]]\" will reorder the blocked operator so its nested with the\n" 479 "velocity and pressure forming an inner two-by-two block, and then the\n" 480 "temperature unknowns forming a two-by-two system with the velocity-pressure\n" 482 pl->set(
"Inverse Type",
"Amesos",
483 "The type of inverse operator the user wants. This can be one of the defaults\n" 484 "from Stratimikos, or a Teko preconditioner defined in the\n" 485 "\"Inverse Factory Library\".");
486 pl->sublist(
"Inverse Factory Library",
false,
"Definition of Teko preconditioners.");
497 Teuchos::RCP<Epetra_Operator> StratimikosFactory::buildWrappedEpetraOperator(
498 const Teuchos::RCP<const Epetra_Operator> & Jac,
499 const Teuchos::RCP<Epetra_Operator> & wrapInput,
503 Teuchos::RCP<Epetra_Operator> wrappedOp = wrapInput;
532 if(wrappedOp==Teuchos::null)
535 std::vector<std::vector<int> > vars;
536 buildStridedVectors(*Jac,decomp_,vars);
540 std::string reorderType = paramList_->get<std::string>(
"Reorder Type");
541 if(reorderType!=
"") {
553 if(paramList_->get<
bool>(
"Test Block Operator")) {
557 out <<
"Teko: Tested operator correctness: " << (result ?
"passed" :
"FAILED!") << std::endl;
565 std::ostringstream oss;
566 oss <<
"Teko::StratimikosFactory";
570 void StratimikosFactory::buildStridedVectors(
const Epetra_Operator & Jac,
571 const std::vector<int> & decomp,
572 std::vector<std::vector<int> > & vars)
const 574 const Epetra_Map & rangeMap = Jac.OperatorRangeMap();
578 for(std::size_t i=0;i<decomp.size();i++)
579 numVars += decomp[i];
582 TEUCHOS_ASSERT((rangeMap.NumMyElements() % numVars)==0);
583 TEUCHOS_ASSERT((rangeMap.NumGlobalElements() % numVars)==0);
585 int * globalIds = rangeMap.MyGlobalElements();
587 vars.resize(decomp.size());
588 for(
int i=0;i<rangeMap.NumMyElements();) {
591 for(std::size_t d=0;d<decomp.size();d++) {
593 int current = decomp[d];
594 for(
int v=0;v<current;v++,i++)
595 vars[d].push_back(globalIds[i]);
600 void addTekoToStratimikosBuilder(Stratimikos::DefaultLinearSolverBuilder & builder,
601 const std::string & stratName)
603 TEUCHOS_TEST_FOR_EXCEPTION(builder.getValidParameters()->sublist(
"Preconditioner Types").isParameter(stratName),std::logic_error,
604 "Teko::addTekoToStratimikosBuilder cannot add \"" + stratName +
"\" because it is already included in builder!");
607 builder.setPreconditioningStrategyFactory(
612 void addTekoToStratimikosBuilder(Stratimikos::DefaultLinearSolverBuilder & builder,
613 const Teuchos::RCP<Teko::RequestHandler> & rh,
614 const std::string & stratName)
616 TEUCHOS_TEST_FOR_EXCEPTION(builder.getValidParameters()->sublist(
"Preconditioner Types").isParameter(stratName),std::logic_error,
617 "Teko::addTekoToStratimikosBuilder cannot add \"" + stratName +
"\" because it is already included in builder!");
621 Teuchos::RCP<TekoFactoryBuilder> tekoFactoryBuilder = Teuchos::rcp(
new TekoFactoryBuilder(rh));
622 builder.setPreconditioningStrategyFactory(tekoFactoryBuilder,stratName);
bool applySupportsConj(Thyra::EConj conj) const
Teuchos::RCP< Teuchos::ParameterList > unsetParameterList()
void setRequestHandler(const Teuchos::RCP< Teko::RequestHandler > &rh)
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
InverseLinearOp buildInverse(const InverseFactory &factory, const LinearOp &A)
Build an inverse operator using a factory and a linear operator.
void initializePrec(const Teuchos::RCP< const Thyra::LinearOpSourceBase< double > > &fwdOp, Thyra::PreconditionerBase< double > *prec, const Thyra::ESupportSolveUse supportSolveUse) const
void uninitializePrec(Thyra::PreconditionerBase< double > *prec, Teuchos::RCP< const Thyra::LinearOpSourceBase< double > > *fwdOp, Thyra::ESupportSolveUse *supportSolveUse) const
bool applyTransposeSupportsConj(Thyra::EConj conj) const
Teuchos::RCP< const Teuchos::ParameterList > getParameterList() const
void rebuildInverse(const InverseFactory &factory, const LinearOp &A, InverseLinearOp &invA)
Teuchos::RCP< Teuchos::ParameterList > getNonconstParameterList()
void initializePrec_Thyra(const Teuchos::RCP< const Thyra::LinearOpSourceBase< double > > &fwdOp, Thyra::PreconditionerBase< double > *prec, const Thyra::ESupportSolveUse supportSolveUse) const
A single Epetra wrapper for all operators constructed from an inverse operator.
bool isCompatible(const Thyra::LinearOpSourceBase< double > &fwdOp) const
Teuchos::RCP< Thyra::PreconditionerBase< double > > createPrec() const
Concrete preconditioner factory subclass based on ML.
This class takes a blocked linear op and represents it in a flattened form.
void initializePrec_Epetra(const Teuchos::RCP< const Thyra::LinearOpSourceBase< double > > &fwdOp, Thyra::PreconditionerBase< double > *prec, const Thyra::ESupportSolveUse supportSolveUse) const
void setParameterList(Teuchos::RCP< Teuchos::ParameterList > const ¶mList)
Tear about a user specified Epetra_Operator (CrsMatrix) using a vector of vectors of GIDs for each bl...
std::string description() const
virtual void WriteBlocks(const std::string &prefix) const
Teuchos::RCP< const BlockReorderManager > blockedReorderFromString(std::string &reorder)
Convert a string to a block reorder manager object.