10#ifndef IFPACK2_DETAILS_TRIDISOLVER_DEF_HPP
11#define IFPACK2_DETAILS_TRIDISOLVER_DEF_HPP
13#include "Ifpack2_LocalFilter.hpp"
14#include "Teuchos_LAPACK.hpp"
15#include "Tpetra_MultiVector.hpp"
16#include "Tpetra_Map.hpp"
17#include "Tpetra_Import.hpp"
18#include "Tpetra_Export.hpp"
22# include "Teuchos_DefaultMpiComm.hpp"
24# include "Teuchos_DefaultSerialComm.hpp"
35template<
class MatrixType>
37TriDiSolver (
const Teuchos::RCP<const row_matrix_type>& A) :
39 initializeTime_ (0.0),
45 isInitialized_ (
false),
50template<
class MatrixType>
51Teuchos::RCP<const typename TriDiSolver<MatrixType, false>::map_type>
54 A_.is_null (), std::runtime_error,
"Ifpack2::Details::TriDiSolver::"
55 "getDomainMap: The input matrix A is null. Please call setMatrix() with a "
56 "nonnull input matrix before calling this method.");
59 return A_->getRangeMap ();
63template<
class MatrixType>
64Teuchos::RCP<const typename TriDiSolver<MatrixType, false>::map_type>
67 A_.is_null (), std::runtime_error,
"Ifpack2::Details::TriDiSolver::"
68 "getRangeMap: The input matrix A is null. Please call setMatrix() with a "
69 "nonnull input matrix before calling this method.");
72 return A_->getDomainMap ();
76template<
class MatrixType>
84template<
class MatrixType>
87 return isInitialized_;
91template<
class MatrixType>
98template<
class MatrixType>
101 return numInitialize_;
105template<
class MatrixType>
112template<
class MatrixType>
119template<
class MatrixType>
122 return initializeTime_;
126template<
class MatrixType>
133template<
class MatrixType>
140template<
class MatrixType>
141Teuchos::RCP<const typename TriDiSolver<MatrixType, false>::row_matrix_type>
147template<
class MatrixType>
151 isInitialized_ =
false;
153 A_local_ = Teuchos::null;
154 A_local_tridi_.reshape (0);
159template<
class MatrixType>
161setMatrix (
const Teuchos::RCP<const row_matrix_type>& A)
165 if (! A_.is_null ()) {
166 const global_size_t numRows = A->getRangeMap ()->getGlobalNumElements ();
167 const global_size_t
numCols = A->getDomainMap ()->getGlobalNumElements ();
169 numRows !=
numCols, std::invalid_argument,
"Ifpack2::Details::TriDiSolver::"
170 "setMatrix: Input matrix must be (globally) square. "
171 "The matrix you provided is " << numRows <<
" by " <<
numCols <<
".");
181template<
class MatrixType>
189 using Teuchos::TimeMonitor;
190 const std::string
timerName (
"Ifpack2::Details::TriDiSolver::initialize");
193 if (
timer.is_null ()) {
203 A_.is_null (), std::runtime_error,
"Ifpack2::Details::TriDiSolver::"
204 "initialize: The input matrix A is null. Please call setMatrix() "
205 "with a nonnull input before calling this method.");
208 ! A_->hasColMap (), std::invalid_argument,
"Ifpack2::Details::TriDiSolver: "
209 "The constructor's input matrix must have a column Map, "
210 "so that it has local indices.");
216 if (A_->getComm ()->getSize () > 1) {
223 A_local_.is_null (), std::logic_error,
"Ifpack2::Details::TriDiSolver::"
224 "initialize: A_local_ is null after it was supposed to have been "
225 "initialized. Please report this bug to the Ifpack2 developers.");
228 const size_t numRows = A_local_->getLocalNumRows ();
229 const size_t numCols = A_local_->getLocalNumCols ();
231 numRows !=
numCols, std::logic_error,
"Ifpack2::Details::TriDiSolver::"
232 "initialize: Local filter matrix is not square. This should never happen. "
233 "Please report this bug to the Ifpack2 developers.");
234 A_local_tridi_.reshape (numRows);
235 ipiv_.resize (numRows);
236 std::fill (ipiv_.begin (), ipiv_.end (), 0);
238 isInitialized_ =
true;
246template<
class MatrixType>
251template<
class MatrixType>
255 const std::string
timerName (
"Ifpack2::Details::TriDiSolver::compute");
258 if (
timer.is_null ()) {
268 A_.is_null (), std::runtime_error,
"Ifpack2::Details::TriDiSolver::"
269 "compute: The input matrix A is null. Please call setMatrix() with a "
270 "nonnull input, then call initialize(), before calling this method.");
273 A_local_.is_null (), std::logic_error,
"Ifpack2::Details::TriDiSolver::"
274 "compute: A_local_ is null. Please report this bug to the Ifpack2 "
278 if (! this->isInitialized ()) {
281 extract (A_local_tridi_, *A_local_);
283 factor (A_local_tridi_, ipiv_ ());
291template<
class MatrixType>
293 const Teuchos::ArrayView<int>& ipiv)
296 std::fill (ipiv.begin (), ipiv.end (), 0);
298 Teuchos::LAPACK<int, scalar_type>
lapack;
300 lapack.GTTRF (A.numRowsCols (),
305 ipiv.getRawPtr (), &
INFO);
308 INFO < 0, std::logic_error,
"Ifpack2::Details::TriDiSolver::factor: "
309 "LAPACK's _GTTRF (tridiagonal LU factorization with partial pivoting) "
310 "was called incorrectly. INFO = " <<
INFO <<
" < 0. "
311 "Please report this bug to the Ifpack2 developers.");
316 INFO > 0, std::runtime_error,
"Ifpack2::Details::TriDiSolver::factor: "
317 "LAPACK's _GTTRF (tridiagonal LU factorization with partial pivoting) "
318 "reports that the computed U factor is exactly singular. U(" <<
INFO <<
319 "," <<
INFO <<
") (one-based index i) is exactly zero. This probably "
320 "means that the input matrix has a singular diagonal block.");
324template<
class MatrixType>
325void TriDiSolver<MatrixType, false>::
326applyImpl (
const MV& X,
328 const Teuchos::ETransp mode,
329 const scalar_type alpha,
330 const scalar_type beta)
const
332 using Teuchos::ArrayRCP;
335 using Teuchos::rcpFromRef;
336 using Teuchos::CONJ_TRANS;
337 using Teuchos::TRANS;
339 const int numVecs =
static_cast<int> (X.getNumVectors ());
340 if (alpha == STS::zero ()) {
341 if (beta == STS::zero ()) {
345 Y.putScalar (STS::zero ());
348 Y.scale (STS::zero ());
352 Teuchos::LAPACK<int, scalar_type> lapack;
358 if (beta == STS::zero () && Y.isConstantStride () && alpha == STS::one ()) {
360 Y_tmp = rcpFromRef (Y);
363 Y_tmp = rcp (
new MV (createCopy(X)));
364 if (alpha != STS::one ()) {
365 Y_tmp->scale (alpha);
368 const int Y_stride =
static_cast<int> (Y_tmp->getStride ());
369 ArrayRCP<scalar_type> Y_view = Y_tmp->get1dViewNonConst ();
370 scalar_type*
const Y_ptr = Y_view.getRawPtr ();
373 (mode == CONJ_TRANS ?
'C' : (mode ==
TRANS ?
'T' :
'N'));
374 lapack.GTTRS (trans, A_local_tridi_.numRowsCols(), numVecs,
378 A_local_tridi_.DU2(),
379 ipiv_.getRawPtr (), Y_ptr, Y_stride, &INFO);
380 TEUCHOS_TEST_FOR_EXCEPTION(
381 INFO != 0, std::runtime_error,
"Ifpack2::Details::TriDiSolver::"
382 "applyImpl: LAPACK's _GTTRS (tridiagonal solve using LU factorization "
383 "with partial pivoting) failed with INFO = " << INFO <<
" != 0.");
385 if (beta != STS::zero ()) {
386 Y.update (alpha, *Y_tmp, beta);
388 else if (! Y.isConstantStride ()) {
389 deep_copy(Y, *Y_tmp);
395template<
class MatrixType>
397apply (
const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
398 Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y,
399 Teuchos::ETransp
mode,
403 using Teuchos::ArrayView;
407 using Teuchos::rcpFromRef;
409 const std::string
timerName (
"Ifpack2::Details::TriDiSolver::apply");
411 if (
timer.is_null ()) {
422 ! isComputed_, std::runtime_error,
"Ifpack2::Details::TriDiSolver::apply: "
423 "You must have called the compute() method before you may call apply(). "
424 "You may call the apply() method as many times as you want after calling "
425 "compute() once, but you must have called compute() at least once.");
427 const size_t numVecs = X.getNumVectors ();
430 numVecs != Y.getNumVectors (), std::runtime_error,
431 "Ifpack2::Details::TriDiSolver::apply: X and Y have different numbers "
432 "of vectors. X has " << X.getNumVectors () <<
", but Y has "
433 << X.getNumVectors () <<
".");
442 const bool multipleProcs = (A_->getRowMap ()->getComm ()->getSize () >= 1);
447 X_local = X.offsetView (A_local_->getDomainMap (), 0);
448 Y_local = Y.offsetViewNonConst (A_local_->getRangeMap (), 0);
467template<
class MatrixType>
470 std::ostringstream
out;
475 out <<
"\"Ifpack2::Details::TriDiSolver\": ";
480 out <<
"Initialized: " << (isInitialized () ?
"true" :
"false") <<
", "
481 <<
"Computed: " << (isComputed () ?
"true" :
"false") <<
", ";
484 out <<
"Matrix: null";
487 out <<
"Matrix: not null"
488 <<
", Global matrix dimensions: ["
489 << A_->getGlobalNumRows () <<
", " << A_->getGlobalNumCols () <<
"]";
497template<
class MatrixType>
499 const Teuchos::EVerbosityLevel
verbLevel)
const {
500 using Teuchos::FancyOStream;
501 using Teuchos::OSTab;
503 using Teuchos::rcpFromRef;
510 RCP<FancyOStream> ptrOut = rcpFromRef (out);
512 if (this->getObjectLabel () !=
"") {
513 out <<
"label: " << this->getObjectLabel () << endl;
515 out <<
"initialized: " << (isInitialized_ ?
"true" :
"false") << endl
516 <<
"computed: " << (isComputed_ ?
"true" :
"false") << endl
517 <<
"number of initialize calls: " << numInitialize_ << endl
518 <<
"number of compute calls: " << numCompute_ << endl
519 <<
"number of apply calls: " << numApply_ << endl
520 <<
"total time in seconds in initialize: " << initializeTime_ << endl
521 <<
"total time in seconds in compute: " << computeTime_ << endl
522 <<
"total time in seconds in apply: " << applyTime_ << endl;
523 if (verbLevel >= Teuchos::VERB_EXTREME) {
524 out <<
"A_local_tridi_:" << endl;
525 A_local_tridi_.print(out);
527 out <<
"ipiv_: " << Teuchos::toString (ipiv_) << endl;
531template<
class MatrixType>
533 const Teuchos::EVerbosityLevel
verbLevel)
const
535 using Teuchos::FancyOStream;
536 using Teuchos::OSTab;
538 using Teuchos::rcpFromRef;
549 out <<
"Ifpack2::Details::TriDiSolver:" <<
endl;
556 const Teuchos::Comm<int>&
comm = * (A_->getRowMap ()->getComm ());
560 out <<
"Ifpack2::Details::TriDiSolver:" <<
endl;
575template<
class MatrixType>
577 const row_matrix_type&
A_local)
579 using Teuchos::Array;
580 using Teuchos::ArrayView;
581 typedef local_ordinal_type LO;
582 typedef typename Teuchos::ArrayView<LO>::size_type size_type;
598 static_cast<size_type
> (
A_local.getLocalMaxNumRowEntries ());
623 const scalar_type val = values[
k];
637template<
class MatrixType>
643template<
class MatrixType>
644Teuchos::RCP<const typename TriDiSolver<MatrixType, true>::map_type>
650template<
class MatrixType>
651Teuchos::RCP<const typename TriDiSolver<MatrixType, true>::map_type>
657template<
class MatrixType>
664template<
class MatrixType>
671template<
class MatrixType>
678template<
class MatrixType>
685template<
class MatrixType>
692template<
class MatrixType>
699template<
class MatrixType>
706template<
class MatrixType>
713template<
class MatrixType>
720template<
class MatrixType>
721Teuchos::RCP<const typename TriDiSolver<MatrixType, true>::row_matrix_type>
727template<
class MatrixType>
734template<
class MatrixType>
741template<
class MatrixType>
748template<
class MatrixType>
755template<
class MatrixType>
757 const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
758 Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y,
759 Teuchos::ETransp
mode,
767template<
class MatrixType>
775template<
class MatrixType>
777 const Teuchos::EVerbosityLevel
verbLevel)
const
785#define IFPACK2_DETAILS_TRIDISOLVER_INSTANT(S,LO,GO,N) \
786 template class Ifpack2::Details::TriDiSolver< Tpetra::RowMatrix<S, LO, GO, N> >;
virtual void setMatrix(const Teuchos::RCP< const Tpetra::RowMatrix< MatrixType::scalar_type, MatrixType::local_ordinal_type, MatrixType::global_ordinal_type, MatrixType::node_type > > &A)=0
Set the new matrix.
Ifpack2's implementation of Trilinos::Details::LinearSolver interface.
Definition Ifpack2_Details_LinearSolver_decl.hpp:77
MatrixType::scalar_type scalar_type
The type of entries in the input (global) matrix.
Definition Ifpack2_Details_TriDiSolver_decl.hpp:76
MatrixType::scalar_type scalar_type
The type of entries in the input (global) matrix.
Definition Ifpack2_Details_TriDiSolver_decl.hpp:337
"Preconditioner" that uses LAPACK's tridi LU.
Definition Ifpack2_Details_TriDiSolver_decl.hpp:52
virtual double getComputeTime() const=0
The time (in seconds) spent in compute().
virtual bool isInitialized() const=0
True if the preconditioner has been successfully initialized, else false.
virtual void compute()=0
Set up the numerical values in this preconditioner.
virtual int getNumCompute() const=0
The number of calls to compute().
virtual Teuchos::RCP< const Tpetra::RowMatrix< MatrixType::scalar_type, MatrixType::local_ordinal_type, MatrixType::global_ordinal_type, MatrixType::node_type > > getMatrix() const=0
The input matrix given to the constructor.
virtual int getNumApply() const=0
The number of calls to apply().
virtual double getApplyTime() const=0
The time (in seconds) spent in apply().
virtual void setParameters(const Teuchos::ParameterList &List)=0
Set this preconditioner's parameters.
virtual void apply(const Tpetra::MultiVector< MatrixType::scalar_type, MatrixType::local_ordinal_type, MatrixType::global_ordinal_type, MatrixType::node_type > &X, Tpetra::MultiVector< MatrixType::scalar_type, MatrixType::local_ordinal_type, MatrixType::global_ordinal_type, MatrixType::node_type > &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, MatrixType::scalar_type alpha=Teuchos::ScalarTraits< MatrixType::scalar_type >::one(), MatrixType::scalar_type beta=Teuchos::ScalarTraits< MatrixType::scalar_type >::zero()) const=0
Apply the preconditioner to X, putting the result in Y.
virtual Teuchos::RCP< const Tpetra::Map< MatrixType::local_ordinal_type, MatrixType::global_ordinal_type, MatrixType::node_type > > getRangeMap() const=0
The range Map of this operator.
virtual Teuchos::RCP< const Tpetra::Map< MatrixType::local_ordinal_type, MatrixType::global_ordinal_type, MatrixType::node_type > > getDomainMap() const=0
The domain Map of this operator.
virtual bool isComputed() const=0
True if the preconditioner has been successfully computed, else false.
virtual void initialize()=0
Set up the graph structure of this preconditioner.
virtual double getInitializeTime() const=0
The time (in seconds) spent in initialize().
virtual int getNumInitialize() const=0
The number of calls to initialize().
Ifpack2 implementation details.
Preconditioners and smoothers for Tpetra sparse matrices.
Definition Ifpack2_AdditiveSchwarz_decl.hpp:41