Ifpack2 Templated Preconditioning Package  Version 1.0
Ifpack2_RILUK_def.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_CRSRILUK_DEF_HPP
44 #define IFPACK2_CRSRILUK_DEF_HPP
45 
46 namespace Ifpack2 {
47 
48 //==============================================================================
49 template<class MatrixType>
50 RILUK<MatrixType>::RILUK(const Teuchos::RCP<const MatrixType>& Matrix_in)
51  : isOverlapped_(false),
52  Graph_(),
53  A_(Matrix_in),
54  UseTranspose_(false),
55  LevelOfFill_(0),
56  LevelOfOverlap_(0),
57  NumMyDiagonals_(0),
58  isAllocated_(false),
59  isInitialized_(false),
60  numInitialize_(0),
61  numCompute_(0),
62  numApply_(0),
63  Factored_(false),
64  RelaxValue_(0.0),
65  Athresh_(0.0),
66  Rthresh_(1.0),
67  Condest_(-1.0),
68  OverlapMode_(Tpetra::REPLACE)
69 {
70 }
71 
72 //==============================================================================
73 //template<class MatrixType>
74 //RILUK<MatrixType>::RILUK(const RILUK<MatrixType>& src)
75 // : isOverlapped_(src.isOverlapped_),
76 // Graph_(src.Graph_),
77 // UseTranspose_(src.UseTranspose_),
78 // LevelOfFill_(src.LevelOfFill_),
79 // LevelOfOverlap_(src.LevelOfOverlap_),
80 // NumMyDiagonals_(src.NumMyDiagonals_),
81 // isAllocated_(src.isAllocated_),
82 // isInitialized_(src.isInitialized_),
83 // Factored_(src.Factored_),
84 // RelaxValue_(src.RelaxValue_),
85 // Athresh_(src.Athresh_),
86 // Rthresh_(src.Rthresh_),
87 // Condest_(src.Condest_),
88 // OverlapMode_(src.OverlapMode_)
89 //{
90 // L_ = Teuchos::rcp( new MatrixType(src.getL()) );
91 // U_ = Teuchos::rcp( new MatrixType(src.getU()) );
92 // D_ = Teuchos::rcp( new Tpetra::Vector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>(src.getD()) );
93 //}
94 
95 //==============================================================================
96 template<class MatrixType>
98 }
99 
100 //==============================================================================
101 template<class MatrixType>
103 
104  // Allocate Matrix using ILUK graphs
105  L_ = Teuchos::rcp( new MatrixType(Graph_->getL_Graph()) );
106  U_ = Teuchos::rcp( new MatrixType(Graph_->getU_Graph()) );
107  L_->setAllToScalar(0.0); // Zero out L and U matrices
108  U_->setAllToScalar(0.0);
109  L_->fillComplete();
110  U_->fillComplete();
111  bool isLower = L_->isLowerTriangular();
112  bool isUpper = U_->isUpperTriangular();
113  if (!isLower || !isUpper) {
114  std::cout << "error in triangular detection" << std::endl;
115  }
116 
117  D_ = Teuchos::rcp( new Tpetra::Vector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>(Graph_->getL_Graph()->getRowMap()) );
118  setAllocated(true);
119 }
120 
121 //==========================================================================
122 template<class MatrixType>
123 void
125 setParameters (const Teuchos::ParameterList& params)
126 {
127  using Teuchos::as;
128  using Teuchos::Exceptions::InvalidParameterName;
129  using Teuchos::Exceptions::InvalidParameterType;
130  typedef Teuchos::ScalarTraits<magnitude_type> STM;
131 
132  // Default values of the various parameters.
133  int fillLevel = 0;
134  int overlapLevel = 0;
135  magnitude_type absThresh = STM::zero ();
136  magnitude_type relThresh = STM::one ();
137  magnitude_type relaxValue = STM::zero ();
138 
139  //
140  // "fact: iluk level-of-fill" parsing is more complicated, because
141  // we want to allow as many types as make sense. int is the native
142  // type, but we also want to accept magnitude_type (for
143  // compatibility with ILUT) and double (for backwards compatibilty
144  // with ILUT).
145  //
146 
147  bool gotFillLevel = false;
148  try {
149  fillLevel = params.get<int> ("fact: iluk level-of-fill");
150  gotFillLevel = true;
151  }
152  catch (InvalidParameterType&) {
153  // Throwing again in the catch block would just unwind the stack.
154  // Instead, we do nothing here, and check the Boolean outside to
155  // see if we got the value.
156  }
157  catch (InvalidParameterName&) {
158  gotFillLevel = true; // Accept the default value.
159  }
160 
161  if (! gotFillLevel) {
162  try {
163  // Try magnitude_type, for compatibility with ILUT.
164  // The cast from magnitude_type to int must succeed.
165  fillLevel = as<int> (params.get<magnitude_type> ("fact: iluk level-of-fill"));
166  gotFillLevel = true;
167  }
168  catch (InvalidParameterType&) {
169  // Try double next.
170  }
171  // Don't catch InvalidParameterName here; we've already done that above.
172  }
173 
174  if (! gotFillLevel) {
175  try {
176  // Try double, for compatibility with ILUT.
177  // The cast from double to int must succeed.
178  fillLevel = as<int> (params.get<double> ("fact: iluk level-of-fill"));
179  gotFillLevel = true;
180  }
181  catch (InvalidParameterType& e) {
182  // We're out of options. The user gave us the parameter, but it
183  // doesn't have the right type. The best thing for us to do in
184  // that case is to throw, telling the user to use the right
185  // type.
186  throw e;
187  }
188  // Don't catch InvalidParameterName here; we've already done that above.
189  }
190 
191  TEUCHOS_TEST_FOR_EXCEPTION(
192  ! gotFillLevel,
193  std::logic_error,
194  "Ifpack2::RILUK::setParameters: We should never get here! "
195  "The method should either have read the \"fact: iluk level-of-fill\" "
196  "parameter by this point, or have thrown an exception. "
197  "Please let the Ifpack2 developers know about this bug.");
198 
199  //
200  // overlapLevel was always int. ILUT doesn't have this parameter.
201  // However, some tests (as of 28 Nov 2012:
202  // Ifpack2_RILUK_small_belos_MPI_1) depend on being able to set this
203  // as a double instead of an int. Thus, we go through the same
204  // procedure as above with fill level.
205  //
206 
207  bool gotOverlapLevel = false;
208  try {
209  overlapLevel = params.get<int> ("fact: iluk level-of-overlap");
210  gotOverlapLevel = true;
211  }
212  catch (InvalidParameterType&) {
213  // Throwing again in the catch block would just unwind the stack.
214  // Instead, we do nothing here, and check the Boolean outside to
215  // see if we got the value.
216  }
217  catch (InvalidParameterName&) {
218  gotOverlapLevel = true; // Accept the default value.
219  }
220 
221  if (! gotOverlapLevel) {
222  try {
223  // Try magnitude_type, for compatibility with ILUT.
224  // The cast from magnitude_type to int must succeed.
225  overlapLevel = as<int> (params.get<magnitude_type> ("fact: iluk level-of-overlap"));
226  gotOverlapLevel = true;
227  }
228  catch (InvalidParameterType&) {
229  // Try double next.
230  }
231  // Don't catch InvalidParameterName here; we've already done that above.
232  }
233 
234  if (! gotOverlapLevel) {
235  try {
236  // Try double, for compatibility with ILUT.
237  // The cast from double to int must succeed.
238  overlapLevel = as<int> (params.get<double> ("fact: iluk level-of-overlap"));
239  gotOverlapLevel = true;
240  }
241  catch (InvalidParameterType& e) {
242  // We're out of options. The user gave us the parameter, but it
243  // doesn't have the right type. The best thing for us to do in
244  // that case is to throw, telling the user to use the right
245  // type.
246  throw e;
247  }
248  // Don't catch InvalidParameterName here; we've already done that above.
249  }
250 
251  TEUCHOS_TEST_FOR_EXCEPTION(
252  ! gotOverlapLevel,
253  std::logic_error,
254  "Ifpack2::RILUK::setParameters: We should never get here! "
255  "The method should either have read the \"fact: iluk level-of-overlap\" "
256  "parameter by this point, or have thrown an exception. "
257  "Please let the Ifpack2 developers know about this bug.");
258 
259  //
260  // For the other parameters, we prefer magnitude_type, but allow
261  // double for backwards compatibility.
262  //
263 
264  try {
265  absThresh = params.get<magnitude_type> ("fact: absolute threshold");
266  }
267  catch (InvalidParameterType&) {
268  // Try double, for backwards compatibility.
269  // The cast from double to magnitude_type must succeed.
270  absThresh = as<magnitude_type> (params.get<double> ("fact: absolute threshold"));
271  }
272  catch (InvalidParameterName&) {
273  // Accept the default value.
274  }
275 
276  try {
277  relThresh = params.get<magnitude_type> ("fact: relative threshold");
278  }
279  catch (InvalidParameterType&) {
280  // Try double, for backwards compatibility.
281  // The cast from double to magnitude_type must succeed.
282  relThresh = as<magnitude_type> (params.get<double> ("fact: relative threshold"));
283  }
284  catch (InvalidParameterName&) {
285  // Accept the default value.
286  }
287 
288  try {
289  relaxValue = params.get<magnitude_type> ("fact: relax value");
290  }
291  catch (InvalidParameterType&) {
292  // Try double, for backwards compatibility.
293  // The cast from double to magnitude_type must succeed.
294  relaxValue = as<magnitude_type> (params.get<double> ("fact: relax value"));
295  }
296  catch (InvalidParameterName&) {
297  // Accept the default value.
298  }
299 
300  // "Commit" the values only after validating all of them. This
301  // ensures that there are no side effects if this routine throws an
302  // exception.
303 
304  LevelOfFill_ = fillLevel;
305  LevelOfOverlap_ = overlapLevel;
306 
307  // mfh 28 Nov 2012: The previous code would not assign Athresh_,
308  // Rthresh_, or RelaxValue_, if the read-in value was -1. I don't
309  // know if keeping this behavior is correct, but I'll keep it just
310  // so as not to change previous behavior.
311 
312  if (absThresh != -STM::one ()) {
313  Athresh_ = absThresh;
314  }
315  if (relThresh != -STM::one ()) {
316  Rthresh_ = relThresh;
317  }
318  if (relaxValue != -STM::one ()) {
319  RelaxValue_ = relaxValue;
320  }
321 }
322 
323 //==========================================================================
324 template<class MatrixType>
326 
327  if (Graph_ != Teuchos::null) return;
328 
329  Graph_ = Teuchos::rcp(new Ifpack2::IlukGraph<local_ordinal_type,global_ordinal_type,node_type>(A_->getCrsGraph(), LevelOfFill_, LevelOfOverlap_));
330 
331  Graph_->constructFilledGraph();
332 
333  setInitialized(true);
334 
335  if (!isAllocated()) allocate_L_and_U();
336 
337  if (isOverlapped_) {
338  throw std::runtime_error("Error, Ifpack2::RILUK::initialize: overlapping not yet supported.");
339 // OverlapA = Teuchos::rcp( new MatrixType(Graph_->OverlapGraph()) );
340 // EPETRA_CHK_ERR(OverlapA->Import(A, *Graph_->OverlapImporter(), Insert));
341 // EPETRA_CHK_ERR(OverlapA->FillComplete());
342  }
343  else {
344  initAllValues(*A_);
345  }
346 
347  ++numInitialize_;
348 }
349 
350 //==========================================================================
351 template<class MatrixType>
352 void RILUK<MatrixType>::initAllValues(const Tpetra::RowMatrix<scalar_type,local_ordinal_type,global_ordinal_type,node_type> & OverlapA) {
353 
354  size_t NumIn = 0, NumL = 0, NumU = 0;
355  bool DiagFound = false;
356  size_t NumNonzeroDiags = 0;
357  size_t MaxNumEntries = OverlapA.getGlobalMaxNumRowEntries();
358 
359 
360  Teuchos::Array<global_ordinal_type> InI(MaxNumEntries); // Allocate temp space
361  Teuchos::Array<global_ordinal_type> LI(MaxNumEntries);
362  Teuchos::Array<global_ordinal_type> UI(MaxNumEntries);
363  Teuchos::Array<scalar_type> InV(MaxNumEntries);
364  Teuchos::Array<scalar_type> LV(MaxNumEntries);
365  Teuchos::Array<scalar_type> UV(MaxNumEntries);
366 
367  bool ReplaceValues = (L_->isStaticGraph() || L_->isLocallyIndexed()); // Check if values should be inserted or replaced
368 
369  L_->resumeFill();
370  U_->resumeFill();
371  if (ReplaceValues) {
372  L_->setAllToScalar(0.0); // Zero out L and U matrices
373  U_->setAllToScalar(0.0);
374  }
375 
376  D_->putScalar(0.0); // Set diagonal values to zero
377  Teuchos::ArrayRCP<scalar_type> DV = D_->get1dViewNonConst(); // Get view of diagonal
378 
379  const Teuchos::RCP<const Tpetra::Map<local_ordinal_type,global_ordinal_type,node_type> >& rowMap =
380  L_->getRowMap();
381 
382  // First we copy the user's matrix into L and U, regardless of fill level
383 
384  for (global_ordinal_type i=rowMap->getMinGlobalIndex(); i<=rowMap->getMaxGlobalIndex(); ++i) {
385  global_ordinal_type global_row = i;
386  local_ordinal_type local_row = rowMap->getLocalElement(global_row);
387 
388  OverlapA.getGlobalRowCopy(global_row, InI(), InV(), NumIn); // Get Values and Indices
389 
390  // Split into L and U (we don't assume that indices are ordered).
391 
392  NumL = 0;
393  NumU = 0;
394  DiagFound = false;
395 
396  for (size_t j=0; j< NumIn; j++) {
397  global_ordinal_type k = InI[j];
398 
399  if (k==i) {
400  DiagFound = true;
401  DV[local_row] += Rthresh_ * InV[j] + IFPACK2_SGN(InV[j]) * Athresh_; // Store perturbed diagonal in Tpetra::Vector D_
402  }
403 
404  else if (k < 0) { // Out of range
405  throw std::runtime_error("out of range (k<0) in Ifpack2::RILUK::initAllValues");
406  }
407 
408  else if (k < i) {
409  LI[NumL] = k;
410  LV[NumL] = InV[j];
411  NumL++;
412  }
413  else if (k <= rowMap->getMaxGlobalIndex()) {
414  UI[NumU] = k;
415  UV[NumU] = InV[j];
416  NumU++;
417  }
418 // else {
419 // throw std::runtime_error("out of range in Ifpack2::RILUK::initAllValues");
420 // }
421  }
422 
423  // Check in things for this row of L and U
424 
425  if (DiagFound) ++NumNonzeroDiags;
426  else DV[local_row] = Athresh_;
427 
428  if (NumL) {
429  if (ReplaceValues) {
430  L_->replaceGlobalValues(global_row, LI(0, NumL), LV(0,NumL));
431  }
432  else {
433  L_->insertGlobalValues(global_row, LI(0,NumL), LV(0,NumL));
434  }
435  }
436 
437  if (NumU) {
438  if (ReplaceValues) {
439  U_->replaceGlobalValues(global_row, UI(0,NumU), UV(0,NumU));
440  }
441  else {
442  U_->insertGlobalValues(global_row, UI(0,NumU), UV(0,NumU));
443  }
444  }
445 
446  }
447 
448  // The domain of L and the range of U are exactly their own row maps (there is no communication).
449  // The domain of U and the range of L must be the same as those of the original matrix,
450  // However if the original matrix is a VbrMatrix, these two latter maps are translation from
451  // a block map to a point map.
452  L_->fillComplete(L_->getColMap(), A_->getRangeMap());
453  U_->fillComplete(A_->getDomainMap(), U_->getRowMap());
454 
455  // At this point L and U have the values of A in the structure of L and U, and diagonal vector D
456 
457  setInitialized(true);
458  setFactored(false);
459 
460  size_t TotalNonzeroDiags = 0;
461  Teuchos::reduceAll(*L_->getRowMap()->getComm(),Teuchos::REDUCE_SUM,
462  1,&NumNonzeroDiags,&TotalNonzeroDiags);
463  NumMyDiagonals_ = NumNonzeroDiags;
464  if (NumNonzeroDiags != U_->getNodeNumRows()) {
465  throw std::runtime_error("Error in Ifpack2::RILUK::initAllValues, wrong number of diagonals.");
466  }
467 }
468 
469 //==========================================================================
470 template<class MatrixType>
472 
473  L_->resumeFill();
474  U_->resumeFill();
475 
476  TEUCHOS_TEST_FOR_EXCEPTION(!isInitialized(), std::runtime_error,
477  "Ifpack2::RILUK::compute() ERROR: isInitialized() must be true.");
478  TEUCHOS_TEST_FOR_EXCEPTION(isComputed() == true, std::runtime_error,
479  "Ifpack2::RILUK::compute() ERROR: Can't have already computed factors.");
480 
481  // MinMachNum should be officially defined, for now pick something a little
482  // bigger than IEEE underflow value
483 
484  scalar_type MinDiagonalValue = Teuchos::ScalarTraits<scalar_type>::rmin();
485  scalar_type MaxDiagonalValue = Teuchos::ScalarTraits<scalar_type>::one()/MinDiagonalValue;
486 
487  size_t NumIn, NumL, NumU;
488 
489  // Get Maximun Row length
490  size_t MaxNumEntries = L_->getNodeMaxNumRowEntries() + U_->getNodeMaxNumRowEntries() + 1;
491 
492  Teuchos::Array<local_ordinal_type> InI(MaxNumEntries); // Allocate temp space
493  Teuchos::Array<scalar_type> InV(MaxNumEntries);
494  size_t num_cols = U_->getColMap()->getNodeNumElements();
495  Teuchos::Array<int> colflag(num_cols);
496 
497  Teuchos::ArrayRCP<scalar_type> DV = D_->get1dViewNonConst(); // Get view of diagonal
498 
499  size_t current_madds = 0; // We will count multiply-add as they happen
500 
501  // Now start the factorization.
502 
503  // Need some integer workspace and pointers
504  size_t NumUU;
505  Teuchos::ArrayView<const local_ordinal_type> UUI;
506  Teuchos::ArrayView<const scalar_type> UUV;
507  for (size_t j=0; j<num_cols; j++) colflag[j] = - 1;
508 
509  for(size_t i=0; i<L_->getNodeNumRows(); i++) {
510  local_ordinal_type local_row = i;
511 
512  // Fill InV, InI with current row of L, D and U combined
513 
514  NumIn = MaxNumEntries;
515  L_->getLocalRowCopy(local_row, InI(), InV(), NumL);
516 
517  InV[NumL] = DV[i]; // Put in diagonal
518  InI[NumL] = local_row;
519 
520  U_->getLocalRowCopy(local_row, InI(NumL+1,MaxNumEntries-NumL-1), InV(NumL+1,MaxNumEntries-NumL-1), NumU);
521  NumIn = NumL+NumU+1;
522 
523  // Set column flags
524  for (size_t j=0; j<NumIn; j++) colflag[InI[j]] = j;
525 
526  scalar_type diagmod = 0.0; // Off-diagonal accumulator
527 
528  for (size_t jj=0; jj<NumL; jj++) {
529  local_ordinal_type j = InI[jj];
530  scalar_type multiplier = InV[jj]; // current_mults++;
531 
532  InV[jj] *= DV[j];
533 
534  U_->getLocalRowView(j, UUI, UUV); // View of row above
535  NumUU = UUI.size();
536 
537  if (RelaxValue_==0.0) {
538  for (size_t k=0; k<NumUU; k++) {
539  int kk = colflag[UUI[k]];
540  if (kk>-1) {
541  InV[kk] -= multiplier*UUV[k];
542  current_madds++;
543  }
544  }
545  }
546  else {
547  for (size_t k=0; k<NumUU; k++) {
548  int kk = colflag[UUI[k]];
549  if (kk>-1) InV[kk] -= multiplier*UUV[k];
550  else diagmod -= multiplier*UUV[k];
551  current_madds++;
552  }
553  }
554  }
555  if (NumL) {
556  L_->replaceLocalValues(local_row, InI(0,NumL), InV(0,NumL)); // Replace current row of L
557  }
558 
559  DV[i] = InV[NumL]; // Extract Diagonal value
560 
561  if (RelaxValue_!=0.0) {
562  DV[i] += RelaxValue_*diagmod; // Add off diagonal modifications
563  // current_madds++;
564  }
565 
566  if (Teuchos::ScalarTraits<scalar_type>::magnitude(DV[i]) > Teuchos::ScalarTraits<scalar_type>::magnitude(MaxDiagonalValue)) {
567  if (Teuchos::ScalarTraits<scalar_type>::real(DV[i]) < 0) DV[i] = - MinDiagonalValue;
568  else DV[i] = MinDiagonalValue;
569  }
570  else
571  DV[i] = Teuchos::ScalarTraits<scalar_type>::one()/DV[i]; // Invert diagonal value
572 
573  for (size_t j=0; j<NumU; j++) InV[NumL+1+j] *= DV[i]; // Scale U by inverse of diagonal
574 
575  if (NumU) {
576  U_->replaceLocalValues(local_row, InI(NumL+1,NumU), InV(NumL+1,NumU)); // Replace current row of L and U
577  }
578 
579  // Reset column flags
580  for (size_t j=0; j<NumIn; j++) colflag[InI[j]] = -1;
581  }
582 
583  L_->fillComplete(L_->getColMap(), A_->getRangeMap());
584  U_->fillComplete(A_->getDomainMap(), U_->getRowMap());
585 
586  // Validate that the L and U factors are actually lower and upper triangular
587 
588  if( !L_->isLowerTriangular() )
589  throw std::runtime_error("Ifpack2::RILUK::compute() ERROR, L isn't lower triangular.");
590  if( !U_->isUpperTriangular() )
591  throw std::runtime_error("Ifpack2::RILUK::compute() ERROR, U isn't lower triangular.");
592 
593  // Add up flops
594 
595  double current_flops = 2 * current_madds;
596  double total_flops = 0;
597 
598  // Get total madds across all PEs
599  Teuchos::reduceAll(*L_->getRowMap()->getComm(),Teuchos::REDUCE_SUM,
600  1,&current_flops,&total_flops);
601 
602  // Now count the rest
603  total_flops += (double) L_->getGlobalNumEntries(); // Accounts for multiplier above
604  total_flops += (double) D_->getGlobalLength(); // Accounts for reciprocal of diagonal
605  if (RelaxValue_!=0.0) total_flops += 2 * (double)D_->getGlobalLength(); // Accounts for relax update of diag
606 
607  //UpdateFlops(total_flops); // Update flop count
608 
609  setFactored(true);
610  ++numCompute_;
611 }
612 
613 //=============================================================================
614 template<class MatrixType>
616  const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
617  Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y,
618  Teuchos::ETransp mode, scalar_type alpha, scalar_type beta) const
619 {
620  typedef Teuchos::ScalarTraits<scalar_type> STS;
621 
622  TEUCHOS_TEST_FOR_EXCEPTION(!isComputed(), std::runtime_error,
623  "Ifpack2::RILUK::apply() ERROR, compute() hasn't been called yet.");
624 
625  TEUCHOS_TEST_FOR_EXCEPTION(
626  alpha != STS::one (),
627  std::logic_error,
628  "Ifpack2::RILUK::apply() does not currently allow alpha != 1.");
629  TEUCHOS_TEST_FOR_EXCEPTION(
630  beta != STS::zero (),
631  std::logic_error,
632  "Ifpack2::RILUK::apply() does not currently allow zero != 0.");
633 
634 //
635 // This function finds Y such that
636 // LDU Y = X, or
637 // U(trans) D L(trans) Y = X
638 // for multiple RHS
639 //
640 
641  // First generate X and Y as needed for this function
642  Teuchos::RCP<const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> > X1;
643  Teuchos::RCP<Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> > Y1;
644  generateXY(mode, X, Y, X1, Y1);
645 
646  scalar_type one = Teuchos::ScalarTraits<scalar_type>::one();
647  scalar_type zero = Teuchos::ScalarTraits<scalar_type>::zero();
648 
649  if (mode == Teuchos::NO_TRANS) {
650 
651  L_->localSolve(*X1, *Y1,mode);
652  Y1->elementWiseMultiply(one, *D_, *Y1, zero); // y = D*y (D_ has inverse of diagonal)
653  U_->localSolve(*Y1, *Y1,mode); // Solve Uy = y
654  if (isOverlapped_) {
655  // Export computed Y values if needed
656  Y.doExport(*Y1,*L_->getGraph()->getExporter(), OverlapMode_);
657  }
658  }
659  else {
660  U_->localSolve(*X1, *Y1,mode); // Solve Uy = y
661  Y1->elementWiseMultiply(one, *D_, *Y1, zero); // y = D*y (D_ has inverse of diagonal)
662  L_->localSolve(*Y1, *Y1,mode);
663  if (isOverlapped_) {Y.doExport(*Y1,*U_->getGraph()->getImporter(), OverlapMode_);} // Export computed Y values if needed
664  }
665 
666  ++numApply_;
667 }
668 
669 //=============================================================================
670 template<class MatrixType>
671 int RILUK<MatrixType>::Multiply(const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
672  Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y,
673  Teuchos::ETransp mode) const {
674 //
675 // This function finds X such that LDU Y = X or U(trans) D L(trans) Y = X for multiple RHS
676 //
677 
678  // First generate X and Y as needed for this function
679  Teuchos::RCP<const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> > X1;
680  Teuchos::RCP<Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> > Y1;
681  generateXY(mode, X, Y, X1, Y1);
682 
683 // Epetra_Flops * counter = this->GetFlopCounter();
684 // if (counter!=0) {
685 // L_->SetFlopCounter(*counter);
686 // Y1->SetFlopCounter(*counter);
687 // U_->SetFlopCounter(*counter);
688 // }
689 
690  if (!mode == Teuchos::NO_TRANS) {
691  U_->apply(*X1, *Y1,mode); //
692  Y1->update(1.0, *X1, 1.0); // Y1 = Y1 + X1 (account for implicit unit diagonal)
693  Y1->elementWiseMultiply(1.0, *D_, *Y1, 0.0); // y = D*y (D_ has inverse of diagonal)
694  Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> Y1temp(*Y1); // Need a temp copy of Y1
695  L_->apply(Y1temp, *Y1,mode);
696  Y1->update(1.0, Y1temp, 1.0); // (account for implicit unit diagonal)
697  if (isOverlapped_) {Y.doExport(*Y1,*L_->getGraph()->getExporter(), OverlapMode_);} // Export computed Y values if needed
698  }
699  else {
700 
701  L_->apply(*X1, *Y1,mode);
702  Y1->update(1.0, *X1, 1.0); // Y1 = Y1 + X1 (account for implicit unit diagonal)
703  Y1->elementWiseMultiply(1, *D_, *Y1, 0); // y = D*y (D_ has inverse of diagonal)
704  Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> Y1temp(*Y1); // Need a temp copy of Y1
705  U_->apply(Y1temp, *Y1,mode);
706  Y1->update(1.0, Y1temp, 1.0); // (account for implicit unit diagonal)
707  if (isOverlapped_) {Y.doExport(*Y1,*L_->getGraph()->getExporter(), OverlapMode_);}
708  }
709  return(0);
710 }
711 
712 //=============================================================================
713 template<class MatrixType>
714 typename Teuchos::ScalarTraits<typename MatrixType::scalar_type>::magnitudeType
715 RILUK<MatrixType>::computeCondEst(Teuchos::ETransp mode) const {
716 
717  if (Condest_>=0.0) {
718  return Condest_;
719  }
720  // Create a vector with all values equal to one
721  Tpetra::Vector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> Ones(U_->getDomainMap());
722  Tpetra::Vector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> OnesResult(L_->getRangeMap());
723  Ones.putScalar(1.0);
724 
725  apply(Ones, OnesResult,mode); // Compute the effect of the solve on the vector of ones
726  OnesResult.abs(OnesResult); // Make all values non-negative
727  Teuchos::Array<magnitude_type> norms(1);
728  OnesResult.normInf(norms());
729  Condest_ = norms[0];
730  return Condest_;
731 }
732 
733 
734 //=========================================================================
735 template<class MatrixType>
736 void RILUK<MatrixType>::generateXY(Teuchos::ETransp mode,
737  const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Xin,
738  const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Yin,
739  Teuchos::RCP<const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> >& Xout,
740  Teuchos::RCP<Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> >& Yout) const {
741 
742  // Generate an X and Y suitable for performing Solve() and Multiply() methods
743 
744  TEUCHOS_TEST_FOR_EXCEPTION(Xin.getNumVectors()!=Yin.getNumVectors(), std::runtime_error,
745  "Ifpack2::RILUK::GenerateXY ERROR: X and Y not the same size");
746 
747  //cout << "Xin = " << Xin << endl;
748  Xout = Teuchos::rcp( (const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> *) &Xin, false );
749  Yout = Teuchos::rcp( (Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> *) &Yin, false );
750  if (!isOverlapped_) return; // Nothing more to do
751 
752  if (isOverlapped_) {
753  // Make sure the number of vectors in the multivector is the same as before.
754  if (OverlapX_!=Teuchos::null) {
755  if (OverlapX_->getNumVectors()!=Xin.getNumVectors()) {
756  OverlapX_ = Teuchos::null;
757  OverlapY_ = Teuchos::null;
758  }
759  }
760  if (OverlapX_==Teuchos::null) { // Need to allocate space for overlap X and Y
761  OverlapX_ = Teuchos::rcp( new Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>(U_->getColMap(), Xout->getNumVectors()) );
762  OverlapY_ = Teuchos::rcp( new Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>(L_->getRowMap(), Yout->getNumVectors()) );
763  }
764  if (mode == Teuchos::NO_TRANS) {
765  OverlapX_->doImport(*Xout,*U_->getGraph()->getImporter(), Tpetra::INSERT); // Import X values for solve
766  }
767  else {
768  OverlapX_->doImport(*Xout,*L_->getGraph()->getExporter(), Tpetra::INSERT); // Import X values for solve
769  }
770  Xout = OverlapX_;
771  Yout = OverlapY_; // Set pointers for Xout and Yout to point to overlap space
772  //cout << "OverlapX_ = " << *OverlapX_ << endl;
773  }
774 }
775 
776 }//namespace Ifpack2
777 
778 #endif
779 
magnitude_type computeCondEst(Teuchos::ETransp mode) const
Returns the maximum over all the condition number estimate for each local ILU set of factors...
Definition: Ifpack2_RILUK_def.hpp:715
MatrixType::global_ordinal_type global_ordinal_type
The type of global indices in the input MatrixType.
Definition: Ifpack2_RILUK_decl.hpp:232
void initialize()
Initialize by computing the symbolic incomplete factorization.
Definition: Ifpack2_RILUK_def.hpp:325
void setParameters(const Teuchos::ParameterList &params)
Definition: Ifpack2_RILUK_def.hpp:125
ILU(k) (incomplete LU with fill level k) factorization of a given Tpetra::RowMatrix.
Definition: Ifpack2_RILUK_decl.hpp:214
bool isComputed() const
Whether compute() has been called.
Definition: Ifpack2_RILUK_decl.hpp:312
int Multiply(const Tpetra::MultiVector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &X, Tpetra::MultiVector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS) const
Apply the incomplete factorization (as a product) to X, resulting in Y.
Definition: Ifpack2_RILUK_def.hpp:671
MatrixType::local_ordinal_type local_ordinal_type
The type of local indices in the input MatrixType.
Definition: Ifpack2_RILUK_decl.hpp:225
virtual ~RILUK()
Ifpack2_RILUK Destructor.
Definition: Ifpack2_RILUK_def.hpp:97
void compute()
Compute the (numeric) incomplete factorization.
Definition: Ifpack2_RILUK_def.hpp:471
void apply(const Tpetra::MultiVector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &X, Tpetra::MultiVector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, scalar_type alpha=Teuchos::ScalarTraits< scalar_type >::one(), scalar_type beta=Teuchos::ScalarTraits< scalar_type >::zero()) const
Apply the (inverse of the) incomplete factorization to X, resulting in Y.
Definition: Ifpack2_RILUK_def.hpp:615
Teuchos::ScalarTraits< scalar_type >::magnitudeType magnitude_type
The type of the magnitude (absolute value) of a matrix entry.
Definition: Ifpack2_RILUK_decl.hpp:246
A class for constructing level filled graphs for use with ILU(k) class preconditioners.
Definition: Ifpack2_IlukGraph.hpp:83
bool isInitialized() const
Whether initialize() has been called.
Definition: Ifpack2_RILUK_decl.hpp:296
Ifpack2::Container: a pure virtual class for creating and solving local linear problems.
Definition: Ifpack2_AdditiveSchwarz_decl.hpp:67
RILUK(const Teuchos::RCP< const MatrixType > &A_in)
RILUK constuctor with variable number of indices per row.
Definition: Ifpack2_RILUK_def.hpp:50
MatrixType::scalar_type scalar_type
The type of the entries of the input MatrixType.
Definition: Ifpack2_RILUK_decl.hpp:218