197 using Ifpack2::Details::getParamTryingTypes;
198 const char prefix[] =
"Ifpack2::ILUT: ";
205 IlutImplType::Enum ilutimplType = IlutImplType::Serial;
207 static const char typeName[] =
"fact: type";
209 if (!params.isType<std::string>(typeName))
break;
212 Teuchos::Array<std::string> ilutimplTypeStrs;
213 Teuchos::Array<IlutImplType::Enum> ilutimplTypeEnums;
214 IlutImplType::loadPLTypeOption(ilutimplTypeStrs, ilutimplTypeEnums);
215 Teuchos::StringToIntegralParameterEntryValidator<IlutImplType::Enum>
216 s2i(ilutimplTypeStrs(), ilutimplTypeEnums(), typeName,
false);
218 ilutimplType = s2i.getIntegralValue(params.get<std::string>(typeName));
221 if (ilutimplType == IlutImplType::PAR_ILUT) {
222 this->useKokkosKernelsParILUT_ =
true;
224 this->useKokkosKernelsParILUT_ =
false;
230 double fillLevel = LevelOfFill_;
232 const std::string paramName(
"fact: ilut level-of-fill");
233 TEUCHOS_TEST_FOR_EXCEPTION(
234 (params.isParameter(paramName) && this->useKokkosKernelsParILUT_), std::runtime_error,
235 "Ifpack2::ILUT: Parameter " << paramName <<
" is meaningless for algorithm par_ilut.");
236 getParamTryingTypes<double, double, float>(fillLevel, params, paramName, prefix);
237 TEUCHOS_TEST_FOR_EXCEPTION(fillLevel < 1.0, std::runtime_error,
238 "Ifpack2::ILUT: The \"" << paramName <<
"\" parameter must be >= "
239 "1.0, but you set it to "
240 << fillLevel <<
". For ILUT, the fill level "
241 "means something different than it does for ILU(k). ILU(0) produces "
242 "factors with the same sparsity structure as the input matrix A. For "
243 "ILUT, level-of-fill = 1.0 will produce factors with nonzeros matching "
244 "the sparsity structure of A. level-of-fill > 1.0 allows for additional "
250 const std::string paramName(
"fact: absolute threshold");
251 getParamTryingTypes<magnitude_type, magnitude_type, double>(absThresh, params, paramName, prefix);
256 const std::string paramName(
"fact: relative threshold");
257 getParamTryingTypes<magnitude_type, magnitude_type, double>(relThresh, params, paramName, prefix);
262 const std::string paramName(
"fact: relax value");
263 getParamTryingTypes<magnitude_type, magnitude_type, double>(relaxValue, params, paramName, prefix);
268 const std::string paramName(
"fact: drop tolerance");
269 getParamTryingTypes<magnitude_type, magnitude_type, double>(dropTol, params, paramName, prefix);
272 int par_ilut_max_iter = 20;
274 int par_ilut_team_size = 0;
275 int par_ilut_vector_size = 0;
276 float par_ilut_fill_in_limit = 0.75;
277 bool par_ilut_verbose =
false;
278 if (this->useKokkosKernelsParILUT_) {
279 par_ilut_max_iter = par_ilut_options_.max_iter;
280 par_ilut_residual_norm_delta_stop = par_ilut_options_.residual_norm_delta_stop;
281 par_ilut_team_size = par_ilut_options_.team_size;
282 par_ilut_vector_size = par_ilut_options_.vector_size;
283 par_ilut_fill_in_limit = par_ilut_options_.fill_in_limit;
284 par_ilut_verbose = par_ilut_options_.verbose;
286 std::string par_ilut_plist_name(
"parallel ILUT options");
287 if (params.isSublist(par_ilut_plist_name)) {
288 Teuchos::ParameterList
const& par_ilut_plist = params.sublist(par_ilut_plist_name);
290 std::string paramName(
"maximum iterations");
291 getParamTryingTypes<int, int>(par_ilut_max_iter, par_ilut_plist, paramName, prefix);
293 paramName =
"residual norm delta stop";
294 getParamTryingTypes<magnitude_type, magnitude_type, double>(par_ilut_residual_norm_delta_stop, par_ilut_plist, paramName, prefix);
296 paramName =
"team size";
297 getParamTryingTypes<int, int>(par_ilut_team_size, par_ilut_plist, paramName, prefix);
299 paramName =
"vector size";
300 getParamTryingTypes<int, int>(par_ilut_vector_size, par_ilut_plist, paramName, prefix);
302 paramName =
"fill in limit";
303 getParamTryingTypes<float, float, double>(par_ilut_fill_in_limit, par_ilut_plist, paramName, prefix);
305 paramName =
"verbose";
306 getParamTryingTypes<bool, bool>(par_ilut_verbose, par_ilut_plist, paramName, prefix);
310 par_ilut_options_.max_iter = par_ilut_max_iter;
311 par_ilut_options_.residual_norm_delta_stop = par_ilut_residual_norm_delta_stop;
312 par_ilut_options_.team_size = par_ilut_team_size;
313 par_ilut_options_.vector_size = par_ilut_vector_size;
314 par_ilut_options_.fill_in_limit = par_ilut_fill_in_limit;
315 par_ilut_options_.verbose = par_ilut_verbose;
320 L_solver_->setParameters(params);
321 U_solver_->setParameters(params);
323 LevelOfFill_ = fillLevel;
324 Athresh_ = absThresh;
325 Rthresh_ = relThresh;
326 RelaxValue_ = relaxValue;
327 DropTolerance_ = dropTol;
500 using Teuchos::Array;
502 using Teuchos::rcp_const_cast;
503 Teuchos::Time timer(
"ILUT::initialize");
504 double startTime = timer.wallTime();
506 Teuchos::TimeMonitor timeMon(timer);
509 TEUCHOS_TEST_FOR_EXCEPTION(
510 A_.is_null(), std::runtime_error,
511 "Ifpack2::ILUT::initialize: "
512 "The matrix to precondition is null. Please call setMatrix() with a "
513 "nonnull input before calling this method.");
516 IsInitialized_ =
false;
518 A_local_ = Teuchos::null;
519 A_local_crs_ = Teuchos::null;
520 A_local_crs_nc_ = Teuchos::null;
524 A_local_ = makeLocalFilter(A_);
525 TEUCHOS_TEST_FOR_EXCEPTION(
526 A_local_.is_null(), std::logic_error,
527 "Ifpack2::RILUT::initialize: "
528 "makeLocalFilter returned null; it failed to compute A_local. "
529 "Please report this bug to the Ifpack2 developers.");
531 if (this->useKokkosKernelsParILUT_) {
532 this->KernelHandle_ = Teuchos::rcp(
new kk_handle_type());
533 KernelHandle_->create_par_ilut_handle();
534 auto par_ilut_handle = KernelHandle_->get_par_ilut_handle();
535 par_ilut_handle->set_residual_norm_delta_stop(par_ilut_options_.residual_norm_delta_stop);
536 par_ilut_handle->set_team_size(par_ilut_options_.team_size);
537 par_ilut_handle->set_vector_size(par_ilut_options_.vector_size);
538 par_ilut_handle->set_max_iter(par_ilut_options_.max_iter);
539 par_ilut_handle->set_fill_in_limit(par_ilut_options_.fill_in_limit);
540 par_ilut_handle->set_verbose(par_ilut_options_.verbose);
541 par_ilut_handle->set_async_update(
false);
544 Kokkos::Profiling::ScopedRegion region(
"Ifpack2::ILUT::initialize::par_ilut::get_or_build_A_local_crs");
545 A_local_crs_ = Ifpack2::Details::getCrsMatrix(A_local_);
546 if (A_local_crs_.is_null()) {
547 A_local_crs_nc_ = buildLocalCrsFromRowMatrix<MatrixType>(A_local_);
548 A_local_crs_ = A_local_crs_nc_;
551 auto A_local_crs_device = A_local_crs_->getLocalMatrixDevice();
554 typedef typename Kokkos::View<usize_type*, array_layout, device_type> ulno_row_view_t;
555 const int NumMyRows = A_local_crs_->getRowMap()->getLocalNumElements();
556 L_rowmap_ = ulno_row_view_t(
"L_row_map", NumMyRows + 1);
557 U_rowmap_ = ulno_row_view_t(
"U_row_map", NumMyRows + 1);
558 L_rowmap_orig_ = ulno_row_view_t(
"L_row_map_orig", NumMyRows + 1);
559 U_rowmap_orig_ = ulno_row_view_t(
"U_row_map_orig", NumMyRows + 1);
561 KokkosSparse::Experimental::par_ilut_symbolic(KernelHandle_.getRawPtr(),
562 A_local_crs_device.graph.row_map, A_local_crs_device.graph.entries,
566 Kokkos::deep_copy(L_rowmap_orig_, L_rowmap_);
567 Kokkos::deep_copy(U_rowmap_orig_, U_rowmap_);
570 IsInitialized_ =
true;
573 InitializeTime_ += (timer.wallTime() - startTime);
584 using Teuchos::Array;
585 using Teuchos::ArrayRCP;
586 using Teuchos::ArrayView;
590 using Teuchos::rcp_const_cast;
591 using Teuchos::reduceAll;
594 if (!isInitialized()) {
598 Teuchos::Time timer(
"ILUT::compute");
599 double startTime = timer.wallTime();
601 Teuchos::TimeMonitor timeMon(timer,
true);
603 if (!this->useKokkosKernelsParILUT_) {
635#ifdef IFPACK2_WRITE_ILUT_FACTORS
636 std::ofstream ofsL(
"L.ifpack2_ilut.mtx", std::ios::out);
637 std::ofstream ofsU(
"U.ifpack2_ilut.mtx", std::ios::out);
642 double local_nnz =
static_cast<double>(A_local_->getLocalNumEntries());
643 double fill = ((getLevelOfFill() - 1.0) * local_nnz) / (2 * myNumRows);
648 double fill_ceil = std::ceil(fill);
652 size_type fillL =
static_cast<size_type
>(fill_ceil);
653 size_type fillU =
static_cast<size_type
>(fill_ceil);
655 Array<scalar_type> InvDiagU(myNumRows, zero);
657 Array<Array<local_ordinal_type> > L_tmp_idx(myNumRows);
658 Array<Array<scalar_type> > L_tmpv(myNumRows);
659 Array<Array<local_ordinal_type> > U_tmp_idx(myNumRows);
660 Array<Array<scalar_type> > U_tmpv(myNumRows);
667 Array<int> pattern(max_col, UNUSED);
668 Array<scalar_type> cur_row(max_col, zero);
669 Array<magnitude_type> unorm(max_col);
671 Array<local_ordinal_type> L_cols_heap;
672 Array<local_ordinal_type> U_cols;
673 Array<local_ordinal_type> L_vals_heap;
674 Array<local_ordinal_type> U_vals_heap;
679 greater_indirect<scalar_type, local_ordinal_type> vals_comp(cur_row);
684 nonconst_local_inds_host_view_type ColIndicesARCP;
685 nonconst_values_host_view_type ColValuesARCP;
686 if (!A_local_->supportsRowViews()) {
687 const size_t maxnz = A_local_->getLocalMaxNumRowEntries();
688 Kokkos::resize(ColIndicesARCP, maxnz);
689 Kokkos::resize(ColValuesARCP, maxnz);
693 local_inds_host_view_type ColIndicesA;
694 values_host_view_type ColValuesA;
697 if (A_local_->supportsRowViews()) {
698 A_local_->getLocalRowView(row_i, ColIndicesA, ColValuesA);
699 RowNnz = ColIndicesA.size();
701 A_local_->getLocalRowCopy(row_i, ColIndicesARCP, ColValuesARCP, RowNnz);
702 ColIndicesA = Kokkos::subview(ColIndicesARCP, std::make_pair((
size_t)0, RowNnz));
703 ColValuesA = Kokkos::subview(ColValuesARCP, std::make_pair((
size_t)0, RowNnz));
708 U_cols.push_back(row_i);
709 cur_row[row_i] = zero;
710 pattern[row_i] = ORIG;
712 size_type L_cols_heaplen = 0;
713 rownorm = STM::zero();
714 for (
size_t i = 0; i < RowNnz; ++i) {
715 if (ColIndicesA[i] < myNumRows) {
716 if (ColIndicesA[i] < row_i) {
717 add_to_heap(ColIndicesA[i], L_cols_heap, L_cols_heaplen);
718 }
else if (ColIndicesA[i] > row_i) {
719 U_cols.push_back(ColIndicesA[i]);
722 cur_row[ColIndicesA[i]] = ColValuesA[i];
723 pattern[ColIndicesA[i]] = ORIG;
724 rownorm += scalar_mag(ColValuesA[i]);
733 cur_row[row_i] = as<scalar_type>(getAbsoluteThreshold() * IFPACK2_SGN(v)) + rthresh * v;
735 size_type orig_U_len = U_cols.size();
736 RowNnz = L_cols_heap.size() + orig_U_len;
737 rownorm = getDropTolerance() * rownorm / RowNnz;
740 size_type L_vals_heaplen = 0;
741 while (L_cols_heaplen > 0) {
744 scalar_type multiplier = cur_row[row_k] * InvDiagU[row_k];
745 cur_row[row_k] = multiplier;
747 if (mag_mult * unorm[row_k] < rownorm) {
748 pattern[row_k] = UNUSED;
752 if (pattern[row_k] != ORIG) {
753 if (L_vals_heaplen < fillL) {
754 add_to_heap(row_k, L_vals_heap, L_vals_heaplen, vals_comp);
755 }
else if (L_vals_heaplen == 0 ||
756 mag_mult < scalar_mag(cur_row[L_vals_heap.front()])) {
757 pattern[row_k] = UNUSED;
761 pattern[L_vals_heap.front()] = UNUSED;
763 add_to_heap(row_k, L_vals_heap, L_vals_heaplen, vals_comp);
769 ArrayView<local_ordinal_type> ColIndicesU = U_tmp_idx[row_k]();
770 ArrayView<scalar_type> ColValuesU = U_tmpv[row_k]();
771 size_type ColNnzU = ColIndicesU.size();
773 for (size_type j = 0; j < ColNnzU; ++j) {
774 if (ColIndicesU[j] > row_k) {
777 if (pattern[col_j] != UNUSED) {
778 cur_row[col_j] -= tmp;
779 }
else if (scalar_mag(tmp) > rownorm) {
780 cur_row[col_j] = -tmp;
781 pattern[col_j] = FILL;
783 U_cols.push_back(col_j);
797 for (size_type i = 0; i < (size_type)ColIndicesA.size(); ++i) {
798 if (ColIndicesA[i] < row_i) {
799 L_tmp_idx[row_i].push_back(ColIndicesA[i]);
800 L_tmpv[row_i].push_back(cur_row[ColIndicesA[i]]);
801 pattern[ColIndicesA[i]] = UNUSED;
806 for (size_type j = 0; j < L_vals_heaplen; ++j) {
807 L_tmp_idx[row_i].push_back(L_vals_heap[j]);
808 L_tmpv[row_i].push_back(cur_row[L_vals_heap[j]]);
809 pattern[L_vals_heap[j]] = UNUSED;
817#ifdef IFPACK2_WRITE_ILUT_FACTORS
818 for (size_type ii = 0; ii < L_tmp_idx[row_i].size(); ++ii) {
819 ofsL << row_i <<
" " << L_tmp_idx[row_i][ii] <<
" "
820 << L_tmpv[row_i][ii] << std::endl;
825 if (cur_row[row_i] == zero) {
826 std::cerr <<
"Ifpack2::ILUT::Compute: zero pivot encountered! "
827 <<
"Replacing with rownorm and continuing..."
828 <<
"(You may need to set the parameter "
829 <<
"'fact: absolute threshold'.)" << std::endl;
830 cur_row[row_i] = rownorm;
832 InvDiagU[row_i] = one / cur_row[row_i];
835 U_tmp_idx[row_i].push_back(row_i);
836 U_tmpv[row_i].push_back(cur_row[row_i]);
837 unorm[row_i] = scalar_mag(cur_row[row_i]);
838 pattern[row_i] = UNUSED;
844 size_type U_vals_heaplen = 0;
845 for (size_type j = 1; j < U_cols.size(); ++j) {
847 if (pattern[col] != ORIG) {
848 if (U_vals_heaplen < fillU) {
849 add_to_heap(col, U_vals_heap, U_vals_heaplen, vals_comp);
850 }
else if (U_vals_heaplen != 0 && scalar_mag(cur_row[col]) >
851 scalar_mag(cur_row[U_vals_heap.front()])) {
853 add_to_heap(col, U_vals_heap, U_vals_heaplen, vals_comp);
856 U_tmp_idx[row_i].push_back(col);
857 U_tmpv[row_i].push_back(cur_row[col]);
858 unorm[row_i] += scalar_mag(cur_row[col]);
860 pattern[col] = UNUSED;
863 for (size_type j = 0; j < U_vals_heaplen; ++j) {
864 U_tmp_idx[row_i].push_back(U_vals_heap[j]);
865 U_tmpv[row_i].push_back(cur_row[U_vals_heap[j]]);
866 unorm[row_i] += scalar_mag(cur_row[U_vals_heap[j]]);
869 unorm[row_i] /= (orig_U_len + U_vals_heaplen);
871#ifdef IFPACK2_WRITE_ILUT_FACTORS
872 for (
int ii = 0; ii < U_tmp_idx[row_i].size(); ++ii) {
873 ofsU << row_i <<
" " << U_tmp_idx[row_i][ii] <<
" "
874 << U_tmpv[row_i][ii] << std::endl;
885 Array<size_t> nnzPerRow(myNumRows);
891 L_solver_->setMatrix(Teuchos::null);
892 U_solver_->setMatrix(Teuchos::null);
895 nnzPerRow[row_i] = L_tmp_idx[row_i].size();
898 L_ = rcp(
new crs_matrix_type(A_local_->getRowMap(), A_local_->getColMap(),
902 L_->insertLocalValues(row_i, L_tmp_idx[row_i](), L_tmpv[row_i]());
908 nnzPerRow[row_i] = U_tmp_idx[row_i].size();
911 U_ = rcp(
new crs_matrix_type(A_local_->getRowMap(), A_local_->getColMap(),
915 U_->insertLocalValues(row_i, U_tmp_idx[row_i](), U_tmpv[row_i]());
920 L_solver_->setMatrix(L_);
921 L_solver_->initialize();
922 L_solver_->compute();
924 U_solver_->setMatrix(U_);
925 U_solver_->initialize();
926 U_solver_->compute();
930 Kokkos::Profiling::ScopedRegion region_total(
"Ifpack2::ILUT::compute::par_ilut");
934 if (this->isComputed()) {
935 Kokkos::resize(L_rowmap_, L_rowmap_orig_.size());
936 Kokkos::resize(U_rowmap_, U_rowmap_orig_.size());
937 Kokkos::deep_copy(L_rowmap_, L_rowmap_orig_);
938 Kokkos::deep_copy(U_rowmap_, U_rowmap_orig_);
941 TEUCHOS_TEST_FOR_EXCEPTION(A_local_crs_.is_null(), std::runtime_error,
942 "Ifpack2::ILUT::compute::par_ilut: A_local_crs_ is null after initialize().");
947 if (A_local_crs_nc_ != Teuchos::null) {
948 Kokkos::Profiling::ScopedRegion region(
"Ifpack2::ILUT::compute::par_ilut::refresh_cached_source_crs_values");
949 refreshCachedSourceCrsFromRowMatrix<MatrixType>(A_local_, A_local_crs_nc_);
953 auto lclMtx = A_local_crs_->getLocalMatrixDevice();
954 A_local_rowmap_ = lclMtx.graph.row_map;
955 A_local_entries_ = lclMtx.graph.entries;
956 A_local_values_ = lclMtx.values;
960 auto par_ilut_handle = KernelHandle_->get_par_ilut_handle();
961 auto nnzL = par_ilut_handle->get_nnzL();
962 static_graph_entries_t L_entries_ = static_graph_entries_t(
"L_entries", nnzL);
963 local_matrix_values_t L_values_ = local_matrix_values_t(
"L_values", nnzL);
965 auto nnzU = par_ilut_handle->get_nnzU();
966 static_graph_entries_t U_entries_ = static_graph_entries_t(
"U_entries", nnzU);
967 local_matrix_values_t U_values_ = local_matrix_values_t(
"U_values", nnzU);
970 Kokkos::Profiling::ScopedRegion region(
"Ifpack2::ILUT::compute::par_ilut::numeric");
971 KokkosSparse::Experimental::par_ilut_numeric(KernelHandle_.getRawPtr(),
972 A_local_rowmap_, A_local_entries_, A_local_values_,
973 L_rowmap_, L_entries_, L_values_, U_rowmap_, U_entries_, U_values_);
977 Kokkos::Profiling::ScopedRegion region(
"Ifpack2::ILUT::compute::par_ilut::build_factors");
978 auto L_kokkosCrsGraph = local_graph_device_type(L_entries_, L_rowmap_);
979 auto U_kokkosCrsGraph = local_graph_device_type(U_entries_, U_rowmap_);
981 local_matrix_device_type L_localCrsMatrix_device;
982 L_localCrsMatrix_device = local_matrix_device_type(
"L_Factor_localmatrix",
983 A_local_->getLocalNumRows(),
988 A_local_crs_->getRowMap(),
989 A_local_crs_->getColMap(),
990 A_local_crs_->getDomainMap(),
991 A_local_crs_->getRangeMap(),
992 A_local_crs_->getGraph()->getImporter(),
993 A_local_crs_->getGraph()->getExporter()));
995 local_matrix_device_type U_localCrsMatrix_device;
996 U_localCrsMatrix_device = local_matrix_device_type(
"U_Factor_localmatrix",
997 A_local_->getLocalNumRows(),
1002 A_local_crs_->getRowMap(),
1003 A_local_crs_->getColMap(),
1004 A_local_crs_->getDomainMap(),
1005 A_local_crs_->getRangeMap(),
1006 A_local_crs_->getGraph()->getImporter(),
1007 A_local_crs_->getGraph()->getExporter()));
1011 Kokkos::Profiling::ScopedRegion region(
"Ifpack2::ILUT::compute::par_ilut::solver_setup");
1012 L_solver_->setMatrix(L_);
1013 L_solver_->compute();
1014 U_solver_->setMatrix(U_);
1015 U_solver_->compute();
1020 ComputeTime_ += (timer.wallTime() - startTime);