10#ifndef IFPACK2_DETAILS_AMESOS2WRAPPER_DEF_HPP
11#define IFPACK2_DETAILS_AMESOS2WRAPPER_DEF_HPP
13#ifdef HAVE_IFPACK2_AMESOS2
15#include "Ifpack2_LocalFilter.hpp"
16#include "Trilinos_Details_LinearSolverFactory.hpp"
17#include "Trilinos_Details_LinearSolver.hpp"
18#include "Teuchos_TimeMonitor.hpp"
19#include "Teuchos_TypeNameTraits.hpp"
26 extern void registerLinearSolverFactory ();
33template <
class MatrixType>
37 InitializeTime_ (0.0),
43 IsInitialized_ (false),
48template <
class MatrixType>
52template <
class MatrixType>
55 using Teuchos::ParameterList;
64 RCP<ParameterList> theList;
65 if (params.name () ==
"Amesos2") {
66 theList = rcp (
new ParameterList (params));
67 }
else if (params.isSublist (
"Amesos2")) {
69 ParameterList subpl = params.sublist (
"Amesos2");
70 theList = rcp (
new ParameterList (subpl));
71 theList->setName (
"Amesos2");
72 if (params.isParameter (
"Amesos2 solver name")) {
73 SolverName_ = params.get<std::string>(
"Amesos2 solver name");
78 TEUCHOS_TEST_FOR_EXCEPTION(
79 true, std::runtime_error,
"The ParameterList passed to Amesos2 must be "
80 "called \"Amesos2\".");
85 if (solver_.is_null ()) {
86 parameterList_ = theList;
92 solver_->setParameters(theList);
96template <
class MatrixType>
97Teuchos::RCP<const Teuchos::Comm<int> >
99 TEUCHOS_TEST_FOR_EXCEPTION(
100 A_.is_null (), std::runtime_error,
"Ifpack2::Amesos2Wrapper::getComm: "
101 "The matrix is null. Please call setMatrix() with a nonnull input "
102 "before calling this method.");
103 return A_->getComm ();
107template <
class MatrixType>
108Teuchos::RCP<const typename Amesos2Wrapper<MatrixType>::row_matrix_type>
114template <
class MatrixType>
115Teuchos::RCP<const typename Amesos2Wrapper<MatrixType>::map_type>
118 TEUCHOS_TEST_FOR_EXCEPTION(
119 A_.is_null (), std::runtime_error,
"Ifpack2::Amesos2Wrapper::getDomainMap: "
120 "The matrix is null. Please call setMatrix() with a nonnull input "
121 "before calling this method.");
122 return A_->getDomainMap ();
126template <
class MatrixType>
127Teuchos::RCP<const typename Amesos2Wrapper<MatrixType>::map_type>
130 TEUCHOS_TEST_FOR_EXCEPTION(
131 A_.is_null (), std::runtime_error,
"Ifpack2::Amesos2Wrapper::getRangeMap: "
132 "The matrix is null. Please call setMatrix() with a nonnull input "
133 "before calling this method.");
134 return A_->getRangeMap ();
138template <
class MatrixType>
144template <
class MatrixType>
146 return NumInitialize_;
150template <
class MatrixType>
156template <
class MatrixType>
162template <
class MatrixType>
164 return InitializeTime_;
168template<
class MatrixType>
174template<
class MatrixType>
179template<
class MatrixType>
186 IsInitialized_ =
false;
207template<
class MatrixType>
208Teuchos::RCP<const typename Amesos2Wrapper<MatrixType>::row_matrix_type>
209Amesos2Wrapper<MatrixType>::makeLocalFilter (
const Teuchos::RCP<const row_matrix_type>& A)
213 using Teuchos::rcp_dynamic_cast;
214 using Teuchos::rcp_implicit_cast;
219 if (A->getRowMap ()->getComm ()->getSize () == 1 ||
220 A->getRowMap ()->isSameAs (* (A->getColMap ()))) {
227 RCP<const LocalFilter<row_matrix_type> > A_lf_r =
228 rcp_dynamic_cast<const LocalFilter<row_matrix_type> > (A);
229 if (! A_lf_r.is_null ()) {
230 return rcp_implicit_cast<const row_matrix_type> (A_lf_r);
236 return rcp (
new LocalFilter<row_matrix_type> (A));
241template<
class MatrixType>
246 using Teuchos::rcp_const_cast;
247 using Teuchos::rcp_dynamic_cast;
249 using Teuchos::TimeMonitor;
250 using Teuchos::Array;
251 using Teuchos::ArrayView;
253 const std::string timerName (
"Ifpack2::Amesos2Wrapper::initialize");
254 RCP<Time> timer = TimeMonitor::lookupCounter (timerName);
255 if (timer.is_null ()) {
256 timer = TimeMonitor::getNewCounter (timerName);
259 double startTime = timer->wallTime();
262 TimeMonitor timeMon (*timer);
265 TEUCHOS_TEST_FOR_EXCEPTION(
266 A_.is_null (), std::runtime_error,
"Ifpack2::Amesos2Wrapper::initialize: "
267 "The matrix to precondition is null. Please call setMatrix() with a "
268 "nonnull input before calling this method.");
271 IsInitialized_ =
false;
274 RCP<const row_matrix_type> A_local = makeLocalFilter (A_);
275 TEUCHOS_TEST_FOR_EXCEPTION(
276 A_local.is_null (), std::logic_error,
"Ifpack2::AmesosWrapper::initialize: "
277 "makeLocalFilter returned null; it failed to compute A_local. "
278 "Please report this bug to the Ifpack2 developers.");
283 A_local_crs_ = rcp_dynamic_cast<const crs_matrix_type> (A_local);
285 if (A_local_crs_.is_null ()) {
287 Array<size_t> entriesPerRow(numRows);
290 entriesPerRow[i] = A_local->getNumEntriesInLocalRow(i);
292 RCP<crs_matrix_type> A_local_crs_nc =
294 A_local->getColMap (),
297 typename crs_matrix_type::nonconst_local_inds_host_view_type indices(
"Indices",A_local->getLocalMaxNumRowEntries() );
298 typename crs_matrix_type::nonconst_values_host_view_type values(
"Values", A_local->getLocalMaxNumRowEntries());
301 size_t numEntries = 0;
302 A_local->getLocalRowCopy(i, indices, values, numEntries);
303 ArrayView<const local_ordinal_type> indicesInsert(indices.data(), numEntries);
304 ArrayView<const scalar_type> valuesInsert((
const scalar_type*)values.data(), numEntries);
305 A_local_crs_nc->insertLocalValues(i, indicesInsert, valuesInsert);
307 A_local_crs_nc->fillComplete (A_local->getDomainMap (), A_local->getRangeMap ());
308 A_local_crs_ = rcp_const_cast<const crs_matrix_type> (A_local_crs_nc);
320 if (! Trilinos::Details::Impl::rememberRegisteredSomeLinearSolverFactory (
"Amesos2")) {
321 Amesos2::Details::registerLinearSolverFactory ();
324 solver_ = Trilinos::Details::getLinearSolver<MV, OP, typename MV::mag_type> (
"Amesos2", SolverName_);
325 TEUCHOS_TEST_FOR_EXCEPTION
326 (solver_.is_null (), std::runtime_error,
"Ifpack2::Details::"
327 "Amesos2Wrapper::initialize: Failed to create Amesos2 solver!");
329 solver_->setMatrix (A_local_crs_);
331 if (parameterList_ != Teuchos::null) {
333 parameterList_ = Teuchos::null;
338 solver_->symbolic ();
341 IsInitialized_ =
true;
344 InitializeTime_ += (timer->wallTime() - startTime);
347template<
class MatrixType>
352 using Teuchos::TimeMonitor;
359 const std::string timerName (
"Ifpack2::Details::Amesos2Wrapper::compute");
360 RCP<Time> timer = TimeMonitor::lookupCounter (timerName);
361 if (timer.is_null ()) {
362 timer = TimeMonitor::getNewCounter (timerName);
365 double startTime = timer->wallTime();
368 TimeMonitor timeMon (*timer);
375 ComputeTime_ += (timer->wallTime() - startTime);
379template <
class MatrixType>
381apply (
const Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type>& X,
382 Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type>& Y,
383 Teuchos::ETransp mode,
387 using Teuchos::ArrayView;
390 using Teuchos::rcpFromRef;
392 using Teuchos::TimeMonitor;
397 const std::string timerName (
"Ifpack2::Amesos2Wrapper::apply");
398 RCP<Time> timer = TimeMonitor::lookupCounter (timerName);
399 if (timer.is_null ()) {
400 timer = TimeMonitor::getNewCounter (timerName);
403 double startTime = timer->wallTime();
406 TimeMonitor timeMon (*timer);
408 TEUCHOS_TEST_FOR_EXCEPTION(
410 "Ifpack2::Amesos2Wrapper::apply: You must call compute() to compute the "
411 "incomplete factorization, before calling apply().");
413 TEUCHOS_TEST_FOR_EXCEPTION(
414 X.getNumVectors() != Y.getNumVectors(), std::runtime_error,
415 "Ifpack2::Amesos2Wrapper::apply: X and Y must have the same number of columns. "
416 "X has " << X.getNumVectors () <<
" columns, but Y has "
417 << Y.getNumVectors () <<
" columns.");
419 TEUCHOS_TEST_FOR_EXCEPTION(
420 mode != Teuchos::NO_TRANS, std::logic_error,
421 "Ifpack2::Amesos2Wrapper::apply: Solving with the transpose (mode == "
422 "Teuchos::TRANS) or conjugate transpose (Teuchos::CONJ_TRANS) is not "
428 RCP<MV> Y_temp = (alpha != STS::one () || beta != STS::zero ()) ?
429 rcp (
new MV (Y.getMap (), Y.getNumVectors ())) :
435 RCP<const MV> X_temp;
438 X_temp = rcp (
new MV (X, Teuchos::Copy));
440 X_temp = rcpFromRef (X);
445 RCP<const MV> X_local;
451 const bool multipleProcs = (A_->getRowMap ()->
getComm ()->getSize () > 1) || (X.getMap ()->
getComm ()->getSize () > 1);
456 X_local = X_temp->offsetView (A_local_crs_->getDomainMap (), 0);
457 Y_local = Y_temp->offsetViewNonConst (A_local_crs_->getRangeMap (), 0);
466 solver_->solve (*Y_local, *X_local);
468 if (alpha != STS::one () || beta != STS::zero ()) {
469 Y.update (alpha, *Y_temp, beta);
475 ApplyTime_ += (timer->wallTime() - startTime);
479template <
class MatrixType>
481 using Teuchos::TypeNameTraits;
482 std::ostringstream os;
487 os <<
"\"Ifpack2::Amesos2Wrapper\": {";
488 if (this->getObjectLabel () !=
"") {
489 os <<
"Label: \"" << this->getObjectLabel () <<
"\", ";
491 os <<
"Initialized: " << (
isInitialized () ?
"true" :
"false")
492 <<
", Computed: " << (
isComputed () ?
"true" :
"false");
494 if (A_local_crs_.is_null ()) {
495 os <<
", Matrix: null";
498 os <<
", Global matrix dimensions: ["
499 << A_local_crs_->getGlobalNumRows () <<
", " << A_local_crs_->getGlobalNumCols () <<
"]";
504 if (! solver_.is_null ()) {
505 Teuchos::Describable* d =
dynamic_cast<Teuchos::Describable*
> (solver_.getRawPtr ());
508 os << d->description ();
517template <
class MatrixType>
520describe (Teuchos::FancyOStream& out,
521 const Teuchos::EVerbosityLevel verbLevel)
const
524 using Teuchos::OSTab;
526 using Teuchos::TypeNameTraits;
529 const Teuchos::EVerbosityLevel vl = (verbLevel == Teuchos::VERB_DEFAULT) ?
530 Teuchos::VERB_LOW : verbLevel;
534 if (vl > Teuchos::VERB_NONE) {
535 out <<
"\"Ifpack2::Amesos2Wrapper\":" << endl;
537 out <<
"MatrixType: \"" << TypeNameTraits<MatrixType>::name ()
540 if (this->getObjectLabel () !=
"") {
541 out <<
"Label: \"" << this->getObjectLabel () <<
"\"" << endl;
544 out <<
"Initialized: " << (
isInitialized () ?
"true" :
"false") << endl;
545 out <<
"Computed: " << (
isComputed () ?
"true" :
"false") << endl;
547 out <<
"Number of compute calls: " <<
getNumCompute () << endl;
548 out <<
"Number of apply calls: " <<
getNumApply () << endl;
550 out <<
"Total time in seconds for compute: " <<
getComputeTime () << endl;
551 out <<
"Total time in seconds for apply: " <<
getApplyTime () << endl;
553 if (vl > Teuchos::VERB_LOW) {
554 out <<
"Matrix:" << endl;
555 A_local_crs_->describe (out, vl);
567#define IFPACK2_DETAILS_AMESOS2WRAPPER_INSTANT(S,LO,GO,N) \
568 template class Ifpack2::Details::Amesos2Wrapper< Tpetra::RowMatrix<S, LO, GO, N> >;
572#define IFPACK2_DETAILS_AMESOS2WRAPPER_INSTANT(S,LO,GO,N)
double getComputeTime() const
The total time in seconds spent in successful calls to compute().
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:169
Teuchos::RCP< const map_type > getRangeMap() const
Tpetra::Map representing the range of this operator.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:128
Teuchos::RCP< const row_matrix_type > getMatrix() const
The input matrix; the matrix to be preconditioned.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:109
MatrixType::scalar_type scalar_type
The type of the entries of the input MatrixType.
Definition Ifpack2_Details_Amesos2Wrapper_decl.hpp:84
int getNumInitialize() const
The total number of successful calls to initialize().
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:145
double getApplyTime() const
The total time in seconds spent in successful calls to apply().
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:175
int getNumApply() const
The total number of successful calls to apply().
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:157
MatrixType::local_ordinal_type local_ordinal_type
The type of local indices in the input MatrixType.
Definition Ifpack2_Details_Amesos2Wrapper_decl.hpp:87
bool isComputed() const
True if compute() completed successfully, else false.
Definition Ifpack2_Details_Amesos2Wrapper_decl.hpp:171
virtual ~Amesos2Wrapper()
Destructor.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:49
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:520
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:381
std::string description() const
A one-line description of this object.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:480
bool isInitialized() const
Returns true if the preconditioner has been successfully initialized.
Definition Ifpack2_Details_Amesos2Wrapper_decl.hpp:153
void initialize()
Compute the preordering and symbolic factorization of the matrix.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:242
int getNumCompute() const
The total number of successful calls to compute().
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:151
void setParameters(const Teuchos::ParameterList ¶ms)
Set parameters.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:53
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:116
Teuchos::RCP< const map_type > getDomainMap() const
Tpetra::Map representing the domain of this operator.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:116
void compute()
Compute the numeric factorization of the matrix.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:348
Teuchos::RCP< const Teuchos::Comm< int > > getComm() const
The input matrix's communicator.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:98
Amesos2Wrapper(const Teuchos::RCP< const row_matrix_type > &A)
Constructor.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:35
double getInitializeTime() const
The total time in seconds spent in successful calls to initialize().
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:163
virtual void setMatrix(const Teuchos::RCP< const row_matrix_type > &A)
Change the matrix to be preconditioned.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:180
bool hasTransposeApply() const
Whether this object's apply() method can apply the transpose (or conjugate transpose,...
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:139
Preconditioners and smoothers for Tpetra sparse matrices.
Definition Ifpack2_AdditiveSchwarz_decl.hpp:41