IFPACK  Development
 All Classes Files Functions Variables Enumerations Friends Pages
Ifpack_IlukGraph.cpp
1 /*@HEADER
2 // ***********************************************************************
3 //
4 // Ifpack: Object-Oriented Algebraic Preconditioner Package
5 // Copyright (2002) 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 #include "Ifpack_IlukGraph.h"
44 #include "Epetra_Object.h"
45 #include "Epetra_Comm.h"
46 #include "Epetra_Import.h"
47 
48 #include <Teuchos_ParameterList.hpp>
49 #include <ifp_parameters.h>
50 
51 //==============================================================================
52 Ifpack_IlukGraph::Ifpack_IlukGraph(const Epetra_CrsGraph & Graph_in, int LevelFill_in, int LevelOverlap_in)
53  : Graph_(Graph_in),
54  DomainMap_(Graph_in.DomainMap()),
55  RangeMap_(Graph_in.RangeMap()),
56  Comm_(Graph_in.Comm()),
57  LevelFill_(LevelFill_in),
58  LevelOverlap_(LevelOverlap_in),
59  IndexBase_(Graph_in.IndexBase()),
60  NumGlobalRows_(Graph_in.NumGlobalRows()),
61  NumGlobalCols_(Graph_in.NumGlobalCols()),
62  NumGlobalBlockRows_(Graph_in.NumGlobalBlockRows()),
63  NumGlobalBlockCols_(Graph_in.NumGlobalBlockCols()),
64  NumGlobalBlockDiagonals_(0),
65  NumGlobalNonzeros_(0),
66  NumGlobalEntries_(0),
67  NumMyBlockRows_(Graph_in.NumMyBlockRows()),
68  NumMyBlockCols_(Graph_in.NumMyBlockCols()),
69  NumMyRows_(Graph_in.NumMyRows()),
70  NumMyCols_(Graph_in.NumMyCols()),
71  NumMyBlockDiagonals_(0),
72  NumMyNonzeros_(0),
73  NumMyEntries_(0)
74 {
75 }
76 
77 //==============================================================================
79  : Graph_(Graph_in.Graph_),
80  DomainMap_(Graph_in.DomainMap()),
81  RangeMap_(Graph_in.RangeMap()),
82  Comm_(Graph_in.Comm()),
83  OverlapGraph_(Graph_in.OverlapGraph_),
84  OverlapRowMap_(Graph_in.OverlapRowMap_),
85  OverlapImporter_(Graph_in.OverlapImporter_),
86  LevelFill_(Graph_in.LevelFill_),
87  LevelOverlap_(Graph_in.LevelOverlap_),
88  IndexBase_(Graph_in.IndexBase_),
89  NumGlobalRows_(Graph_in.NumGlobalRows_),
90  NumGlobalCols_(Graph_in.NumGlobalCols_),
91  NumGlobalBlockRows_(Graph_in.NumGlobalBlockRows_),
92  NumGlobalBlockCols_(Graph_in.NumGlobalBlockCols_),
93  NumGlobalBlockDiagonals_(Graph_in.NumGlobalBlockDiagonals_),
94  NumGlobalNonzeros_(Graph_in.NumGlobalNonzeros_),
95  NumGlobalEntries_(Graph_in.NumGlobalEntries_),
96  NumMyBlockRows_(Graph_in.NumMyBlockRows_),
97  NumMyBlockCols_(Graph_in.NumMyBlockCols_),
98  NumMyRows_(Graph_in.NumMyRows_),
99  NumMyCols_(Graph_in.NumMyCols_),
100  NumMyBlockDiagonals_(Graph_in.NumMyBlockDiagonals_),
101  NumMyNonzeros_(Graph_in.NumMyNonzeros_),
102  NumMyEntries_(Graph_in.NumMyEntries_)
103 {
104  Epetra_CrsGraph & L_Graph_In = Graph_in.L_Graph();
105  Epetra_CrsGraph & U_Graph_In = Graph_in.U_Graph();
106  L_Graph_ = Teuchos::rcp( new Epetra_CrsGraph(L_Graph_In) );
107  U_Graph_ = Teuchos::rcp( new Epetra_CrsGraph(U_Graph_In) );
108 }
109 
110 //==============================================================================
112 {
113 }
114 
115 //==============================================================================
116 int Ifpack_IlukGraph::SetParameters(const Teuchos::ParameterList& parameterlist,
117  bool cerr_warning_if_unused)
118 {
119  Ifpack::param_struct params;
120  params.int_params[Ifpack::level_fill-FIRST_INT_PARAM] = LevelFill_;
121  params.int_params[Ifpack::level_overlap-FIRST_INT_PARAM] = LevelOverlap_;
122 
123  Ifpack::set_parameters(parameterlist, params, cerr_warning_if_unused);
124 
125  LevelFill_ = params.int_params[Ifpack::level_fill-FIRST_INT_PARAM];
126  LevelOverlap_ = params.int_params[Ifpack::level_overlap-FIRST_INT_PARAM];
127  return(0);
128 }
129 
130 //==============================================================================
132 
133  OverlapGraph_ = Teuchos::rcp( (Epetra_CrsGraph *) &Graph_, false );
134  OverlapRowMap_ = Teuchos::rcp( (Epetra_BlockMap *) &Graph_.RowMap(), false );
135 
136  if (LevelOverlap_==0 || !Graph_.DomainMap().DistributedGlobal()) return(0); // Nothing to do
137 
138  Teuchos::RefCountPtr<Epetra_CrsGraph> OldGraph;
139  Teuchos::RefCountPtr<Epetra_BlockMap> OldRowMap;
140  Epetra_BlockMap * DomainMap_tmp = (Epetra_BlockMap *) &Graph_.DomainMap();
141  Epetra_BlockMap * RangeMap_tmp = (Epetra_BlockMap *) &Graph_.RangeMap();
142  for (int level=1; level <= LevelOverlap_; level++) {
143  OldGraph = OverlapGraph_;
144  OldRowMap = OverlapRowMap_;
145 
146  OverlapImporter_ = Teuchos::rcp( (Epetra_Import *) OldGraph->Importer(), false );
147  OverlapRowMap_ = Teuchos::rcp( new Epetra_BlockMap(OverlapImporter_->TargetMap()) );
148 
149 
150  if (level<LevelOverlap_)
151  OverlapGraph_ = Teuchos::rcp( new Epetra_CrsGraph(Copy, *OverlapRowMap_, 0) );
152  else
153  // On last iteration, we want to filter out all columns except those that correspond
154  // to rows in the graph. This assures that our matrix is square
155  OverlapGraph_ = Teuchos::rcp( new Epetra_CrsGraph(Copy, *OverlapRowMap_, *OverlapRowMap_, 0) );
156 
157  EPETRA_CHK_ERR(OverlapGraph_->Import( Graph_, *OverlapImporter_, Insert));
158  if (level<LevelOverlap_) {
159  EPETRA_CHK_ERR(OverlapGraph_->FillComplete(*DomainMap_tmp, *RangeMap_tmp));
160  }
161  else {
162  // Copy last OverlapImporter because we will use it later
163  OverlapImporter_ = Teuchos::rcp( new Epetra_Import(*OverlapRowMap_, *DomainMap_tmp) );
164  EPETRA_CHK_ERR(OverlapGraph_->FillComplete(*DomainMap_tmp, *RangeMap_tmp));
165  }
166  }
167 
168  NumMyBlockRows_ = OverlapGraph_->NumMyBlockRows();
169  NumMyBlockCols_ = OverlapGraph_->NumMyBlockCols();
170  NumMyRows_ = OverlapGraph_->NumMyRows();
171  NumMyCols_ = OverlapGraph_->NumMyCols();
172 
173  return(0);
174 }
175 
176 //==============================================================================
178  int ierr = 0;
179  int i, j;
180  int * In=0;
181  int NumIn, NumL, NumU;
182  bool DiagFound;
183 
184 
185  EPETRA_CHK_ERR(ConstructOverlapGraph());
186 
187  L_Graph_ = Teuchos::rcp( new Epetra_CrsGraph(Copy, OverlapGraph_->RowMap(), OverlapGraph_->RowMap(), 0) );
188  U_Graph_ = Teuchos::rcp( new Epetra_CrsGraph(Copy, OverlapGraph_->RowMap(), OverlapGraph_->RowMap(), 0));
189 
190 
191  // Get Maximun Row length
192  int MaxNumIndices = OverlapGraph_->MaxNumIndices();
193 
194  vector<int> L(MaxNumIndices);
195  vector<int> U(MaxNumIndices);
196 
197  // First we copy the user's graph into L and U, regardless of fill level
198 
199  for (i=0; i< NumMyBlockRows_; i++) {
200 
201 
202  OverlapGraph_->ExtractMyRowView(i, NumIn, In); // Get Indices
203 
204 
205  // Split into L and U (we don't assume that indices are ordered).
206 
207  NumL = 0;
208  NumU = 0;
209  DiagFound = false;
210 
211  for (j=0; j< NumIn; j++) {
212  int k = In[j];
213 
214  if (k<NumMyBlockRows_) { // Ignore column elements that are not in the square matrix
215 
216  if (k==i) DiagFound = true;
217 
218  else if (k < i) {
219  L[NumL] = k;
220  NumL++;
221  }
222  else {
223  U[NumU] = k;
224  NumU++;
225  }
226  }
227  }
228 
229  // Check in things for this row of L and U
230 
231  if (DiagFound) NumMyBlockDiagonals_++;
232  if (NumL) L_Graph_->InsertMyIndices(i, NumL, &L[0]);
233  if (NumU) U_Graph_->InsertMyIndices(i, NumU, &U[0]);
234 
235  }
236 
237  if (LevelFill_ > 0) {
238 
239  // Complete Fill steps
240  Epetra_BlockMap * L_DomainMap = (Epetra_BlockMap *) &OverlapGraph_->RowMap();
241  Epetra_BlockMap * L_RangeMap = (Epetra_BlockMap *) &Graph_.RangeMap();
242  Epetra_BlockMap * U_DomainMap = (Epetra_BlockMap *) &Graph_.DomainMap();
243  Epetra_BlockMap * U_RangeMap = (Epetra_BlockMap *) &OverlapGraph_->RowMap();
244  EPETRA_CHK_ERR(L_Graph_->FillComplete(*L_DomainMap, *L_RangeMap));
245  EPETRA_CHK_ERR(U_Graph_->FillComplete(*U_DomainMap, *U_RangeMap));
246 
247  // At this point L_Graph and U_Graph are filled with the pattern of input graph,
248  // sorted and have redundant indices (if any) removed. Indices are zero based.
249  // LevelFill is greater than zero, so continue...
250 
251  int MaxRC = NumMyBlockRows_;
252  vector<vector<int> > Levels(MaxRC);
253  vector<int> LinkList(MaxRC);
254  vector<int> CurrentLevel(MaxRC);
255  vector<int> CurrentRow(MaxRC);
256  vector<int> LevelsRowU(MaxRC);
257 
258  for (i=0; i<NumMyBlockRows_; i++)
259  {
260  int First, Next;
261 
262  // copy column indices of row into workspace and sort them
263 
264  int LenL = L_Graph_->NumMyIndices(i);
265  int LenU = U_Graph_->NumMyIndices(i);
266  int Len = LenL + LenU + 1;
267 
268  EPETRA_CHK_ERR(L_Graph_->ExtractMyRowCopy(i, LenL, LenL, &CurrentRow[0])); // Get L Indices
269  CurrentRow[LenL] = i; // Put in Diagonal
270  //EPETRA_CHK_ERR(U_Graph_->ExtractMyRowCopy(i, LenU, LenU, CurrentRow+LenL+1)); // Get U Indices
271  int ierr1 = 0;
272  if (LenU) {
273  // Get U Indices
274  ierr1 = U_Graph_->ExtractMyRowCopy(i, LenU, LenU, &CurrentRow[LenL+1]);
275  }
276  if (ierr1!=0) {
277  cout << "ierr1 = "<< ierr1 << endl;
278  cout << "i = " << i << endl;
279  cout << "NumMyBlockRows_ = " << U_Graph_->NumMyBlockRows() << endl;
280  }
281 
282  // Construct linked list for current row
283 
284  for (j=0; j<Len-1; j++) {
285  LinkList[CurrentRow[j]] = CurrentRow[j+1];
286  CurrentLevel[CurrentRow[j]] = 0;
287  }
288 
289  LinkList[CurrentRow[Len-1]] = NumMyBlockRows_;
290  CurrentLevel[CurrentRow[Len-1]] = 0;
291 
292  // Merge List with rows in U
293 
294  First = CurrentRow[0];
295  Next = First;
296  while (Next < i)
297  {
298  int PrevInList = Next;
299  int NextInList = LinkList[Next];
300  int RowU = Next;
301  int LengthRowU;
302  int * IndicesU;
303  // Get Indices for this row of U
304  EPETRA_CHK_ERR(U_Graph_->ExtractMyRowView(RowU, LengthRowU, IndicesU));
305 
306  int ii;
307 
308  // Scan RowU
309 
310  for (ii=0; ii<LengthRowU; /*nop*/)
311  {
312  int CurInList = IndicesU[ii];
313  if (CurInList < NextInList)
314  {
315  // new fill-in
316  int NewLevel = CurrentLevel[RowU] + Levels[RowU][ii+1] + 1;
317  if (NewLevel <= LevelFill_)
318  {
319  LinkList[PrevInList] = CurInList;
320  LinkList[CurInList] = NextInList;
321  PrevInList = CurInList;
322  CurrentLevel[CurInList] = NewLevel;
323  }
324  ii++;
325  }
326  else if (CurInList == NextInList)
327  {
328  PrevInList = NextInList;
329  NextInList = LinkList[PrevInList];
330  int NewLevel = CurrentLevel[RowU] + Levels[RowU][ii+1] + 1;
331  CurrentLevel[CurInList] = EPETRA_MIN(CurrentLevel[CurInList], NewLevel);
332  ii++;
333  }
334  else // (CurInList > NextInList)
335  {
336  PrevInList = NextInList;
337  NextInList = LinkList[PrevInList];
338  }
339  }
340  Next = LinkList[Next];
341  }
342 
343  // Put pattern into L and U
344 
345  LenL = 0;
346 
347  Next = First;
348 
349  // Lower
350 
351  while (Next < i) {
352  CurrentRow[LenL++] = Next;
353  Next = LinkList[Next];
354  }
355 
356  EPETRA_CHK_ERR(L_Graph_->RemoveMyIndices(i)); // Delete current set of Indices
357  int ierr11 = L_Graph_->InsertMyIndices(i, LenL, &CurrentRow[0]);
358  if (ierr11 < 0) EPETRA_CHK_ERR(ierr1);
359 
360  // Diagonal
361 
362  if (Next != i) return(-2); // Fatal: U has zero diagonal.
363  else {
364  LevelsRowU[0] = CurrentLevel[Next];
365  Next = LinkList[Next];
366  }
367 
368  // Upper
369 
370  LenU = 0;
371 
372  while (Next < NumMyBlockRows_) // Should be "Next < NumMyBlockRows_"?
373  {
374  LevelsRowU[LenU+1] = CurrentLevel[Next];
375  CurrentRow[LenU++] = Next;
376  Next = LinkList[Next];
377  }
378 
379  EPETRA_CHK_ERR(U_Graph_->RemoveMyIndices(i)); // Delete current set of Indices
380  int ierr2 = U_Graph_->InsertMyIndices(i, LenU, &CurrentRow[0]);
381  if (ierr2<0) EPETRA_CHK_ERR(ierr2);
382 
383  // Allocate and fill Level info for this row
384  Levels[i] = vector<int>(LenU+1);
385  for (int jj=0; jj<LenU+1; jj++) Levels[i][jj] = LevelsRowU[jj];
386 
387  }
388  }
389 
390  // Complete Fill steps
391  Epetra_BlockMap L_DomainMap = (Epetra_BlockMap) OverlapGraph_->RowMap();
392  Epetra_BlockMap L_RangeMap = (Epetra_BlockMap) Graph_.RangeMap();
393  Epetra_BlockMap U_DomainMap = (Epetra_BlockMap) Graph_.DomainMap();
394  Epetra_BlockMap U_RangeMap = (Epetra_BlockMap) OverlapGraph_->RowMap();
395  EPETRA_CHK_ERR(L_Graph_->FillComplete(L_DomainMap, L_RangeMap));
396  EPETRA_CHK_ERR(U_Graph_->FillComplete(U_DomainMap, U_RangeMap));
397 
398  // Optimize graph storage
399 
400  EPETRA_CHK_ERR(L_Graph_->OptimizeStorage());
401  EPETRA_CHK_ERR(U_Graph_->OptimizeStorage());
402 
403  // Compute global quantities
404 
405  NumGlobalBlockDiagonals_ = 0;
406 
407  EPETRA_CHK_ERR(L_Graph_->Comm().SumAll(&NumMyBlockDiagonals_, &NumGlobalBlockDiagonals_, 1));
408 
409  NumGlobalNonzeros_ = L_Graph_->NumGlobalNonzeros()+U_Graph_->NumGlobalNonzeros();
410  NumMyNonzeros_ = L_Graph_->NumMyNonzeros()+U_Graph_->NumMyNonzeros();
411  NumGlobalEntries_ = L_Graph_->NumGlobalEntries()+U_Graph_->NumGlobalEntries();
412  NumMyEntries_ = L_Graph_->NumMyEntries()+U_Graph_->NumMyEntries();
413  return(ierr);
414 }
415 //==========================================================================
416 
417 // Non-member functions
418 
419 ostream& operator << (ostream& os, const Ifpack_IlukGraph& A)
420 {
421 /* Epetra_fmtflags olda = os.setf(ios::right,ios::adjustfield);
422  Epetra_fmtflags oldf = os.setf(ios::scientific,ios::floatfield);
423  int oldp = os.precision(12); */
424  int LevelFill = A.LevelFill();
425  Epetra_CrsGraph & L = (Epetra_CrsGraph &) A.L_Graph();
426  Epetra_CrsGraph & U = (Epetra_CrsGraph &) A.U_Graph();
427  os.width(14);
428  os << " Level of Fill = "; os << LevelFill;
429  os << endl;
430 
431  os.width(14);
432  os << " Graph of L = ";
433  os << endl;
434  os << L; // Let Epetra_CrsGraph handle the rest.
435 
436  os.width(14);
437  os << " Graph of U = ";
438  os << endl;
439  os << U; // Let Epetra_CrsGraph handle the rest.
440 
441  // Reset os flags
442 
443 /* os.setf(olda,ios::adjustfield);
444  os.setf(oldf,ios::floatfield);
445  os.precision(oldp); */
446 
447  return os;
448 }
Ifpack_IlukGraph(const Epetra_CrsGraph &Graph_in, int LevelFill_in, int LevelOverlap_in)
Ifpack_IlukGraph constuctor.
virtual Epetra_CrsGraph & L_Graph()
Returns the graph of lower triangle of the ILU(k) graph as a Epetra_CrsGraph.
int SetParameters(const Teuchos::ParameterList &parameterlist, bool cerr_warning_if_unused=false)
Set parameters using Teuchos::ParameterList object.
Ifpack_IlukGraph: A class for constructing level filled graphs for use with ILU(k) class precondition...
virtual int LevelFill() const
Returns the level of fill used to construct this graph.
virtual int ConstructOverlapGraph()
Does the actual construction of the overlap matrix graph.
virtual Epetra_CrsGraph & U_Graph()
Returns the graph of upper triangle of the ILU(k) graph as a Epetra_CrsGraph.
virtual ~Ifpack_IlukGraph()
Ifpack_IlukGraph Destructor.
virtual int ConstructFilledGraph()
Does the actual construction of the graph.