134 using Ifpack2::Details::getParamTryingTypes;
135 const char prefix[] =
"Ifpack2::ILUT: ";
142 IlutImplType::Enum ilutimplType = IlutImplType::Serial;
144 static const char typeName[] =
"fact: type";
146 if ( ! params.isType<std::string>(typeName))
break;
149 Teuchos::Array<std::string> ilutimplTypeStrs;
150 Teuchos::Array<IlutImplType::Enum> ilutimplTypeEnums;
151 IlutImplType::loadPLTypeOption (ilutimplTypeStrs, ilutimplTypeEnums);
152 Teuchos::StringToIntegralParameterEntryValidator<IlutImplType::Enum>
153 s2i(ilutimplTypeStrs (), ilutimplTypeEnums (), typeName,
false);
155 ilutimplType = s2i.getIntegralValue(params.get<std::string>(typeName));
158 if (ilutimplType == IlutImplType::PAR_ILUT) {
159 this->useKokkosKernelsParILUT_ =
true;
162 this->useKokkosKernelsParILUT_ =
false;
168 double fillLevel = LevelOfFill_;
170 const std::string paramName (
"fact: ilut level-of-fill");
171 TEUCHOS_TEST_FOR_EXCEPTION(
172 (params.isParameter(paramName) && this->useKokkosKernelsParILUT_), std::runtime_error,
173 "Ifpack2::ILUT: Parameter " << paramName <<
" is meaningless for algorithm par_ilut.");
174 getParamTryingTypes<double, double, float>
175 (fillLevel, params, paramName, prefix);
176 TEUCHOS_TEST_FOR_EXCEPTION
177 (fillLevel < 1.0, std::runtime_error,
178 "Ifpack2::ILUT: The \"" << paramName <<
"\" parameter must be >= "
179 "1.0, but you set it to " << fillLevel <<
". For ILUT, the fill level "
180 "means something different than it does for ILU(k). ILU(0) produces "
181 "factors with the same sparsity structure as the input matrix A. For "
182 "ILUT, level-of-fill = 1.0 will produce factors with nonzeros matching "
183 "the sparsity structure of A. level-of-fill > 1.0 allows for additional "
189 const std::string paramName (
"fact: absolute threshold");
190 getParamTryingTypes<magnitude_type, magnitude_type, double>
191 (absThresh, params, paramName, prefix);
196 const std::string paramName (
"fact: relative threshold");
197 getParamTryingTypes<magnitude_type, magnitude_type, double>
198 (relThresh, params, paramName, prefix);
203 const std::string paramName (
"fact: relax value");
204 getParamTryingTypes<magnitude_type, magnitude_type, double>
205 (relaxValue, params, paramName, prefix);
210 const std::string paramName (
"fact: drop tolerance");
211 getParamTryingTypes<magnitude_type, magnitude_type, double>
212 (dropTol, params, paramName, prefix);
215 int par_ilut_max_iter=20;
217 int par_ilut_team_size=0;
218 int par_ilut_vector_size=0;
219 float par_ilut_fill_in_limit=0.75;
220 bool par_ilut_verbose=
false;
221 if (this->useKokkosKernelsParILUT_) {
222 par_ilut_max_iter = par_ilut_options_.max_iter;
223 par_ilut_residual_norm_delta_stop = par_ilut_options_.residual_norm_delta_stop;
224 par_ilut_team_size = par_ilut_options_.team_size;
225 par_ilut_vector_size = par_ilut_options_.vector_size;
226 par_ilut_fill_in_limit = par_ilut_options_.fill_in_limit;
227 par_ilut_verbose = par_ilut_options_.verbose;
229 std::string par_ilut_plist_name(
"parallel ILUT options");
230 if (params.isSublist(par_ilut_plist_name)) {
231 Teuchos::ParameterList
const &par_ilut_plist = params.sublist(par_ilut_plist_name);
233 std::string paramName(
"maximum iterations");
234 getParamTryingTypes<int, int>(par_ilut_max_iter, par_ilut_plist, paramName, prefix);
236 paramName =
"residual norm delta stop";
237 getParamTryingTypes<magnitude_type, magnitude_type, double>(par_ilut_residual_norm_delta_stop, par_ilut_plist, paramName, prefix);
239 paramName =
"team size";
240 getParamTryingTypes<int, int>(par_ilut_team_size, par_ilut_plist, paramName, prefix);
242 paramName =
"vector size";
243 getParamTryingTypes<int, int>(par_ilut_vector_size, par_ilut_plist, paramName, prefix);
245 paramName =
"fill in limit";
246 getParamTryingTypes<float, float, double>(par_ilut_fill_in_limit, par_ilut_plist, paramName, prefix);
248 paramName =
"verbose";
249 getParamTryingTypes<bool, bool>(par_ilut_verbose, par_ilut_plist, paramName, prefix);
253 par_ilut_options_.max_iter = par_ilut_max_iter;
254 par_ilut_options_.residual_norm_delta_stop = par_ilut_residual_norm_delta_stop;
255 par_ilut_options_.team_size = par_ilut_team_size;
256 par_ilut_options_.vector_size = par_ilut_vector_size;
257 par_ilut_options_.fill_in_limit = par_ilut_fill_in_limit;
258 par_ilut_options_.verbose = par_ilut_verbose;
263 L_solver_->setParameters(params);
264 U_solver_->setParameters(params);
266 LevelOfFill_ = fillLevel;
267 Athresh_ = absThresh;
268 Rthresh_ = relThresh;
269 RelaxValue_ = relaxValue;
270 DropTolerance_ = dropTol;
458 using Teuchos::Array;
459 using Teuchos::rcp_const_cast;
460 Teuchos::Time timer (
"ILUT::initialize");
461 double startTime = timer.wallTime();
463 Teuchos::TimeMonitor timeMon (timer);
466 TEUCHOS_TEST_FOR_EXCEPTION(
467 A_.is_null (), std::runtime_error,
"Ifpack2::ILUT::initialize: "
468 "The matrix to precondition is null. Please call setMatrix() with a "
469 "nonnull input before calling this method.");
472 IsInitialized_ =
false;
474 A_local_ = Teuchos::null;
478 A_local_ = makeLocalFilter(A_);
479 TEUCHOS_TEST_FOR_EXCEPTION(
480 A_local_.is_null(), std::logic_error,
"Ifpack2::RILUT::initialize: "
481 "makeLocalFilter returned null; it failed to compute A_local. "
482 "Please report this bug to the Ifpack2 developers.");
484 if (this->useKokkosKernelsParILUT_) {
485 this->KernelHandle_ = Teuchos::rcp(
new kk_handle_type());
486 KernelHandle_->create_par_ilut_handle();
487 auto par_ilut_handle = KernelHandle_->get_par_ilut_handle();
488 par_ilut_handle->set_residual_norm_delta_stop(par_ilut_options_.residual_norm_delta_stop);
489 par_ilut_handle->set_team_size(par_ilut_options_.team_size);
490 par_ilut_handle->set_vector_size(par_ilut_options_.vector_size);
491 par_ilut_handle->set_fill_in_limit(par_ilut_options_.fill_in_limit);
492 par_ilut_handle->set_verbose(par_ilut_options_.verbose);
493 par_ilut_handle->set_async_update(
false);
495 RCP<const crs_matrix_type> A_local_crs = Teuchos::rcp_dynamic_cast<const crs_matrix_type>(A_local_);
496 if (A_local_crs.is_null()) {
499 Array<size_t> entriesPerRow(numRows);
501 entriesPerRow[i] = A_local_->getNumEntriesInLocalRow(i);
503 RCP<crs_matrix_type> A_local_crs_nc =
505 A_local_->getColMap (),
508 nonconst_local_inds_host_view_type indices(
"indices",A_local_->getLocalMaxNumRowEntries());
509 nonconst_values_host_view_type values(
"values",A_local_->getLocalMaxNumRowEntries());
511 size_t numEntries = 0;
512 A_local_->getLocalRowCopy(i, indices, values, numEntries);
513 A_local_crs_nc->insertLocalValues(i, numEntries,
reinterpret_cast<scalar_type*
>(values.data()), indices.data());
515 A_local_crs_nc->fillComplete (A_local_->getDomainMap (), A_local_->getRangeMap ());
516 A_local_crs = rcp_const_cast<const crs_matrix_type> (A_local_crs_nc);
518 auto A_local_crs_device = A_local_crs->getLocalMatrixDevice();
521 typedef typename Kokkos::View<usize_type*, array_layout, device_type> ulno_row_view_t;
522 const int NumMyRows = A_local_crs->getRowMap()->getLocalNumElements();
523 L_rowmap_ = ulno_row_view_t(
"L_row_map", NumMyRows + 1);
524 U_rowmap_ = ulno_row_view_t(
"U_row_map", NumMyRows + 1);
525 L_rowmap_orig_ = ulno_row_view_t(
"L_row_map_orig", NumMyRows + 1);
526 U_rowmap_orig_ = ulno_row_view_t(
"U_row_map_orig", NumMyRows + 1);
528 KokkosSparse::Experimental::par_ilut_symbolic(KernelHandle_.getRawPtr(),
529 A_local_crs_device.graph.row_map, A_local_crs_device.graph.entries,
533 Kokkos::deep_copy(L_rowmap_orig_, L_rowmap_);
534 Kokkos::deep_copy(U_rowmap_orig_, U_rowmap_);
537 IsInitialized_ =
true;
540 InitializeTime_ += (timer.wallTime() - startTime);
555 using Teuchos::Array;
556 using Teuchos::ArrayRCP;
557 using Teuchos::ArrayView;
560 using Teuchos::reduceAll;
562 using Teuchos::rcp_const_cast;
569 Teuchos::Time timer (
"ILUT::compute");
570 double startTime = timer.wallTime();
572 Teuchos::TimeMonitor timeMon (timer,
true);
574 if (!this->useKokkosKernelsParILUT_)
607#ifdef IFPACK2_WRITE_ILUT_FACTORS
608 std::ofstream ofsL(
"L.ifpack2_ilut.mtx", std::ios::out);
609 std::ofstream ofsU(
"U.ifpack2_ilut.mtx", std::ios::out);
614 double local_nnz =
static_cast<double> (A_local_->getLocalNumEntries ());
615 double fill = ((
getLevelOfFill () - 1.0) * local_nnz) / (2 * myNumRows);
620 double fill_ceil=std::ceil(fill);
624 size_type fillL =
static_cast<size_type
>(fill_ceil);
625 size_type fillU =
static_cast<size_type
>(fill_ceil);
627 Array<scalar_type> InvDiagU (myNumRows, zero);
629 Array<Array<local_ordinal_type> > L_tmp_idx(myNumRows);
630 Array<Array<scalar_type> > L_tmpv(myNumRows);
631 Array<Array<local_ordinal_type> > U_tmp_idx(myNumRows);
632 Array<Array<scalar_type> > U_tmpv(myNumRows);
634 enum { UNUSED, ORIG, FILL };
637 Array<int> pattern(max_col, UNUSED);
638 Array<scalar_type> cur_row(max_col, zero);
639 Array<magnitude_type> unorm(max_col);
641 Array<local_ordinal_type> L_cols_heap;
642 Array<local_ordinal_type> U_cols;
643 Array<local_ordinal_type> L_vals_heap;
644 Array<local_ordinal_type> U_vals_heap;
649 greater_indirect<scalar_type,local_ordinal_type> vals_comp(cur_row);
654 nonconst_local_inds_host_view_type ColIndicesARCP;
655 nonconst_values_host_view_type ColValuesARCP;
656 if (! A_local_->supportsRowViews ()) {
657 const size_t maxnz = A_local_->getLocalMaxNumRowEntries ();
658 Kokkos::resize(ColIndicesARCP,maxnz);
659 Kokkos::resize(ColValuesARCP,maxnz);
663 local_inds_host_view_type ColIndicesA;
664 values_host_view_type ColValuesA;
667 if (A_local_->supportsRowViews ()) {
668 A_local_->getLocalRowView (row_i, ColIndicesA, ColValuesA);
669 RowNnz = ColIndicesA.size ();
672 A_local_->getLocalRowCopy (row_i, ColIndicesARCP, ColValuesARCP, RowNnz);
673 ColIndicesA = Kokkos::subview(ColIndicesARCP,std::make_pair((
size_t)0, RowNnz));
674 ColValuesA = Kokkos::subview(ColValuesARCP,std::make_pair((
size_t)0, RowNnz));
679 U_cols.push_back(row_i);
680 cur_row[row_i] = zero;
681 pattern[row_i] = ORIG;
683 size_type L_cols_heaplen = 0;
684 rownorm = STM::zero ();
685 for (
size_t i = 0; i < RowNnz; ++i) {
686 if (ColIndicesA[i] < myNumRows) {
687 if (ColIndicesA[i] < row_i) {
688 add_to_heap(ColIndicesA[i], L_cols_heap, L_cols_heaplen);
690 else if (ColIndicesA[i] > row_i) {
691 U_cols.push_back(ColIndicesA[i]);
694 cur_row[ColIndicesA[i]] = ColValuesA[i];
695 pattern[ColIndicesA[i]] = ORIG;
696 rownorm += scalar_mag(ColValuesA[i]);
707 size_type orig_U_len = U_cols.size();
708 RowNnz = L_cols_heap.size() + orig_U_len;
712 size_type L_vals_heaplen = 0;
713 while (L_cols_heaplen > 0) {
716 scalar_type multiplier = cur_row[row_k] * InvDiagU[row_k];
717 cur_row[row_k] = multiplier;
719 if (mag_mult*unorm[row_k] < rownorm) {
720 pattern[row_k] = UNUSED;
724 if (pattern[row_k] != ORIG) {
725 if (L_vals_heaplen < fillL) {
726 add_to_heap(row_k, L_vals_heap, L_vals_heaplen, vals_comp);
728 else if (L_vals_heaplen==0 ||
729 mag_mult < scalar_mag(cur_row[L_vals_heap.front()])) {
730 pattern[row_k] = UNUSED;
735 pattern[L_vals_heap.front()] = UNUSED;
737 add_to_heap(row_k, L_vals_heap, L_vals_heaplen, vals_comp);
743 ArrayView<local_ordinal_type> ColIndicesU = U_tmp_idx[row_k]();
744 ArrayView<scalar_type> ColValuesU = U_tmpv[row_k]();
745 size_type ColNnzU = ColIndicesU.size();
747 for(size_type j=0; j<ColNnzU; ++j) {
748 if (ColIndicesU[j] > row_k) {
751 if (pattern[col_j] != UNUSED) {
752 cur_row[col_j] -= tmp;
754 else if (scalar_mag(tmp) > rownorm) {
755 cur_row[col_j] = -tmp;
756 pattern[col_j] = FILL;
758 U_cols.push_back(col_j);
774 for (size_type i = 0; i < (size_type)ColIndicesA.size (); ++i) {
775 if (ColIndicesA[i] < row_i) {
776 L_tmp_idx[row_i].push_back(ColIndicesA[i]);
777 L_tmpv[row_i].push_back(cur_row[ColIndicesA[i]]);
778 pattern[ColIndicesA[i]] = UNUSED;
783 for (size_type j = 0; j < L_vals_heaplen; ++j) {
784 L_tmp_idx[row_i].push_back(L_vals_heap[j]);
785 L_tmpv[row_i].push_back(cur_row[L_vals_heap[j]]);
786 pattern[L_vals_heap[j]] = UNUSED;
794#ifdef IFPACK2_WRITE_ILUT_FACTORS
795 for (size_type ii = 0; ii < L_tmp_idx[row_i].size (); ++ii) {
796 ofsL << row_i <<
" " << L_tmp_idx[row_i][ii] <<
" "
797 << L_tmpv[row_i][ii] << std::endl;
803 if (cur_row[row_i] == zero) {
804 std::cerr <<
"Ifpack2::ILUT::Compute: zero pivot encountered! "
805 <<
"Replacing with rownorm and continuing..."
806 <<
"(You may need to set the parameter "
807 <<
"'fact: absolute threshold'.)" << std::endl;
808 cur_row[row_i] = rownorm;
810 InvDiagU[row_i] = one / cur_row[row_i];
813 U_tmp_idx[row_i].push_back(row_i);
814 U_tmpv[row_i].push_back(cur_row[row_i]);
815 unorm[row_i] = scalar_mag(cur_row[row_i]);
816 pattern[row_i] = UNUSED;
822 size_type U_vals_heaplen = 0;
823 for(size_type j=1; j<U_cols.size(); ++j) {
825 if (pattern[col] != ORIG) {
826 if (U_vals_heaplen < fillU) {
827 add_to_heap(col, U_vals_heap, U_vals_heaplen, vals_comp);
829 else if (U_vals_heaplen!=0 && scalar_mag(cur_row[col]) >
830 scalar_mag(cur_row[U_vals_heap.front()])) {
832 add_to_heap(col, U_vals_heap, U_vals_heaplen, vals_comp);
836 U_tmp_idx[row_i].push_back(col);
837 U_tmpv[row_i].push_back(cur_row[col]);
838 unorm[row_i] += scalar_mag(cur_row[col]);
840 pattern[col] = UNUSED;
843 for(size_type j=0; j<U_vals_heaplen; ++j) {
844 U_tmp_idx[row_i].push_back(U_vals_heap[j]);
845 U_tmpv[row_i].push_back(cur_row[U_vals_heap[j]]);
846 unorm[row_i] += scalar_mag(cur_row[U_vals_heap[j]]);
849 unorm[row_i] /= (orig_U_len + U_vals_heaplen);
851#ifdef IFPACK2_WRITE_ILUT_FACTORS
852 for(
int ii=0; ii<U_tmp_idx[row_i].size(); ++ii) {
853 ofsU <<row_i<<
" " <<U_tmp_idx[row_i][ii]<<
" "
854 <<U_tmpv[row_i][ii]<< std::endl;
865 Array<size_t> nnzPerRow(myNumRows);
871 L_solver_->setMatrix(Teuchos::null);
872 U_solver_->setMatrix(Teuchos::null);
875 nnzPerRow[row_i] = L_tmp_idx[row_i].size();
878 L_ = rcp (
new crs_matrix_type (A_local_->getRowMap(), A_local_->getColMap(),
882 L_->insertLocalValues (row_i, L_tmp_idx[row_i](), L_tmpv[row_i]());
888 nnzPerRow[row_i] = U_tmp_idx[row_i].size();
891 U_ = rcp (
new crs_matrix_type (A_local_->getRowMap(), A_local_->getColMap(),
895 U_->insertLocalValues (row_i, U_tmp_idx[row_i](), U_tmpv[row_i]());
900 L_solver_->setMatrix(L_);
901 L_solver_->initialize ();
902 L_solver_->compute ();
904 U_solver_->setMatrix(U_);
905 U_solver_->initialize ();
906 U_solver_->compute ();
913 Kokkos::resize(L_rowmap_, L_rowmap_orig_.size());
914 Kokkos::resize(U_rowmap_, U_rowmap_orig_.size());
915 Kokkos::deep_copy(L_rowmap_, L_rowmap_orig_);
916 Kokkos::deep_copy(U_rowmap_, U_rowmap_orig_);
919 RCP<const crs_matrix_type> A_local_crs = Teuchos::rcp_dynamic_cast<const crs_matrix_type>(A_local_);
921 if(A_local_crs.is_null()) {
923 Array<size_t> entriesPerRow(numRows);
925 entriesPerRow[i] = A_local_->getNumEntriesInLocalRow(i);
927 RCP<crs_matrix_type> A_local_crs_nc =
929 A_local_->getColMap (),
932 nonconst_local_inds_host_view_type indices(
"indices",A_local_->getLocalMaxNumRowEntries());
933 nonconst_values_host_view_type values(
"values",A_local_->getLocalMaxNumRowEntries());
935 size_t numEntries = 0;
936 A_local_->getLocalRowCopy(i, indices, values, numEntries);
937 A_local_crs_nc->insertLocalValues(i, numEntries,
reinterpret_cast<scalar_type*
>(values.data()),indices.data());
939 A_local_crs_nc->fillComplete (A_local_->getDomainMap (), A_local_->getRangeMap ());
940 A_local_crs = rcp_const_cast<const crs_matrix_type> (A_local_crs_nc);
942 auto lclMtx = A_local_crs->getLocalMatrixDevice();
943 A_local_rowmap_ = lclMtx.graph.row_map;
944 A_local_entries_ = lclMtx.graph.entries;
945 A_local_values_ = lclMtx.values;
949 auto par_ilut_handle = KernelHandle_->get_par_ilut_handle();
950 auto nnzL = par_ilut_handle->get_nnzL();
951 static_graph_entries_t L_entries_ = static_graph_entries_t(
"L_entries", nnzL);
952 local_matrix_values_t L_values_ = local_matrix_values_t(
"L_values", nnzL);
954 auto nnzU = par_ilut_handle->get_nnzU();
955 static_graph_entries_t U_entries_ = static_graph_entries_t(
"U_entries", nnzU);
956 local_matrix_values_t U_values_ = local_matrix_values_t(
"U_values", nnzU);
958 KokkosSparse::Experimental::par_ilut_numeric(KernelHandle_.getRawPtr(),
959 A_local_rowmap_, A_local_entries_, A_local_values_,
960 L_rowmap_, L_entries_, L_values_, U_rowmap_, U_entries_, U_values_);
962 auto L_kokkosCrsGraph = local_graph_device_type(L_entries_, L_rowmap_);
963 auto U_kokkosCrsGraph = local_graph_device_type(U_entries_, U_rowmap_);
965 local_matrix_device_type L_localCrsMatrix_device;
966 L_localCrsMatrix_device = local_matrix_device_type(
"L_Factor_localmatrix",
967 A_local_->getLocalNumRows(),
972 A_local_crs->getRowMap(),
973 A_local_crs->getColMap(),
974 A_local_crs->getDomainMap(),
975 A_local_crs->getRangeMap(),
976 A_local_crs->getGraph()->getImporter(),
977 A_local_crs->getGraph()->getExporter()));
979 local_matrix_device_type U_localCrsMatrix_device;
980 U_localCrsMatrix_device = local_matrix_device_type(
"U_Factor_localmatrix",
981 A_local_->getLocalNumRows(),
986 A_local_crs->getRowMap(),
987 A_local_crs->getColMap(),
988 A_local_crs->getDomainMap(),
989 A_local_crs->getRangeMap(),
990 A_local_crs->getGraph()->getImporter(),
991 A_local_crs->getGraph()->getExporter()));
993 L_solver_->setMatrix (L_);
994 L_solver_->compute ();
995 U_solver_->setMatrix (U_);
996 U_solver_->compute ();
1000 ComputeTime_ += (timer.wallTime() - startTime);