IFPACK  Development
Ifpack_CrsRiluk.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 #include "Ifpack_CrsRiluk.h"
43 #include "Epetra_Comm.h"
44 #include "Epetra_Map.h"
45 #include "Epetra_CrsGraph.h"
46 #include "Epetra_VbrMatrix.h"
47 #include "Epetra_RowMatrix.h"
48 #include "Epetra_MultiVector.h"
49 
50 #include <Teuchos_ParameterList.hpp>
51 #include <ifp_parameters.h>
52 
53 //==============================================================================
55  : UserMatrixIsVbr_(false),
56  UserMatrixIsCrs_(false),
57  Graph_(Graph_in),
58  Comm_(Graph_in.Comm()),
59  UseTranspose_(false),
60  NumMyDiagonals_(0),
61  Allocated_(false),
62  ValuesInitialized_(false),
63  Factored_(false),
64  RelaxValue_(0.0),
65  Athresh_(0.0),
66  Rthresh_(1.0),
67  Condest_(-1.0),
68  OverlapMode_(Zero)
69 {
70  // Test for non-trivial overlap here so we can use it later.
71  IsOverlapped_ = (Graph_in.LevelOverlap()>0 && Graph_in.DomainMap().DistributedGlobal());
72 }
73 
74 //==============================================================================
76  : UserMatrixIsVbr_(FactoredMatrix.UserMatrixIsVbr_),
77  UserMatrixIsCrs_(FactoredMatrix.UserMatrixIsCrs_),
78  IsOverlapped_(FactoredMatrix.IsOverlapped_),
79  Graph_(FactoredMatrix.Graph_),
80  IlukRowMap_(FactoredMatrix.IlukRowMap_),
81  IlukDomainMap_(FactoredMatrix.IlukDomainMap_),
82  IlukRangeMap_(FactoredMatrix.IlukRangeMap_),
83  Comm_(FactoredMatrix.Comm_),
84  UseTranspose_(FactoredMatrix.UseTranspose_),
85  NumMyDiagonals_(FactoredMatrix.NumMyDiagonals_),
86  Allocated_(FactoredMatrix.Allocated_),
87  ValuesInitialized_(FactoredMatrix.ValuesInitialized_),
88  Factored_(FactoredMatrix.Factored_),
89  RelaxValue_(FactoredMatrix.RelaxValue_),
90  Athresh_(FactoredMatrix.Athresh_),
91  Rthresh_(FactoredMatrix.Rthresh_),
92  Condest_(FactoredMatrix.Condest_),
93  OverlapMode_(FactoredMatrix.OverlapMode_)
94 {
95  L_ = Teuchos::rcp( new Epetra_CrsMatrix(FactoredMatrix.L()) );
96  U_ = Teuchos::rcp( new Epetra_CrsMatrix(FactoredMatrix.U()) );
97  D_ = Teuchos::rcp( new Epetra_Vector(FactoredMatrix.D()) );
98  if (IlukRowMap_!=Teuchos::null) IlukRowMap_ = Teuchos::rcp( new Epetra_Map(*IlukRowMap_) );
99  if (IlukDomainMap_!=Teuchos::null) IlukDomainMap_ = Teuchos::rcp( new Epetra_Map(*IlukDomainMap_) );
100  if (IlukRangeMap_!=Teuchos::null) IlukRangeMap_ = Teuchos::rcp( new Epetra_Map(*IlukRangeMap_) );
101 
102 }
103 //==============================================================================
105 
106  ValuesInitialized_ = false;
107  Factored_ = false;
108  Allocated_ = false;
109 }
110 //==============================================================================
111 int Ifpack_CrsRiluk::AllocateCrs() {
112 
113  // Allocate Epetra_CrsMatrix using ILUK graphs
114  L_ = Teuchos::rcp( new Epetra_CrsMatrix(Copy, Graph_.L_Graph()) );
115  U_ = Teuchos::rcp( new Epetra_CrsMatrix(Copy, Graph_.U_Graph()) );
116  D_ = Teuchos::rcp( new Epetra_Vector(Graph_.L_Graph().RowMap()) );
117  L_Graph_ = Teuchos::null;
118  U_Graph_ = Teuchos::null;
119  SetAllocated(true);
120  return(0);
121 }
122 //==============================================================================
123 int Ifpack_CrsRiluk::AllocateVbr() {
124 
125  // First we need to create a set of Epetra_Maps that has the same number of points as the
126  // Epetra_BlockMaps associated with the Overlap Graph.
127  EPETRA_CHK_ERR(BlockMap2PointMap(Graph_.L_Graph().RowMap(), &IlukRowMap_));
128  EPETRA_CHK_ERR(BlockMap2PointMap(Graph_.U_Graph().DomainMap(), &IlukDomainMap_));
129  EPETRA_CHK_ERR(BlockMap2PointMap(Graph_.L_Graph().RangeMap(), &IlukRangeMap_));
130 
131  // Set L range map and U domain map
132  U_DomainMap_ = IlukDomainMap_;
133  L_RangeMap_ = IlukRangeMap_;
134  // If there is fill, then pre-build the L and U structures from the Block version of L and U.
135  if (Graph().LevelFill()) {
136  L_Graph_ = Teuchos::rcp( new Epetra_CrsGraph(Copy, *IlukRowMap_, *IlukRowMap_, 0) );
137  U_Graph_ = Teuchos::rcp( new Epetra_CrsGraph(Copy, *IlukRowMap_, *IlukRowMap_, 0) );
138  EPETRA_CHK_ERR(BlockGraph2PointGraph(Graph_.L_Graph(), *L_Graph_, false));
139  EPETRA_CHK_ERR(BlockGraph2PointGraph(Graph_.U_Graph(), *U_Graph_, true));
140 
141  L_Graph_->FillComplete(*IlukRowMap_, *IlukRangeMap_);
142  U_Graph_->FillComplete(*IlukDomainMap_, *IlukRowMap_);
143 
144  L_ = Teuchos::rcp( new Epetra_CrsMatrix(Copy, *L_Graph_) );
145  U_ = Teuchos::rcp( new Epetra_CrsMatrix(Copy, *U_Graph_) );
146  D_ = Teuchos::rcp( new Epetra_Vector(*IlukRowMap_) );
147  }
148  else {
149  // Allocate Epetra_CrsMatrix using ILUK graphs
150  L_ = Teuchos::rcp( new Epetra_CrsMatrix(Copy, *IlukRowMap_, *IlukRowMap_, 0) );
151  U_ = Teuchos::rcp( new Epetra_CrsMatrix(Copy, *IlukRowMap_, *IlukRowMap_, 0) );
152  D_ = Teuchos::rcp( new Epetra_Vector(*IlukRowMap_) );
153  L_Graph_ = Teuchos::null;
154  U_Graph_ = Teuchos::null;
155  }
156  SetAllocated(true);
157  return(0);
158 }
159 
160 //==========================================================================
161 int Ifpack_CrsRiluk::SetParameters(const Teuchos::ParameterList& parameterlist,
162  bool cerr_warning_if_unused)
163 {
164  Ifpack::param_struct params;
165  params.double_params[Ifpack::relax_value] = RelaxValue_;
166  params.double_params[Ifpack::absolute_threshold] = Athresh_;
167  params.double_params[Ifpack::relative_threshold] = Rthresh_;
168  params.overlap_mode = OverlapMode_;
169 
170  Ifpack::set_parameters(parameterlist, params, cerr_warning_if_unused);
171 
172  RelaxValue_ = params.double_params[Ifpack::relax_value];
173  Athresh_ = params.double_params[Ifpack::absolute_threshold];
174  Rthresh_ = params.double_params[Ifpack::relative_threshold];
175  OverlapMode_ = params.overlap_mode;
176 
177  return(0);
178 }
179 
180 //==========================================================================
181 int Ifpack_CrsRiluk::InitValues(const Epetra_CrsMatrix & A) {
182 
183  UserMatrixIsCrs_ = true;
184 
185  if (!Allocated()) AllocateCrs();
186 
187  Teuchos::RefCountPtr<Epetra_CrsMatrix> OverlapA = Teuchos::rcp( (Epetra_CrsMatrix *) &A, false );
188 
189  if (IsOverlapped_) {
190 
191  OverlapA = Teuchos::rcp( new Epetra_CrsMatrix(Copy, *Graph_.OverlapGraph()) );
192  EPETRA_CHK_ERR(OverlapA->Import(A, *Graph_.OverlapImporter(), Insert));
193  EPETRA_CHK_ERR(OverlapA->FillComplete());
194  }
195 
196  // Get Maximun Row length
197  int MaxNumEntries = OverlapA->MaxNumEntries();
198 
199  // Set L range map and U domain map
200  U_DomainMap_ = Teuchos::rcp( &(A.DomainMap()), false );
201  L_RangeMap_ = Teuchos::rcp( &(A.RangeMap()), false );
202  // Do the rest using generic Epetra_RowMatrix interface
203 
204  EPETRA_CHK_ERR(InitAllValues(*OverlapA, MaxNumEntries));
205 
206  return(0);
207 }
208 
209 //==========================================================================
210 int Ifpack_CrsRiluk::InitValues(const Epetra_VbrMatrix & A) {
211 
212  UserMatrixIsVbr_ = true;
213 
214  if (!Allocated()) AllocateVbr();
215 
216  //cout << "Original Graph " << endl << A.Graph() << endl << flush;
217  //A.Comm().Barrier();
218  //if (A.Comm().MyPID()==0) cout << "*****************************************************" <<endl;
219  //cout << "Original Matrix " << endl << A << endl << flush;
220  //A.Comm().Barrier();
221  //if (A.Comm().MyPID()==0) cout << "*****************************************************" <<endl;
222  //cout << "Overlap Graph " << endl << *Graph_.OverlapGraph() << endl << flush;
223  //A.Comm().Barrier();
224  //if (A.Comm().MyPID()==0) cout << "*****************************************************" <<endl;
225 
226  Teuchos::RefCountPtr<Epetra_VbrMatrix> OverlapA = Teuchos::rcp( (Epetra_VbrMatrix *) &A, false );
227 
228  if (IsOverlapped_) {
229 
230  OverlapA = Teuchos::rcp( new Epetra_VbrMatrix(Copy, *Graph_.OverlapGraph()) );
231  EPETRA_CHK_ERR(OverlapA->Import(A, *Graph_.OverlapImporter(), Insert));
232  EPETRA_CHK_ERR(OverlapA->FillComplete());
233  }
234 
235  //cout << "Overlap Matrix " << endl << *OverlapA << endl << flush;
236 
237  // Get Maximun Row length
238  int MaxNumEntries = OverlapA->MaxNumNonzeros();
239 
240  // Do the rest using generic Epetra_RowMatrix interface
241 
242  EPETRA_CHK_ERR(InitAllValues(*OverlapA, MaxNumEntries));
243 
244  return(0);
245 }
246 //==========================================================================
247 
248 int Ifpack_CrsRiluk::InitAllValues(const Epetra_RowMatrix & OverlapA, int MaxNumEntries) {
249 
250  int ierr = 0;
251  int i, j;
252  int NumIn, NumL, NumU;
253  bool DiagFound;
254  int NumNonzeroDiags = 0;
255 
256 
257  vector<int> InI(MaxNumEntries); // Allocate temp space
258  vector<int> LI(MaxNumEntries);
259  vector<int> UI(MaxNumEntries);
260  vector<double> InV(MaxNumEntries);
261  vector<double> LV(MaxNumEntries);
262  vector<double> UV(MaxNumEntries);
263 
264  bool ReplaceValues = (L_->StaticGraph() || L_->IndicesAreLocal()); // Check if values should be inserted or replaced
265 
266  if (ReplaceValues) {
267  L_->PutScalar(0.0); // Zero out L and U matrices
268  U_->PutScalar(0.0);
269  }
270 
271  D_->PutScalar(0.0); // Set diagonal values to zero
272  double *DV;
273  EPETRA_CHK_ERR(D_->ExtractView(&DV)); // Get view of diagonal
274 
275 
276  // First we copy the user's matrix into L and U, regardless of fill level
277 
278  for (i=0; i< NumMyRows(); i++) {
279 
280  EPETRA_CHK_ERR(OverlapA.ExtractMyRowCopy(i, MaxNumEntries, NumIn, &InV[0], &InI[0])); // Get Values and Indices
281 
282  // Split into L and U (we don't assume that indices are ordered).
283 
284  NumL = 0;
285  NumU = 0;
286  DiagFound = false;
287 
288  for (j=0; j< NumIn; j++) {
289  int k = InI[j];
290 
291  if (k==i) {
292  DiagFound = true;
293  DV[i] += Rthresh_ * InV[j] + EPETRA_SGN(InV[j]) * Athresh_; // Store perturbed diagonal in Epetra_Vector D_
294  }
295 
296  else if (k < 0) {EPETRA_CHK_ERR(-1);} // Out of range
297 
298  else if (k < i) {
299  LI[NumL] = k;
300  LV[NumL] = InV[j];
301  NumL++;
302  }
303  else if (k<NumMyRows()) {
304  UI[NumU] = k;
305  UV[NumU] = InV[j];
306  NumU++;
307  }
308  }
309 
310  // Check in things for this row of L and U
311 
312  if (DiagFound) NumNonzeroDiags++;
313  else DV[i] = Athresh_;
314 
315  if (NumL) {
316  if (ReplaceValues) {
317  EPETRA_CHK_ERR(L_->ReplaceMyValues(i, NumL, &LV[0], &LI[0]));
318  }
319  else {
320  EPETRA_CHK_ERR(L_->InsertMyValues(i, NumL, &LV[0], &LI[0]));
321  }
322  }
323 
324  if (NumU) {
325  if (ReplaceValues) {
326  EPETRA_CHK_ERR(U_->ReplaceMyValues(i, NumU, &UV[0], &UI[0]));
327  }
328  else {
329  EPETRA_CHK_ERR(U_->InsertMyValues(i, NumU, &UV[0], &UI[0]));
330  }
331  }
332 
333  }
334 
335  if (!ReplaceValues) {
336  // The domain of L and the range of U are exactly their own row maps (there is no communication).
337  // The domain of U and the range of L must be the same as those of the original matrix,
338  // However if the original matrix is a VbrMatrix, these two latter maps are translation from
339  // a block map to a point map.
340  EPETRA_CHK_ERR(L_->FillComplete(L_->RowMatrixColMap(), *L_RangeMap_));
341  EPETRA_CHK_ERR(U_->FillComplete(*U_DomainMap_, U_->RowMatrixRowMap()));
342  }
343 
344  // At this point L and U have the values of A in the structure of L and U, and diagonal vector D
345 
346  SetValuesInitialized(true);
347  SetFactored(false);
348 
349  int TotalNonzeroDiags = 0;
350  EPETRA_CHK_ERR(Graph_.L_Graph().RowMap().Comm().SumAll(&NumNonzeroDiags, &TotalNonzeroDiags, 1));
351  NumMyDiagonals_ = NumNonzeroDiags;
352  if (NumNonzeroDiags != NumMyRows()) ierr = 1; // Diagonals are not right, warn user
353 
354  return(ierr);
355 }
356 
357 //==========================================================================
359 
360  // if (!Allocated()) return(-1); // This test is not needed at this time. All constructors allocate.
361  if (!ValuesInitialized()) return(-2); // Must have values initialized.
362  if (Factored()) return(-3); // Can't have already computed factors.
363 
364  SetValuesInitialized(false);
365 
366  // MinMachNum should be officially defined, for now pick something a little
367  // bigger than IEEE underflow value
368 
369  double MinDiagonalValue = Epetra_MinDouble;
370  double MaxDiagonalValue = 1.0/MinDiagonalValue;
371 
372  int ierr = 0;
373  int i, j, k;
374  int * LI=0, * UI = 0;
375  double * LV=0, * UV = 0;
376  int NumIn, NumL, NumU;
377 
378  // Get Maximun Row length
379  int MaxNumEntries = L_->MaxNumEntries() + U_->MaxNumEntries() + 1;
380 
381  vector<int> InI(MaxNumEntries); // Allocate temp space
382  vector<double> InV(MaxNumEntries);
383  vector<int> colflag(NumMyCols());
384 
385  double *DV;
386  ierr = D_->ExtractView(&DV); // Get view of diagonal
387 
388 #ifdef IFPACK_FLOPCOUNTERS
389  int current_madds = 0; // We will count multiply-add as they happen
390 #endif
391 
392  // Now start the factorization.
393 
394  // Need some integer workspace and pointers
395  int NumUU;
396  int * UUI;
397  double * UUV;
398  for (j=0; j<NumMyCols(); j++) colflag[j] = - 1;
399 
400  for(i=0; i<NumMyRows(); i++) {
401 
402  // Fill InV, InI with current row of L, D and U combined
403 
404  NumIn = MaxNumEntries;
405  EPETRA_CHK_ERR(L_->ExtractMyRowCopy(i, NumIn, NumL, &InV[0], &InI[0]));
406  LV = &InV[0];
407  LI = &InI[0];
408 
409  InV[NumL] = DV[i]; // Put in diagonal
410  InI[NumL] = i;
411 
412  EPETRA_CHK_ERR(U_->ExtractMyRowCopy(i, NumIn-NumL-1, NumU, &InV[NumL+1], &InI[NumL+1]));
413  NumIn = NumL+NumU+1;
414  UV = &InV[NumL+1];
415  UI = &InI[NumL+1];
416 
417  // Set column flags
418  for (j=0; j<NumIn; j++) colflag[InI[j]] = j;
419 
420  double diagmod = 0.0; // Off-diagonal accumulator
421 
422  for (int jj=0; jj<NumL; jj++) {
423  j = InI[jj];
424  double multiplier = InV[jj]; // current_mults++;
425 
426  InV[jj] *= DV[j];
427 
428  EPETRA_CHK_ERR(U_->ExtractMyRowView(j, NumUU, UUV, UUI)); // View of row above
429 
430  if (RelaxValue_==0.0) {
431  for (k=0; k<NumUU; k++) {
432  int kk = colflag[UUI[k]];
433  if (kk>-1) {
434  InV[kk] -= multiplier*UUV[k];
435 #ifdef IFPACK_FLOPCOUNTERS
436  current_madds++;
437 #endif
438  }
439  }
440  }
441  else {
442  for (k=0; k<NumUU; k++) {
443  int kk = colflag[UUI[k]];
444  if (kk>-1) InV[kk] -= multiplier*UUV[k];
445  else diagmod -= multiplier*UUV[k];
446 #ifdef IFPACK_FLOPCOUNTERS
447  current_madds++;
448 #endif
449  }
450  }
451  }
452  if (NumL) {
453  EPETRA_CHK_ERR(L_->ReplaceMyValues(i, NumL, LV, LI)); // Replace current row of L
454  }
455 
456  DV[i] = InV[NumL]; // Extract Diagonal value
457 
458  if (RelaxValue_!=0.0) {
459  DV[i] += RelaxValue_*diagmod; // Add off diagonal modifications
460  // current_madds++;
461  }
462 
463  if (fabs(DV[i]) > MaxDiagonalValue) {
464  if (DV[i] < 0) DV[i] = - MinDiagonalValue;
465  else DV[i] = MinDiagonalValue;
466  }
467  else
468  DV[i] = 1.0/DV[i]; // Invert diagonal value
469 
470  for (j=0; j<NumU; j++) UV[j] *= DV[i]; // Scale U by inverse of diagonal
471 
472  if (NumU) {
473  EPETRA_CHK_ERR(U_->ReplaceMyValues(i, NumU, UV, UI)); // Replace current row of L and U
474  }
475 
476  // Reset column flags
477  for (j=0; j<NumIn; j++) colflag[InI[j]] = -1;
478  }
479 
480  // Validate that the L and U factors are actually lower and upper triangular
481 
482  if( !L_->LowerTriangular() )
483  EPETRA_CHK_ERR(-2);
484  if( !U_->UpperTriangular() )
485  EPETRA_CHK_ERR(-3);
486 
487 #ifdef IFPACK_FLOPCOUNTERS
488  // Add up flops
489 
490  double current_flops = 2 * current_madds;
491  double total_flops = 0;
492 
493  EPETRA_CHK_ERR(Graph_.L_Graph().RowMap().Comm().SumAll(&current_flops, &total_flops, 1)); // Get total madds across all PEs
494 
495  // Now count the rest
496  total_flops += (double) L_->NumGlobalNonzeros(); // Accounts for multiplier above
497  total_flops += (double) D_->GlobalLength(); // Accounts for reciprocal of diagonal
498  if (RelaxValue_!=0.0) total_flops += 2 * (double)D_->GlobalLength(); // Accounts for relax update of diag
499 
500  UpdateFlops(total_flops); // Update flop count
501 #endif
502 
503  SetFactored(true);
504 
505  return(ierr);
506 
507 }
508 
509 //=============================================================================
510 int Ifpack_CrsRiluk::Solve(bool Trans, const Epetra_MultiVector& X,
511  Epetra_MultiVector& Y) const {
512 //
513 // This function finds Y such that LDU Y = X or U(trans) D L(trans) Y = X for multiple RHS
514 //
515 
516  // First generate X and Y as needed for this function
517  Teuchos::RefCountPtr<Epetra_MultiVector> X1;
518  Teuchos::RefCountPtr<Epetra_MultiVector> Y1;
519  EPETRA_CHK_ERR(GenerateXY(Trans, X, Y, &X1, &Y1));
520 
521  bool Upper = true;
522  bool Lower = false;
523  bool UnitDiagonal = true;
524 
525 #ifdef IFPACK_FLOPCOUNTERS
526  Epetra_Flops * counter = this->GetFlopCounter();
527  if (counter!=0) {
528  L_->SetFlopCounter(*counter);
529  Y1->SetFlopCounter(*counter);
530  U_->SetFlopCounter(*counter);
531  }
532 #endif
533 
534  if (!Trans) {
535 
536  EPETRA_CHK_ERR(L_->Solve(Lower, Trans, UnitDiagonal, *X1, *Y1));
537  EPETRA_CHK_ERR(Y1->Multiply(1.0, *D_, *Y1, 0.0)); // y = D*y (D_ has inverse of diagonal)
538  EPETRA_CHK_ERR(U_->Solve(Upper, Trans, UnitDiagonal, *Y1, *Y1)); // Solve Uy = y
539  if (IsOverlapped_) {EPETRA_CHK_ERR(Y.Export(*Y1,*L_->Exporter(), OverlapMode_));} // Export computed Y values if needed
540  }
541  else {
542  EPETRA_CHK_ERR(U_->Solve(Upper, Trans, UnitDiagonal, *X1, *Y1)); // Solve Uy = y
543  EPETRA_CHK_ERR(Y1->Multiply(1.0, *D_, *Y1, 0.0)); // y = D*y (D_ has inverse of diagonal)
544  EPETRA_CHK_ERR(L_->Solve(Lower, Trans, UnitDiagonal, *Y1, *Y1));
545  if (IsOverlapped_) {EPETRA_CHK_ERR(Y.Export(*Y1,*U_->Importer(), OverlapMode_));} // Export computed Y values if needed
546  }
547 
548  return(0);
549 }
550 //=============================================================================
551 int Ifpack_CrsRiluk::Multiply(bool Trans, const Epetra_MultiVector& X,
552  Epetra_MultiVector& Y) const {
553 //
554 // This function finds X such that LDU Y = X or U(trans) D L(trans) Y = X for multiple RHS
555 //
556 
557  // First generate X and Y as needed for this function
558  Teuchos::RefCountPtr<Epetra_MultiVector> X1;
559  Teuchos::RefCountPtr<Epetra_MultiVector> Y1;
560  EPETRA_CHK_ERR(GenerateXY(Trans, X, Y, &X1, &Y1));
561 
562 #ifdef IFPACK_FLOPCOUNTERS
563  Epetra_Flops * counter = this->GetFlopCounter();
564  if (counter!=0) {
565  L_->SetFlopCounter(*counter);
566  Y1->SetFlopCounter(*counter);
567  U_->SetFlopCounter(*counter);
568  }
569 #endif
570 
571  if (!Trans) {
572  EPETRA_CHK_ERR(U_->Multiply(Trans, *X1, *Y1)); //
573  EPETRA_CHK_ERR(Y1->Update(1.0, *X1, 1.0)); // Y1 = Y1 + X1 (account for implicit unit diagonal)
574  EPETRA_CHK_ERR(Y1->ReciprocalMultiply(1.0, *D_, *Y1, 0.0)); // y = D*y (D_ has inverse of diagonal)
575  Epetra_MultiVector Y1temp(*Y1); // Need a temp copy of Y1
576  EPETRA_CHK_ERR(L_->Multiply(Trans, Y1temp, *Y1));
577  EPETRA_CHK_ERR(Y1->Update(1.0, Y1temp, 1.0)); // (account for implicit unit diagonal)
578  if (IsOverlapped_) {EPETRA_CHK_ERR(Y.Export(*Y1,*L_->Exporter(), OverlapMode_));} // Export computed Y values if needed
579  }
580  else {
581 
582  EPETRA_CHK_ERR(L_->Multiply(Trans, *X1, *Y1));
583  EPETRA_CHK_ERR(Y1->Update(1.0, *X1, 1.0)); // Y1 = Y1 + X1 (account for implicit unit diagonal)
584  EPETRA_CHK_ERR(Y1->ReciprocalMultiply(1.0, *D_, *Y1, 0.0)); // y = D*y (D_ has inverse of diagonal)
585  Epetra_MultiVector Y1temp(*Y1); // Need a temp copy of Y1
586  EPETRA_CHK_ERR(U_->Multiply(Trans, Y1temp, *Y1));
587  EPETRA_CHK_ERR(Y1->Update(1.0, Y1temp, 1.0)); // (account for implicit unit diagonal)
588  if (IsOverlapped_) {EPETRA_CHK_ERR(Y.Export(*Y1,*L_->Exporter(), OverlapMode_));}
589  }
590  return(0);
591 }
592 //=============================================================================
593 int Ifpack_CrsRiluk::Condest(bool Trans, double & ConditionNumberEstimate) const {
594 
595  if (Condest_>=0.0) {
596  ConditionNumberEstimate = Condest_;
597  return(0);
598  }
599  // Create a vector with all values equal to one
600  Epetra_Vector Ones(U_->DomainMap());
601  Epetra_Vector OnesResult(L_->RangeMap());
602  Ones.PutScalar(1.0);
603 
604  EPETRA_CHK_ERR(Solve(Trans, Ones, OnesResult)); // Compute the effect of the solve on the vector of ones
605  EPETRA_CHK_ERR(OnesResult.Abs(OnesResult)); // Make all values non-negative
606  EPETRA_CHK_ERR(OnesResult.MaxValue(&ConditionNumberEstimate)); // Get the maximum value across all processors
607  Condest_ = ConditionNumberEstimate; // Save value for possible later calls
608  return(0);
609 }
610 //==============================================================================
611 int Ifpack_CrsRiluk::BlockGraph2PointGraph(const Epetra_CrsGraph & BG, Epetra_CrsGraph & PG, bool Upper) {
612 
613  if (!BG.IndicesAreLocal()) {EPETRA_CHK_ERR(-1);} // Must have done FillComplete on BG
614 
615  int * ColFirstPointInElementList = BG.RowMap().FirstPointInElementList();
616  int * ColElementSizeList = BG.RowMap().ElementSizeList();
617  if (BG.Importer()!=0) {
618  ColFirstPointInElementList = BG.ImportMap().FirstPointInElementList();
619  ColElementSizeList = BG.ImportMap().ElementSizeList();
620  }
621 
622  int Length = (BG.MaxNumIndices()+1) * BG.ImportMap().MaxMyElementSize();
623  vector<int> tmpIndices(Length);
624 
625  int BlockRow, BlockOffset, NumEntries;
626  int NumBlockEntries;
627  int * BlockIndices;
628 
629  int NumMyRows_tmp = PG.NumMyRows();
630 
631  for (int i=0; i<NumMyRows_tmp; i++) {
632  EPETRA_CHK_ERR(BG.RowMap().FindLocalElementID(i, BlockRow, BlockOffset));
633  EPETRA_CHK_ERR(BG.ExtractMyRowView(BlockRow, NumBlockEntries, BlockIndices));
634 
635  int * ptr = &tmpIndices[0]; // Set pointer to beginning of buffer
636 
637  int RowDim = BG.RowMap().ElementSize(BlockRow);
638  NumEntries = 0;
639 
640  // This next line make sure that the off-diagonal entries in the block diagonal of the
641  // original block entry matrix are included in the nonzero pattern of the point graph
642  if (Upper) {
643  int jstart = i+1;
644  int jstop = EPETRA_MIN(NumMyRows_tmp,i+RowDim-BlockOffset);
645  for (int j= jstart; j< jstop; j++) {*ptr++ = j; NumEntries++;}
646  }
647 
648  for (int j=0; j<NumBlockEntries; j++) {
649  int ColDim = ColElementSizeList[BlockIndices[j]];
650  NumEntries += ColDim;
651  assert(NumEntries<=Length); // Sanity test
652  int Index = ColFirstPointInElementList[BlockIndices[j]];
653  for (int k=0; k < ColDim; k++) *ptr++ = Index++;
654  }
655 
656  // This next line make sure that the off-diagonal entries in the block diagonal of the
657  // original block entry matrix are included in the nonzero pattern of the point graph
658  if (!Upper) {
659  int jstart = EPETRA_MAX(0,i-RowDim+1);
660  int jstop = i;
661  for (int j = jstart; j < jstop; j++) {*ptr++ = j; NumEntries++;}
662  }
663 
664  EPETRA_CHK_ERR(PG.InsertMyIndices(i, NumEntries, &tmpIndices[0]));
665  }
666 
667  SetAllocated(true);
668 
669  return(0);
670 }
671 //=========================================================================
672 int Ifpack_CrsRiluk::BlockMap2PointMap(const Epetra_BlockMap & BlockMap, Teuchos::RefCountPtr<Epetra_Map>* PointMap) {
673  // Generate an Epetra_Map that has the same number and distribution of points
674  // as the input Epetra_BlockMap object. The global IDs for the output PointMap
675  // are computed by using the MaxElementSize of the BlockMap. For variable block
676  // sizes this will create gaps in the GID space, but that is OK for Epetra_Maps.
677 
678  int MaxElementSize = BlockMap.MaxElementSize();
679  int PtNumMyElements = BlockMap.NumMyPoints();
680  vector<int> PtMyGlobalElements;
681  if (PtNumMyElements>0) PtMyGlobalElements.resize(PtNumMyElements);
682 
683  int NumMyElements = BlockMap.NumMyElements();
684 
685  int curID = 0;
686  for (int i=0; i<NumMyElements; i++) {
687  int StartID = BlockMap.GID(i)*MaxElementSize;
688  int ElementSize = BlockMap.ElementSize(i);
689  for (int j=0; j<ElementSize; j++) PtMyGlobalElements[curID++] = StartID+j;
690  }
691  assert(curID==PtNumMyElements); // Sanity test
692 
693  (*PointMap) = Teuchos::rcp( new Epetra_Map(-1, PtNumMyElements, &PtMyGlobalElements[0], BlockMap.IndexBase(), BlockMap.Comm()) );
694 
695  if (!BlockMap.PointSameAs(*(*PointMap))) {EPETRA_CHK_ERR(-1);} // Maps not compatible
696  return(0);
697 }
698 //=========================================================================
699 int Ifpack_CrsRiluk::GenerateXY(bool Trans,
700  const Epetra_MultiVector& Xin, const Epetra_MultiVector& Yin,
701  Teuchos::RefCountPtr<Epetra_MultiVector>* Xout,
702  Teuchos::RefCountPtr<Epetra_MultiVector>* Yout) const {
703 
704  // Generate an X and Y suitable for performing Solve() and Multiply() methods
705 
706  if (Xin.NumVectors()!=Yin.NumVectors()) EPETRA_CHK_ERR(-1); // Return error: X and Y not the same size
707 
708  //cout << "Xin = " << Xin << endl;
709  (*Xout) = Teuchos::rcp( (Epetra_MultiVector *) &Xin, false );
710  (*Yout) = Teuchos::rcp( (Epetra_MultiVector *) &Yin, false );
711  if (!IsOverlapped_ && UserMatrixIsCrs_) return(0); // Nothing more to do
712 
713  if (UserMatrixIsVbr_) {
714  if (VbrX_!=Teuchos::null) {
715  if (VbrX_->NumVectors()!=Xin.NumVectors()) {
716  VbrX_ = Teuchos::null;
717  VbrY_ = Teuchos::null;
718  }
719  }
720  if (VbrX_==Teuchos::null) { // Need to allocate space for overlap X and Y
721  VbrX_ = Teuchos::rcp( new Epetra_MultiVector(View, *U_DomainMap_, (*Xout)->Pointers(), (*Xout)->NumVectors()) );
722  VbrY_ = Teuchos::rcp( new Epetra_MultiVector(View, *L_RangeMap_, (*Yout)->Pointers(), (*Yout)->NumVectors()) );
723  }
724  else {
725  EPETRA_CHK_ERR(VbrX_->ResetView((*Xout)->Pointers()));
726  EPETRA_CHK_ERR(VbrY_->ResetView((*Yout)->Pointers()));
727  }
728  (*Xout) = VbrX_;
729  (*Yout) = VbrY_;
730  }
731 
732  if (IsOverlapped_) {
733  // Make sure the number of vectors in the multivector is the same as before.
734  if (OverlapX_!=Teuchos::null) {
735  if (OverlapX_->NumVectors()!=Xin.NumVectors()) {
736  OverlapX_ = Teuchos::null;
737  OverlapY_ = Teuchos::null;
738  }
739  }
740  if (OverlapX_==Teuchos::null) { // Need to allocate space for overlap X and Y
741  OverlapX_ = Teuchos::rcp( new Epetra_MultiVector(U_->RowMatrixColMap(), (*Xout)->NumVectors()) );
742  OverlapY_ = Teuchos::rcp( new Epetra_MultiVector(L_->RowMatrixRowMap(), (*Yout)->NumVectors()) );
743  }
744  if (!Trans) {
745  EPETRA_CHK_ERR(OverlapX_->Import(*(*Xout),*U_->Importer(), Insert)); // Import X values for solve
746  }
747  else {
748  EPETRA_CHK_ERR(OverlapX_->Import(*(*Xout),*L_->Exporter(), Insert)); // Import X values for solve
749  }
750  (*Xout) = OverlapX_;
751  (*Yout) = OverlapY_; // Set pointers for Xout and Yout to point to overlap space
752  //cout << "OverlapX_ = " << *OverlapX_ << endl;
753  }
754 
755  return(0);
756 }
757 //=============================================================================
758 // Non-member functions
759 
760 ostream& operator << (ostream& os, const Ifpack_CrsRiluk& A)
761 {
762 /* Epetra_fmtflags olda = os.setf(ios::right,ios::adjustfield);
763  Epetra_fmtflags oldf = os.setf(ios::scientific,ios::floatfield);
764  int oldp = os.precision(12); */
765  int LevelFill = A.Graph().LevelFill();
766  int LevelOverlap = A.Graph().LevelOverlap();
767  Epetra_CrsMatrix & L = (Epetra_CrsMatrix &) A.L();
768  Epetra_CrsMatrix & U = (Epetra_CrsMatrix &) A.U();
769  Epetra_Vector & D = (Epetra_Vector &) A.D();
770 
771  os.width(14);
772  os << endl;
773  os << " Level of Fill = "; os << LevelFill;
774  os << endl;
775  os.width(14);
776  os << " Level of Overlap = "; os << LevelOverlap;
777  os << endl;
778 
779  os.width(14);
780  os << " Lower Triangle = ";
781  os << endl;
782  os << L; // Let Epetra_CrsMatrix handle the rest.
783  os << endl;
784 
785  os.width(14);
786  os << " Inverse of Diagonal = ";
787  os << endl;
788  os << D; // Let Epetra_Vector handle the rest.
789  os << endl;
790 
791  os.width(14);
792  os << " Upper Triangle = ";
793  os << endl;
794  os << U; // Let Epetra_CrsMatrix handle the rest.
795  os << endl;
796 
797  // Reset os flags
798 
799 /* os.setf(olda,ios::adjustfield);
800  os.setf(oldf,ios::floatfield);
801  os.precision(oldp); */
802 
803  return os;
804 }
int Multiply(bool Trans, const Epetra_MultiVector &X, Epetra_MultiVector &Y) const
Returns the result of multiplying U, D and L in that order on an Epetra_MultiVector X in Y...
Ifpack_CrsRiluk: A class for constructing and using an incomplete lower/upper (ILU) factorization of ...
int Condest(bool Trans, double &ConditionNumberEstimate) const
Returns the maximum over all the condition number estimate for each local ILU set of factors...
const Epetra_CrsMatrix & L() const
Returns the address of the L factor associated with this factored matrix.
friend ostream & operator<<(ostream &os, const Ifpack_CrsRiluk &A)
<< operator will work for Ifpack_CrsRiluk.
virtual Epetra_CrsGraph & L_Graph()
Returns the graph of lower triangle of the ILU(k) graph as a Epetra_CrsGraph.
virtual Epetra_CrsGraph * OverlapGraph() const
Returns the the overlapped graph.
Ifpack_IlukGraph: A class for constructing level filled graphs for use with ILU(k) class precondition...
virtual const Epetra_BlockMap & DomainMap() const
Returns the Epetra_BlockMap object associated with the domain of this matrix operator.
virtual int LevelFill() const
Returns the level of fill used to construct this graph.
virtual Epetra_CrsGraph & U_Graph()
Returns the graph of upper 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 a Teuchos::ParameterList object.
int NumMyCols() const
Returns the number of local matrix columns.
int InitValues(const Epetra_CrsMatrix &A)
Initialize L and U with values from user matrix A.
const Epetra_CrsMatrix & U() const
Returns the address of the L factor associated with this factored matrix.
virtual int LevelOverlap() const
Returns the level of overlap used to construct this graph.
virtual Epetra_Import * OverlapImporter() const
Returns the importer used to create the overlapped graph.
int Solve(bool Trans, const Epetra_MultiVector &X, Epetra_MultiVector &Y) const
Returns the result of a Ifpack_CrsRiluk forward/back solve on a Epetra_MultiVector X in Y (works for ...
bool Factored() const
If factor is completed, this query returns true, otherwise it returns false.
int NumMyRows() const
Returns the number of local matrix rows.
bool ValuesInitialized() const
If values have been initialized, this query returns true, otherwise it returns false.
const Epetra_Vector & D() const
Returns the address of the D factor associated with this factored matrix.
virtual ~Ifpack_CrsRiluk()
Ifpack_CrsRiluk Destructor.
int Factor()
Compute ILU factors L and U using the specified graph, diagonal perturbation thresholds and relaxatio...
const Ifpack_IlukGraph & Graph() const
Returns the address of the Ifpack_IlukGraph associated with this factored matrix. ...
Ifpack_CrsRiluk(const Ifpack_IlukGraph &Graph_in)
Ifpack_CrsRiluk constuctor with variable number of indices per row.