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"
26extern void registerLinearSolverFactory();
33template <
class MatrixType>
37 , InitializeTime_(0.0)
43 , IsInitialized_(false)
47template <
class MatrixType>
50template <
class MatrixType>
52 using Teuchos::ParameterList;
61 RCP<ParameterList> theList;
62 if (params.name() ==
"Amesos2") {
63 theList = rcp(
new ParameterList(params));
64 }
else if (params.isSublist(
"Amesos2")) {
66 ParameterList subpl = params.sublist(
"Amesos2");
67 theList = rcp(
new ParameterList(subpl));
68 theList->setName(
"Amesos2");
69 if (params.isParameter(
"Amesos2 solver name")) {
70 SolverName_ = params.get<std::string>(
"Amesos2 solver name");
75 TEUCHOS_TEST_FOR_EXCEPTION(
76 true, std::runtime_error,
77 "The ParameterList passed to Amesos2 must be "
78 "called \"Amesos2\".");
83 if (solver_.is_null()) {
84 parameterList_ = theList;
90 solver_->setParameters(theList);
93template <
class MatrixType>
94Teuchos::RCP<const Teuchos::Comm<int> >
96 TEUCHOS_TEST_FOR_EXCEPTION(
97 A_.is_null(), std::runtime_error,
98 "Ifpack2::Amesos2Wrapper::getComm: "
99 "The matrix is null. Please call setMatrix() with a nonnull input "
100 "before calling this method.");
101 return A_->getComm();
104template <
class MatrixType>
105Teuchos::RCP<const typename Amesos2Wrapper<MatrixType>::row_matrix_type>
110template <
class MatrixType>
111Teuchos::RCP<const typename Amesos2Wrapper<MatrixType>::map_type>
113 TEUCHOS_TEST_FOR_EXCEPTION(
114 A_.is_null(), std::runtime_error,
115 "Ifpack2::Amesos2Wrapper::getDomainMap: "
116 "The matrix is null. Please call setMatrix() with a nonnull input "
117 "before calling this method.");
118 return A_->getDomainMap();
121template <
class MatrixType>
122Teuchos::RCP<const typename Amesos2Wrapper<MatrixType>::map_type>
124 TEUCHOS_TEST_FOR_EXCEPTION(
125 A_.is_null(), std::runtime_error,
126 "Ifpack2::Amesos2Wrapper::getRangeMap: "
127 "The matrix is null. Please call setMatrix() with a nonnull input "
128 "before calling this method.");
129 return A_->getRangeMap();
132template <
class MatrixType>
137template <
class MatrixType>
139 return NumInitialize_;
142template <
class MatrixType>
147template <
class MatrixType>
152template <
class MatrixType>
154 return InitializeTime_;
157template <
class MatrixType>
162template <
class MatrixType>
167template <
class MatrixType>
173 IsInitialized_ =
false;
175 A_local_ = Teuchos::null;
194template <
class MatrixType>
195Teuchos::RCP<const typename Amesos2Wrapper<MatrixType>::row_matrix_type>
196Amesos2Wrapper<MatrixType>::makeLocalFilter(
const Teuchos::RCP<const row_matrix_type>& A) {
199 using Teuchos::rcp_dynamic_cast;
200 using Teuchos::rcp_implicit_cast;
205 if (A->getRowMap()->getComm()->getSize() == 1 ||
206 A->getRowMap()->isSameAs(*(A->getColMap()))) {
213 RCP<const LocalFilter<row_matrix_type> > A_lf_r =
214 rcp_dynamic_cast<const LocalFilter<row_matrix_type> >(A);
215 if (!A_lf_r.is_null()) {
216 return rcp_implicit_cast<const row_matrix_type>(A_lf_r);
221 return rcp(
new LocalFilter<row_matrix_type>(A));
225template <
class MatrixType>
227 using Teuchos::Array;
228 using Teuchos::ArrayView;
231 using Teuchos::rcp_const_cast;
232 using Teuchos::rcp_dynamic_cast;
234 using Teuchos::TimeMonitor;
236 const std::string timerName(
"Ifpack2::Amesos2Wrapper::initialize");
237 RCP<Time> timer = TimeMonitor::lookupCounter(timerName);
238 if (timer.is_null()) {
239 timer = TimeMonitor::getNewCounter(timerName);
242 double startTime = timer->wallTime();
245 TimeMonitor timeMon(*timer);
248 TEUCHOS_TEST_FOR_EXCEPTION(
249 A_.is_null(), std::runtime_error,
250 "Ifpack2::Amesos2Wrapper::initialize: "
251 "The matrix to precondition is null. Please call setMatrix() with a "
252 "nonnull input before calling this method.");
255 IsInitialized_ =
false;
258 A_local_ = makeLocalFilter(A_);
259 TEUCHOS_TEST_FOR_EXCEPTION(
260 A_local_.is_null(), std::logic_error,
261 "Ifpack2::AmesosWrapper::initialize: "
262 "makeLocalFilter returned null; it failed to compute A_local. "
263 "Please report this bug to the Ifpack2 developers.");
268 A_local_crs_ = rcp_dynamic_cast<const crs_matrix_type>(A_local_);
270 if (A_local_crs_.is_null()) {
272 Array<size_t> entriesPerRow(numRows);
274 entriesPerRow[i] = A_local_->getNumEntriesInLocalRow(i);
278 A_local_->getColMap(),
281 typename crs_matrix_type::nonconst_local_inds_host_view_type indices(
"Indices", A_local_->getLocalMaxNumRowEntries());
282 typename crs_matrix_type::nonconst_values_host_view_type values(
"Values", A_local_->getLocalMaxNumRowEntries());
284 size_t numEntries = 0;
285 A_local_->getLocalRowCopy(i, indices, values, numEntries);
286 ArrayView<const local_ordinal_type> indicesInsert(indices.data(), numEntries);
287 ArrayView<const scalar_type> valuesInsert((
const scalar_type*)values.data(), numEntries);
288 A_local_crs_nc_->insertLocalValues(i, indicesInsert, valuesInsert);
290 A_local_crs_nc_->fillComplete(A_local_->getDomainMap(), A_local_->getRangeMap());
291 A_local_crs_ = rcp_const_cast<const crs_matrix_type>(A_local_crs_nc_);
302 if (!Trilinos::Details::Impl::rememberRegisteredSomeLinearSolverFactory(
"Amesos2")) {
303 Amesos2::Details::registerLinearSolverFactory();
306 solver_ = Trilinos::Details::getLinearSolver<MV, OP, typename MV::mag_type>(
"Amesos2", SolverName_);
307 TEUCHOS_TEST_FOR_EXCEPTION(solver_.is_null(), std::runtime_error,
309 "Amesos2Wrapper::initialize: Failed to create Amesos2 solver!");
311 solver_->setMatrix(A_local_crs_);
313 if (parameterList_ != Teuchos::null) {
315 parameterList_ = Teuchos::null;
323 IsInitialized_ =
true;
326 InitializeTime_ += (timer->wallTime() - startTime);
329template <
class MatrixType>
333 using Teuchos::TimeMonitor;
341 if (!A_local_crs_nc_.is_null()) {
343 A_local_crs_nc_->resumeFill();
345 typename crs_matrix_type::nonconst_local_inds_host_view_type indices(
"Indices", A_local_->getLocalMaxNumRowEntries());
346 typename crs_matrix_type::nonconst_values_host_view_type values(
"Values", A_local_->getLocalMaxNumRowEntries());
348 size_t numEntries = 0;
349 A_local_->getLocalRowCopy(i, indices, values, numEntries);
350 A_local_crs_nc_->replaceLocalValues(i, numEntries,
reinterpret_cast<scalar_type*
>(values.data()), indices.data());
352 A_local_crs_nc_->fillComplete(A_local_->getDomainMap(), A_local_->getRangeMap());
355 const std::string timerName(
"Ifpack2::Details::Amesos2Wrapper::compute");
356 RCP<Time> timer = TimeMonitor::lookupCounter(timerName);
357 if (timer.is_null()) {
358 timer = TimeMonitor::getNewCounter(timerName);
361 double startTime = timer->wallTime();
364 TimeMonitor timeMon(*timer);
371 ComputeTime_ += (timer->wallTime() - startTime);
374template <
class MatrixType>
376 apply(
const Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type>& X,
377 Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type>& Y,
378 Teuchos::ETransp mode,
381 using Teuchos::ArrayView;
384 using Teuchos::rcpFromRef;
386 using Teuchos::TimeMonitor;
391 const std::string timerName(
"Ifpack2::Amesos2Wrapper::apply");
392 RCP<Time> timer = TimeMonitor::lookupCounter(timerName);
393 if (timer.is_null()) {
394 timer = TimeMonitor::getNewCounter(timerName);
397 double startTime = timer->wallTime();
400 TimeMonitor timeMon(*timer);
402 TEUCHOS_TEST_FOR_EXCEPTION(
404 "Ifpack2::Amesos2Wrapper::apply: You must call compute() to compute the "
405 "incomplete factorization, before calling apply().");
407 TEUCHOS_TEST_FOR_EXCEPTION(
408 X.getNumVectors() != Y.getNumVectors(), std::runtime_error,
409 "Ifpack2::Amesos2Wrapper::apply: X and Y must have the same number of columns. "
411 << X.getNumVectors() <<
" columns, but Y has "
412 << Y.getNumVectors() <<
" columns.");
414 TEUCHOS_TEST_FOR_EXCEPTION(
415 mode != Teuchos::NO_TRANS, std::logic_error,
416 "Ifpack2::Amesos2Wrapper::apply: Solving with the transpose (mode == "
417 "Teuchos::TRANS) or conjugate transpose (Teuchos::CONJ_TRANS) is not "
423 RCP<MV> Y_temp = (alpha != STS::one() || beta != STS::zero()) ? rcp(
new MV(Y.getMap(), Y.getNumVectors())) : rcpFromRef(Y);
428 RCP<const MV> X_temp;
431 X_temp = rcp(
new MV(X, Teuchos::Copy));
433 X_temp = rcpFromRef(X);
438 RCP<const MV> X_local;
444 const bool multipleProcs = (A_->getRowMap()->
getComm()->getSize() > 1) || (X.getMap()->
getComm()->getSize() > 1);
449 X_local = X_temp->offsetView(A_local_crs_->getDomainMap(), 0);
450 Y_local = Y_temp->offsetViewNonConst(A_local_crs_->getRangeMap(), 0);
458 solver_->solve(*Y_local, *X_local);
460 if (alpha != STS::one() || beta != STS::zero()) {
461 Y.update(alpha, *Y_temp, beta);
467 ApplyTime_ += (timer->wallTime() - startTime);
470template <
class MatrixType>
472 using Teuchos::TypeNameTraits;
473 std::ostringstream os;
478 os <<
"\"Ifpack2::Amesos2Wrapper\": {";
479 if (this->getObjectLabel() !=
"") {
480 os <<
"Label: \"" << this->getObjectLabel() <<
"\", ";
482 os <<
"Initialized: " << (
isInitialized() ?
"true" :
"false")
483 <<
", Computed: " << (
isComputed() ?
"true" :
"false");
485 if (A_local_crs_.is_null()) {
486 os <<
", Matrix: null";
488 os <<
", Global matrix dimensions: ["
489 << A_local_crs_->getGlobalNumRows() <<
", " << A_local_crs_->getGlobalNumCols() <<
"]";
494 if (!solver_.is_null()) {
495 Teuchos::Describable* d =
dynamic_cast<Teuchos::Describable*
>(solver_.getRawPtr());
498 os << d->description();
506template <
class MatrixType>
508 describe(Teuchos::FancyOStream& out,
509 const Teuchos::EVerbosityLevel verbLevel)
const {
512 using Teuchos::OSTab;
514 using Teuchos::TypeNameTraits;
516 const Teuchos::EVerbosityLevel vl = (verbLevel == Teuchos::VERB_DEFAULT) ? Teuchos::VERB_LOW : verbLevel;
520 if (vl > Teuchos::VERB_NONE) {
521 out <<
"\"Ifpack2::Amesos2Wrapper\":" << endl;
523 out <<
"MatrixType: \"" << TypeNameTraits<MatrixType>::name()
526 if (this->getObjectLabel() !=
"") {
527 out <<
"Label: \"" << this->getObjectLabel() <<
"\"" << endl;
530 out <<
"Initialized: " << (
isInitialized() ?
"true" :
"false") << endl;
531 out <<
"Computed: " << (
isComputed() ?
"true" :
"false") << endl;
533 out <<
"Number of compute calls: " <<
getNumCompute() << endl;
534 out <<
"Number of apply calls: " <<
getNumApply() << endl;
536 out <<
"Total time in seconds for compute: " <<
getComputeTime() << endl;
537 out <<
"Total time in seconds for apply: " <<
getApplyTime() << endl;
539 if (vl > Teuchos::VERB_LOW) {
540 out <<
"Matrix:" << endl;
541 A_local_crs_->describe(out, vl);
553#define IFPACK2_DETAILS_AMESOS2WRAPPER_INSTANT(S, LO, GO, N) \
554 template class Ifpack2::Details::Amesos2Wrapper<Tpetra::RowMatrix<S, LO, GO, N> >;
558#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:158
Teuchos::RCP< const map_type > getRangeMap() const
Tpetra::Map representing the range of this operator.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:123
Teuchos::RCP< const row_matrix_type > getMatrix() const
The input matrix; the matrix to be preconditioned.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:106
MatrixType::scalar_type scalar_type
The type of the entries of the input MatrixType.
Definition Ifpack2_Details_Amesos2Wrapper_decl.hpp:81
int getNumInitialize() const
The total number of successful calls to initialize().
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:138
double getApplyTime() const
The total time in seconds spent in successful calls to apply().
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:163
int getNumApply() const
The total number of successful calls to apply().
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:148
MatrixType::local_ordinal_type local_ordinal_type
The type of local indices in the input MatrixType.
Definition Ifpack2_Details_Amesos2Wrapper_decl.hpp:84
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:48
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:508
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:376
std::string description() const
A one-line description of this object.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:471
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:226
int getNumCompute() const
The total number of successful calls to compute().
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:143
void setParameters(const Teuchos::ParameterList ¶ms)
Set parameters.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:51
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:112
void compute()
Compute the numeric factorization of the matrix.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:330
Teuchos::RCP< const Teuchos::Comm< int > > getComm() const
The input matrix's communicator.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:95
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:153
virtual void setMatrix(const Teuchos::RCP< const row_matrix_type > &A)
Change the matrix to be preconditioned.
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:168
bool hasTransposeApply() const
Whether this object's apply() method can apply the transpose (or conjugate transpose,...
Definition Ifpack2_Details_Amesos2Wrapper_def.hpp:133
Preconditioners and smoothers for Tpetra sparse matrices.
Definition Ifpack2_AdditiveSchwarz_decl.hpp:40