129 using Ifpack2::Details::getParamTryingTypes;
130 const char prefix[] =
"Ifpack2::ILUT: ";
137 IlutImplType::Enum ilutimplType = IlutImplType::Serial;
139 static const char typeName[] =
"fact: type";
141 if (!params.isType<std::string>(typeName))
break;
144 Teuchos::Array<std::string> ilutimplTypeStrs;
145 Teuchos::Array<IlutImplType::Enum> ilutimplTypeEnums;
146 IlutImplType::loadPLTypeOption(ilutimplTypeStrs, ilutimplTypeEnums);
147 Teuchos::StringToIntegralParameterEntryValidator<IlutImplType::Enum>
148 s2i(ilutimplTypeStrs(), ilutimplTypeEnums(), typeName,
false);
150 ilutimplType = s2i.getIntegralValue(params.get<std::string>(typeName));
153 if (ilutimplType == IlutImplType::PAR_ILUT) {
154 this->useKokkosKernelsParILUT_ =
true;
156 this->useKokkosKernelsParILUT_ =
false;
162 double fillLevel = LevelOfFill_;
164 const std::string paramName(
"fact: ilut level-of-fill");
165 TEUCHOS_TEST_FOR_EXCEPTION(
166 (params.isParameter(paramName) && this->useKokkosKernelsParILUT_), std::runtime_error,
167 "Ifpack2::ILUT: Parameter " << paramName <<
" is meaningless for algorithm par_ilut.");
168 getParamTryingTypes<double, double, float>(fillLevel, params, paramName, prefix);
169 TEUCHOS_TEST_FOR_EXCEPTION(fillLevel < 1.0, std::runtime_error,
170 "Ifpack2::ILUT: The \"" << paramName <<
"\" parameter must be >= "
171 "1.0, but you set it to "
172 << fillLevel <<
". For ILUT, the fill level "
173 "means something different than it does for ILU(k). ILU(0) produces "
174 "factors with the same sparsity structure as the input matrix A. For "
175 "ILUT, level-of-fill = 1.0 will produce factors with nonzeros matching "
176 "the sparsity structure of A. level-of-fill > 1.0 allows for additional "
182 const std::string paramName(
"fact: absolute threshold");
183 getParamTryingTypes<magnitude_type, magnitude_type, double>(absThresh, params, paramName, prefix);
188 const std::string paramName(
"fact: relative threshold");
189 getParamTryingTypes<magnitude_type, magnitude_type, double>(relThresh, params, paramName, prefix);
194 const std::string paramName(
"fact: relax value");
195 getParamTryingTypes<magnitude_type, magnitude_type, double>(relaxValue, params, paramName, prefix);
200 const std::string paramName(
"fact: drop tolerance");
201 getParamTryingTypes<magnitude_type, magnitude_type, double>(dropTol, params, paramName, prefix);
204 int par_ilut_max_iter = 20;
206 int par_ilut_team_size = 0;
207 int par_ilut_vector_size = 0;
208 float par_ilut_fill_in_limit = 0.75;
209 bool par_ilut_verbose =
false;
210 if (this->useKokkosKernelsParILUT_) {
211 par_ilut_max_iter = par_ilut_options_.max_iter;
212 par_ilut_residual_norm_delta_stop = par_ilut_options_.residual_norm_delta_stop;
213 par_ilut_team_size = par_ilut_options_.team_size;
214 par_ilut_vector_size = par_ilut_options_.vector_size;
215 par_ilut_fill_in_limit = par_ilut_options_.fill_in_limit;
216 par_ilut_verbose = par_ilut_options_.verbose;
218 std::string par_ilut_plist_name(
"parallel ILUT options");
219 if (params.isSublist(par_ilut_plist_name)) {
220 Teuchos::ParameterList
const& par_ilut_plist = params.sublist(par_ilut_plist_name);
222 std::string paramName(
"maximum iterations");
223 getParamTryingTypes<int, int>(par_ilut_max_iter, par_ilut_plist, paramName, prefix);
225 paramName =
"residual norm delta stop";
226 getParamTryingTypes<magnitude_type, magnitude_type, double>(par_ilut_residual_norm_delta_stop, par_ilut_plist, paramName, prefix);
228 paramName =
"team size";
229 getParamTryingTypes<int, int>(par_ilut_team_size, par_ilut_plist, paramName, prefix);
231 paramName =
"vector size";
232 getParamTryingTypes<int, int>(par_ilut_vector_size, par_ilut_plist, paramName, prefix);
234 paramName =
"fill in limit";
235 getParamTryingTypes<float, float, double>(par_ilut_fill_in_limit, par_ilut_plist, paramName, prefix);
237 paramName =
"verbose";
238 getParamTryingTypes<bool, bool>(par_ilut_verbose, par_ilut_plist, paramName, prefix);
242 par_ilut_options_.max_iter = par_ilut_max_iter;
243 par_ilut_options_.residual_norm_delta_stop = par_ilut_residual_norm_delta_stop;
244 par_ilut_options_.team_size = par_ilut_team_size;
245 par_ilut_options_.vector_size = par_ilut_vector_size;
246 par_ilut_options_.fill_in_limit = par_ilut_fill_in_limit;
247 par_ilut_options_.verbose = par_ilut_verbose;
252 L_solver_->setParameters(params);
253 U_solver_->setParameters(params);
255 LevelOfFill_ = fillLevel;
256 Athresh_ = absThresh;
257 Rthresh_ = relThresh;
258 RelaxValue_ = relaxValue;
259 DropTolerance_ = dropTol;
430 using Teuchos::Array;
432 using Teuchos::rcp_const_cast;
433 Teuchos::Time timer(
"ILUT::initialize");
434 double startTime = timer.wallTime();
436 Teuchos::TimeMonitor timeMon(timer);
439 TEUCHOS_TEST_FOR_EXCEPTION(
440 A_.is_null(), std::runtime_error,
441 "Ifpack2::ILUT::initialize: "
442 "The matrix to precondition is null. Please call setMatrix() with a "
443 "nonnull input before calling this method.");
446 IsInitialized_ =
false;
448 A_local_ = Teuchos::null;
452 A_local_ = makeLocalFilter(A_);
453 TEUCHOS_TEST_FOR_EXCEPTION(
454 A_local_.is_null(), std::logic_error,
455 "Ifpack2::RILUT::initialize: "
456 "makeLocalFilter returned null; it failed to compute A_local. "
457 "Please report this bug to the Ifpack2 developers.");
459 if (this->useKokkosKernelsParILUT_) {
460 this->KernelHandle_ = Teuchos::rcp(
new kk_handle_type());
461 KernelHandle_->create_par_ilut_handle();
462 auto par_ilut_handle = KernelHandle_->get_par_ilut_handle();
463 par_ilut_handle->set_residual_norm_delta_stop(par_ilut_options_.residual_norm_delta_stop);
464 par_ilut_handle->set_team_size(par_ilut_options_.team_size);
465 par_ilut_handle->set_vector_size(par_ilut_options_.vector_size);
466 par_ilut_handle->set_fill_in_limit(par_ilut_options_.fill_in_limit);
467 par_ilut_handle->set_verbose(par_ilut_options_.verbose);
468 par_ilut_handle->set_async_update(
false);
470 RCP<const crs_matrix_type> A_local_crs = Teuchos::rcp_dynamic_cast<const crs_matrix_type>(A_local_);
471 if (A_local_crs.is_null()) {
474 Array<size_t> entriesPerRow(numRows);
476 entriesPerRow[i] = A_local_->getNumEntriesInLocalRow(i);
478 RCP<crs_matrix_type> A_local_crs_nc =
480 A_local_->getColMap(),
483 nonconst_local_inds_host_view_type indices(
"indices", A_local_->getLocalMaxNumRowEntries());
484 nonconst_values_host_view_type values(
"values", A_local_->getLocalMaxNumRowEntries());
486 size_t numEntries = 0;
487 A_local_->getLocalRowCopy(i, indices, values, numEntries);
488 A_local_crs_nc->insertLocalValues(i, numEntries,
reinterpret_cast<scalar_type*
>(values.data()), indices.data());
490 A_local_crs_nc->fillComplete(A_local_->getDomainMap(), A_local_->getRangeMap());
491 A_local_crs = rcp_const_cast<const crs_matrix_type>(A_local_crs_nc);
493 auto A_local_crs_device = A_local_crs->getLocalMatrixDevice();
496 typedef typename Kokkos::View<usize_type*, array_layout, device_type> ulno_row_view_t;
497 const int NumMyRows = A_local_crs->getRowMap()->getLocalNumElements();
498 L_rowmap_ = ulno_row_view_t(
"L_row_map", NumMyRows + 1);
499 U_rowmap_ = ulno_row_view_t(
"U_row_map", NumMyRows + 1);
500 L_rowmap_orig_ = ulno_row_view_t(
"L_row_map_orig", NumMyRows + 1);
501 U_rowmap_orig_ = ulno_row_view_t(
"U_row_map_orig", NumMyRows + 1);
503 KokkosSparse::Experimental::par_ilut_symbolic(KernelHandle_.getRawPtr(),
504 A_local_crs_device.graph.row_map, A_local_crs_device.graph.entries,
508 Kokkos::deep_copy(L_rowmap_orig_, L_rowmap_);
509 Kokkos::deep_copy(U_rowmap_orig_, U_rowmap_);
512 IsInitialized_ =
true;
515 InitializeTime_ += (timer.wallTime() - startTime);
526 using Teuchos::Array;
527 using Teuchos::ArrayRCP;
528 using Teuchos::ArrayView;
532 using Teuchos::rcp_const_cast;
533 using Teuchos::reduceAll;
536 if (!isInitialized()) {
540 Teuchos::Time timer(
"ILUT::compute");
541 double startTime = timer.wallTime();
543 Teuchos::TimeMonitor timeMon(timer,
true);
545 if (!this->useKokkosKernelsParILUT_) {
577#ifdef IFPACK2_WRITE_ILUT_FACTORS
578 std::ofstream ofsL(
"L.ifpack2_ilut.mtx", std::ios::out);
579 std::ofstream ofsU(
"U.ifpack2_ilut.mtx", std::ios::out);
584 double local_nnz =
static_cast<double>(A_local_->getLocalNumEntries());
585 double fill = ((getLevelOfFill() - 1.0) * local_nnz) / (2 * myNumRows);
590 double fill_ceil = std::ceil(fill);
594 size_type fillL =
static_cast<size_type
>(fill_ceil);
595 size_type fillU =
static_cast<size_type
>(fill_ceil);
597 Array<scalar_type> InvDiagU(myNumRows, zero);
599 Array<Array<local_ordinal_type> > L_tmp_idx(myNumRows);
600 Array<Array<scalar_type> > L_tmpv(myNumRows);
601 Array<Array<local_ordinal_type> > U_tmp_idx(myNumRows);
602 Array<Array<scalar_type> > U_tmpv(myNumRows);
609 Array<int> pattern(max_col, UNUSED);
610 Array<scalar_type> cur_row(max_col, zero);
611 Array<magnitude_type> unorm(max_col);
613 Array<local_ordinal_type> L_cols_heap;
614 Array<local_ordinal_type> U_cols;
615 Array<local_ordinal_type> L_vals_heap;
616 Array<local_ordinal_type> U_vals_heap;
621 greater_indirect<scalar_type, local_ordinal_type> vals_comp(cur_row);
626 nonconst_local_inds_host_view_type ColIndicesARCP;
627 nonconst_values_host_view_type ColValuesARCP;
628 if (!A_local_->supportsRowViews()) {
629 const size_t maxnz = A_local_->getLocalMaxNumRowEntries();
630 Kokkos::resize(ColIndicesARCP, maxnz);
631 Kokkos::resize(ColValuesARCP, maxnz);
635 local_inds_host_view_type ColIndicesA;
636 values_host_view_type ColValuesA;
639 if (A_local_->supportsRowViews()) {
640 A_local_->getLocalRowView(row_i, ColIndicesA, ColValuesA);
641 RowNnz = ColIndicesA.size();
643 A_local_->getLocalRowCopy(row_i, ColIndicesARCP, ColValuesARCP, RowNnz);
644 ColIndicesA = Kokkos::subview(ColIndicesARCP, std::make_pair((
size_t)0, RowNnz));
645 ColValuesA = Kokkos::subview(ColValuesARCP, std::make_pair((
size_t)0, RowNnz));
650 U_cols.push_back(row_i);
651 cur_row[row_i] = zero;
652 pattern[row_i] = ORIG;
654 size_type L_cols_heaplen = 0;
655 rownorm = STM::zero();
656 for (
size_t i = 0; i < RowNnz; ++i) {
657 if (ColIndicesA[i] < myNumRows) {
658 if (ColIndicesA[i] < row_i) {
659 add_to_heap(ColIndicesA[i], L_cols_heap, L_cols_heaplen);
660 }
else if (ColIndicesA[i] > row_i) {
661 U_cols.push_back(ColIndicesA[i]);
664 cur_row[ColIndicesA[i]] = ColValuesA[i];
665 pattern[ColIndicesA[i]] = ORIG;
666 rownorm += scalar_mag(ColValuesA[i]);
675 cur_row[row_i] = as<scalar_type>(getAbsoluteThreshold() * IFPACK2_SGN(v)) + rthresh * v;
677 size_type orig_U_len = U_cols.size();
678 RowNnz = L_cols_heap.size() + orig_U_len;
679 rownorm = getDropTolerance() * rownorm / RowNnz;
682 size_type L_vals_heaplen = 0;
683 while (L_cols_heaplen > 0) {
686 scalar_type multiplier = cur_row[row_k] * InvDiagU[row_k];
687 cur_row[row_k] = multiplier;
689 if (mag_mult * unorm[row_k] < rownorm) {
690 pattern[row_k] = UNUSED;
694 if (pattern[row_k] != ORIG) {
695 if (L_vals_heaplen < fillL) {
696 add_to_heap(row_k, L_vals_heap, L_vals_heaplen, vals_comp);
697 }
else if (L_vals_heaplen == 0 ||
698 mag_mult < scalar_mag(cur_row[L_vals_heap.front()])) {
699 pattern[row_k] = UNUSED;
703 pattern[L_vals_heap.front()] = UNUSED;
705 add_to_heap(row_k, L_vals_heap, L_vals_heaplen, vals_comp);
711 ArrayView<local_ordinal_type> ColIndicesU = U_tmp_idx[row_k]();
712 ArrayView<scalar_type> ColValuesU = U_tmpv[row_k]();
713 size_type ColNnzU = ColIndicesU.size();
715 for (size_type j = 0; j < ColNnzU; ++j) {
716 if (ColIndicesU[j] > row_k) {
719 if (pattern[col_j] != UNUSED) {
720 cur_row[col_j] -= tmp;
721 }
else if (scalar_mag(tmp) > rownorm) {
722 cur_row[col_j] = -tmp;
723 pattern[col_j] = FILL;
725 U_cols.push_back(col_j);
739 for (size_type i = 0; i < (size_type)ColIndicesA.size(); ++i) {
740 if (ColIndicesA[i] < row_i) {
741 L_tmp_idx[row_i].push_back(ColIndicesA[i]);
742 L_tmpv[row_i].push_back(cur_row[ColIndicesA[i]]);
743 pattern[ColIndicesA[i]] = UNUSED;
748 for (size_type j = 0; j < L_vals_heaplen; ++j) {
749 L_tmp_idx[row_i].push_back(L_vals_heap[j]);
750 L_tmpv[row_i].push_back(cur_row[L_vals_heap[j]]);
751 pattern[L_vals_heap[j]] = UNUSED;
759#ifdef IFPACK2_WRITE_ILUT_FACTORS
760 for (size_type ii = 0; ii < L_tmp_idx[row_i].size(); ++ii) {
761 ofsL << row_i <<
" " << L_tmp_idx[row_i][ii] <<
" "
762 << L_tmpv[row_i][ii] << std::endl;
767 if (cur_row[row_i] == zero) {
768 std::cerr <<
"Ifpack2::ILUT::Compute: zero pivot encountered! "
769 <<
"Replacing with rownorm and continuing..."
770 <<
"(You may need to set the parameter "
771 <<
"'fact: absolute threshold'.)" << std::endl;
772 cur_row[row_i] = rownorm;
774 InvDiagU[row_i] = one / cur_row[row_i];
777 U_tmp_idx[row_i].push_back(row_i);
778 U_tmpv[row_i].push_back(cur_row[row_i]);
779 unorm[row_i] = scalar_mag(cur_row[row_i]);
780 pattern[row_i] = UNUSED;
786 size_type U_vals_heaplen = 0;
787 for (size_type j = 1; j < U_cols.size(); ++j) {
789 if (pattern[col] != ORIG) {
790 if (U_vals_heaplen < fillU) {
791 add_to_heap(col, U_vals_heap, U_vals_heaplen, vals_comp);
792 }
else if (U_vals_heaplen != 0 && scalar_mag(cur_row[col]) >
793 scalar_mag(cur_row[U_vals_heap.front()])) {
795 add_to_heap(col, U_vals_heap, U_vals_heaplen, vals_comp);
798 U_tmp_idx[row_i].push_back(col);
799 U_tmpv[row_i].push_back(cur_row[col]);
800 unorm[row_i] += scalar_mag(cur_row[col]);
802 pattern[col] = UNUSED;
805 for (size_type j = 0; j < U_vals_heaplen; ++j) {
806 U_tmp_idx[row_i].push_back(U_vals_heap[j]);
807 U_tmpv[row_i].push_back(cur_row[U_vals_heap[j]]);
808 unorm[row_i] += scalar_mag(cur_row[U_vals_heap[j]]);
811 unorm[row_i] /= (orig_U_len + U_vals_heaplen);
813#ifdef IFPACK2_WRITE_ILUT_FACTORS
814 for (
int ii = 0; ii < U_tmp_idx[row_i].size(); ++ii) {
815 ofsU << row_i <<
" " << U_tmp_idx[row_i][ii] <<
" "
816 << U_tmpv[row_i][ii] << std::endl;
827 Array<size_t> nnzPerRow(myNumRows);
833 L_solver_->setMatrix(Teuchos::null);
834 U_solver_->setMatrix(Teuchos::null);
837 nnzPerRow[row_i] = L_tmp_idx[row_i].size();
840 L_ = rcp(
new crs_matrix_type(A_local_->getRowMap(), A_local_->getColMap(),
844 L_->insertLocalValues(row_i, L_tmp_idx[row_i](), L_tmpv[row_i]());
850 nnzPerRow[row_i] = U_tmp_idx[row_i].size();
853 U_ = rcp(
new crs_matrix_type(A_local_->getRowMap(), A_local_->getColMap(),
857 U_->insertLocalValues(row_i, U_tmp_idx[row_i](), U_tmpv[row_i]());
862 L_solver_->setMatrix(L_);
863 L_solver_->initialize();
864 L_solver_->compute();
866 U_solver_->setMatrix(U_);
867 U_solver_->initialize();
868 U_solver_->compute();
874 if (this->isComputed()) {
875 Kokkos::resize(L_rowmap_, L_rowmap_orig_.size());
876 Kokkos::resize(U_rowmap_, U_rowmap_orig_.size());
877 Kokkos::deep_copy(L_rowmap_, L_rowmap_orig_);
878 Kokkos::deep_copy(U_rowmap_, U_rowmap_orig_);
881 RCP<const crs_matrix_type> A_local_crs = Teuchos::rcp_dynamic_cast<const crs_matrix_type>(A_local_);
883 if (A_local_crs.is_null()) {
885 Array<size_t> entriesPerRow(numRows);
887 entriesPerRow[i] = A_local_->getNumEntriesInLocalRow(i);
889 RCP<crs_matrix_type> A_local_crs_nc =
891 A_local_->getColMap(),
894 nonconst_local_inds_host_view_type indices(
"indices", A_local_->getLocalMaxNumRowEntries());
895 nonconst_values_host_view_type values(
"values", A_local_->getLocalMaxNumRowEntries());
897 size_t numEntries = 0;
898 A_local_->getLocalRowCopy(i, indices, values, numEntries);
899 A_local_crs_nc->insertLocalValues(i, numEntries,
reinterpret_cast<scalar_type*
>(values.data()), indices.data());
901 A_local_crs_nc->fillComplete(A_local_->getDomainMap(), A_local_->getRangeMap());
902 A_local_crs = rcp_const_cast<const crs_matrix_type>(A_local_crs_nc);
904 auto lclMtx = A_local_crs->getLocalMatrixDevice();
905 A_local_rowmap_ = lclMtx.graph.row_map;
906 A_local_entries_ = lclMtx.graph.entries;
907 A_local_values_ = lclMtx.values;
911 auto par_ilut_handle = KernelHandle_->get_par_ilut_handle();
912 auto nnzL = par_ilut_handle->get_nnzL();
913 static_graph_entries_t L_entries_ = static_graph_entries_t(
"L_entries", nnzL);
914 local_matrix_values_t L_values_ = local_matrix_values_t(
"L_values", nnzL);
916 auto nnzU = par_ilut_handle->get_nnzU();
917 static_graph_entries_t U_entries_ = static_graph_entries_t(
"U_entries", nnzU);
918 local_matrix_values_t U_values_ = local_matrix_values_t(
"U_values", nnzU);
920 KokkosSparse::Experimental::par_ilut_numeric(KernelHandle_.getRawPtr(),
921 A_local_rowmap_, A_local_entries_, A_local_values_,
922 L_rowmap_, L_entries_, L_values_, U_rowmap_, U_entries_, U_values_);
924 auto L_kokkosCrsGraph = local_graph_device_type(L_entries_, L_rowmap_);
925 auto U_kokkosCrsGraph = local_graph_device_type(U_entries_, U_rowmap_);
927 local_matrix_device_type L_localCrsMatrix_device;
928 L_localCrsMatrix_device = local_matrix_device_type(
"L_Factor_localmatrix",
929 A_local_->getLocalNumRows(),
934 A_local_crs->getRowMap(),
935 A_local_crs->getColMap(),
936 A_local_crs->getDomainMap(),
937 A_local_crs->getRangeMap(),
938 A_local_crs->getGraph()->getImporter(),
939 A_local_crs->getGraph()->getExporter()));
941 local_matrix_device_type U_localCrsMatrix_device;
942 U_localCrsMatrix_device = local_matrix_device_type(
"U_Factor_localmatrix",
943 A_local_->getLocalNumRows(),
948 A_local_crs->getRowMap(),
949 A_local_crs->getColMap(),
950 A_local_crs->getDomainMap(),
951 A_local_crs->getRangeMap(),
952 A_local_crs->getGraph()->getImporter(),
953 A_local_crs->getGraph()->getExporter()));
955 L_solver_->setMatrix(L_);
956 L_solver_->compute();
957 U_solver_->setMatrix(U_);
958 U_solver_->compute();
962 ComputeTime_ += (timer.wallTime() - startTime);