IFPACK  Development
 All Classes Files Functions Variables Enumerations Friends Pages
Ifpack_SILU.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_ConfigDefs.h"
44 #include "Ifpack_SILU.h"
45 #ifdef HAVE_IFPACK_SUPERLU
46 
47 #include "Ifpack_CondestType.h"
48 #include "Epetra_ConfigDefs.h"
49 #include "Epetra_Comm.h"
50 #include "Epetra_Map.h"
51 #include "Epetra_RowMatrix.h"
52 #include "Epetra_Vector.h"
53 #include "Epetra_MultiVector.h"
54 #include "Epetra_CrsGraph.h"
55 #include "Epetra_CrsMatrix.h"
56 #include "Teuchos_ParameterList.hpp"
57 #include "Teuchos_RefCountPtr.hpp"
58 
59 // SuperLU includes
60 extern "C" {int dfill_diag(int n, NCformat *Astore);}
61 
62 using Teuchos::RefCountPtr;
63 using Teuchos::rcp;
64 
65 #ifdef IFPACK_TEUCHOS_TIME_MONITOR
66 # include "Teuchos_TimeMonitor.hpp"
67 #endif
68 
69 //==============================================================================
70 Ifpack_SILU::Ifpack_SILU(Epetra_RowMatrix* Matrix_in) :
71  A_(rcp(Matrix_in,false)),
72  Comm_(Matrix_in->Comm()),
73  UseTranspose_(false),
74  NumMyDiagonals_(0),
75  DropTol_(1e-4),
76  FillTol_(1e-2),
77  FillFactor_(10.0),
78  DropRule_(9),
79  Condest_(-1.0),
80  IsInitialized_(false),
81  IsComputed_(false),
82  NumInitialize_(0),
83  NumCompute_(0),
84  NumApplyInverse_(0),
85  InitializeTime_(0.0),
86  ComputeTime_(0.0),
87  ApplyInverseTime_(0.0),
88  Time_(Comm()),
89  etree_(0),
90  perm_r_(0),
91  perm_c_(0)
92 {
93  Teuchos::ParameterList List;
94  SetParameters(List);
95  SY_.Store=0;
96 }
97 
98 
99 
100 //==============================================================================
101 void Ifpack_SILU::Destroy()
102 {
103  if(IsInitialized_){
104  // SuperLU cleanup
105  StatFree(&stat_);
106 
107  Destroy_CompCol_Permuted(&SAc_);
108  Destroy_SuperNode_Matrix(&SL_);
109  Destroy_CompCol_Matrix(&SU_);
110 
111  // Make sure NOT to clean up Epetra's memory
112  Destroy_SuperMatrix_Store(&SA_);
113  if(SY_.Store) Destroy_SuperMatrix_Store(&SY_);
114  SY_.Store=0;
115 
116  // Cleanup stuff I allocated
117  delete [] etree_;etree_=0;
118  delete [] perm_r_;perm_r_=0;
119  delete [] perm_c_;perm_c_=0;
120  }
121 }
122 
123 //==========================================================================
124 int Ifpack_SILU::SetParameters(Teuchos::ParameterList& List)
125 {
126  DropTol_=List.get("fact: drop tolerance",DropTol_);
127  FillTol_=List.get("fact: zero pivot threshold",FillTol_);
128  FillFactor_=List.get("fact: maximum fill factor",FillFactor_);
129  DropRule_=List.get("fact: silu drop rule",DropRule_);
130 
131  // set label
132  sprintf(Label_, "IFPACK SILU (drop=%d, zpv=%f, ffact=%f, rthr=%f)",
133  DropRule(),FillTol(),FillFactor(),DropTol());
134  return(0);
135 }
136 
137 //==========================================================================
139 {
140 
141 #ifdef IFPACK_TEUCHOS_TIME_MONITOR
142  TEUCHOS_FUNC_TIME_MONITOR("Ifpack_SILU::Initialize");
143 #endif
144 
145  Time_.ResetStartTime();
146 
147  // reset this object
148  Destroy();
149 
150  IsInitialized_ = false;
151 
152  Epetra_CrsMatrix* CrsMatrix;
153  CrsMatrix = dynamic_cast<Epetra_CrsMatrix*>(&*A_);
154 
155  if(CrsMatrix && CrsMatrix->RowMap().SameAs(CrsMatrix->ColMap()) && CrsMatrix->IndicesAreContiguous()){
156  // Case #1: Use original matrix
157  Aover_=rcp(CrsMatrix,false);
158  }
159  else if(CrsMatrix && CrsMatrix->IndicesAreContiguous()){
160  // Case #2: Extract using CrsDataPointers w/ contiguous indices
161  int size = A_->MaxNumEntries();
162  int N=A_->NumMyRows();
163  Aover_ = rcp(new Epetra_CrsMatrix(Copy,A_->RowMatrixRowMap(), size));
164  vector<int> Indices(size);
165  vector<double> Values(size);
166 
167  int i,j,ct,*rowptr,*colind;
168  double *values;
169  IFPACK_CHK_ERR(CrsMatrix->ExtractCrsDataPointers(rowptr,colind,values));
170 
171  // Use the fact that EpetraCrsMatrices always number the off-processor columns *LAST*
172  for(i=0;i<N;i++){
173  for(j=rowptr[i],ct=0;j<rowptr[i+1];j++){
174  if(colind[j]<N){
175  Indices[ct]=CrsMatrix->GCID(colind[j]);
176  Values[ct]=values[j];
177  ct++;
178  }
179  }
180  Aover_->InsertGlobalValues(CrsMatrix->GRID(i),ct,&Values[0],&Indices[0]);
181  }
182  IFPACK_CHK_ERR(Aover_->FillComplete(CrsMatrix->RowMap(),CrsMatrix->RowMap()));
183  }
184  else{
185  // Case #3: Extract using copys
186  int size = A_->MaxNumEntries();
187  Aover_ = rcp(new Epetra_CrsMatrix(Copy,A_->RowMatrixRowMap(), size));
188  if (Aover_.get() == 0) IFPACK_CHK_ERR(-5); // memory allocation error
189 
190  vector<int> Indices1(size),Indices2(size);
191  vector<double> Values1(size),Values2(size);
192 
193  // extract each row at-a-time, and insert it into
194  // the graph, ignore all off-process entries
195  int N=A_->NumMyRows();
196  for (int i = 0 ; i < N ; ++i) {
197  int NumEntries;
198  int GlobalRow = A_->RowMatrixRowMap().GID(i);
199  IFPACK_CHK_ERR(A_->ExtractMyRowCopy(i, size, NumEntries,
200  &Values1[0], &Indices1[0]));
201 
202  // convert to global indices, keeping only on-proc entries
203  int ct=0;
204  for (int j=0; j < NumEntries ; ++j) {
205  if(Indices1[j] < N){
206  Indices2[ct] = A_->RowMatrixColMap().GID(Indices1[j]);
207  Values2[ct]=Values1[j];
208  ct++;
209  }
210  }
211  IFPACK_CHK_ERR(Aover_->InsertGlobalValues(GlobalRow,ct,
212  &Values2[0],&Indices2[0]));
213  }
214  IFPACK_CHK_ERR(Aover_->FillComplete(A_->RowMatrixRowMap(),A_->RowMatrixRowMap()));
215  }
216 
217  // Finishing touches
218  Aover_->OptimizeStorage();
219  Graph_=rcp(const_cast<Epetra_CrsGraph*>(&Aover_->Graph()),false);
220 
221  IsInitialized_ = true;
222  NumInitialize_++;
223  InitializeTime_ += Time_.ElapsedTime();
224 
225  return(0);
226 }
227 
228 //==========================================================================
230 {
231 
232 #ifdef IFPACK_TEUCHOS_TIME_MONITOR
233  TEUCHOS_FUNC_TIME_MONITOR("Ifpack_SILU::Compute");
234 #endif
235 
236  if (!IsInitialized())
237  IFPACK_CHK_ERR(Initialize());
238 
239  Time_.ResetStartTime();
240  IsComputed_ = false;
241 
242  // Initialize the SuperLU statistics & options variables.
243  StatInit(&stat_);
244  ilu_set_default_options(&options_);
245  options_.ILU_DropTol=DropTol_;
246  options_.ILU_FillTol=FillTol_;
247  options_.ILU_DropRule=DropRule_;
248  options_.ILU_FillFactor=FillFactor_;
249 
250  // Grab pointers from Aover_
251  int *rowptr,*colind;
252  double *values;
253  IFPACK_CHK_ERR(Aover_->ExtractCrsDataPointers(rowptr,colind,values));
254  int N=Aover_->NumMyRows();
255 
256  // Copy the data over to SuperLU land - mark as a transposed CompCol Matrix
257  dCreate_CompCol_Matrix(&SA_,N,N,Aover_->NumMyNonzeros(),
258  values,colind,rowptr,SLU_NC,SLU_D,SLU_GE);
259 
260  // Fill any zeros on the diagonal
261  // Commented out for now
262  dfill_diag(N, (NCformat*)SA_.Store);
263 
264  // Allocate SLU memory
265  etree_=new int [N];
266  perm_r_=new int[N];
267  perm_c_=new int[N];
268 
269  // Get column permutation
270  int permc_spec=options_.ColPerm;
271  if ( permc_spec != MY_PERMC && options_.Fact == DOFACT )
272  get_perm_c(permc_spec,&SA_,perm_c_);
273 
274  // Preorder by column permutation
275  sp_preorder(&options_, &SA_, perm_c_, etree_, &SAc_);
276 
277  // Call the factorization
278  int panel_size = sp_ienv(1);
279  int relax = sp_ienv(2);
280  int info=0;
281  dgsitrf(&options_,&SAc_,relax,panel_size,etree_,NULL,0,perm_c_,perm_r_,&SL_,&SU_,&stat_,&info);
282  if(info<0) IFPACK_CHK_ERR(info);
283 
284  IsComputed_ = true;
285  NumCompute_++;
286  ComputeTime_ += Time_.ElapsedTime();
287  return 0;
288 }
289 
290 //=============================================================================
291 // This function finds Y such that LDU Y = X or U(trans) D L(trans) Y = X for multiple RHS
292 int Ifpack_SILU::Solve(bool Trans, const Epetra_MultiVector& X,
293  Epetra_MultiVector& Y) const
294 {
295 
296 #ifdef IFPACK_TEUCHOS_TIME_MONITOR
297  TEUCHOS_FUNC_TIME_MONITOR("Ifpack_SILU::ApplyInverse - Solve");
298 #endif
299 
300  if (!IsComputed())
301  IFPACK_CHK_ERR(-3);
302  int nrhs=X.NumVectors();
303  int N=A_->NumMyRows();
304 
305  // Copy X over to Y
306  Y=X;
307 
308  // Move to SuperLU land
309  // NTS: Need to do epetra-style realloc-if-nrhs-changes thing
310  if(SY_.Store) Destroy_SuperMatrix_Store(&SY_);
311  SY_.Store=0;
312  dCreate_Dense_Matrix(&SY_,N,nrhs,Y[0],N,SLU_DN,SLU_D,SLU_GE);
313 
314  int info;
315  dgstrs(TRANS,&SL_,&SU_,perm_c_,perm_r_,&SY_,&stat_,&info);
316  if(!info) IFPACK_CHK_ERR(info);
317 
318  return(info);
319 }
320 
321 //=============================================================================
322 // This function finds X such that LDU Y = X or U(trans) D L(trans) Y = X for multiple RHS
323 int Ifpack_SILU::Multiply(bool Trans, const Epetra_MultiVector& X,
324  Epetra_MultiVector& Y) const
325 {
326 
327  if (!IsComputed())
328  IFPACK_CHK_ERR(-1);
329 
330  return(0);
331 }
332 
333 //=============================================================================
334 // This function finds X such that LDU Y = X or U(trans) D L(trans) Y = X for multiple RHS
335 int Ifpack_SILU::ApplyInverse(const Epetra_MultiVector& X,
336  Epetra_MultiVector& Y) const
337 {
338 
339 #ifdef IFPACK_TEUCHOS_TIME_MONITOR
340  TEUCHOS_FUNC_TIME_MONITOR("Ifpack_SILU::ApplyInverse");
341 #endif
342 
343  if (!IsComputed())
344  IFPACK_CHK_ERR(-3);
345 
346  if (X.NumVectors() != Y.NumVectors())
347  IFPACK_CHK_ERR(-2);
348 
349  Time_.ResetStartTime();
350 
351  // AztecOO gives X and Y pointing to the same memory location,
352  // need to create an auxiliary vector, Xcopy
353  Teuchos::RefCountPtr< const Epetra_MultiVector > Xcopy;
354  if (X.Pointers()[0] == Y.Pointers()[0])
355  Xcopy = Teuchos::rcp( new Epetra_MultiVector(X) );
356  else
357  Xcopy = Teuchos::rcp( &X, false );
358 
359  IFPACK_CHK_ERR(Solve(Ifpack_SILU::UseTranspose(), *Xcopy, Y));
360 
361  ++NumApplyInverse_;
362  ApplyInverseTime_ += Time_.ElapsedTime();
363 
364  return(0);
365 
366 }
367 
368 //=============================================================================
369 double Ifpack_SILU::Condest(const Ifpack_CondestType CT,
370  const int MaxIters, const double Tol,
371  Epetra_RowMatrix* Matrix_in)
372 {
373 
374 #ifdef IFPACK_TEUCHOS_TIME_MONITOR
375  TEUCHOS_FUNC_TIME_MONITOR("Ifpack_SILU::Condest");
376 #endif
377 
378  if (!IsComputed()) // cannot compute right now
379  return(-1.0);
380 
381  Condest_ = Ifpack_Condest(*this, CT, MaxIters, Tol, Matrix_in);
382 
383  return(Condest_);
384 }
385 
386 //=============================================================================
387 std::ostream&
388 Ifpack_SILU::Print(std::ostream& os) const
389 {
390  if (!Comm().MyPID()) {
391  os << endl;
392  os << "================================================================================" << endl;
393  os << "Ifpack_SILU: " << Label() << endl << endl;
394  os << "Dropping rule = "<< DropRule() << endl;
395  os << "Zero pivot thresh = "<< FillTol() << endl;
396  os << "Max fill factor = "<< FillFactor() << endl;
397  os << "Drop tolerance = "<< DropTol() << endl;
398  os << "Condition number estimate = " << Condest() << endl;
399  os << "Global number of rows = " << A_->NumGlobalRows() << endl;
400  if (IsComputed_) {
401  // Internal SuperLU info
402  int fnnz=0;
403  if(SL_.Store) fnnz+=((SCformat*)SL_.Store)->nnz;
404  if(SU_.Store) fnnz+=((NCformat*)SU_.Store)->nnz;
405  int lufill=fnnz - A_->NumMyRows();
406  os << "No. of nonzeros in L+U = "<< lufill<<endl;
407  os << "Fill ratio: nnz(F)/nnz(A) = "<<(double)lufill / (double)A_->NumMyNonzeros()<<endl;
408  }
409  os << endl;
410  os << "Phase # calls Total Time (s) Total MFlops MFlops/s" << endl;
411  os << "----- ------- -------------- ------------ --------" << endl;
412  os << "Initialize() " << std::setw(5) << NumInitialize()
413  << " " << std::setw(15) << InitializeTime()
414  << " 0.0 0.0" << endl;
415  os << "Compute() " << std::setw(5) << NumCompute()
416  << " " << std::setw(15) << ComputeTime()
417  << " " << std::setw(15) << 1.0e-6 * ComputeFlops();
418  if (ComputeTime() != 0.0)
419  os << " " << std::setw(15) << 1.0e-6 * ComputeFlops() / ComputeTime() << endl;
420  else
421  os << " " << std::setw(15) << 0.0 << endl;
422  os << "ApplyInverse() " << std::setw(5) << NumApplyInverse()
423  << " " << std::setw(15) << ApplyInverseTime()
424  << " " << std::setw(15) << 1.0e-6 * ApplyInverseFlops();
425  if (ApplyInverseTime() != 0.0)
426  os << " " << std::setw(15) << 1.0e-6 * ApplyInverseFlops() / ApplyInverseTime() << endl;
427  else
428  os << " " << std::setw(15) << 0.0 << endl;
429  os << "================================================================================" << endl;
430  os << endl;
431  }
432 
433  return(os);
434 }
435 
436 #endif
virtual double ApplyInverseTime() const
Returns the time spent in ApplyInverse().
Definition: Ifpack_SILU.h:255
const char * Label() const
Returns a character string describing the operator.
Definition: Ifpack_SILU.h:188
Ifpack_SILU(Epetra_RowMatrix *A)
Constructor.
Definition: Ifpack_SILU.cpp:70
virtual int NumInitialize() const
Returns the number of calls to Initialize().
Definition: Ifpack_SILU.h:225
bool UseTranspose() const
Returns the current UseTranspose setting.
Definition: Ifpack_SILU.h:204
int ApplyInverse(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const
Returns the result of a Epetra_Operator inverse applied to an Epetra_MultiVector X in Y...
virtual ostream & Print(ostream &os) const
Prints on stream basic information about this object.
virtual double InitializeTime() const
Returns the time spent in Initialize().
Definition: Ifpack_SILU.h:243
bool IsInitialized() const
Returns true if the preconditioner has been successfully initialized.
Definition: Ifpack_SILU.h:105
int Compute()
Compute ILU factors L and U using the specified graph, diagonal perturbation thresholds and relaxatio...
int Initialize()
Initialize the preconditioner, does not touch matrix values.
int SetParameters(Teuchos::ParameterList &parameterlist)
Set parameters using a Teuchos::ParameterList object.
bool IsComputed() const
If factor is completed, this query returns true, otherwise it returns false.
Definition: Ifpack_SILU.h:114
virtual double ComputeTime() const
Returns the time spent in Compute().
Definition: Ifpack_SILU.h:249
virtual double ApplyInverseFlops() const
Returns the number of flops in the application of the preconditioner.
Definition: Ifpack_SILU.h:270
virtual int NumCompute() const
Returns the number of calls to Compute().
Definition: Ifpack_SILU.h:231
const Epetra_Comm & Comm() const
Returns the Epetra_BlockMap object associated with the range of this matrix operator.
Definition: Ifpack_SILU.h:213
virtual int NumApplyInverse() const
Returns the number of calls to ApplyInverse().
Definition: Ifpack_SILU.h:237
virtual double ComputeFlops() const
Returns the number of flops in the computation phase.
Definition: Ifpack_SILU.h:265
double Condest() const
Returns the computed estimated condition number, or -1.0 if not computed.
Definition: Ifpack_SILU.h:177