49 #ifndef IFPACK2_ILUK_GRAPH_HPP
50 #define IFPACK2_ILUK_GRAPH_HPP
55 #include "KokkosSparse_spiluk.hpp"
57 #include <Ifpack2_ConfigDefs.hpp>
59 #include <Teuchos_CommHelpers.hpp>
60 #include <Tpetra_CrsGraph.hpp>
61 #include <Tpetra_Details_WrappedDualView.hpp>
62 #include <Tpetra_Import.hpp>
63 #include <Ifpack2_CreateOverlapGraph.hpp>
64 #include <Ifpack2_Parameters.hpp>
99 template<
class GraphType,
class KKHandleType>
102 typedef typename GraphType::local_ordinal_type local_ordinal_type;
103 typedef typename GraphType::global_ordinal_type global_ordinal_type;
104 typedef typename GraphType::node_type node_type;
107 typedef Tpetra::RowGraph<local_ordinal_type,
111 typedef Tpetra::CrsGraph<local_ordinal_type,
117 typedef typename crs_graph_type::nonconst_global_inds_host_view_type nonconst_global_inds_host_view_type;
118 typedef typename crs_graph_type::nonconst_local_inds_host_view_type nonconst_local_inds_host_view_type;
119 typedef typename crs_graph_type::global_inds_host_view_type global_inds_host_view_type;
120 typedef typename crs_graph_type::local_inds_host_view_type local_inds_host_view_type;
135 const int levelOverlap,
136 const double overalloc = 2.);
185 return OverlapGraph_;
192 typedef typename GraphType::map_type map_type;
215 void constructOverlapGraph();
221 const double Overalloc_;
224 size_t NumMyDiagonals_;
225 size_t NumGlobalDiagonals_;
229 template<
class GraphType,
class KKHandleType>
233 const int levelOverlap,
234 const double overalloc)
236 LevelFill_ (levelFill),
237 LevelOverlap_ (levelOverlap),
238 Overalloc_ (overalloc),
240 NumGlobalDiagonals_ (0)
243 "Ifpack2::IlukGraph: FATAL: overalloc must be greater than 1.")
247 template<
class GraphType,
class KKHandleType>
252 template<
class GraphType,
class KKHandleType>
256 getParameter (parameterlist,
"iluk level-of-fill", LevelFill_);
257 getParameter (parameterlist,
"iluk level-of-overlap", LevelOverlap_);
261 template<
class GraphType,
class KKHandleType>
266 if (OverlapGraph_ == Teuchos::null) {
267 OverlapGraph_ = createOverlapGraph<GraphType> (Graph_, LevelOverlap_);
272 template<
class GraphType,
class KKHandleType>
280 using Teuchos::reduceAll;
282 size_t NumIn, NumL, NumU;
285 constructOverlapGraph();
288 const int MaxNumIndices = OverlapGraph_->getLocalMaxNumRowEntries ();
292 const int NumMyRows = OverlapGraph_->getRowMap ()->getLocalNumElements ();
294 using device_type =
typename node_type::device_type;
295 using execution_space =
typename device_type::execution_space;
296 using dual_view_type = Kokkos::DualView<size_t*,device_type>;
297 dual_view_type numEntPerRow_dv(
"numEntPerRow",NumMyRows);
298 Tpetra::Details::WrappedDualView<dual_view_type> numEntPerRow(numEntPerRow_dv);
300 const auto overalloc = Overalloc_;
301 const auto levelfill = LevelFill_;
304 auto numEntPerRow_d = numEntPerRow.getDeviceView(Tpetra::Access::OverwriteAll);
305 auto localOverlapGraph = OverlapGraph_->getLocalGraphDevice();
306 Kokkos::parallel_for(
"CountOverlapGraphRowEntries",
307 Kokkos::RangePolicy<execution_space>(0, NumMyRows),
308 KOKKOS_LAMBDA(
const int i)
311 int RowMaxNumIndices = localOverlapGraph.rowConst(i).length;
312 numEntPerRow_d(i) = (levelfill == 0) ? RowMaxNumIndices
313 : Kokkos::ceil(static_cast<double>(RowMaxNumIndices)
314 * Kokkos::pow(overalloc, levelfill));
324 OverlapGraph_->getRowMap (),
327 OverlapGraph_->getRowMap (),
330 Array<local_ordinal_type> L (MaxNumIndices);
331 Array<local_ordinal_type> U (MaxNumIndices);
337 for (
int i = 0; i< NumMyRows; ++i) {
338 local_inds_host_view_type my_indices;
339 OverlapGraph_->getLocalRowView (i, my_indices);
346 NumIn = my_indices.size();
348 for (
size_t j = 0; j < NumIn; ++j) {
349 const local_ordinal_type k = my_indices[j];
373 L_Graph_->insertLocalIndices (i, NumL, L.data());
376 U_Graph_->insertLocalIndices (i, NumU, U.data());
380 if (LevelFill_ > 0) {
382 RCP<const map_type> L_DomainMap = OverlapGraph_->getRowMap ();
383 RCP<const map_type> L_RangeMap = Graph_->getRangeMap ();
384 RCP<const map_type> U_DomainMap = Graph_->getDomainMap ();
385 RCP<const map_type> U_RangeMap = OverlapGraph_->getRowMap ();
386 RCP<Teuchos::ParameterList> params = Teuchos::parameterList ();
387 params->set (
"Optimize Storage",
false);
388 L_Graph_->fillComplete (L_DomainMap, L_RangeMap, params);
389 U_Graph_->fillComplete (U_DomainMap, U_RangeMap, params);
390 L_Graph_->resumeFill ();
391 U_Graph_->resumeFill ();
397 int MaxRC = NumMyRows;
398 std::vector<std::vector<int> > Levels(MaxRC);
399 std::vector<int> LinkList(MaxRC);
400 std::vector<int> CurrentLevel(MaxRC);
401 Array<local_ordinal_type> CurrentRow (MaxRC + 1);
402 std::vector<int> LevelsRowU(MaxRC);
405 for (
int i = 0; i < NumMyRows; ++i) {
410 size_t LenL = L_Graph_->getNumEntriesInLocalRow(i);
411 size_t LenU = U_Graph_->getNumEntriesInLocalRow(i);
412 size_t Len = LenL + LenU + 1;
413 CurrentRow.resize(Len);
414 nonconst_local_inds_host_view_type CurrentRow_view(CurrentRow.data(),CurrentRow.size());
415 L_Graph_->getLocalRowCopy(i, CurrentRow_view, LenL);
416 CurrentRow[LenL] = i;
418 ArrayView<local_ordinal_type> URowView = CurrentRow.
view (LenL+1,LenU);
419 nonconst_local_inds_host_view_type URowView_v(URowView.data(),URowView.size());
422 U_Graph_->getLocalRowCopy (i, URowView_v, LenU);
427 for (
size_t j=0; j<Len-1; j++) {
428 LinkList[CurrentRow[j]] = CurrentRow[j+1];
429 CurrentLevel[CurrentRow[j]] = 0;
432 LinkList[CurrentRow[Len-1]] = NumMyRows;
433 CurrentLevel[CurrentRow[Len-1]] = 0;
437 First = CurrentRow[0];
440 int PrevInList = Next;
441 int NextInList = LinkList[Next];
444 local_inds_host_view_type IndicesU;
445 U_Graph_->getLocalRowView (RowU, IndicesU);
447 int LengthRowU = IndicesU.size ();
453 for (ii = 0; ii < LengthRowU; ) {
454 int CurInList = IndicesU[ii];
455 if (CurInList < NextInList) {
457 int NewLevel = CurrentLevel[RowU] + Levels[RowU][ii+1] + 1;
458 if (NewLevel <= LevelFill_) {
459 LinkList[PrevInList] = CurInList;
460 LinkList[CurInList] = NextInList;
461 PrevInList = CurInList;
462 CurrentLevel[CurInList] = NewLevel;
466 else if (CurInList == NextInList) {
467 PrevInList = NextInList;
468 NextInList = LinkList[PrevInList];
469 int NewLevel = CurrentLevel[RowU] + Levels[RowU][ii+1] + 1;
470 CurrentLevel[CurInList] = std::min (CurrentLevel[CurInList],
475 PrevInList = NextInList;
476 NextInList = LinkList[PrevInList];
479 Next = LinkList[Next];
483 CurrentRow.resize(0);
489 CurrentRow.push_back(Next);
490 Next = LinkList[Next];
496 L_Graph_->removeLocalIndices (i);
497 if (CurrentRow.size() > 0) {
498 L_Graph_->insertLocalIndices (i, CurrentRow.size(),CurrentRow.data());
504 Next != i, std::runtime_error,
505 "Ifpack2::IlukGraph::initialize: FATAL: U has zero diagonal")
507 LevelsRowU[0] = CurrentLevel[Next];
508 Next = LinkList[Next];
511 CurrentRow.resize(0);
514 while (Next < NumMyRows) {
515 LevelsRowU[LenU+1] = CurrentLevel[Next];
516 CurrentRow.push_back (Next);
518 Next = LinkList[Next];
525 U_Graph_->removeLocalIndices (i);
527 U_Graph_->insertLocalIndices (i, CurrentRow.size(),CurrentRow.data());
531 Levels[i] = std::vector<int> (LenU+1);
532 for (
size_t jj=0; jj<LenU+1; jj++) {
533 Levels[i][jj] = LevelsRowU[jj];
537 catch (std::runtime_error &e) {
539 auto numEntPerRow_d = numEntPerRow.getDeviceView(Tpetra::Access::OverwriteAll);
540 Kokkos::parallel_for(
"CountOverlapGraphRowEntries",
541 Kokkos::RangePolicy<execution_space>(0, NumMyRows),
542 KOKKOS_LAMBDA(
const int i)
544 const auto numRowEnt = numEntPerRow_d(i);
545 numEntPerRow_d(i) = ceil(static_cast<double>((numRowEnt != 0 ? numRowEnt : 1)) * overalloc);
548 const int localInsertError = insertError ? 1 : 0;
549 int globalInsertError = 0;
550 reduceAll (* (OverlapGraph_->getRowMap ()->getComm ()), REDUCE_SUM, 1,
551 &localInsertError, &globalInsertError);
552 insertError = globalInsertError > 0;
554 }
while (insertError);
557 RCP<const map_type> L_DomainMap = OverlapGraph_->getRowMap ();
558 RCP<const map_type> L_RangeMap = Graph_->getRangeMap ();
559 RCP<const map_type> U_DomainMap = Graph_->getDomainMap ();
560 RCP<const map_type> U_RangeMap = OverlapGraph_->getRowMap ();
561 L_Graph_->fillComplete (L_DomainMap, L_RangeMap);
562 U_Graph_->fillComplete (U_DomainMap, U_RangeMap);
564 reduceAll<int, size_t> (* (L_DomainMap->getComm ()), REDUCE_SUM, 1,
565 &NumMyDiagonals_, &NumGlobalDiagonals_);
569 template<
class GraphType,
class KKHandleType>
577 using Teuchos::reduceAll;
579 typedef typename crs_graph_type::local_graph_device_type local_graph_device_type;
580 typedef typename local_graph_device_type::size_type size_type;
581 typedef typename local_graph_device_type::data_type data_type;
582 typedef typename local_graph_device_type::array_layout array_layout;
583 typedef typename local_graph_device_type::device_type device_type;
585 typedef typename Kokkos::View<size_type*, array_layout, device_type> lno_row_view_t;
586 typedef typename Kokkos::View<data_type*, array_layout, device_type> lno_nonzero_view_t;
588 constructOverlapGraph();
592 const int NumMyRows = OverlapGraph_->getRowMap()->getLocalNumElements();
593 auto localOverlapGraph = OverlapGraph_->getLocalGraphDevice();
595 if (KernelHandle->get_spiluk_handle()->get_nrows() <
static_cast<size_type
>(NumMyRows)) {
596 KernelHandle->get_spiluk_handle()->reset_handle(NumMyRows,
597 KernelHandle->get_spiluk_handle()->get_nnzL(),
598 KernelHandle->get_spiluk_handle()->get_nnzU());
601 lno_row_view_t L_row_map(
"L_row_map", NumMyRows + 1);
602 lno_nonzero_view_t L_entries(
"L_entries", KernelHandle->get_spiluk_handle()->get_nnzL());
603 lno_row_view_t U_row_map(
"U_row_map", NumMyRows + 1);
604 lno_nonzero_view_t U_entries(
"U_entries", KernelHandle->get_spiluk_handle()->get_nnzU());
608 symbolicError =
false;
610 KokkosSparse::Experimental::spiluk_symbolic( KernelHandle.
getRawPtr(), LevelFill_,
611 localOverlapGraph.row_map, localOverlapGraph.entries,
612 L_row_map, L_entries, U_row_map, U_entries );
614 catch (std::runtime_error &e) {
615 symbolicError =
true;
616 data_type nnzL =
static_cast<data_type
>(Overalloc_)*L_entries.extent(0);
617 data_type nnzU =
static_cast<data_type
>(Overalloc_)*U_entries.extent(0);
618 KernelHandle->get_spiluk_handle()->reset_handle(NumMyRows, nnzL, nnzU);
619 Kokkos::resize(L_entries, KernelHandle->get_spiluk_handle()->get_nnzL());
620 Kokkos::resize(U_entries, KernelHandle->get_spiluk_handle()->get_nnzU());
622 const int localSymbolicError = symbolicError ? 1 : 0;
623 int globalSymbolicError = 0;
624 reduceAll (* (OverlapGraph_->getRowMap ()->getComm ()), REDUCE_SUM, 1,
625 &localSymbolicError, &globalSymbolicError);
626 symbolicError = globalSymbolicError > 0;
627 }
while (symbolicError);
629 Kokkos::resize(L_entries, KernelHandle->get_spiluk_handle()->get_nnzL());
630 Kokkos::resize(U_entries, KernelHandle->get_spiluk_handle()->get_nnzU());
632 RCP<Teuchos::ParameterList> params = Teuchos::parameterList ();
633 params->set (
"Optimize Storage",
false);
635 L_Graph_ =
rcp (
new crs_graph_type (OverlapGraph_->getRowMap (),
636 OverlapGraph_->getRowMap (),
637 L_row_map, L_entries));
638 U_Graph_ =
rcp (
new crs_graph_type (OverlapGraph_->getRowMap (),
639 OverlapGraph_->getRowMap (),
640 U_row_map, U_entries));
642 RCP<const map_type> L_DomainMap = OverlapGraph_->getRowMap ();
643 RCP<const map_type> L_RangeMap = Graph_->getRangeMap ();
644 RCP<const map_type> U_DomainMap = Graph_->getDomainMap ();
645 RCP<const map_type> U_RangeMap = OverlapGraph_->getRowMap ();
646 L_Graph_->fillComplete (L_DomainMap, L_RangeMap, params);
647 U_Graph_->fillComplete (U_DomainMap, U_RangeMap, params);
IlukGraph(const Teuchos::RCP< const GraphType > &G, const int levelFill, const int levelOverlap, const double overalloc=2.)
Constructor.
Definition: Ifpack2_IlukGraph.hpp:231
Teuchos::RCP< const GraphType > getA_Graph() const
Returns the original graph given.
Definition: Ifpack2_IlukGraph.hpp:169
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void getParameter(const Teuchos::ParameterList ¶ms, const std::string &name, T &value)
Set a value from a ParameterList if a parameter with the specified name exists.
Definition: Ifpack2_Parameters.hpp:59
size_t getNumGlobalDiagonals() const
Returns the global number of diagonals in the ILU(k) graph.
Definition: Ifpack2_IlukGraph.hpp:189
virtual ~IlukGraph()
IlukGraph Destructor.
Definition: Ifpack2_IlukGraph.hpp:248
Tpetra::CrsGraph< local_ordinal_type, global_ordinal_type, node_type > crs_graph_type
Tpetra::CrsGraph specialization used by this class.
Definition: Ifpack2_IlukGraph.hpp:113
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Teuchos::RCP< const crs_graph_type > getOverlapGraph() const
Returns the the overlapped graph.
Definition: Ifpack2_IlukGraph.hpp:184
Construct a level filled graph for use in computing an ILU(k) incomplete factorization.
Definition: Ifpack2_IlukGraph.hpp:100
Teuchos::RCP< crs_graph_type > getU_Graph() const
Returns the graph of upper triangle of the ILU(k) graph as a Tpetra::CrsGraph.
Definition: Ifpack2_IlukGraph.hpp:179
Teuchos::RCP< crs_graph_type > getL_Graph() const
Returns the graph of lower triangle of the ILU(k) graph as a Tpetra::CrsGraph.
Definition: Ifpack2_IlukGraph.hpp:174
void initialize()
Set up the graph structure of the L and U factors.
Definition: Ifpack2_IlukGraph.hpp:273
int getLevelFill() const
The level of fill used to construct this graph.
Definition: Ifpack2_IlukGraph.hpp:163
ArrayView< T > view(size_type offset, size_type size) const
int getLevelOverlap() const
The level of overlap used to construct this graph.
Definition: Ifpack2_IlukGraph.hpp:166
Tpetra::RowGraph< local_ordinal_type, global_ordinal_type, node_type > row_graph_type
Tpetra::RowGraph specialization used by this class.
Definition: Ifpack2_IlukGraph.hpp:109
void setParameters(const Teuchos::ParameterList ¶meterlist)
Set parameters.
Definition: Ifpack2_IlukGraph.hpp:254