10 #ifndef IFPACK2_MDF_DEF_HPP
11 #define IFPACK2_MDF_DEF_HPP
13 #include "Ifpack2_LocalFilter.hpp"
15 #include "Tpetra_CrsMatrix.hpp"
16 #include "Teuchos_StandardParameterEntryValidators.hpp"
17 #include "Ifpack2_LocalSparseTriangularSolver.hpp"
18 #include "Ifpack2_Details_getParamTryingTypes.hpp"
19 #include "Kokkos_Core.hpp"
20 #include "Kokkos_Sort.hpp"
21 #include "KokkosKernels_Sorting.hpp"
23 #include <type_traits>
32 template<
class dev_view_t>
33 auto copy_view(
const dev_view_t & vals)
35 using Kokkos::view_alloc;
36 using Kokkos::WithoutInitializing;
37 typename dev_view_t::non_const_type newvals (view_alloc (vals.label(), WithoutInitializing), vals.extent (0));
38 Kokkos::deep_copy(newvals,vals);
42 template<
class array_t,
class dev_view_t>
43 void copy_dev_view_to_host_array(array_t & array,
const dev_view_t & dev_view)
45 using host_view_t =
typename dev_view_t::HostMirror;
48 const auto ext = dev_view.extent(0);
51 ext !=
size_t(array.size()), std::logic_error,
"Ifpack2::MDF::copy_dev_view_to_host_array: "
52 "Size of permuations on host and device do not match. "
53 "Please report this bug to the Ifpack2 developers.");
56 Kokkos::deep_copy(host_view_t(array.get(),ext),dev_view);
59 template<
class scalar_type,
class local_ordinal_type,
class global_ordinal_type,
class node_type>
60 void applyReorderingPermutations(
61 const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
62 Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y,
66 "Ifpack2::MDF::applyReorderingPermuations ERROR: X.getNumVectors() != Y.getNumVectors().");
71 for(
size_t k=0; k < X.getNumVectors(); k++)
72 for(local_ordinal_type i=0; (size_t)i< X.getLocalLength(); i++)
73 y_ptr[k][perm[i]] = x_ptr[k][i];
77 template<
class scalar_type,
class local_ordinal_type,
class global_ordinal_type,
class node_type>
78 auto get_local_crs_row_matrix(
79 Teuchos::RCP<
const Tpetra::RowMatrix<scalar_type,local_ordinal_type,global_ordinal_type,node_type>> A_local)
84 using Teuchos::rcp_const_cast;
85 using Teuchos::rcp_dynamic_cast;
87 using crs_matrix_type = Tpetra::CrsMatrix<scalar_type, local_ordinal_type, global_ordinal_type, node_type>;
89 using nonconst_local_inds_host_view_type =
typename crs_matrix_type::nonconst_local_inds_host_view_type;
90 using nonconst_values_host_view_type =
typename crs_matrix_type::nonconst_values_host_view_type;
92 RCP<const crs_matrix_type> A_local_crs = rcp_dynamic_cast<
const crs_matrix_type>(A_local);
93 if (A_local_crs.is_null ()) {
94 local_ordinal_type numRows = A_local->getLocalNumRows();
95 Array<size_t> entriesPerRow(numRows);
96 for(local_ordinal_type i = 0; i < numRows; i++) {
97 entriesPerRow[i] = A_local->getNumEntriesInLocalRow(i);
99 RCP<crs_matrix_type> A_local_crs_nc =
100 rcp (
new crs_matrix_type (A_local->getRowMap (),
101 A_local->getColMap (),
104 nonconst_local_inds_host_view_type indices(
"indices",A_local->getLocalMaxNumRowEntries());
105 nonconst_values_host_view_type values(
"values",A_local->getLocalMaxNumRowEntries());
106 for(local_ordinal_type i = 0; i < numRows; i++) {
107 size_t numEntries = 0;
108 A_local->getLocalRowCopy(i, indices, values, numEntries);
109 A_local_crs_nc->insertLocalValues(i, numEntries, reinterpret_cast<scalar_type*>(values.data()), indices.data());
111 A_local_crs_nc->fillComplete (A_local->getDomainMap (), A_local->getRangeMap ());
112 A_local_crs = rcp_const_cast<
const crs_matrix_type> (A_local_crs_nc);
124 template<
class MatrixType>
130 isAllocated_ (false),
131 isInitialized_ (false),
136 initializeTime_ (0.0),
141 allocatePermutations();
145 template<
class MatrixType>
151 isAllocated_ (false),
152 isInitialized_ (false),
157 initializeTime_ (0.0),
162 allocatePermutations();
165 template<
class MatrixType>
171 if (force || permutations_.is_null() || A_->getLocalNumRows() != size_t(permutations_.size()))
173 permutations_ = Teuchos::null;
174 reversePermutations_ = Teuchos::null;
175 permutations_ = permutations_type(A_->getLocalNumRows());
176 reversePermutations_ = permutations_type(A_->getLocalNumRows());
180 template<
class MatrixType>
181 void MDF<MatrixType>::allocateSolvers ()
183 L_solver_ = Teuchos::null;
184 U_solver_ = Teuchos::null;
185 L_solver_ =
Teuchos::rcp (
new LocalSparseTriangularSolver<row_matrix_type> ());
186 L_solver_->setObjectLabel(
"lower");
187 U_solver_ =
Teuchos::rcp (
new LocalSparseTriangularSolver<row_matrix_type> ());
188 U_solver_->setObjectLabel(
"upper");
191 template<
class MatrixType>
200 isAllocated_ =
false;
201 isInitialized_ =
false;
203 A_local_ = Teuchos::null;
204 MDF_handle_ = Teuchos::null;
211 if (! L_solver_.is_null ()) {
212 L_solver_->setMatrix (Teuchos::null);
214 if (! U_solver_.is_null ()) {
215 U_solver_->setMatrix (Teuchos::null);
222 allocatePermutations(
true);
228 template<
class MatrixType>
233 L_.is_null (), std::runtime_error,
"Ifpack2::MDF::getL: The L factor "
234 "is null. Please call initialize() and compute() "
235 "before calling this method. If the input matrix has not yet been set, "
236 "you must first call setMatrix() with a nonnull input matrix before you "
237 "may call initialize() or compute().");
241 template<
class MatrixType>
246 permutations_.is_null (), std::runtime_error,
"Ifpack2::MDF::getPermutations: "
247 "The permulations are null. Please call initialize() and compute() "
248 "before calling this method. If the input matrix has not yet been set, "
249 "you must first call setMatrix() with a nonnull input matrix before you "
250 "may call initialize() or compute().");
253 template<
class MatrixType>
258 reversePermutations_.is_null (), std::runtime_error,
"Ifpack2::MDF::getReversePermutations: "
259 "The permulations are null. Please call initialize() and compute() "
260 "before calling this method. If the input matrix has not yet been set, "
261 "you must first call setMatrix() with a nonnull input matrix before you "
262 "may call initialize() or compute().");
266 template<
class MatrixType>
271 U_.is_null (), std::runtime_error,
"Ifpack2::MDF::getU: The U factor "
272 "is null. Please call initialize() and compute() "
273 "before calling this method. If the input matrix has not yet been set, "
274 "you must first call setMatrix() with a nonnull input matrix before you "
275 "may call initialize() or compute().");
280 template<
class MatrixType>
283 A_.
is_null (), std::runtime_error,
"Ifpack2::MDF::getNodeSmootherComplexity: "
284 "The input matrix A is null. Please call setMatrix() with a nonnull "
285 "input matrix, then call compute(), before calling this method.");
287 if(!L_.is_null() && !U_.is_null())
288 return A_->getLocalNumEntries() + L_->getLocalNumEntries() + U_->getLocalNumEntries();
294 template<
class MatrixType>
300 A_.
is_null (), std::runtime_error,
"Ifpack2::MDF::getDomainMap: "
301 "The matrix is null. Please call setMatrix() with a nonnull input "
302 "before calling this method.");
306 L_.is_null (), std::runtime_error,
"Ifpack2::MDF::getDomainMap: "
307 "The computed graph is null. Please call initialize() and compute() before calling "
309 return L_->getDomainMap ();
313 template<
class MatrixType>
319 A_.
is_null (), std::runtime_error,
"Ifpack2::MDF::getRangeMap: "
320 "The matrix is null. Please call setMatrix() with a nonnull input "
321 "before calling this method.");
325 L_.is_null (), std::runtime_error,
"Ifpack2::MDF::getRangeMap: "
326 "The computed graph is null. Please call initialize() abd compute() before calling "
328 return L_->getRangeMap ();
331 template<
class MatrixType>
339 using Details::getParamTryingTypes;
340 const char prefix[] =
"Ifpack2::MDF: ";
344 double overalloc = 2.;
356 const std::string paramName (
"fact: mdf level-of-fill");
357 getParamTryingTypes<int, int, global_ordinal_type, double, float>
358 (fillLevel, params, paramName, prefix);
361 (fillLevel != 0, std::runtime_error, prefix <<
"MDF with level of fill != 0 is not yet implemented.");
364 const std::string paramName (
"Verbosity");
365 getParamTryingTypes<int, int, global_ordinal_type, double, float>
366 (verbosity, params, paramName, prefix);
369 const std::string paramName (
"fact: mdf overalloc");
370 getParamTryingTypes<double, double>
371 (overalloc, params, paramName, prefix);
375 L_solver_->setParameters(params);
376 U_solver_->setParameters(params);
382 LevelOfFill_ = fillLevel;
383 Overalloc_ = overalloc;
384 Verbosity_ = verbosity;
388 template<
class MatrixType>
395 template<
class MatrixType>
402 template<
class MatrixType>
408 using Teuchos::rcp_dynamic_cast;
409 using Teuchos::rcp_implicit_cast;
414 if (A->getRowMap ()->getComm ()->getSize () == 1 ||
415 A->getRowMap ()->isSameAs (* (A->getColMap ()))) {
422 RCP<const LocalFilter<row_matrix_type> > A_lf_r =
423 rcp_dynamic_cast<
const LocalFilter<row_matrix_type> > (A);
424 if (! A_lf_r.is_null ()) {
425 return rcp_implicit_cast<
const row_matrix_type> (A_lf_r);
431 return rcp (
new LocalFilter<row_matrix_type> (A));
436 template<
class MatrixType>
441 using Teuchos::rcp_const_cast;
442 using Teuchos::rcp_dynamic_cast;
443 using Teuchos::rcp_implicit_cast;
446 const char prefix[] =
"Ifpack2::MDF::initialize: ";
449 (A_.
is_null (), std::runtime_error, prefix <<
"The matrix is null. Please "
450 "call setMatrix() with a nonnull input before calling this method.");
452 (! A_->isFillComplete (), std::runtime_error, prefix <<
"The matrix is not "
453 "fill complete. You may not invoke initialize() or compute() with this "
454 "matrix until the matrix is fill complete. If your matrix is a "
455 "Tpetra::CrsMatrix, please call fillComplete on it (with the domain and "
456 "range Maps, if appropriate) before calling this method.");
459 double startTime = timer.wallTime();
470 isInitialized_ =
false;
471 isAllocated_ =
false;
473 MDF_handle_ = Teuchos::null;
475 A_local_ = makeLocalFilter (A_);
477 A_local_.is_null (), std::logic_error,
"Ifpack2::MDF::initialize: "
478 "makeLocalFilter returned null; it failed to compute A_local. "
479 "Please report this bug to the Ifpack2 developers.");
487 RCP<const crs_matrix_type> A_local_crs = Details::MDFImpl::get_local_crs_row_matrix(A_local_);
489 auto A_local_device = A_local_crs->getLocalMatrixDevice();
490 MDF_handle_ =
rcp(
new MDF_handle_device_type(A_local_device) );
491 MDF_handle_->set_verbosity(Verbosity_);
493 KokkosSparse::Experimental::mdf_symbolic(A_local_device,*MDF_handle_);
498 checkOrderingConsistency (*A_local_);
501 isInitialized_ =
true;
503 initializeTime_ += (timer.wallTime() - startTime);
506 template<
class MatrixType>
516 bool gidsAreConsistentlyOrdered=
true;
517 global_ordinal_type indexOfInconsistentGID=0;
518 for (global_ordinal_type i=0; i<rowGIDs.
size(); ++i) {
519 if (rowGIDs[i] != colGIDs[i]) {
520 gidsAreConsistentlyOrdered=
false;
521 indexOfInconsistentGID=i;
526 "The ordering of the local GIDs in the row and column maps is not the same"
527 << std::endl <<
"at index " << indexOfInconsistentGID
528 <<
". Consistency is required, as all calculations are done with"
529 << std::endl <<
"local indexing.");
532 template<
class MatrixType>
537 using Teuchos::rcp_const_cast;
538 using Teuchos::rcp_dynamic_cast;
541 const char prefix[] =
"Ifpack2::MDF::compute: ";
547 (A_.
is_null (), std::runtime_error, prefix <<
"The matrix is null. Please "
548 "call setMatrix() with a nonnull input before calling this method.");
550 (! A_->isFillComplete (), std::runtime_error, prefix <<
"The matrix is not "
551 "fill complete. You may not invoke initialize() or compute() with this "
552 "matrix until the matrix is fill complete. If your matrix is a "
553 "Tpetra::CrsMatrix, please call fillComplete on it (with the domain and "
554 "range Maps, if appropriate) before calling this method.");
556 if (! isInitialized ()) {
564 double startTime = timer.
wallTime();
569 RCP<const crs_matrix_type> A_local_crs = Details::MDFImpl::get_local_crs_row_matrix(A_local_);
572 auto A_local_device = A_local_crs->getLocalMatrixDevice();
574 KokkosSparse::Experimental::mdf_numeric(A_local_device,*MDF_handle_);
578 Details::MDFImpl::copy_dev_view_to_host_array(reversePermutations_, MDF_handle_->permutation);
579 Details::MDFImpl::copy_dev_view_to_host_array(permutations_, MDF_handle_->permutation_inv);
584 auto L_mdf = MDF_handle_->getL();
586 A_local_->getRowMap (),
587 A_local_->getColMap (),
588 Details::MDFImpl::copy_view(L_mdf.graph.row_map),
589 Details::MDFImpl::copy_view(L_mdf.graph.entries),
590 Details::MDFImpl::copy_view(L_mdf.values)
594 auto U_mdf = MDF_handle_->getU();
596 A_local_->getRowMap (),
597 A_local_->getColMap (),
598 Details::MDFImpl::copy_view(U_mdf.graph.row_map),
599 Details::MDFImpl::copy_view(U_mdf.graph.entries),
600 Details::MDFImpl::copy_view(U_mdf.values)
605 L_solver_->setMatrix (L_);
606 L_solver_->initialize ();
607 L_solver_->compute ();
608 U_solver_->setMatrix (U_);
609 U_solver_->initialize ();
610 U_solver_->compute ();
614 computeTime_ += (timer.
wallTime() - startTime);
617 template<
class MatrixType>
620 apply_impl (
const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
621 Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y,
624 scalar_type beta)
const
626 const scalar_type one = STS::one ();
627 const scalar_type zero = STS::zero ();
629 if (alpha == one && beta == zero) {
630 MV tmp (Y.getMap (), Y.getNumVectors ());
631 Details::MDFImpl::applyReorderingPermutations(X,tmp,permutations_);
634 L_solver_->apply (tmp, Y, mode);
635 U_solver_->apply (Y, tmp, mode);
639 U_solver_->apply (tmp, Y, mode);
640 L_solver_->apply (Y, tmp, mode);
642 Details::MDFImpl::applyReorderingPermutations(tmp,Y,reversePermutations_);
655 MV Y_tmp (Y.getMap (), Y.getNumVectors ());
656 apply_impl (X, Y_tmp, mode);
657 Y.update (alpha, Y_tmp, beta);
662 template<
class MatrixType>
665 apply (
const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
666 Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y,
672 using Teuchos::rcpFromRef;
675 A_.
is_null (), std::runtime_error,
"Ifpack2::MDF::apply: The matrix is "
676 "null. Please call setMatrix() with a nonnull input, then initialize() "
677 "and compute(), before calling this method.");
679 ! isComputed (), std::runtime_error,
680 "Ifpack2::MDF::apply: If you have not yet called compute(), "
681 "you must call compute() before calling this method.");
682 TEUCHOS_TEST_FOR_EXCEPTION(
683 X.getNumVectors () != Y.getNumVectors (), std::invalid_argument,
684 "Ifpack2::MDF::apply: X and Y do not have the same number of columns. "
685 "X.getNumVectors() = " << X.getNumVectors ()
686 <<
" != Y.getNumVectors() = " << Y.getNumVectors () <<
".");
687 TEUCHOS_TEST_FOR_EXCEPTION(
689 "Ifpack2::MDF::apply: mode = Teuchos::CONJ_TRANS is not implemented for "
690 "complex Scalar type. Please talk to the Ifpack2 developers to get this "
691 "fixed. There is a FIXME in this file about this very issue.");
692 #ifdef HAVE_IFPACK2_DEBUG
697 for (
size_t j = 0; j < X.getNumVectors (); ++j) {
698 if (STM::isnaninf (norms[j])) {
703 TEUCHOS_TEST_FOR_EXCEPTION( ! good, std::runtime_error,
"Ifpack2::MDF::apply: The 1-norm of the input X is NaN or Inf.");
705 #endif // HAVE_IFPACK2_DEBUG
708 double startTime = timer.
wallTime();
711 apply_impl(X,Y,mode,alpha,beta);
714 #ifdef HAVE_IFPACK2_DEBUG
719 for (
size_t j = 0; j < Y.getNumVectors (); ++j) {
720 if (STM::isnaninf (norms[j])) {
725 TEUCHOS_TEST_FOR_EXCEPTION( ! good, std::runtime_error,
"Ifpack2::MDF::apply: The 1-norm of the output Y is NaN or Inf.");
727 #endif // HAVE_IFPACK2_DEBUG
730 applyTime_ += (timer.
wallTime() - startTime);
733 template<
class MatrixType>
736 std::ostringstream os;
741 os <<
"\"Ifpack2::MDF\": {";
742 os <<
"Initialized: " << (isInitialized () ?
"true" :
"false") <<
", "
743 <<
"Computed: " << (isComputed () ?
"true" :
"false") <<
", ";
745 os <<
"Level-of-fill: " << getLevelOfFill() <<
", ";
748 os <<
"Matrix: null";
751 os <<
"Global matrix dimensions: ["
752 << A_->getGlobalNumRows () <<
", " << A_->getGlobalNumCols () <<
"]"
753 <<
", Global nnz: " << A_->getGlobalNumEntries();
756 if (! L_solver_.is_null ()) os <<
", " << L_solver_->description ();
757 if (! U_solver_.is_null ()) os <<
", " << U_solver_->description ();
765 #define IFPACK2_MDF_INSTANT(S,LO,GO,N) \
766 template class Ifpack2::MDF< Tpetra::RowMatrix<S, LO, GO, N> >;
Tpetra::CrsMatrix< scalar_type, local_ordinal_type, global_ordinal_type, node_type > crs_matrix_type
Tpetra::CrsMatrix specialization used by this class for representing L and U.
Definition: Ifpack2_MDF_decl.hpp:89
Ifpack2::ScalingType enumerable type.
void compute()
Compute the (numeric) incomplete factorization.
Definition: Ifpack2_MDF_def.hpp:533
MDF (incomplete LU factorization with minimum discarded fill reordering) of a Tpetra sparse matrix...
Definition: Ifpack2_MDF_decl.hpp:50
permutations_type & getReversePermutations() const
Return the reverse permutations of the MDF factorization.
Definition: Ifpack2_MDF_def.hpp:255
MatrixType::node_type node_type
The Node type used by the input MatrixType.
Definition: Ifpack2_MDF_decl.hpp:71
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
std::string description() const
A one-line description of this object.
Definition: Ifpack2_MDF_def.hpp:734
Teuchos::RCP< const Tpetra::Map< local_ordinal_type, global_ordinal_type, node_type > > getRangeMap() const
Returns the Tpetra::Map object associated with the range of this operator.
Definition: Ifpack2_MDF_def.hpp:317
MatrixType::global_ordinal_type global_ordinal_type
The type of global indices in the input MatrixType.
Definition: Ifpack2_MDF_decl.hpp:68
Teuchos::RCP< const crs_matrix_type > getCrsMatrix() const
Return the input matrix A as a Tpetra::CrsMatrix, if possible; else throws.
Definition: Ifpack2_MDF_def.hpp:397
size_t getNodeSmootherComplexity() const
Get a rough estimate of cost per iteration.
Definition: Ifpack2_MDF_def.hpp:281
permutations_type & getPermutations() const
Return the permutations of the MDF factorization.
Definition: Ifpack2_MDF_def.hpp:243
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual void setMatrix(const Teuchos::RCP< const row_matrix_type > &A)
Change the matrix to be preconditioned.
Definition: Ifpack2_MDF_def.hpp:193
void setParameters(const Teuchos::ParameterList ¶ms)
Definition: Ifpack2_MDF_def.hpp:334
const crs_matrix_type & getL() const
Return the L factor of the MDF factorization.
Definition: Ifpack2_MDF_def.hpp:230
Teuchos::RCP< const Tpetra::Map< local_ordinal_type, global_ordinal_type, node_type > > getDomainMap() const
Returns the Tpetra::Map object associated with the domain of this operator.
Definition: Ifpack2_MDF_def.hpp:298
Tpetra::RowMatrix< scalar_type, local_ordinal_type, global_ordinal_type, node_type > row_matrix_type
Tpetra::RowMatrix specialization used by this class.
Definition: Ifpack2_MDF_decl.hpp:86
MatrixType::scalar_type scalar_type
The type of the entries of the input MatrixType.
Definition: Ifpack2_MDF_decl.hpp:62
Teuchos::RCP< const row_matrix_type > getMatrix() const
Get the input matrix.
Definition: Ifpack2_MDF_def.hpp:390
const crs_matrix_type & getU() const
Return the U factor of the MDF factorization.
Definition: Ifpack2_MDF_def.hpp:268
void initialize()
Initialize by computing the symbolic incomplete factorization.
Definition: Ifpack2_MDF_def.hpp:437
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 (inverse of the) incomplete factorization to X, resulting in Y.
Definition: Ifpack2_MDF_def.hpp:665