Ifpack2 Templated Preconditioning Package  Version 1.0
Ifpack2_ConstructLevelFillGraph.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_CONSTRUCTLEVELFILLGRAPH_HPP
44 #define IFPACK2_CONSTRUCTLEVELFILLGRAPH_HPP
45 
46 #include "Ifpack2_ConfigDefs.hpp"
47 #include <Teuchos_Describable.hpp>
48 #include "Tpetra_RowGraph.hpp"
49 #include "Tpetra_CrsGraph.hpp"
50 #include "Teuchos_RefCountPtr.hpp"
51 
52 namespace Teuchos {
53  class ParameterList;
54 }
55 
56 namespace Ifpack2 {
57 
58 
60 
83  template<class LocalOrdinal, class GlobalOrdinal = LocalOrdinal>
84  void ConstructLevelFillGraph(const RowGraph<LocalOrdinal,GlobalOrdinal> & userGraph,
85  Teuchos::RCP<CrsGraph<LocalOrdinal,GlobalOrdinal>> graphL, Teuchos::RCP<CrsGraph<LocalOrdinal,GlobalOrdinal>> graphU) {
86 
87  LocalOrdinal i, j;
88  LocalOrdinal numIn, numL, numU;
89  LocalOrdinal numMyDiagonals = 0;
90  bool diagFound;
91 
92 
93 
94  graphL = Teuchos::rcp(new CrsGraph(userGraph.getRowMap(), userGraph.getRowMap(), 0));
95  graphU = Teuchos::rcp(new CrsGraph(userGraph.getRowMap(), userGraph.getRowMap(), 0));
96 
97 
98  // Get Maximun Row length
99  int MaxNumIndices = userGraph.getMaxNumIndices();
100 
101  Teuchos::ArrayView<LocalOrdinal> userGraphRow;
102  vector<LocalOrdinal> L(MaxNumIndices);
103  vector<LocalOrdinal> U(MaxNumIndices);
104  LocalOrdinal * pL = &L[0];
105  LocalOrdinal * pU = &U[0];
106 
107  LocalOrdinal numMyRows = userGraph.getNumMyRows();
108 
109  // First we copy the user's graph into L and U, regardless of fill level
110 
111  for (i=0; i< numMyRows; ++i) {
112 
113 
114  userGraph.extractMyRowConstView(i, userGraphRow); // Get Indices
115 
116 
117  // Split into L and U (we don't assume that indices are ordered).
118 
119  numL = 0;
120  numU = 0;
121  diagFound = false;
122  numIn = userGraphRow.size();
123 
124  for (j=0; j< numIn; ++j) {
125  int k = userGraphRow[j];
126 
127  if (k<numMyRows) { // Ignore column elements that are not in the square matrix
128  if (k==i) {DiagFound = true;}
129  else if (k < i) {pL[numL++] = k;}
130  else {pU[numU++] = k;}
131  }
132  }
133 
134  // Check in things for this row of L and U
135 
136  if (diagFound) numMyDiagonals++;
137  if (NumL>0) {graphL->insertMyIndices(i, ArrayView(pL, numL));}
138  if (NumU>0) {graphU->insertMyIndices(i, ArrayView(pU, numU));}
139  }
140 
141  // if (LevelFill_ > 0) {
142  //
143  // // Complete Fill steps
144  // graphL->fillComplete(userGraph.getRowMap(), userGraph.getRangeMap());
145  // graphU->fillComplete(userGraph.getDomainMap(), userGraph.getRowMap());
146  //
147  // // At this point L_Graph and U_Graph are filled with the pattern of input graph,
148  // // sorted and have redundant indices (if any) removed. Indices are zero based.
149  // // LevelFill is greater than zero, so continue...
150  //
151  // vector<vector<LocalOrdinal> > levels(numMyRows);
152  // vector<LocalOrdinal> linkList(numMyRows);
153  // vector<LocalOrdinal> currentLevel(numMyRows);
154  // vector<LocalOrdinal> currentRow(numMyRows);
155  // vector<LocalOrdinal> levelsRowU(numMyRows);
156  //
157  // for (i=0; i<numMyRows; ++i) {
158  // int First, Next;
159  //
160  // // copy column indices of row into workspace and sort them
161  //
162  // int LenL = L_Graph_->NumMyIndices(i);
163  // int LenU = U_Graph_->NumMyIndices(i);
164  // int Len = LenL + LenU + 1;
165  //
166  // EPETRA_CHK_ERR(L_Graph_->ExtractMyRowCopy(i, LenL, LenL, &CurrentRow[0])); // Get L Indices
167  // CurrentRow[LenL] = i; // Put in Diagonal
168  // int ierr1 = 0;
169  // if (LenU) {
170  // // Get U Indices
171  // ierr1 = U_Graph_->ExtractMyRowCopy(i, LenU, LenU, &CurrentRow[LenL+1]);
172  // }
173  // if (ierr1!=0) {
174  // cout << "ierr1 = "<< ierr1 << endl;
175  // cout << "i = " << i << endl;
176  // cout << "NumMyBlockRows_ = " << graphU->NumMyBlockRows() << endl;
177  // }
178  //
179  // // Construct linked list for current row
180  //
181  // for (j=0; j<Len-1; j++) {
182  // LinkList[CurrentRow[j]] = CurrentRow[j+1];
183  // CurrentLevel[CurrentRow[j]] = 0;
184  // }
185  //
186  // LinkList[CurrentRow[Len-1]] = NumMyBlockRows_;
187  // CurrentLevel[CurrentRow[Len-1]] = 0;
188  //
189  // // Merge List with rows in U
190  //
191  // First = CurrentRow[0];
192  // Next = First;
193  // while (Next < i)
194  // {
195  // int PrevInList = Next;
196  // int NextInList = LinkList[Next];
197  // int RowU = Next;
198  // int LengthRowU;
199  // int * IndicesU;
200  // // Get Indices for this row of U
201  // EPETRA_CHK_ERR(U_Graph_->ExtractMyRowView(RowU, LengthRowU, IndicesU));
202  //
203  // int ii;
204  //
205  // // Scan RowU
206  //
207  // for (ii=0; ii<LengthRowU; /*nop*/) {
208  // int CurInList = IndicesU[ii];
209  // if (CurInList < NextInList) {
210  // // new fill-in
211  // int NewLevel = CurrentLevel[RowU] + Levels[RowU][ii+1] + 1;
212  // if (NewLevel <= LevelFill_)
213  // {
214  // LinkList[PrevInList] = CurInList;
215  // LinkList[CurInList] = NextInList;
216  // PrevInList = CurInList;
217  // CurrentLevel[CurInList] = NewLevel;
218  // }
219  // ii++;
220  // }
221  // else if (CurInList == NextInList)
222  // {
223  // PrevInList = NextInList;
224  // NextInList = LinkList[PrevInList];
225  // int NewLevel = CurrentLevel[RowU] + Levels[RowU][ii+1] + 1;
226  // CurrentLevel[CurInList] = EPETRA_MIN(CurrentLevel[CurInList], NewLevel);
227  // ii++;
228  // }
229  // else // (CurInList > NextInList)
230  // {
231  // PrevInList = NextInList;
232  // NextInList = LinkList[PrevInList];
233  // }
234  // }
235  // Next = LinkList[Next];
236  // }
237  //
238  // // Put pattern into L and U
239  //
240  // LenL = 0;
241  //
242  // Next = First;
243  //
244  // // Lower
245  //
246  // while (Next < i) {
247  // CurrentRow[LenL++] = Next;
248  // Next = LinkList[Next];
249  // }
250  //
251  // EPETRA_CHK_ERR(L_Graph_->RemoveMyIndices(i)); // Delete current set of Indices
252  // int ierr11 = L_Graph_->InsertMyIndices(i, LenL, &CurrentRow[0]);
253  // if (ierr11 < 0) EPETRA_CHK_ERR(ierr1);
254  //
255  // // Diagonal
256  //
257  // if (Next != i) return(-2); // Fatal: U has zero diagonal.
258  // else {
259  // LevelsRowU[0] = CurrentLevel[Next];
260  // Next = LinkList[Next];
261  // }
262  //
263  // // Upper
264  //
265  // LenU = 0;
266  //
267  // while (Next < NumMyBlockRows_) // Should be "Next < NumMyBlockRows_"?
268  // {
269  // LevelsRowU[LenU+1] = CurrentLevel[Next];
270  // CurrentRow[LenU++] = Next;
271  // Next = LinkList[Next];
272  // }
273  //
274  // EPETRA_CHK_ERR(U_Graph_->RemoveMyIndices(i)); // Delete current set of Indices
275  // int ierr2 = U_Graph_->InsertMyIndices(i, LenU, &CurrentRow[0]);
276  // if (ierr2<0) EPETRA_CHK_ERR(ierr2);
277  //
278  // // Allocate and fill Level info for this row
279  // Levels[i] = vector<int>(LenU+1);
280  // for (int jj=0; jj<LenU+1; jj++) Levels[i][jj] = LevelsRowU[jj];
281  //
282  // }
283  // }
284 
285  // Complete Fill steps
286  graphL->fillComplete(userGraph.getRowMap(), userGraph.getRangeMap());
287  graphU->fillComplete(userGraph.getDomainMap(), userGraph.getRowMap());
288 
289 
290  // Optimize graph storage
291 
292  graphL->optimizeStorage();
293  graphU->optimizeStorage();
294 
295  // Compute global quantities
296 
297  GlobalOrdinal numGlobalDiagonals = 0;
298 
299  graphL->getComm().sumAll(&numMyDiagonals, &numGlobalDiagonals, 1));
300 
301  return;
302 }
303 
304 } // namespace Ifpack2
305 
306 #endif /* IFPACK2_CONSTRUCTLEVELFILLGRAPH_HPP */
void ConstructLevelFillGraph(const RowGraph< LocalOrdinal, GlobalOrdinal > &userGraph, Teuchos::RCP< CrsGraph< LocalOrdinal, GlobalOrdinal >> graphL, Teuchos::RCP< CrsGraph< LocalOrdinal, GlobalOrdinal >> graphU)
ConstructLevelFillGraph: A function for constructing level filled graphs for use with ILU(k) class pr...
Definition: Ifpack2_ConstructLevelFillGraph.hpp:84
Definition: Ifpack2_BlockRelaxation_decl.hpp:64
Ifpack2::Container: a pure virtual class for creating and solving local linear problems.
Definition: Ifpack2_AdditiveSchwarz_decl.hpp:67