Ifpack2 Templated Preconditioning Package  Version 1.0
Ifpack2_IlukGraph.hpp
1 /*@HEADER
2 // ***********************************************************************
3 //
4 // Ifpack2: Tempated Object-Oriented Algebraic Preconditioner Package
5 // Copyright (2009) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 //@HEADER
41  */
42 
43 #ifndef IFPACK2_ILUK_GRAPH_HPP
44 #define IFPACK2_ILUK_GRAPH_HPP
45 
46 #include <vector>
47 #include <algorithm>
48 
49 #include <Ifpack2_ConfigDefs.hpp>
50 #include <Teuchos_RCP.hpp>
51 #include <Teuchos_ParameterList.hpp>
52 #include <Teuchos_Describable.hpp>
53 #include <Teuchos_CommHelpers.hpp>
54 #include <Tpetra_CrsGraph.hpp>
55 #include <Tpetra_Import.hpp>
56 #include <Ifpack2_CreateOverlapGraph.hpp>
57 #include <Ifpack2_Parameters.hpp>
58 
59 namespace Ifpack2 {
60 
61 
63 
82 template<class LocalOrdinal, class GlobalOrdinal = LocalOrdinal, class Node = Kokkos::DefaultNode::DefaultNodeType>
83 class IlukGraph : public Teuchos::Describable
84 {
85 
86 public:
87 
89 
101  IlukGraph(const Teuchos::RCP<const Tpetra::CrsGraph<LocalOrdinal,GlobalOrdinal,Node> > &Graph_in, int LevelFill_in, int LevelOverlap_in);
102 
105 
107  virtual ~IlukGraph();
108 
110 
115  void setParameters(const Teuchos::ParameterList& parameterlist);
116 
118 
120  void constructFilledGraph();
121 
123 
125  void constructOverlapGraph();
126 
128  int getLevelFill() const {return(LevelFill_);}
129 
131  int getLevelOverlap() const {return(LevelOverlap_);}
132 
134  const Teuchos::RCP<Tpetra::CrsGraph<LocalOrdinal,GlobalOrdinal,Node> >& getL_Graph() const {return(L_Graph_);}
135 
137  const Teuchos::RCP<Tpetra::CrsGraph<LocalOrdinal,GlobalOrdinal,Node> >& getU_Graph() const {return(U_Graph_);}
138 
140  const Teuchos::RCP<Tpetra::CrsGraph<LocalOrdinal,GlobalOrdinal,Node> >& getOverlapGraph() const {return(OverlapGraph_);}
141 
143  size_t getNumGlobalDiagonals() const { return NumGlobalDiagonals_; }
144 
145 private:
146  typedef Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node> TpetraMapType;
147  typedef Tpetra::CrsGraph<LocalOrdinal,GlobalOrdinal,Node> TpetraCrsGraphType;
148 
149  Teuchos::RCP<const TpetraCrsGraphType > Graph_;
150  Teuchos::RCP<const TpetraCrsGraphType > OverlapGraph_;
151  int LevelFill_;
152  int LevelOverlap_;
153  Teuchos::RCP<TpetraCrsGraphType > L_Graph_;
154  Teuchos::RCP<TpetraCrsGraphType > U_Graph_;
155  size_t NumMyDiagonals_;
156  size_t NumGlobalDiagonals_;
157 };
158 
159 //==============================================================================
160 template<class LocalOrdinal, class GlobalOrdinal, class Node>
161 IlukGraph<LocalOrdinal,GlobalOrdinal,Node>::IlukGraph(const Teuchos::RCP<const Tpetra::CrsGraph<LocalOrdinal,GlobalOrdinal,Node> >& Graph_in, int LevelFill_in, int LevelOverlap_in)
162 : Graph_(Graph_in),
163  OverlapGraph_(),
164  LevelFill_(LevelFill_in),
165  LevelOverlap_(LevelOverlap_in),
166  NumMyDiagonals_(0),
167  NumGlobalDiagonals_(0)
168 {
169 }
170 
171 //==============================================================================
172 template<class LocalOrdinal, class GlobalOrdinal, class Node>
174 : Graph_(Graph_in.Graph_),
175  OverlapGraph_(Graph_in.OverlapGraph_),
176  LevelFill_(Graph_in.LevelFill_),
177  LevelOverlap_(Graph_in.LevelOverlap_),
178  L_Graph_(),
179  U_Graph_(),
180  NumMyDiagonals_(0),
181  NumGlobalDiagonals_(0)
182 {
183  TpetraCrsGraphType & L_Graph_In = Graph_in.L_Graph();
184  TpetraCrsGraphType & U_Graph_In = Graph_in.U_Graph();
185  L_Graph_ = Teuchos::rcp( new TpetraCrsGraphType(L_Graph_In) );
186  U_Graph_ = Teuchos::rcp( new TpetraCrsGraphType(U_Graph_In) );
187 }
188 
189 //==============================================================================
190 template<class LocalOrdinal, class GlobalOrdinal, class Node>
192 {
193 }
194 
195 //==============================================================================
196 template<class LocalOrdinal, class GlobalOrdinal, class Node>
197 void IlukGraph<LocalOrdinal,GlobalOrdinal,Node>::setParameters(const Teuchos::ParameterList& parameterlist)
198 {
199  getParameter(parameterlist, "iluk level-of-fill", LevelFill_);
200  getParameter(parameterlist, "iluk level-of-overlap", LevelOverlap_);
201 }
202 
203 //==============================================================================
204 template<class LocalOrdinal, class GlobalOrdinal, class Node>
206 
207  if (OverlapGraph_ == Teuchos::null) {
208  OverlapGraph_ = CreateOverlapGraph(Graph_, LevelOverlap_);
209  }
210 }
211 
212 //==============================================================================
213 template<class LocalOrdinal, class GlobalOrdinal, class Node>
215  size_t NumIn, NumL, NumU;
216  bool DiagFound;
217 
219 
220  L_Graph_ = Teuchos::rcp( new TpetraCrsGraphType(OverlapGraph_->getRowMap(), OverlapGraph_->getRowMap(), 0));
221  U_Graph_ = Teuchos::rcp( new TpetraCrsGraphType(OverlapGraph_->getRowMap(), OverlapGraph_->getRowMap(), 0));
222 
223 
224  // Get Maximum Row length
225  int MaxNumIndices = OverlapGraph_->getNodeMaxNumRowEntries();
226 
227  Teuchos::Array<LocalOrdinal> L(MaxNumIndices);
228  Teuchos::Array<LocalOrdinal> U(MaxNumIndices);
229 
230  // First we copy the user's graph into L and U, regardless of fill level
231 
232  int NumMyRows = OverlapGraph_->getRowMap()->getNodeNumElements();
233  NumMyDiagonals_ = 0;
234 
235  for (int i=0; i< NumMyRows; i++) {
236 
237  Teuchos::ArrayView<const LocalOrdinal> my_indices;
238  OverlapGraph_->getLocalRowView(i,my_indices);
239 
240  // Split into L and U (we don't assume that indices are ordered).
241 
242  NumL = 0;
243  NumU = 0;
244  DiagFound = false;
245  NumIn = my_indices.size();
246 
247  for (size_t j=0; j< NumIn; j++) {
248  LocalOrdinal k = my_indices[j];
249 
250  if (k<NumMyRows) { // Ignore column elements that are not in the square matrix
251 
252  if (k==i) DiagFound = true;
253 
254  else if (k < i) {
255  L[NumL] = k;
256  NumL++;
257  }
258  else {
259  U[NumU] = k;
260  NumU++;
261  }
262  }
263  }
264 
265  // Check in things for this row of L and U
266 
267  if (DiagFound) ++NumMyDiagonals_;
268  if (NumL) {
269  Teuchos::ArrayView<LocalOrdinal> Lview(&L[0], NumL);
270  L_Graph_->insertLocalIndices(i, Lview );
271  }
272  if (NumU) {
273  Teuchos::ArrayView<LocalOrdinal> Uview(&U[0], NumU);
274  U_Graph_->insertLocalIndices(i, Uview );
275  }
276  }
277 
278  if (LevelFill_ > 0) {
279 
280  // Complete Fill steps
281  Teuchos::RCP<const TpetraMapType> L_DomainMap = OverlapGraph_->getRowMap();
282  Teuchos::RCP<const TpetraMapType> L_RangeMap = Graph_->getRangeMap();
283  Teuchos::RCP<const TpetraMapType> U_DomainMap = Graph_->getDomainMap();
284  Teuchos::RCP<const TpetraMapType> U_RangeMap = OverlapGraph_->getRowMap();
285  Teuchos::RCP<Teuchos::ParameterList> params = Teuchos::rcp(new Teuchos::ParameterList());
286  params->set("Optimize Storage",false);
287  L_Graph_->fillComplete(L_DomainMap, L_RangeMap, params);
288  U_Graph_->fillComplete(U_DomainMap, U_RangeMap, params);
289  L_Graph_->resumeFill();
290  U_Graph_->resumeFill();
291 
292  // At this point L_Graph and U_Graph are filled with the pattern of input graph,
293  // sorted and have redundant indices (if any) removed. Indices are zero based.
294  // LevelFill is greater than zero, so continue...
295 
296  int MaxRC = NumMyRows;
297  std::vector<std::vector<int> > Levels(MaxRC);
298  std::vector<int> LinkList(MaxRC);
299  std::vector<int> CurrentLevel(MaxRC);
300  Teuchos::Array<LocalOrdinal> CurrentRow(MaxRC+1);
301  std::vector<int> LevelsRowU(MaxRC);
302 
303  for (int i=0; i<NumMyRows; i++)
304  {
305  int First, Next;
306 
307  // copy column indices of row into workspace and sort them
308 
309  size_t LenL = L_Graph_->getNumEntriesInLocalRow(i);
310  size_t LenU = U_Graph_->getNumEntriesInLocalRow(i);
311  size_t Len = LenL + LenU + 1;
312 
313  CurrentRow.resize(Len);
314 
315  L_Graph_->getLocalRowCopy(i, CurrentRow(), LenL); // Get L Indices
316  CurrentRow[LenL] = i; // Put in Diagonal
317  if (LenU > 0) {
318  Teuchos::ArrayView<LocalOrdinal> URowView(&CurrentRow[LenL+1], LenU);
319  // Get U Indices
320  U_Graph_->getLocalRowCopy(i, URowView, LenU);
321  }
322 
323  // Construct linked list for current row
324 
325  for (size_t j=0; j<Len-1; j++) {
326  LinkList[CurrentRow[j]] = CurrentRow[j+1];
327  CurrentLevel[CurrentRow[j]] = 0;
328  }
329 
330  LinkList[CurrentRow[Len-1]] = NumMyRows;
331  CurrentLevel[CurrentRow[Len-1]] = 0;
332 
333  // Merge List with rows in U
334 
335  First = CurrentRow[0];
336  Next = First;
337  while (Next < i)
338  {
339  int PrevInList = Next;
340  int NextInList = LinkList[Next];
341  int RowU = Next;
342  // Get Indices for this row of U
343  Teuchos::ArrayView<const LocalOrdinal> IndicesU;
344  U_Graph_->getLocalRowView(RowU,IndicesU);
345  int LengthRowU = IndicesU.size();
346 
347  int ii;
348 
349  // Scan RowU
350 
351  for (ii=0; ii<LengthRowU; /*nop*/)
352  {
353  int CurInList = IndicesU[ii];
354  if (CurInList < NextInList)
355  {
356  // new fill-in
357  int NewLevel = CurrentLevel[RowU] + Levels[RowU][ii+1] + 1;
358  if (NewLevel <= LevelFill_)
359  {
360  LinkList[PrevInList] = CurInList;
361  LinkList[CurInList] = NextInList;
362  PrevInList = CurInList;
363  CurrentLevel[CurInList] = NewLevel;
364  }
365  ii++;
366  }
367  else if (CurInList == NextInList)
368  {
369  PrevInList = NextInList;
370  NextInList = LinkList[PrevInList];
371  int NewLevel = CurrentLevel[RowU] + Levels[RowU][ii+1] + 1;
372  CurrentLevel[CurInList] = std::min(CurrentLevel[CurInList], NewLevel);
373  ii++;
374  }
375  else // (CurInList > NextInList)
376  {
377  PrevInList = NextInList;
378  NextInList = LinkList[PrevInList];
379  }
380  }
381  Next = LinkList[Next];
382  }
383 
384  // Put pattern into L and U
385 
386  CurrentRow.resize(0);
387 
388  Next = First;
389 
390  // Lower
391 
392  while (Next < i) {
393  CurrentRow.push_back(Next);
394  Next = LinkList[Next];
395  }
396 
397  L_Graph_->removeLocalIndices(i); // Delete current set of Indices
398  if (CurrentRow.size() > 0) {
399  L_Graph_->insertLocalIndices(i, CurrentRow());
400  }
401 
402  // Diagonal
403 
404  TEUCHOS_TEST_FOR_EXCEPTION(Next != i, std::runtime_error,
405  "Ifpack2::IlukGraph::constructFilledGraph: FATAL: U has zero diagonal")
406 
407  LevelsRowU[0] = CurrentLevel[Next];
408  Next = LinkList[Next];
409 
410  // Upper
411 
412  CurrentRow.resize(0);
413  LenU = 0;
414 
415  while (Next < NumMyRows) {
416  LevelsRowU[LenU+1] = CurrentLevel[Next];
417  CurrentRow.push_back(Next);
418  ++LenU;
419  Next = LinkList[Next];
420  }
421 
422  U_Graph_->removeLocalIndices(i); // Delete current set of Indices
423  if (LenU > 0) {
424  U_Graph_->insertLocalIndices(i, CurrentRow());
425  }
426 
427  // Allocate and fill Level info for this row
428  Levels[i] = std::vector<int>(LenU+1);
429  for (size_t jj=0; jj<LenU+1; jj++) {
430  Levels[i][jj] = LevelsRowU[jj];
431  }
432  }
433  }
434 
435  // Complete Fill steps
436  Teuchos::RCP<const TpetraMapType> L_DomainMap = OverlapGraph_->getRowMap();
437  Teuchos::RCP<const TpetraMapType> L_RangeMap = Graph_->getRangeMap();
438  Teuchos::RCP<const TpetraMapType> U_DomainMap = Graph_->getDomainMap();
439  Teuchos::RCP<const TpetraMapType> U_RangeMap = OverlapGraph_->getRowMap();
440  L_Graph_->fillComplete(L_DomainMap, L_RangeMap);//DoOptimizeStorage is default here...
441  U_Graph_->fillComplete(U_DomainMap, U_RangeMap);//DoOptimizeStorage is default here...
442 
443  Teuchos::reduceAll<int,size_t>(*(L_DomainMap->getComm()), Teuchos::REDUCE_SUM, 1, &NumMyDiagonals_, &NumGlobalDiagonals_);
444 }
445 
446 } // namespace Ifpack2
447 
448 #endif /* IFPACK2_ILUK_GRAPH_HPP */
size_t getNumGlobalDiagonals() const
Returns the global number of diagonals in the ILU(k) graph.
Definition: Ifpack2_IlukGraph.hpp:143
Teuchos::RCP< const Tpetra::CrsGraph< LocalOrdinal, GlobalOrdinal, Node > > CreateOverlapGraph(const Teuchos::RCP< const Tpetra::CrsGraph< LocalOrdinal, GlobalOrdinal, Node > > &input_graph, int OverlapLevel)
Constructs an overlapped graph for use with Ifpack2 preconditioners.
Definition: Ifpack2_CreateOverlapGraph.hpp:60
int getLevelFill() const
Returns the level of fill used to construct this graph.
Definition: Ifpack2_IlukGraph.hpp:128
void getParameter(const Teuchos::ParameterList &params, 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
void constructFilledGraph()
This method performs the actual construction of the graph.
Definition: Ifpack2_IlukGraph.hpp:214
void constructOverlapGraph()
Does the actual construction of the overlap matrix graph.
Definition: Ifpack2_IlukGraph.hpp:205
const Teuchos::RCP< Tpetra::CrsGraph< LocalOrdinal, GlobalOrdinal, Node > > & getU_Graph() const
Returns the graph of upper triangle of the ILU(k) graph as a Tpetra::CrsGraph.
Definition: Ifpack2_IlukGraph.hpp:137
const Teuchos::RCP< Tpetra::CrsGraph< LocalOrdinal, GlobalOrdinal, Node > > & getL_Graph() const
Returns the graph of lower triangle of the ILU(k) graph as a Tpetra::CrsGraph.
Definition: Ifpack2_IlukGraph.hpp:134
A class for constructing level filled graphs for use with ILU(k) class preconditioners.
Definition: Ifpack2_IlukGraph.hpp:83
const Teuchos::RCP< Tpetra::CrsGraph< LocalOrdinal, GlobalOrdinal, Node > > & getOverlapGraph() const
Returns the the overlapped graph.
Definition: Ifpack2_IlukGraph.hpp:140
int getLevelOverlap() const
Returns the level of overlap used to construct this graph.
Definition: Ifpack2_IlukGraph.hpp:131
IlukGraph(const Teuchos::RCP< const Tpetra::CrsGraph< LocalOrdinal, GlobalOrdinal, Node > > &Graph_in, int LevelFill_in, int LevelOverlap_in)
IlukGraph constuctor.
Definition: Ifpack2_IlukGraph.hpp:161
void setParameters(const Teuchos::ParameterList &parameterlist)
Set parameters using Teuchos::ParameterList object.
Definition: Ifpack2_IlukGraph.hpp:197
Ifpack2::Container: a pure virtual class for creating and solving local linear problems.
Definition: Ifpack2_AdditiveSchwarz_decl.hpp:67
virtual ~IlukGraph()
IlukGraph Destructor.
Definition: Ifpack2_IlukGraph.hpp:191