10 #ifndef IFPACK2_DETAILS_DENSESOLVER_DEF_HPP
11 #define IFPACK2_DETAILS_DENSESOLVER_DEF_HPP
13 #include "Ifpack2_LocalFilter.hpp"
15 #include "Ifpack2_Details_DenseSolver.hpp"
16 #include "Tpetra_Map.hpp"
22 # include "Teuchos_DefaultSerialComm.hpp"
33 template<
class MatrixType>
37 initializeTime_ (0.0),
43 isInitialized_ (false),
48 template<
class MatrixType>
52 A_.
is_null (), std::runtime_error,
"Ifpack2::Details::DenseSolver::"
53 "getDomainMap: The input matrix A is null. Please call setMatrix() with a "
54 "nonnull input matrix before calling this method.");
57 return A_->getRangeMap ();
61 template<
class MatrixType>
65 A_.
is_null (), std::runtime_error,
"Ifpack2::Details::DenseSolver::"
66 "getRangeMap: The input matrix A is null. Please call setMatrix() with a "
67 "nonnull input matrix before calling this method.");
70 return A_->getDomainMap ();
74 template<
class MatrixType>
82 template<
class MatrixType>
85 return isInitialized_;
89 template<
class MatrixType>
96 template<
class MatrixType>
99 return numInitialize_;
103 template<
class MatrixType>
110 template<
class MatrixType>
117 template<
class MatrixType>
120 return initializeTime_;
124 template<
class MatrixType>
131 template<
class MatrixType>
138 template<
class MatrixType>
145 template<
class MatrixType>
149 isInitialized_ =
false;
151 A_local_ = Teuchos::null;
152 A_local_dense_.reshape (0, 0);
157 template<
class MatrixType>
164 const global_size_t numRows = A->getRangeMap ()->getGlobalNumElements ();
165 const global_size_t numCols = A->getDomainMap ()->getGlobalNumElements ();
167 numRows != numCols, std::invalid_argument,
"Ifpack2::Details::DenseSolver::"
168 "setMatrix: Input matrix must be (globally) square. "
169 "The matrix you provided is " << numRows <<
" by " << numCols <<
".");
179 template<
class MatrixType>
188 const std::string timerName (
"Ifpack2::Details::DenseSolver::initialize");
190 RCP<Time> timer = TimeMonitor::lookupCounter (timerName);
191 if (timer.is_null ()) {
192 timer = TimeMonitor::getNewCounter (timerName);
195 double startTime = timer->wallTime();
201 A_.
is_null (), std::runtime_error,
"Ifpack2::Details::DenseSolver::"
202 "initialize: The input matrix A is null. Please call setMatrix() "
203 "with a nonnull input before calling this method.");
206 ! A_->hasColMap (), std::invalid_argument,
"Ifpack2::Details::DenseSolver: "
207 "The constructor's input matrix must have a column Map, "
208 "so that it has local indices.");
214 if (A_->getComm ()->getSize () > 1) {
221 A_local_.is_null (), std::logic_error,
"Ifpack2::Details::DenseSolver::"
222 "initialize: A_local_ is null after it was supposed to have been "
223 "initialized. Please report this bug to the Ifpack2 developers.");
226 const size_t numRows = A_local_->getLocalNumRows ();
227 const size_t numCols = A_local_->getLocalNumCols ();
229 numRows != numCols, std::logic_error,
"Ifpack2::Details::DenseSolver::"
230 "initialize: Local filter matrix is not square. This should never happen. "
231 "Please report this bug to the Ifpack2 developers.");
232 A_local_dense_.reshape (numRows, numCols);
233 ipiv_.resize (std::min (numRows, numCols));
234 std::fill (ipiv_.begin (), ipiv_.end (), 0);
236 isInitialized_ =
true;
240 initializeTime_ += (timer->wallTime() - startTime);
244 template<
class MatrixType>
249 template<
class MatrixType>
253 const std::string timerName (
"Ifpack2::Details::DenseSolver::compute");
256 if (timer.is_null ()) {
260 double startTime = timer->wallTime();
266 A_.
is_null (), std::runtime_error,
"Ifpack2::Details::DenseSolver::"
267 "compute: The input matrix A is null. Please call setMatrix() with a "
268 "nonnull input, then call initialize(), before calling this method.");
271 A_local_.is_null (), std::logic_error,
"Ifpack2::Details::DenseSolver::"
272 "compute: A_local_ is null. Please report this bug to the Ifpack2 "
279 extract (A_local_dense_, *A_local_);
280 factor (A_local_dense_, ipiv_ ());
285 computeTime_ += (timer->wallTime() - startTime);
288 template<
class MatrixType>
294 std::fill (ipiv.
begin (), ipiv.
end (), 0);
302 INFO < 0, std::logic_error,
"Ifpack2::Details::DenseSolver::factor: "
303 "LAPACK's _GETRF (LU factorization with partial pivoting) was called "
304 "incorrectly. INFO = " << INFO <<
" < 0. "
305 "Please report this bug to the Ifpack2 developers.");
310 INFO > 0, std::runtime_error,
"Ifpack2::Details::DenseSolver::factor: "
311 "LAPACK's _GETRF (LU factorization with partial pivoting) reports that the "
312 "computed U factor is exactly singular. U(" << INFO <<
"," << INFO <<
") "
313 "(one-based index i) is exactly zero. This probably means that the input "
314 "matrix has a singular diagonal block.");
318 template<
class MatrixType>
319 void DenseSolver<MatrixType, false>::
320 applyImpl (
const MV& X,
323 const scalar_type alpha,
324 const scalar_type beta)
const
329 using Teuchos::rcpFromRef;
333 const int numVecs =
static_cast<int> (X.getNumVectors ());
334 if (alpha == STS::zero ()) {
335 if (beta == STS::zero ()) {
339 Y.putScalar (STS::zero ());
342 Y.scale (STS::zero ());
352 if (beta == STS::zero () && Y.isConstantStride () && alpha == STS::one ()) {
354 Y_tmp = rcpFromRef (Y);
358 if (alpha != STS::one ()) {
359 Y_tmp->scale (alpha);
362 const int Y_stride =
static_cast<int> (Y_tmp->getStride ());
363 ArrayRCP<scalar_type> Y_view = Y_tmp->get1dViewNonConst ();
364 scalar_type*
const Y_ptr = Y_view.getRawPtr ();
368 lapack.
GETRS (trans, A_local_dense_.numRows (), numVecs,
369 A_local_dense_.values (), A_local_dense_.stride (),
370 ipiv_.getRawPtr (), Y_ptr, Y_stride, &INFO);
372 INFO != 0, std::runtime_error,
"Ifpack2::Details::DenseSolver::"
373 "applyImpl: LAPACK's _GETRS (solve using LU factorization with "
374 "partial pivoting) failed with INFO = " << INFO <<
" != 0.");
376 if (beta != STS::zero ()) {
377 Y.update (alpha, *Y_tmp, beta);
379 else if (! Y.isConstantStride ()) {
380 deep_copy (Y, *Y_tmp);
386 template<
class MatrixType>
388 apply (
const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
389 Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y,
398 using Teuchos::rcpFromRef;
400 const std::string timerName (
"Ifpack2::Details::DenseSolver::apply");
402 if (timer.is_null ()) {
406 double startTime = timer->wallTime();
413 ! isComputed_, std::runtime_error,
"Ifpack2::Details::DenseSolver::apply: "
414 "You must have called the compute() method before you may call apply(). "
415 "You may call the apply() method as many times as you want after calling "
416 "compute() once, but you must have called compute() at least once.");
418 const size_t numVecs = X.getNumVectors ();
421 numVecs != Y.getNumVectors (), std::runtime_error,
422 "Ifpack2::Details::DenseSolver::apply: X and Y have different numbers "
423 "of vectors. X has " << X.getNumVectors () <<
", but Y has "
424 << X.getNumVectors () <<
".");
431 RCP<const MV> X_local;
433 const bool multipleProcs = (A_->getRowMap ()->getComm ()->getSize () >= 1);
438 X_local = X.offsetView (A_local_->getDomainMap (), 0);
439 Y_local = Y.offsetViewNonConst (A_local_->getRangeMap (), 0);
443 X_local = rcpFromRef (X);
444 Y_local = rcpFromRef (Y);
449 this->applyImpl (*X_local, *Y_local, mode, alpha, beta);
454 applyTime_ += (timer->wallTime() - startTime);
458 template<
class MatrixType>
462 std::ostringstream out;
467 out <<
"\"Ifpack2::Details::DenseSolver\": ";
469 if (this->getObjectLabel () !=
"") {
470 out <<
"Label: \"" << this->getObjectLabel () <<
"\", ";
472 out <<
"Initialized: " << (
isInitialized () ?
"true" :
"false") <<
", "
473 <<
"Computed: " << (
isComputed () ?
"true" :
"false") <<
", ";
476 out <<
"Matrix: null";
479 out <<
"Matrix: not null"
480 <<
", Global matrix dimensions: ["
481 << A_->getGlobalNumRows () <<
", " << A_->getGlobalNumCols () <<
"]";
489 template<
class MatrixType>
497 using Teuchos::rcpFromRef;
504 RCP<FancyOStream> ptrOut = rcpFromRef (out);
506 if (this->getObjectLabel () !=
"") {
507 out <<
"label: " << this->getObjectLabel () << endl;
509 out <<
"initialized: " << (isInitialized_ ?
"true" :
"false") << endl
510 <<
"computed: " << (isComputed_ ?
"true" :
"false") << endl
511 <<
"number of initialize calls: " << numInitialize_ << endl
512 <<
"number of compute calls: " << numCompute_ << endl
513 <<
"number of apply calls: " << numApply_ << endl
514 <<
"total time in seconds in initialize: " << initializeTime_ << endl
515 <<
"total time in seconds in compute: " << computeTime_ << endl
516 <<
"total time in seconds in apply: " << applyTime_ << endl;
518 out <<
"A_local_dense_:" << endl;
522 for (
int i = 0; i < A_local_dense_.numRows (); ++i) {
523 for (
int j = 0; j < A_local_dense_.numCols (); ++j) {
524 out << A_local_dense_(i,j);
525 if (j + 1 < A_local_dense_.numCols ()) {
529 if (i + 1 < A_local_dense_.numRows ()) {
541 template<
class MatrixType>
549 using Teuchos::rcpFromRef;
552 RCP<FancyOStream> ptrOut = rcpFromRef (out);
560 out <<
"Ifpack2::Details::DenseSolver:" << endl;
562 describeLocal (out, verbLevel);
568 const int myRank = comm.
getRank ();
569 const int numProcs = comm.
getSize ();
571 out <<
"Ifpack2::Details::DenseSolver:" << endl;
574 for (
int p = 0; p < numProcs; ++p) {
576 out <<
"Process " << myRank <<
":" << endl;
577 describeLocal (out, verbLevel);
587 template<
class MatrixType>
590 const row_matrix_type& A_local)
594 typedef local_ordinal_type LO;
606 const map_type& rowMap = * (A_local.getRowMap ());
610 const size_type maxNumRowEntries =
611 static_cast<size_type
> (A_local.getLocalMaxNumRowEntries ());
612 nonconst_local_inds_host_view_type localIndices (
"localIndices",maxNumRowEntries);
613 nonconst_values_host_view_type values (
"values",maxNumRowEntries);
615 const LO numLocalRows =
static_cast<LO
> (rowMap.getLocalNumElements ());
616 const LO minLocalRow = rowMap.getMinLocalIndex ();
619 const LO maxLocalRow = minLocalRow + numLocalRows;
620 for (LO localRow = minLocalRow; localRow < maxLocalRow; ++localRow) {
627 const size_type numEntriesInRow =
628 static_cast<size_type
> (A_local.getNumEntriesInLocalRow (localRow));
629 size_t numEntriesOut = 0;
630 A_local.getLocalRowCopy (localRow,
634 for (LO k = 0; k < numEntriesInRow; ++k) {
635 const LO localCol = localIndices[k];
636 const scalar_type val = values[k];
639 A_local_dense(localRow, localCol) += val;
648 template<
class MatrixType>
649 DenseSolver<MatrixType, true>::
655 template<
class MatrixType>
662 template<
class MatrixType>
669 template<
class MatrixType>
677 template<
class MatrixType>
684 template<
class MatrixType>
691 template<
class MatrixType>
698 template<
class MatrixType>
705 template<
class MatrixType>
712 template<
class MatrixType>
719 template<
class MatrixType>
726 template<
class MatrixType>
733 template<
class MatrixType>
740 template<
class MatrixType>
748 template<
class MatrixType>
755 template<
class MatrixType>
756 DenseSolver<MatrixType, true>::~DenseSolver ()
762 template<
class MatrixType>
769 template<
class MatrixType>
771 apply (
const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
772 Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y,
775 scalar_type beta)
const
781 template<
class MatrixType>
783 DenseSolver<MatrixType, true>::description ()
const
789 template<
class MatrixType>
790 void DenseSolver<MatrixType, true>::
801 #define IFPACK2_DETAILS_DENSESOLVER_INSTANT(S,LO,GO,N) \
802 template class Ifpack2::Details::DenseSolver< Tpetra::RowMatrix<S, LO, GO, N> >;
804 #endif // IFPACK2_DETAILS_DENSESOLVER_HPP
ScalarType * values() const
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 int getNumApply() const =0
The number of calls to apply().
virtual int getSize() const =0
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 getRank() const =0
basic_OSTab< char > OSTab
DenseSolver(const Teuchos::RCP< const row_matrix_type > &matrix)
Constructor.
Definition: Ifpack2_Details_DenseSolver_def.hpp:35
basic_FancyOStream< char > FancyOStream
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual void compute()=0
Set up the numerical values in this preconditioner.
"Preconditioner" that uses LAPACK's dense LU.
Definition: Ifpack2_Details_DenseSolver_decl.hpp:42
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print the object with some verbosity level to the given FancyOStream.
Definition: Ifpack2_Details_DenseSolver_def.hpp:543
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.
void GETRF(const OrdinalType &m, const OrdinalType &n, ScalarType *A, const OrdinalType &lda, OrdinalType *IPIV, OrdinalType *info) const
virtual void barrier() const =0
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual int getNumCompute() const =0
The number of calls to compute().
int putScalar(const ScalarType value=Teuchos::ScalarTraits< ScalarType >::zero())
virtual double getInitializeTime() const =0
The time (in seconds) spent in initialize().
virtual void initialize()=0
Set up the graph structure of this preconditioner.
virtual bool isComputed() const =0
True if the preconditioner has been successfully computed, else false.
virtual int getNumInitialize() const =0
The number of calls to initialize().
OrdinalType numCols() const
void GETRS(const char &TRANS, const OrdinalType &n, const OrdinalType &nrhs, const ScalarType *A, const OrdinalType &lda, const OrdinalType *IPIV, ScalarType *B, const OrdinalType &ldb, OrdinalType *info) const
virtual void setParameters(const Teuchos::ParameterList &List)=0
Set this preconditioner's parameters.
TypeTo as(const TypeFrom &t)
virtual double getComputeTime() const =0
The time (in seconds) spent in compute().
Access only local rows and columns of a sparse matrix.
Definition: Ifpack2_LocalFilter_decl.hpp:128
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_typealpha=Teuchos::ScalarTraits< MatrixType::scalar_type >::one(), MatrixType::scalar_typebeta=Teuchos::ScalarTraits< MatrixType::scalar_type >::zero()) const =0
Apply the preconditioner to X, putting the result in Y.
MatrixType::scalar_type scalar_type
The type of entries in the input (global) matrix.
Definition: Ifpack2_Details_DenseSolver_decl.hpp:75
virtual bool isInitialized() const =0
True if the preconditioner has been successfully initialized, else false.
virtual double getApplyTime() const =0
The time (in seconds) spent in apply().
OrdinalType stride() const
OrdinalType numRows() const
std::string toString(const T &t)
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.