Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_TpetraBlockPreconditioner.cpp
1 /*
2 // @HEADER
3 //
4 // ***********************************************************************
5 //
6 // Teko: A package for block and physics based preconditioning
7 // Copyright 2010 Sandia Corporation
8 //
9 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
10 // the U.S. Government retains certain rights in this software.
11 //
12 // Redistribution and use in source and binary forms, with or without
13 // modification, are permitted provided that the following conditions are
14 // met:
15 //
16 // 1. Redistributions of source code must retain the above copyright
17 // notice, this list of conditions and the following disclaimer.
18 //
19 // 2. Redistributions in binary form must reproduce the above copyright
20 // notice, this list of conditions and the following disclaimer in the
21 // documentation and/or other materials provided with the distribution.
22 //
23 // 3. Neither the name of the Corporation nor the names of the
24 // contributors may be used to endorse or promote products derived from
25 // this software without specific prior written permission.
26 //
27 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
28 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
31 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 //
39 // Questions? Contact Eric C. Cyr (eccyr@sandia.gov)
40 //
41 // ***********************************************************************
42 //
43 // @HEADER
44 
45 */
46 
47 #include "Teko_TpetraBlockPreconditioner.hpp"
48 #include "Teko_Preconditioner.hpp"
49 
50 // Thyra includes
51 #include "Thyra_DefaultLinearOpSource.hpp"
52 #include "Thyra_TpetraLinearOp.hpp"
53 
54 // Teuchos includes
55 #include "Teuchos_Time.hpp"
56 
57 // Teko includes
58 #include "Teko_TpetraBasicMappingStrategy.hpp"
59 #include "Teko_Utilities.hpp"
60 
61 namespace Teko {
62 namespace TpetraHelpers {
63 
64 using Teuchos::RCP;
65 using Teuchos::rcp;
66 using Teuchos::rcp_dynamic_cast;
67 using Teuchos::rcpFromRef;
68 
75 TpetraBlockPreconditioner::TpetraBlockPreconditioner(
76  const Teuchos::RCP<const PreconditionerFactory>& bfp)
77  : preconFactory_(bfp), firstBuildComplete_(false) {}
78 
80  if ((not clearOld) && preconObj_ != Teuchos::null) return;
81  preconObj_ = preconFactory_->createPrec();
82 }
83 
97  const Teuchos::RCP<const Tpetra::Operator<ST, LO, GO, NT> >& A, bool clear) {
98  Teko_DEBUG_SCOPE("TBP::buildPreconditioner", 10);
99 
100  // extract EpetraOperatorWrapper (throw on failure) and corresponding thyra operator
101  RCP<const Thyra::LinearOpBase<ST> > thyraA = extractLinearOp(A);
102 
103  // set the mapping strategy
104  // SetMapStrategy(rcp(new InverseMappingStrategy(eow->getMapStrategy())));
105  SetMapStrategy(rcp(new InverseMappingStrategy(extractMappingStrategy(A))));
106 
107  // build preconObj_
108  initPreconditioner(clear);
109 
110  // actually build the preconditioner
111  RCP<const Thyra::LinearOpSourceBase<ST> > lOpSrc = Thyra::defaultLinearOpSource(thyraA);
112  preconFactory_->initializePrec(lOpSrc, &*preconObj_, Thyra::SUPPORT_SOLVE_UNSPECIFIED);
113 
114  // extract preconditioner operator
115  RCP<const Thyra::LinearOpBase<ST> > preconditioner = preconObj_->getUnspecifiedPrecOp();
116 
117  SetOperator(preconditioner, false);
118 
119  firstBuildComplete_ = true;
120 
121  TEUCHOS_ASSERT(preconObj_ != Teuchos::null);
122  TEUCHOS_ASSERT(getThyraOp() != Teuchos::null);
123  TEUCHOS_ASSERT(getMapStrategy() != Teuchos::null);
124  TEUCHOS_ASSERT(firstBuildComplete_ == true);
125 }
126 
141  const Teuchos::RCP<const Tpetra::Operator<ST, LO, GO, NT> >& A,
142  const Tpetra::MultiVector<ST, LO, GO, NT>& tpetra_mv, bool clear) {
143  Teko_DEBUG_SCOPE("TBP::buildPreconditioner - with solution", 10);
144 
145  // extract EpetraOperatorWrapper (throw on failure) and corresponding thyra operator
146  RCP<const Thyra::LinearOpBase<ST> > thyraA = extractLinearOp(A);
147 
148  // set the mapping strategy
149  SetMapStrategy(rcp(new InverseMappingStrategy(extractMappingStrategy(A))));
150 
151  TEUCHOS_ASSERT(getMapStrategy() != Teuchos::null);
152 
153  // build the thyra version of the source multivector
154  RCP<Thyra::MultiVectorBase<ST> > thyra_mv =
155  Thyra::createMembers(thyraA->range(), tpetra_mv.getNumVectors());
156  getMapStrategy()->copyTpetraIntoThyra(tpetra_mv, thyra_mv.ptr());
157 
158  // build preconObj_
159  initPreconditioner(clear);
160 
161  // actually build the preconditioner
162  preconFactory_->initializePrec(Thyra::defaultLinearOpSource(thyraA), thyra_mv, &*preconObj_,
163  Thyra::SUPPORT_SOLVE_UNSPECIFIED);
164  RCP<const Thyra::LinearOpBase<ST> > preconditioner = preconObj_->getUnspecifiedPrecOp();
165 
166  SetOperator(preconditioner, false);
167 
168  firstBuildComplete_ = true;
169 
170  TEUCHOS_ASSERT(preconObj_ != Teuchos::null);
171  TEUCHOS_ASSERT(getThyraOp() != Teuchos::null);
172  TEUCHOS_ASSERT(getMapStrategy() != Teuchos::null);
173  TEUCHOS_ASSERT(firstBuildComplete_ == true);
174 }
175 
190  const Teuchos::RCP<const Tpetra::Operator<ST, LO, GO, NT> >& A) {
191  Teko_DEBUG_SCOPE("TBP::rebuildPreconditioner", 10);
192 
193  // if the preconditioner hasn't been built yet, rebuild from scratch
194  if (not firstBuildComplete_) {
195  buildPreconditioner(A, false);
196  return;
197  }
198  Teko_DEBUG_EXPR(Teuchos::Time timer(""));
199 
200  // extract EpetraOperatorWrapper (throw on failure) and corresponding thyra operator
201  Teko_DEBUG_EXPR(timer.start(true));
202  RCP<const Thyra::LinearOpBase<ST> > thyraA = extractLinearOp(A);
203  Teko_DEBUG_EXPR(timer.stop());
204  Teko_DEBUG_MSG("TBP::rebuild get thyraop time = " << timer.totalElapsedTime(), 2);
205 
206  // reinitialize the preconditioner
207  Teko_DEBUG_EXPR(timer.start(true));
208  preconFactory_->initializePrec(Thyra::defaultLinearOpSource(thyraA), &*preconObj_,
209  Thyra::SUPPORT_SOLVE_UNSPECIFIED);
210  RCP<const Thyra::LinearOpBase<ST> > preconditioner = preconObj_->getUnspecifiedPrecOp();
211  Teko_DEBUG_EXPR(timer.stop());
212  Teko_DEBUG_MSG("TBP::rebuild initialize prec time = " << timer.totalElapsedTime(), 2);
213 
214  Teko_DEBUG_EXPR(timer.start(true));
215  SetOperator(preconditioner, false);
216  Teko_DEBUG_EXPR(timer.stop());
217  Teko_DEBUG_MSG("TBP::rebuild set operator time = " << timer.totalElapsedTime(), 2);
218 
219  TEUCHOS_ASSERT(preconObj_ != Teuchos::null);
220  TEUCHOS_ASSERT(getThyraOp() != Teuchos::null);
221  TEUCHOS_ASSERT(firstBuildComplete_ == true);
222 }
223 
238  const Teuchos::RCP<const Tpetra::Operator<ST, LO, GO, NT> >& A,
239  const Tpetra::MultiVector<ST, LO, GO, NT>& tpetra_mv) {
240  Teko_DEBUG_SCOPE("TBP::rebuildPreconditioner - with solution", 10);
241 
242  // if the preconditioner hasn't been built yet, rebuild from scratch
243  if (not firstBuildComplete_) {
244  buildPreconditioner(A, tpetra_mv, false);
245  return;
246  }
247  Teko_DEBUG_EXPR(Teuchos::Time timer(""));
248 
249  // extract EpetraOperatorWrapper (throw on failure) and corresponding thyra operator
250  Teko_DEBUG_EXPR(timer.start(true));
251  RCP<const Thyra::LinearOpBase<ST> > thyraA = extractLinearOp(A);
252  Teko_DEBUG_EXPR(timer.stop());
253  Teko_DEBUG_MSG("TBP::rebuild get thyraop time = " << timer.totalElapsedTime(), 2);
254 
255  // build the thyra version of the source multivector
256  Teko_DEBUG_EXPR(timer.start(true));
257  RCP<Thyra::MultiVectorBase<ST> > thyra_mv =
258  Thyra::createMembers(thyraA->range(), tpetra_mv.getNumVectors());
259  getMapStrategy()->copyTpetraIntoThyra(tpetra_mv, thyra_mv.ptr());
260  Teko_DEBUG_EXPR(timer.stop());
261  Teko_DEBUG_MSG("TBP::rebuild vector copy time = " << timer.totalElapsedTime(), 2);
262 
263  // reinitialize the preconditioner
264  Teko_DEBUG_EXPR(timer.start(true));
265  preconFactory_->initializePrec(Thyra::defaultLinearOpSource(thyraA), thyra_mv, &*preconObj_,
266  Thyra::SUPPORT_SOLVE_UNSPECIFIED);
267  RCP<const Thyra::LinearOpBase<ST> > preconditioner = preconObj_->getUnspecifiedPrecOp();
268  Teko_DEBUG_EXPR(timer.stop());
269  Teko_DEBUG_MSG("TBP::rebuild initialize prec time = " << timer.totalElapsedTime(), 2);
270 
271  Teko_DEBUG_EXPR(timer.start(true));
272  SetOperator(preconditioner, false);
273  Teko_DEBUG_EXPR(timer.stop());
274  Teko_DEBUG_MSG("TBP::rebuild set operator time = " << timer.totalElapsedTime(), 2);
275 
276  TEUCHOS_ASSERT(preconObj_ != Teuchos::null);
277  TEUCHOS_ASSERT(getThyraOp() != Teuchos::null);
278  TEUCHOS_ASSERT(firstBuildComplete_ == true);
279 }
280 
290 Teuchos::RCP<PreconditionerState> TpetraBlockPreconditioner::getPreconditionerState() {
291  Teuchos::RCP<Preconditioner> bp = rcp_dynamic_cast<Preconditioner>(preconObj_);
292 
293  if (bp != Teuchos::null) return bp->getStateObject();
294 
295  return Teuchos::null;
296 }
297 
307 Teuchos::RCP<const PreconditionerState> TpetraBlockPreconditioner::getPreconditionerState() const {
308  Teuchos::RCP<const Preconditioner> bp = rcp_dynamic_cast<const Preconditioner>(preconObj_);
309 
310  if (bp != Teuchos::null) return bp->getStateObject();
311 
312  return Teuchos::null;
313 }
314 
315 Teuchos::RCP<const Thyra::LinearOpBase<ST> > TpetraBlockPreconditioner::extractLinearOp(
316  const Teuchos::RCP<const Tpetra::Operator<ST, LO, GO, NT> >& A) const {
317  // extract EpetraOperatorWrapper (throw on failure) and corresponding thyra operator
318  const RCP<const TpetraOperatorWrapper>& tow = rcp_dynamic_cast<const TpetraOperatorWrapper>(A);
319 
320  // if it is an EpetraOperatorWrapper, then get the Thyra operator
321  if (tow != Teuchos::null) return tow->getThyraOp();
322 
323  // otherwise wrap it up as a thyra operator
324  return Thyra::constTpetraLinearOp<ST, LO, GO, NT>(
325  Thyra::tpetraVectorSpace<ST, LO, GO, NT>(A->getDomainMap()),
326  Thyra::tpetraVectorSpace<ST, LO, GO, NT>(A->getRangeMap()), A);
327 }
328 
329 Teuchos::RCP<const MappingStrategy> TpetraBlockPreconditioner::extractMappingStrategy(
330  const Teuchos::RCP<const Tpetra::Operator<ST, LO, GO, NT> >& A) const {
331  // extract EpetraOperatorWrapper (throw on failure) and corresponding thyra operator
332  const RCP<const TpetraOperatorWrapper>& tow = rcp_dynamic_cast<const TpetraOperatorWrapper>(A);
333 
334  // if it is an EpetraOperatorWrapper, then get the Thyra operator
335  if (tow != Teuchos::null) return tow->getMapStrategy();
336 
337  // otherwise wrap it up as a thyra operator
338  RCP<const Tpetra::Map<LO, GO, NT> > range = A->getRangeMap();
339  RCP<const Tpetra::Map<LO, GO, NT> > domain = A->getDomainMap();
340  return rcp(
341  new BasicMappingStrategy(range, domain, *Thyra::convertTpetraToThyraComm(range->getComm())));
342 }
343 
344 } // namespace TpetraHelpers
345 } // end namespace Teko
virtual const RCP< PreconditionerState > getStateObject()
virtual void buildPreconditioner(const Teuchos::RCP< const Tpetra::Operator< ST, LO, GO, NT > > &A, bool clear=true)
Build this preconditioner from an Epetra_Operator passed in to this object.
virtual Teuchos::RCP< PreconditionerState > getPreconditionerState()
const RCP< const Thyra::LinearOpBase< ST > > getThyraOp() const
Return the thyra operator associated with this wrapper.
Flip a mapping strategy object around to give the &quot;inverse&quot; mapping strategy.
virtual void initPreconditioner(bool clearOld=false)
Build the underlying data structure for the preconditioner.
An extension of the Thyra::DefaultPreconditioner class with some specializations useful for use withi...
const RCP< const MappingStrategy > getMapStrategy() const
Get the mapping strategy for this wrapper (translate between Thyra and Epetra)
virtual void rebuildPreconditioner(const Teuchos::RCP< const Tpetra::Operator< ST, LO, GO, NT > > &A)
Rebuild this preconditioner from an Epetra_Operator passed in this to object.
Implements the Epetra_Operator interface with a Thyra LinearOperator. This enables the use of absrtac...