43#ifndef IFPACK2_DETAILS_AMESOS2WRAPPER_DEF_HPP
44#define IFPACK2_DETAILS_AMESOS2WRAPPER_DEF_HPP
46#ifdef HAVE_IFPACK2_AMESOS2
48#include "Ifpack2_LocalFilter.hpp"
49#include "Trilinos_Details_LinearSolverFactory.hpp"
50#include "Trilinos_Details_LinearSolver.hpp"
51#include "Teuchos_TimeMonitor.hpp"
52#include "Teuchos_TypeNameTraits.hpp"
66template <
class MatrixType>
70 InitializeTime_ (0.0),
76 IsInitialized_ (false),
81template <
class MatrixType>
85template <
class MatrixType>
88 using Teuchos::ParameterList;
97 RCP<ParameterList> theList;
98 if (params.name () ==
"Amesos2") {
99 theList = rcp (
new ParameterList (params));
100 }
else if (params.isSublist (
"Amesos2")) {
102 ParameterList subpl = params.sublist (
"Amesos2");
103 theList = rcp (
new ParameterList (subpl));
104 theList->setName (
"Amesos2");
105 if (params.isParameter (
"Amesos2 solver name")) {
106 SolverName_ = params.get<std::string>(
"Amesos2 solver name");
111 TEUCHOS_TEST_FOR_EXCEPTION(
112 true, std::runtime_error,
"The ParameterList passed to Amesos2 must be "
113 "called \"Amesos2\".");
118 if (solver_.is_null ()) {
119 parameterList_ = theList;
125 solver_->setParameters(theList);
129template <
class MatrixType>
130Teuchos::RCP<const Teuchos::Comm<int> >
132 TEUCHOS_TEST_FOR_EXCEPTION(
133 A_.is_null (), std::runtime_error,
"Ifpack2::Amesos2Wrapper::getComm: "
134 "The matrix is null. Please call setMatrix() with a nonnull input "
135 "before calling this method.");
136 return A_->getComm ();
140template <
class MatrixType>
141Teuchos::RCP<const typename Amesos2Wrapper<MatrixType>::row_matrix_type>
147template <
class MatrixType>
148Teuchos::RCP<const typename Amesos2Wrapper<MatrixType>::map_type>
151 TEUCHOS_TEST_FOR_EXCEPTION(
152 A_.is_null (), std::runtime_error,
"Ifpack2::Amesos2Wrapper::getDomainMap: "
153 "The matrix is null. Please call setMatrix() with a nonnull input "
154 "before calling this method.");
155 return A_->getDomainMap ();
159template <
class MatrixType>
160Teuchos::RCP<const typename Amesos2Wrapper<MatrixType>::map_type>
163 TEUCHOS_TEST_FOR_EXCEPTION(
164 A_.is_null (), std::runtime_error,
"Ifpack2::Amesos2Wrapper::getRangeMap: "
165 "The matrix is null. Please call setMatrix() with a nonnull input "
166 "before calling this method.");
167 return A_->getRangeMap ();
171template <
class MatrixType>
177template <
class MatrixType>
179 return NumInitialize_;
183template <
class MatrixType>
189template <
class MatrixType>
195template <
class MatrixType>
197 return InitializeTime_;
201template<
class MatrixType>
207template<
class MatrixType>
212template<
class MatrixType>
219 IsInitialized_ =
false;
240template<
class MatrixType>
241Teuchos::RCP<const typename Amesos2Wrapper<MatrixType>::row_matrix_type>
246 using Teuchos::rcp_dynamic_cast;
247 using Teuchos::rcp_implicit_cast;
252 if (A->getRowMap ()->getComm ()->getSize () == 1 ||
253 A->getRowMap ()->isSameAs (* (A->getColMap ()))) {
260 RCP<const LocalFilter<row_matrix_type> > A_lf_r =
261 rcp_dynamic_cast<const LocalFilter<row_matrix_type> > (A);
262 if (! A_lf_r.is_null ()) {
263 return rcp_implicit_cast<const row_matrix_type> (A_lf_r);
269 return rcp (
new LocalFilter<row_matrix_type> (A));
274template<
class MatrixType>
279 using Teuchos::rcp_const_cast;
280 using Teuchos::rcp_dynamic_cast;
282 using Teuchos::TimeMonitor;
283 using Teuchos::Array;
284 using Teuchos::ArrayView;
286 const std::string timerName (
"Ifpack2::Amesos2Wrapper::initialize");
287 RCP<Time> timer = TimeMonitor::lookupCounter (timerName);
288 if (timer.is_null ()) {
289 timer = TimeMonitor::getNewCounter (timerName);
292 double startTime = timer->wallTime();
295 TimeMonitor timeMon (*timer);
298 TEUCHOS_TEST_FOR_EXCEPTION(
299 A_.is_null (), std::runtime_error,
"Ifpack2::Amesos2Wrapper::initialize: "
300 "The matrix to precondition is null. Please call setMatrix() with a "
301 "nonnull input before calling this method.");
304 IsInitialized_ =
false;
307 RCP<const row_matrix_type> A_local = makeLocalFilter (A_);
308 TEUCHOS_TEST_FOR_EXCEPTION(
309 A_local.is_null (), std::logic_error,
"Ifpack2::AmesosWrapper::initialize: "
310 "makeLocalFilter returned null; it failed to compute A_local. "
311 "Please report this bug to the Ifpack2 developers.");
316 A_local_crs_ = rcp_dynamic_cast<const crs_matrix_type> (A_local);
318 if (A_local_crs_.is_null ()) {
320 Array<size_t> entriesPerRow(numRows);
323 entriesPerRow[i] = A_local->getNumEntriesInLocalRow(i);
325 RCP<crs_matrix_type> A_local_crs_nc =
327 A_local->getColMap (),
330 typename crs_matrix_type::nonconst_local_inds_host_view_type indices(
"Indices",A_local->getLocalMaxNumRowEntries() );
331 typename crs_matrix_type::nonconst_values_host_view_type values(
"Values", A_local->getLocalMaxNumRowEntries());
334 size_t numEntries = 0;
335 A_local->getLocalRowCopy(i, indices, values, numEntries);
336 ArrayView<const local_ordinal_type> indicesInsert(indices.data(), numEntries);
337 ArrayView<const scalar_type> valuesInsert((
const scalar_type*)values.data(), numEntries);
338 A_local_crs_nc->insertLocalValues(i, indicesInsert, valuesInsert);
340 A_local_crs_nc->fillComplete (A_local->getDomainMap (), A_local->getRangeMap ());
341 A_local_crs_ = rcp_const_cast<const crs_matrix_type> (A_local_crs_nc);
353 if (! Trilinos::Details::Impl::rememberRegisteredSomeLinearSolverFactory (
"Amesos2")) {
354 Amesos2::Details::registerLinearSolverFactory ();
357 solver_ = Trilinos::Details::getLinearSolver<MV, OP, typename MV::mag_type> (
"Amesos2", SolverName_);
358 TEUCHOS_TEST_FOR_EXCEPTION
359 (solver_.is_null (), std::runtime_error,
"Ifpack2::Details::"
360 "Amesos2Wrapper::initialize: Failed to create Amesos2 solver!");
362 solver_->setMatrix (A_local_crs_);
364 if (parameterList_ != Teuchos::null) {
365 setParameters (*parameterList_);
366 parameterList_ = Teuchos::null;
371 solver_->symbolic ();
374 IsInitialized_ =
true;
377 InitializeTime_ += (timer->wallTime() - startTime);
380template<
class MatrixType>
385 using Teuchos::TimeMonitor;
388 if (! isInitialized ()) {
392 const std::string timerName (
"Ifpack2::Details::Amesos2Wrapper::compute");
393 RCP<Time> timer = TimeMonitor::lookupCounter (timerName);
394 if (timer.is_null ()) {
395 timer = TimeMonitor::getNewCounter (timerName);
398 double startTime = timer->wallTime();
401 TimeMonitor timeMon (*timer);
408 ComputeTime_ += (timer->wallTime() - startTime);
412template <
class MatrixType>
414apply (
const Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type>& X,
415 Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type>& Y,
416 Teuchos::ETransp mode,
420 using Teuchos::ArrayView;
423 using Teuchos::rcpFromRef;
425 using Teuchos::TimeMonitor;
430 const std::string timerName (
"Ifpack2::Amesos2Wrapper::apply");
431 RCP<Time> timer = TimeMonitor::lookupCounter (timerName);
432 if (timer.is_null ()) {
433 timer = TimeMonitor::getNewCounter (timerName);
436 double startTime = timer->wallTime();
439 TimeMonitor timeMon (*timer);
441 TEUCHOS_TEST_FOR_EXCEPTION(
442 ! isComputed (), std::runtime_error,
443 "Ifpack2::Amesos2Wrapper::apply: You must call compute() to compute the "
444 "incomplete factorization, before calling apply().");
446 TEUCHOS_TEST_FOR_EXCEPTION(
447 X.getNumVectors() != Y.getNumVectors(), std::runtime_error,
448 "Ifpack2::Amesos2Wrapper::apply: X and Y must have the same number of columns. "
449 "X has " << X.getNumVectors () <<
" columns, but Y has "
450 << Y.getNumVectors () <<
" columns.");
452 TEUCHOS_TEST_FOR_EXCEPTION(
453 mode != Teuchos::NO_TRANS, std::logic_error,
454 "Ifpack2::Amesos2Wrapper::apply: Solving with the transpose (mode == "
455 "Teuchos::TRANS) or conjugate transpose (Teuchos::CONJ_TRANS) is not "
461 RCP<MV> Y_temp = (alpha != STS::one () || beta != STS::zero ()) ?
462 rcp (
new MV (Y.getMap (), Y.getNumVectors ())) :
468 RCP<const MV> X_temp;
471 X_temp = rcp (
new MV (X, Teuchos::Copy));
473 X_temp = rcpFromRef (X);
478 RCP<const MV> X_local;
484 const bool multipleProcs = (A_->getRowMap ()->getComm ()->getSize () > 1) || (X.getMap ()->getComm ()->getSize () > 1);
489 X_local = X_temp->offsetView (A_local_crs_->getDomainMap (), 0);
490 Y_local = Y_temp->offsetViewNonConst (A_local_crs_->getRangeMap (), 0);
499 solver_->solve (*Y_local, *X_local);
501 if (alpha != STS::one () || beta != STS::zero ()) {
502 Y.update (alpha, *Y_temp, beta);
508 ApplyTime_ += (timer->wallTime() - startTime);
512template <
class MatrixType>
514 using Teuchos::TypeNameTraits;
515 std::ostringstream os;
520 os <<
"\"Ifpack2::Amesos2Wrapper\": {";
521 if (this->getObjectLabel () !=
"") {
522 os <<
"Label: \"" << this->getObjectLabel () <<
"\", ";
524 os <<
"Initialized: " << (isInitialized () ?
"true" :
"false")
525 <<
", Computed: " << (isComputed () ?
"true" :
"false");
527 if (A_local_crs_.is_null ()) {
528 os <<
", Matrix: null";
531 os <<
", Global matrix dimensions: ["
532 << A_local_crs_->getGlobalNumRows () <<
", " << A_local_crs_->getGlobalNumCols () <<
"]";
537 if (! solver_.is_null ()) {
538 Teuchos::Describable* d =
dynamic_cast<Teuchos::Describable*
> (solver_.getRawPtr ());
541 os << d->description ();
550template <
class MatrixType>
553describe (Teuchos::FancyOStream& out,
554 const Teuchos::EVerbosityLevel verbLevel)
const
557 using Teuchos::OSTab;
559 using Teuchos::TypeNameTraits;
562 const Teuchos::EVerbosityLevel vl = (verbLevel == Teuchos::VERB_DEFAULT) ?
563 Teuchos::VERB_LOW : verbLevel;
567 if (vl > Teuchos::VERB_NONE) {
568 out <<
"\"Ifpack2::Amesos2Wrapper\":" << endl;
570 out <<
"MatrixType: \"" << TypeNameTraits<MatrixType>::name ()
573 if (this->getObjectLabel () !=
"") {
574 out <<
"Label: \"" << this->getObjectLabel () <<
"\"" << endl;
577 out <<
"Initialized: " << (isInitialized () ?
"true" :
"false") << endl;
578 out <<
"Computed: " << (isComputed () ?
"true" :
"false") << endl;
579 out <<
"Number of initialize calls: " << getNumInitialize () << endl;
580 out <<
"Number of compute calls: " << getNumCompute () << endl;
581 out <<
"Number of apply calls: " << getNumApply () << endl;
582 out <<
"Total time in seconds for initialize: " << getInitializeTime () << endl;
583 out <<
"Total time in seconds for compute: " << getComputeTime () << endl;
584 out <<
"Total time in seconds for apply: " << getApplyTime () << endl;
586 if (vl > Teuchos::VERB_LOW) {
587 out <<
"Matrix:" << endl;
588 A_local_crs_->describe (out, vl);
600#define IFPACK2_DETAILS_AMESOS2WRAPPER_INSTANT(S,LO,GO,N) \
601 template class Ifpack2::Details::Amesos2Wrapper< Tpetra::RowMatrix<S, LO, GO, N> >;
605#define IFPACK2_DETAILS_AMESOS2WRAPPER_INSTANT(S,LO,GO,N)
Wrapper class for direct solvers in Amesos2.
Definition Ifpack2_Details_Amesos2Wrapper_decl.hpp:111
double getComputeTime() const
The total time in seconds spent in successful calls to compute().
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:202
Teuchos::RCP< const map_type > getRangeMap() const
Tpetra::Map representing the range of this operator.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:161
Teuchos::RCP< const row_matrix_type > getMatrix() const
The input matrix; the matrix to be preconditioned.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:142
MatrixType::scalar_type scalar_type
The type of the entries of the input MatrixType.
Definition Ifpack2_Details_Amesos2Wrapper_decl.hpp:117
int getNumInitialize() const
The total number of successful calls to initialize().
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:178
double getApplyTime() const
The total time in seconds spent in successful calls to apply().
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:208
int getNumApply() const
The total number of successful calls to apply().
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:190
MatrixType::local_ordinal_type local_ordinal_type
The type of local indices in the input MatrixType.
Definition Ifpack2_Details_Amesos2Wrapper_decl.hpp:120
virtual ~Amesos2Wrapper()
Destructor.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:82
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print the object with some verbosity level to the given output stream.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:553
void apply(const Tpetra::MultiVector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &X, Tpetra::MultiVector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, scalar_type alpha=Teuchos::ScalarTraits< scalar_type >::one(), scalar_type beta=Teuchos::ScalarTraits< scalar_type >::zero()) const
Apply the preconditioner to X, resulting in Y.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:414
std::string description() const
A one-line description of this object.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:513
void initialize()
Compute the preordering and symbolic factorization of the matrix.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:275
int getNumCompute() const
The total number of successful calls to compute().
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:184
void setParameters(const Teuchos::ParameterList ¶ms)
Set parameters.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:86
Tpetra::CrsMatrix< scalar_type, local_ordinal_type, global_ordinal_type, node_type > crs_matrix_type
Type of the Tpetra::CrsMatrix specialization that this class uses.
Definition Ifpack2_Details_Amesos2Wrapper_decl.hpp:149
Teuchos::RCP< const map_type > getDomainMap() const
Tpetra::Map representing the domain of this operator.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:149
void compute()
Compute the numeric factorization of the matrix.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:381
Teuchos::RCP< const Teuchos::Comm< int > > getComm() const
The input matrix's communicator.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:131
Amesos2Wrapper(const Teuchos::RCP< const row_matrix_type > &A)
Constructor.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:68
double getInitializeTime() const
The total time in seconds spent in successful calls to initialize().
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:196
virtual void setMatrix(const Teuchos::RCP< const row_matrix_type > &A)
Change the matrix to be preconditioned.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:213
bool hasTransposeApply() const
Whether this object's apply() method can apply the transpose (or conjugate transpose,...
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:172
void registerLinearSolverFactory()
Ifpack2 implementation details.
Preconditioners and smoothers for Tpetra sparse matrices.
Definition Ifpack2_AdditiveSchwarz_decl.hpp:74