733 using Teuchos::rcp_const_cast;
734 using Teuchos::rcp_dynamic_cast;
735 using Teuchos::Array;
736 using Teuchos::ArrayView;
737 const char prefix[] =
"Ifpack2::RILUK::compute: ";
742 TEUCHOS_TEST_FOR_EXCEPTION
743 (A_.is_null (), std::runtime_error, prefix <<
"The matrix is null. Please "
744 "call setMatrix() with a nonnull input before calling this method.");
745 TEUCHOS_TEST_FOR_EXCEPTION
746 (! A_->isFillComplete (), std::runtime_error, prefix <<
"The matrix is not "
747 "fill complete. You may not invoke initialize() or compute() with this "
748 "matrix until the matrix is fill complete. If your matrix is a "
749 "Tpetra::CrsMatrix, please call fillComplete on it (with the domain and "
750 "range Maps, if appropriate) before calling this method.");
752 if (! isInitialized ()) {
756 Teuchos::Time timer (
"RILUK::compute");
759 Teuchos::TimeMonitor timeMon (timer);
760 double startTime = timer.wallTime();
764 if (!this->isKokkosKernelsSpiluk_) {
767 initAllValues (*A_local_);
773 const scalar_type MaxDiagonalValue = STS::one () / MinDiagonalValue;
775 size_t NumIn, NumL, NumU;
778 const size_t MaxNumEntries =
779 L_->getLocalMaxNumRowEntries () + U_->getLocalMaxNumRowEntries () + 1;
781 Teuchos::Array<local_ordinal_type> InI(MaxNumEntries);
782 Teuchos::Array<scalar_type> InV(MaxNumEntries);
783 size_t num_cols = U_->getColMap()->getLocalNumElements();
784 Teuchos::Array<int> colflag(num_cols);
786 auto DV = Kokkos::subview(D_->getLocalViewHost(Tpetra::Access::ReadWrite), Kokkos::ALL(), 0);
790 for (
size_t j = 0; j < num_cols; ++j) {
793 using IST =
typename row_matrix_type::impl_scalar_type;
794 for (
size_t i = 0; i < L_->getLocalNumRows (); ++i) {
798 local_inds_host_view_type UUI;
799 values_host_view_type UUV;
803 NumIn = MaxNumEntries;
804 nonconst_local_inds_host_view_type InI_v(InI.data(),MaxNumEntries);
805 nonconst_values_host_view_type InV_v(
reinterpret_cast<IST*
>(InV.data()),MaxNumEntries);
807 L_->getLocalRowCopy (local_row, InI_v , InV_v, NumL);
810 InI[NumL] = local_row;
812 nonconst_local_inds_host_view_type InI_sub(InI.data()+NumL+1,MaxNumEntries-NumL-1);
813 nonconst_values_host_view_type InV_sub(
reinterpret_cast<IST*
>(InV.data())+NumL+1,MaxNumEntries-NumL-1);
815 U_->getLocalRowCopy (local_row, InI_sub,InV_sub, NumU);
819 for (
size_t j = 0; j < NumIn; ++j) {
825 for (
size_t jj = 0; jj < NumL; ++jj) {
827 IST multiplier = InV[jj];
831 U_->getLocalRowView(j, UUI, UUV);
834 if (RelaxValue_ == STM::zero ()) {
835 for (
size_t k = 0; k < NumUU; ++k) {
836 const int kk = colflag[UUI[k]];
841 InV[kk] -=
static_cast<scalar_type>(multiplier * UUV[k]);
847 for (
size_t k = 0; k < NumUU; ++k) {
851 const int kk = colflag[UUI[k]];
853 InV[kk] -=
static_cast<scalar_type>(multiplier*UUV[k]);
856 diagmod -=
static_cast<scalar_type>(multiplier*UUV[k]);
864 L_->replaceLocalValues (local_row, InI (0, NumL), InV (0, NumL));
869 if (RelaxValue_ != STM::zero ()) {
870 DV(i) += RelaxValue_*diagmod;
873 if (STS::magnitude (DV(i)) > STS::magnitude (MaxDiagonalValue)) {
874 if (STS::real (DV(i)) < STM::zero ()) {
875 DV(i) = -MinDiagonalValue;
878 DV(i) = MinDiagonalValue;
885 for (
size_t j = 0; j < NumU; ++j) {
891 U_->replaceLocalValues (local_row, InI (NumL+1, NumU), InV (NumL+1, NumU));
895 for (
size_t j = 0; j < NumIn; ++j) {
896 colflag[InI[j]] = -1;
907 L_->fillComplete (L_->getColMap (), A_local_->getRangeMap ());
908 U_->fillComplete (A_local_->getDomainMap (), U_->getRowMap ());
911 L_solver_->setMatrix (L_);
912 L_solver_->compute ();
913 U_solver_->setMatrix (U_);
914 U_solver_->compute ();
918 RCP<const crs_matrix_type> A_local_crs = Details::getCrsMatrix(A_local_);
919 if(A_local_crs.is_null()) {
921 Array<size_t> entriesPerRow(numRows);
923 entriesPerRow[i] = A_local_->getNumEntriesInLocalRow(i);
925 RCP<crs_matrix_type> A_local_crs_nc =
927 A_local_->getColMap (),
930 nonconst_local_inds_host_view_type indices(
"indices",A_local_->getLocalMaxNumRowEntries());
931 nonconst_values_host_view_type values(
"values",A_local_->getLocalMaxNumRowEntries());
933 size_t numEntries = 0;
934 A_local_->getLocalRowCopy(i, indices, values, numEntries);
935 A_local_crs_nc->insertLocalValues(i, numEntries,
reinterpret_cast<scalar_type*
>(values.data()),indices.data());
937 A_local_crs_nc->fillComplete (A_local_->getDomainMap (), A_local_->getRangeMap ());
938 A_local_crs = rcp_const_cast<const crs_matrix_type> (A_local_crs_nc);
940 auto lclMtx = A_local_crs->getLocalMatrixDevice();
941 A_local_rowmap_ = lclMtx.graph.row_map;
942 A_local_entries_ = lclMtx.graph.entries;
943 A_local_values_ = lclMtx.values;
949 if (L_->isStaticGraph () || L_->isLocallyIndexed ()) {
950 L_->setAllToScalar (STS::zero ());
951 U_->setAllToScalar (STS::zero ());
954 using row_map_type =
typename crs_matrix_type::local_matrix_device_type::row_map_type;
957 auto lclL = L_->getLocalMatrixDevice();
958 row_map_type L_rowmap = lclL.graph.row_map;
959 auto L_entries = lclL.graph.entries;
960 auto L_values = lclL.values;
962 auto lclU = U_->getLocalMatrixDevice();
963 row_map_type U_rowmap = lclU.graph.row_map;
964 auto U_entries = lclU.graph.entries;
965 auto U_values = lclU.values;
967 KokkosSparse::Experimental::spiluk_numeric( KernelHandle_.getRawPtr(), LevelOfFill_,
968 A_local_rowmap_, A_local_entries_, A_local_values_,
969 L_rowmap, L_entries, L_values, U_rowmap, U_entries, U_values );
972 L_->fillComplete (L_->getColMap (), A_local_->getRangeMap ());
973 U_->fillComplete (A_local_->getDomainMap (), U_->getRowMap ());
975 L_solver_->setMatrix (L_);
976 L_solver_->compute ();
977 U_solver_->setMatrix (U_);
978 U_solver_->compute ();
983 computeTime_ += (timer.wallTime() - startTime);
990apply (
const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
991 Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y,
992 Teuchos::ETransp mode,
997 using Teuchos::rcpFromRef;
999 TEUCHOS_TEST_FOR_EXCEPTION(
1000 A_.is_null (), std::runtime_error,
"Ifpack2::RILUK::apply: The matrix is "
1001 "null. Please call setMatrix() with a nonnull input, then initialize() "
1002 "and compute(), before calling this method.");
1003 TEUCHOS_TEST_FOR_EXCEPTION(
1004 ! isComputed (), std::runtime_error,
1005 "Ifpack2::RILUK::apply: If you have not yet called compute(), "
1006 "you must call compute() before calling this method.");
1007 TEUCHOS_TEST_FOR_EXCEPTION(
1008 X.getNumVectors () != Y.getNumVectors (), std::invalid_argument,
1009 "Ifpack2::RILUK::apply: X and Y do not have the same number of columns. "
1010 "X.getNumVectors() = " << X.getNumVectors ()
1011 <<
" != Y.getNumVectors() = " << Y.getNumVectors () <<
".");
1012 TEUCHOS_TEST_FOR_EXCEPTION(
1013 STS::isComplex && mode == Teuchos::CONJ_TRANS, std::logic_error,
1014 "Ifpack2::RILUK::apply: mode = Teuchos::CONJ_TRANS is not implemented for "
1015 "complex Scalar type. Please talk to the Ifpack2 developers to get this "
1016 "fixed. There is a FIXME in this file about this very issue.");
1017#ifdef HAVE_IFPACK2_DEBUG
1020 TEUCHOS_TEST_FOR_EXCEPTION( STM::isnaninf (D_nrm1), std::runtime_error,
"Ifpack2::RILUK::apply: The 1-norm of the stored diagonal is NaN or Inf.");
1021 Teuchos::Array<magnitude_type> norms (X.getNumVectors ());
1024 for (
size_t j = 0; j < X.getNumVectors (); ++j) {
1025 if (STM::isnaninf (norms[j])) {
1030 TEUCHOS_TEST_FOR_EXCEPTION( ! good, std::runtime_error,
"Ifpack2::RILUK::apply: The 1-norm of the input X is NaN or Inf.");
1037 Teuchos::Time timer (
"RILUK::apply");
1038 double startTime = timer.wallTime();
1040 Teuchos::TimeMonitor timeMon (timer);
1041 if (alpha == one && beta == zero) {
1042 if (mode == Teuchos::NO_TRANS) {
1043#if defined(KOKKOSKERNELS_ENABLE_TPL_CUSPARSE) && defined(KOKKOS_ENABLE_CUDA) && (CUDA_VERSION >= 11030)
1047 MV Y_tmp (Y.getMap (), Y.getNumVectors ());
1050 L_solver_->apply (X, Y_tmp, mode);
1052 if (!this->isKokkosKernelsSpiluk_) {
1055 Y_tmp.elementWiseMultiply (one, *D_, Y_tmp, zero);
1058 U_solver_->apply (Y_tmp, Y, mode);
1061 L_solver_->apply (X, Y, mode);
1063 if (!this->isKokkosKernelsSpiluk_) {
1066 Y.elementWiseMultiply (one, *D_, Y, zero);
1069 U_solver_->apply (Y, Y, mode);
1073#if defined(KOKKOSKERNELS_ENABLE_TPL_CUSPARSE) && defined(KOKKOS_ENABLE_CUDA) && (CUDA_VERSION >= 11030)
1077 MV Y_tmp (Y.getMap (), Y.getNumVectors ());
1080 U_solver_->apply (X, Y_tmp, mode);
1082 if (!this->isKokkosKernelsSpiluk_) {
1088 Y_tmp.elementWiseMultiply (one, *D_, Y_tmp, zero);
1091 L_solver_->apply (Y_tmp, Y, mode);
1094 U_solver_->apply (X, Y, mode);
1096 if (!this->isKokkosKernelsSpiluk_) {
1102 Y.elementWiseMultiply (one, *D_, Y, zero);
1105 L_solver_->apply (Y, Y, mode);
1110 if (alpha == zero) {
1120 MV Y_tmp (Y.getMap (), Y.getNumVectors ());
1121 apply (X, Y_tmp, mode);
1122 Y.update (alpha, Y_tmp, beta);
1127#ifdef HAVE_IFPACK2_DEBUG
1129 Teuchos::Array<magnitude_type> norms (Y.getNumVectors ());
1132 for (
size_t j = 0; j < Y.getNumVectors (); ++j) {
1133 if (STM::isnaninf (norms[j])) {
1138 TEUCHOS_TEST_FOR_EXCEPTION( ! good, std::runtime_error,
"Ifpack2::RILUK::apply: The 1-norm of the output Y is NaN or Inf.");
1143 applyTime_ += (timer.wallTime() - startTime);