12 #ifndef __IFPACK2_FASTILU_BASE_DEF_HPP__
13 #define __IFPACK2_FASTILU_BASE_DEF_HPP__
16 #include "Tpetra_BlockCrsMatrix.hpp"
17 #include "Tpetra_BlockCrsMatrix_Helpers.hpp"
18 #include "Ifpack2_Details_getCrsMatrix.hpp"
19 #include <KokkosKernels_Utils.hpp>
20 #include <Kokkos_Timer.hpp>
29 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
42 params_(Params::getDefaults()) {}
44 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
49 return mat_->getDomainMap();
52 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
57 return mat_->getRangeMap();
60 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
62 apply (
const Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &X,
63 Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &Y,
68 const std::string timerName (
"Ifpack2::FastILU::apply");
75 if(!isInitialized() || !isComputed())
77 throw std::runtime_error(std::string(
"Called ") + getName() +
"::apply() without first calling initialize() and/or compute().");
79 if(X.getNumVectors() != Y.getNumVectors())
81 throw std::invalid_argument(getName() +
"::apply: X and Y have different numbers of vectors (pass X and Y with exactly matching dimensions)");
83 if(X.getLocalLength() != Y.getLocalLength())
85 throw std::invalid_argument(getName() +
"::apply: X and Y have different lengths (pass X and Y with exactly matching dimensions)");
89 int nvecs = X.getNumVectors();
90 auto nrowsX = X.getLocalLength();
91 auto nrowsY = Y.getLocalLength();
94 auto x2d = X.getLocalViewDevice(Tpetra::Access::ReadOnly);
95 auto y2d = Y.getLocalViewDevice(Tpetra::Access::ReadWrite);
99 applyLocalPrec(x1d, y1d);
104 auto x2d = X.getLocalViewDevice(Tpetra::Access::ReadOnly);
105 auto y2d = Y.getLocalViewDevice(Tpetra::Access::ReadWrite);
106 for(
int i = 0; i < nvecs; i++)
108 auto xColView1d = Kokkos::subview(x2d, Kokkos::ALL(), i);
109 auto yColView1d = Kokkos::subview(y2d, Kokkos::ALL(), i);
110 ImplScalarArray x1d (const_cast<ImplScalar*>(xColView1d.data()), nrowsX);
111 ImplScalarArray y1d (const_cast<ImplScalar*>(yColView1d.data()), nrowsY);
113 applyLocalPrec(x1d, y1d);
118 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
123 params_ = Params(List, getName());
126 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
130 return params_.blockCrs && params_.blockCrsSize > 1;
133 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
137 const std::string timerName (
"Ifpack2::FastILU::initialize");
146 throw std::runtime_error(std::string(
"Called ") + getName() +
"::initialize() but matrix was null (call setMatrix() with a non-null matrix first)");
150 auto crs_matrix = Ifpack2::Details::getCrsMatrix(this->mat_);
152 if (params_.fillBlocks) {
154 auto crs_matrix_block_filled = Tpetra::fillLogicalBlocks(*crs_matrix, params_.blockCrsSize);
155 auto bcrs_matrix = Tpetra::convertToBlockCrsMatrix(*crs_matrix_block_filled, params_.blockCrsSize);
160 auto bcrs_matrix = Tpetra::convertToBlockCrsMatrix(*crs_matrix, params_.blockCrsSize);
165 Kokkos::Timer copyTimer;
166 CrsArrayReader<Scalar, ImplScalar, LocalOrdinal, GlobalOrdinal, Node>::getStructure(mat_.get(), localRowPtrsHost_, localRowPtrs_, localColInds_);
167 CrsArrayReader<Scalar, ImplScalar, LocalOrdinal, GlobalOrdinal, Node>::getValues(mat_.get(), localValues_, localRowPtrsHost_);
168 crsCopyTime_ = copyTimer.seconds();
170 if (params_.use_metis)
172 assert(!params_.blockCrs);
173 const std::string timerNameMetis (
"Ifpack2::FastILU::Metis");
179 #ifdef HAVE_IFPACK2_METIS
180 idx_t nrows = localRowPtrsHost_.size() - 1;
183 metis_perm_ = MetisArrayHost(Kokkos::ViewAllocateWithoutInitializing(
"metis_perm"), nrows);
184 metis_iperm_ = MetisArrayHost(Kokkos::ViewAllocateWithoutInitializing(
"metis_iperm"), nrows);
187 auto localColIndsHost_ = Kokkos::create_mirror_view(localColInds_);
188 Kokkos::deep_copy(localColIndsHost_, localColInds_);
191 idx_t nnz = localColIndsHost_.size();
192 MetisArrayHost metis_rowptr;
193 MetisArrayHost metis_colidx;
195 bool metis_symmetrize =
true;
196 if (metis_symmetrize) {
198 using OrdinalArrayMirror =
typename OrdinalArray::host_mirror_type;
199 KokkosKernels::Impl::symmetrize_graph_symbolic_hashmap<
200 OrdinalArrayHost, OrdinalArrayMirror, MetisArrayHost, MetisArrayHost, Kokkos::HostSpace::execution_space>
201 (nrows, localRowPtrsHost_, localColIndsHost_, metis_rowptr, metis_colidx);
204 idx_t old_nnz = nnz = 0;
205 for (idx_t i = 0; i < nrows; i++) {
206 for (LocalOrdinal k = old_nnz; k < metis_rowptr(i+1); k++) {
207 if (metis_colidx(k) != i) {
208 metis_colidx(nnz) = metis_colidx(k);
212 old_nnz = metis_rowptr(i+1);
213 metis_rowptr(i+1) = nnz;
217 metis_rowptr = MetisArrayHost(Kokkos::ViewAllocateWithoutInitializing(
"metis_rowptr"), nrows+1);
218 metis_colidx = MetisArrayHost(Kokkos::ViewAllocateWithoutInitializing(
"metis_colidx"), nnz);
221 for (idx_t i = 0; i < nrows; i++) {
222 for (LocalOrdinal k = localRowPtrsHost_(i); k < localRowPtrsHost_(i+1); k++) {
223 if (localColIndsHost_(k) != i) {
224 metis_colidx(nnz) = localColIndsHost_(k);
228 metis_rowptr(i+1) = nnz;
233 int info = METIS_NodeND(&nrows, metis_rowptr.data(), metis_colidx.data(),
234 NULL, NULL, metis_perm_.data(), metis_iperm_.data());
235 if (METIS_OK != info) {
236 throw std::runtime_error(std::string(
"METIS_NodeND returned info = " + info));
240 throw std::runtime_error(std::string(
"TPL METIS is not enabled"));
249 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
256 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
262 throw std::runtime_error(getName() +
": initialize() must be called before compute()");
265 const std::string timerName (
"Ifpack2::FastILU::compute");
273 Kokkos::Timer copyTimer;
274 CrsArrayReader<Scalar, ImplScalar, LocalOrdinal, GlobalOrdinal, Node>::getValues(mat_.get(), localValues_, localRowPtrsHost_);
275 crsCopyTime_ += copyTimer.seconds();
277 computedFlag_ =
true;
281 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
285 return computedFlag_;
288 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
296 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
303 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
310 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
317 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
324 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
331 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
338 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
345 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
350 throw std::runtime_error(std::string(
"Preconditioner type Ifpack2::Details::") + getName() +
" doesn't support checkLocalILU().");
353 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
358 throw std::runtime_error(std::string(
"Preconditioner type Ifpack2::Details::") + getName() +
" doesn't support checkLocalIC().");
361 template<
typename Scalar,
typename LocalOrdinal,
typename GlobalOrdinal,
typename Node>
364 std::ostringstream os;
366 os <<
"\"Ifpack2::Details::" << getName() <<
"\": {";
367 os <<
"Initialized: " << (isInitialized() ?
"true" :
"false") <<
", ";
368 os <<
"Computed: " << (isComputed() ?
"true" :
"false") <<
", ";
369 os <<
"Sweeps: " << getSweeps() <<
", ";
370 os <<
"Triangular solve type: " << getSpTrsvType() <<
", ";
371 if (getSpTrsvType() ==
"Fast") {
372 os <<
"# of triangular solve iterations: " << getNTrisol() <<
", ";
376 os <<
"Matrix: null";
380 os <<
"Global matrix dimensions: [" << mat_->getGlobalNumRows() <<
", " << mat_->getGlobalNumCols() <<
"]";
381 os <<
", Global nnz: " << mat_->getGlobalNumEntries();
386 template<
typename Scalar,
typename LocalOrdinal,
typename GlobalOrdinal,
typename Node>
392 throw std::invalid_argument(std::string(
"Ifpack2::Details::") + getName() +
"::setMatrix() called with a null matrix. Pass a non-null matrix.");
395 if(mat_.get() != A.
get())
399 computedFlag_ =
false;
403 template<
typename Scalar,
typename LocalOrdinal,
typename GlobalOrdinal,
typename Node>
410 p.sptrsv_algo = FastILU::SpTRSV::Fast;
421 p.fillBlocks =
false;
425 template<
typename Scalar,
typename LocalOrdinal,
typename GlobalOrdinal,
typename Node>
426 FastILU_Base<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
429 *
this = getDefaults();
434 #define TYPE_ERROR(name, correctTypeName) {throw std::invalid_argument(precType + "::setParameters(): parameter \"" + name + "\" has the wrong type (must be " + correctTypeName + ")");}
435 #define CHECK_VALUE(param, member, cond, msg) {if(cond) {throw std::invalid_argument(precType + "::setParameters(): parameter \"" + param + "\" has value " + std::to_string(member) + " but " + msg);}}
440 if(pL.
isType<
bool>(
"metis"))
441 use_metis = pL.
get<
bool>(
"metis");
443 TYPE_ERROR(
"metis",
"bool");
448 if(pL.
isType<
int>(
"sweeps"))
450 nFact = pL.
get<
int>(
"sweeps");
451 CHECK_VALUE(
"sweeps", nFact, nFact < 1,
"must have a value of at least 1");
454 TYPE_ERROR(
"sweeps",
"int");
456 std::string sptrsv_type =
"Fast";
458 sptrsv_type = pL.
get<std::string> (
"triangular solve type");
460 if (sptrsv_type ==
"Standard Host") {
461 sptrsv_algo = FastILU::SpTRSV::StandardHost;
462 }
else if (sptrsv_type ==
"Standard") {
463 sptrsv_algo = FastILU::SpTRSV::Standard;
469 if(pL.
isType<
int>(
"triangular solve iterations"))
471 nTrisol = pL.
get<
int>(
"triangular solve iterations");
472 CHECK_VALUE(
"triangular solve iterations", nTrisol, nTrisol < 1,
"must have a value of at least 1");
475 TYPE_ERROR(
"triangular solve iterations",
"int");
480 if(pL.
isType<
int>(
"level"))
482 level = pL.
get<
int>(
"level");
484 else if(pL.
isType<
double>(
"level"))
488 double dval = pL.
get<
double>(
"level");
490 double fpart = modf(dval, &ipart);
492 CHECK_VALUE(
"level", level, fpart != 0,
"must be an integral value");
496 TYPE_ERROR(
"level",
"int");
498 CHECK_VALUE(
"level", level, level < 0,
"must be nonnegative");
502 if(pL.
isType<
double>(
"damping factor"))
503 omega = pL.
get<
double>(
"damping factor");
505 TYPE_ERROR(
"damping factor",
"double");
509 if(pL.
isType<
double>(
"shift"))
510 shift = pL.
get<
double>(
"shift");
512 TYPE_ERROR(
"shift",
"double");
517 if(pL.
isType<
bool>(
"guess"))
518 guessFlag = pL.
get<
bool>(
"guess");
520 TYPE_ERROR(
"guess",
"bool");
525 if(pL.
isType<
int>(
"block size for ILU"))
527 blockSizeILU = pL.
get<
int>(
"block size for ILU");
528 CHECK_VALUE(
"block size for ILU", blockSizeILU, blockSizeILU < 1,
"must have a value of at least 1");
531 TYPE_ERROR(
"block size for ILU",
"int");
536 if(pL.
isType<
int>(
"block size for SpTRSV"))
537 blockSize = pL.
get<
int>(
"block size for SpTRSV");
539 TYPE_ERROR(
"block size for SpTRSV",
"int");
544 if(pL.
isType<
bool>(
"block crs"))
545 blockCrs = pL.
get<
bool>(
"block crs");
547 TYPE_ERROR(
"block crs",
"bool");
552 if(pL.
isType<
int>(
"block crs block size"))
553 blockCrsSize = pL.
get<
int>(
"block crs block size");
555 TYPE_ERROR(
"block crs block size",
"int");
560 if(pL.
isType<
bool>(
"fill blocks for input"))
561 blockCrsSize = pL.
get<
bool>(
"fill blocks for input");
563 TYPE_ERROR(
"fill blocks for input",
"bool");
570 #define IFPACK2_DETAILS_FASTILU_BASE_INSTANT(S, L, G, N) \
571 template class Ifpack2::Details::FastILU_Base<S, L, G, N>;
int getNumCompute() const
Get the number of times compute() was called.
Definition: Ifpack2_Details_FastILU_Base_def.hpp:305
virtual void checkLocalIC() const
Verify and print debug information about the underlying IC preconditioner.
Definition: Ifpack2_Details_FastILU_Base_def.hpp:355
double getComputeTime() const
Get the time spent in the last compute() call.
Definition: Ifpack2_Details_FastILU_Base_def.hpp:326
Kokkos::View< LocalOrdinal *, execution_space >::HostMirror OrdinalArrayHost
Array of LocalOrdinal on host.
Definition: Ifpack2_Details_FastILU_Base_decl.hpp:60
T & get(const std::string &name, T def_value)
double getCopyTime() const
Get the time spent deep copying local 3-array CRS out of the matrix.
Definition: Ifpack2_Details_FastILU_Base_def.hpp:340
double getInitializeTime() const
Get the time spent in the last initialize() call.
Definition: Ifpack2_Details_FastILU_Base_def.hpp:319
Teuchos::RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > getRangeMap() const
Get the range map of the matrix.
Definition: Ifpack2_Details_FastILU_Base_def.hpp:55
double getApplyTime() const
Get the time spent in the last apply() call.
Definition: Ifpack2_Details_FastILU_Base_def.hpp:333
Teuchos::RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > getDomainMap() const
Get the domain map of the matrix.
Definition: Ifpack2_Details_FastILU_Base_def.hpp:47
bool isParameter(const std::string &name) const
Teuchos::RCP< const TRowMatrix > getMatrix() const
Get the current matrix.
Definition: Ifpack2_Details_FastILU_Base_def.hpp:291
virtual void checkLocalILU() const
Verify and print debug information about the underlying ILU preconditioner (only supported if this is...
Definition: Ifpack2_Details_FastILU_Base_def.hpp:347
int getNumApply() const
Get the number of times apply() was called.
Definition: Ifpack2_Details_FastILU_Base_def.hpp:312
The base class of the Ifpack2 FastILU wrappers (Filu, Fildl and Fic)
Definition: Ifpack2_Details_FastILU_Base_decl.hpp:38
void setParameters(const Teuchos::ParameterList &List)
Validate parameters, and set defaults when parameters are not provided.
Definition: Ifpack2_Details_FastILU_Base_def.hpp:120
std::string description() const
Return a brief description of the preconditioner, in YAML format.
Definition: Ifpack2_Details_FastILU_Base_def.hpp:362
void compute()
Compute the preconditioner.
Definition: Ifpack2_Details_FastILU_Base_def.hpp:258
Kokkos::View< ImplScalar *, execution_space > ImplScalarArray
Array of Scalar on device.
Definition: Ifpack2_Details_FastILU_Base_decl.hpp:62
FastILU_Base(Teuchos::RCP< const TRowMatrix > mat_)
Constructor.
Definition: Ifpack2_Details_FastILU_Base_def.hpp:31
bool isComputed() const
Whether compute() has been called since the last time the matrix's values or structure were changed...
Definition: Ifpack2_Details_FastILU_Base_def.hpp:283
bool isInitialized() const
Whether initialize() has been called since the last time the matrix's structure was changed...
Definition: Ifpack2_Details_FastILU_Base_def.hpp:251
void initialize()
Initialize the preconditioner.
Definition: Ifpack2_Details_FastILU_Base_def.hpp:135
bool isType(const std::string &name) const
void setMatrix(const Teuchos::RCP< const TRowMatrix > &A)
Definition: Ifpack2_Details_FastILU_Base_def.hpp:388
void apply(const TMultiVec &X, TMultiVec &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, Scalar alpha=Teuchos::ScalarTraits< Scalar >::one(), Scalar beta=Teuchos::ScalarTraits< Scalar >::zero()) const
Apply the preconditioner.
Definition: Ifpack2_Details_FastILU_Base_def.hpp:62
int getNumInitialize() const
Get the number of times initialize() was called.
Definition: Ifpack2_Details_FastILU_Base_def.hpp:298
Provides functions for retrieving local CRS arrays (row pointers, column indices, and values) from Tp...