Ifpack2 Templated Preconditioning Package  Version 1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Ifpack2_ILUT_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 //-----------------------------------------------------
44 // Ifpack2::ILUT is a translation of the Aztec ILUT
45 // implementation. The Aztec ILUT implementation was
46 // written by Ray Tuminaro.
47 // See notes below, in the Ifpack2::ILUT::Compute method.
48 // ABW.
49 //------------------------------------------------------
50 
51 #ifndef IFPACK2_ILUT_DEF_HPP
52 #define IFPACK2_ILUT_DEF_HPP
53 
54 namespace Ifpack2 {
55 
56  namespace {
57 
82  template<class ScalarType>
84  ilutDefaultDropTolerance () {
85  using Teuchos::as;
87  typedef typename STS::magnitudeType magnitude_type;
89 
90  // 1/2. Hopefully this can be represented in magnitude_type.
91  const magnitude_type oneHalf = STM::one() / (STM::one() + STM::one());
92 
93  // The min ensures that in case magnitude_type has very low
94  // precision, we'll at least get some value strictly less than
95  // one.
96  return std::min (as<magnitude_type> (1000) * STS::magnitude (STS::eps ()), oneHalf);
97  }
98 
99  // Full specialization for ScalarType = double.
100  // This specialization preserves ILUT's previous default behavior.
101  template<>
103  ilutDefaultDropTolerance<double> () {
104  return 1e-12;
105  }
106 
107  } // namespace (anonymous)
108 
109 //Definitions for the ILUT methods:
110 
111 //==============================================================================
112 template <class MatrixType>
113 ILUT<MatrixType>::ILUT(const Teuchos::RCP<const Tpetra::RowMatrix<scalar_type,local_ordinal_type,global_ordinal_type,node_type> >& A) :
114  A_ (A),
115  Athresh_ (Teuchos::ScalarTraits<magnitude_type>::zero ()),
116  Rthresh_ (Teuchos::ScalarTraits<magnitude_type>::one ()),
117  RelaxValue_ (Teuchos::ScalarTraits<magnitude_type>::zero ()),
118  LevelOfFill_ (Teuchos::ScalarTraits<magnitude_type>::one ()),
119  DropTolerance_ (ilutDefaultDropTolerance<scalar_type> ()),
120  Condest_ (-Teuchos::ScalarTraits<magnitude_type>::one ()),
121  InitializeTime_(0.0),
122  ComputeTime_(0.0),
123  ApplyTime_(0.0),
124  NumInitialize_(0),
125  NumCompute_(0),
126  NumApply_(0),
127  IsInitialized_ (false),
128  IsComputed_ (false)
129 {
131  A_.is_null (), std::runtime_error,
132  "Ifpack2::ILUT: Input matrix is null.");
133 }
134 
135 //==========================================================================
136 template <class MatrixType>
138 }
139 
140 //==========================================================================
141 template <class MatrixType>
143  using Teuchos::as;
146 
147  // Default values of the various parameters.
148  magnitude_type fillLevel = STM::one ();
149  magnitude_type absThresh = STM::zero ();
150  magnitude_type relThresh = STM::one ();
151  magnitude_type relaxValue = STM::zero ();
152  magnitude_type dropTol = ilutDefaultDropTolerance<scalar_type> ();
153 
154  try {
155  fillLevel = params.get<magnitude_type> ("fact: ilut level-of-fill");
156  }
157  catch (InvalidParameterType&) {
158  // Try double, for backwards compatibility.
159  // The cast from double to magnitude_type must succeed.
160  fillLevel = as<magnitude_type> (params.get<double> ("fact: ilut level-of-fill"));
161  }
162  catch (InvalidParameterName&) {
163  // Accept the default value.
164  }
165 
166  // FIXME (mfh 28 Nov 2012) This doesn't make any sense. Isn't zero
167  // fill allowed? The exception test doesn't match the exception
168  // message. However, I'm leaving it alone for now, so as not to
169  // change current behavior.
170  TEUCHOS_TEST_FOR_EXCEPTION(fillLevel <= 0.0, std::runtime_error,
171  "Ifpack2::ILUT::SetParameters ERROR, level-of-fill must be >= 0.");
172 
173  try {
174  absThresh = params.get<magnitude_type> ("fact: absolute threshold");
175  }
176  catch (InvalidParameterType&) {
177  // Try double, for backwards compatibility.
178  // The cast from double to magnitude_type must succeed.
179  absThresh = as<magnitude_type> (params.get<double> ("fact: absolute threshold"));
180  }
181  catch (InvalidParameterName&) {
182  // Accept the default value.
183  }
184 
185  try {
186  relThresh = params.get<magnitude_type> ("fact: relative threshold");
187  }
188  catch (InvalidParameterType&) {
189  // Try double, for backwards compatibility.
190  // The cast from double to magnitude_type must succeed.
191  relThresh = as<magnitude_type> (params.get<double> ("fact: relative threshold"));
192  }
193  catch (InvalidParameterName&) {
194  // Accept the default value.
195  }
196 
197  try {
198  relaxValue = params.get<magnitude_type> ("fact: relax value");
199  }
200  catch (InvalidParameterType&) {
201  // Try double, for backwards compatibility.
202  // The cast from double to magnitude_type must succeed.
203  relaxValue = as<magnitude_type> (params.get<double> ("fact: relax value"));
204  }
205  catch (InvalidParameterName&) {
206  // Accept the default value.
207  }
208 
209  try {
210  dropTol = params.get<magnitude_type> ("fact: drop tolerance");
211  }
212  catch (InvalidParameterType&) {
213  // Try double, for backwards compatibility.
214  // The cast from double to magnitude_type must succeed.
215  dropTol = as<magnitude_type> (params.get<double> ("fact: drop tolerance"));
216  }
217  catch (InvalidParameterName&) {
218  // Accept the default value.
219  }
220 
221  // "Commit" the values only after validating all of them. This
222  // ensures that there are no side effects if this routine throws an
223  // exception.
224 
225  // mfh 28 Nov 2012: The previous code would not assign Athresh_,
226  // Rthresh_, RelaxValue_, or DropTolerance_ if the read-in value was
227  // -1. I don't know if keeping this behavior is correct, but I'll
228  // keep it just so as not to change previous behavior.
229 
230  LevelOfFill_ = fillLevel;
231  if (absThresh != -STM::one ()) {
232  Athresh_ = absThresh;
233  }
234  if (relThresh != -STM::one ()) {
235  Rthresh_ = relThresh;
236  }
237  if (relaxValue != -STM::one ()) {
238  RelaxValue_ = relaxValue;
239  }
240  if (dropTol != -STM::one ()) {
241  DropTolerance_ = dropTol;
242  }
243 }
244 
245 //==========================================================================
246 template <class MatrixType>
249  return A_->getComm ();
250 }
251 
252 //==========================================================================
253 template <class MatrixType>
256  return A_;
257 }
258 
259 //==========================================================================
260 template <class MatrixType>
263 {
264  return A_->getDomainMap();
265 }
266 
267 //==========================================================================
268 template <class MatrixType>
271 {
272  return A_->getRangeMap();
273 }
274 
275 //==============================================================================
276 template <class MatrixType>
278  return true;
279 }
280 
281 //==========================================================================
282 template <class MatrixType>
284  return(NumInitialize_);
285 }
286 
287 //==========================================================================
288 template <class MatrixType>
290  return(NumCompute_);
291 }
292 
293 //==========================================================================
294 template <class MatrixType>
296  return(NumApply_);
297 }
298 
299 //==========================================================================
300 template <class MatrixType>
302  return(InitializeTime_);
303 }
304 
305 //==========================================================================
306 template<class MatrixType>
308  return(ComputeTime_);
309 }
310 
311 //==========================================================================
312 template<class MatrixType>
314  return(ApplyTime_);
315 }
316 
317 //==========================================================================
318 template<class MatrixType>
320  return(L_->getGlobalNumEntries() + U_->getGlobalNumEntries());
321 }
322 
323 //==========================================================================
324 template<class MatrixType>
326  return(L_->getNodeNumEntries() + U_->getNodeNumEntries());
327 }
328 
329 //=============================================================================
330 template<class MatrixType>
334  local_ordinal_type MaxIters,
335  magnitude_type Tol,
336  const Teuchos::Ptr<const Tpetra::RowMatrix<scalar_type,local_ordinal_type,global_ordinal_type,node_type> > &matrix) {
337 
338  if (! isComputed ()) {
339  return -STM::one ();
340  }
341  // NOTE: this is computing the *local* condest
342  if (Condest_ == -STM::one ()) {
343  Condest_ = Ifpack2::Condest(*this, CT, MaxIters, Tol, matrix);
344  }
345  return(Condest_);
346 }
347 
348 //==========================================================================
349 template<class MatrixType>
351  Teuchos::Time timer ("ILUT::initialize");
352  {
353  Teuchos::TimeMonitor timeMon (timer);
354 
355  // clear any previous allocation
356  IsInitialized_ = false;
357  IsComputed_ = false;
358  L_ = Teuchos::null;
359  U_ = Teuchos::null;
360 
361  // check only in serial if the matrix is square
363  getComm ()->getSize () == 1 && A_->getNodeNumRows() != A_->getNodeNumCols(),
364  std::runtime_error, "Ifpack2::ILUT::initialize: In serial or one-process "
365  "mode, the input matrix must be square.");
366  }
367  IsInitialized_ = true;
368  ++NumInitialize_;
369  InitializeTime_ += timer.totalElapsedTime ();
370 }
371 
372 template<typename ScalarType>
373 typename Teuchos::ScalarTraits<ScalarType>::magnitudeType scalar_mag(const ScalarType& s)
374 {
376 }
377 
378 //==========================================================================
379 template<class MatrixType>
381  using Teuchos::Array;
382  using Teuchos::ArrayRCP;
383  using Teuchos::ArrayView;
384  using Teuchos::as;
385  using Teuchos::rcp;
386  using Teuchos::reduceAll;
387 
388  //--------------------------------------------------------------------------
389  // Ifpack2::ILUT is a translation of the Aztec ILUT implementation. The Aztec
390  // ILUT implementation was written by Ray Tuminaro.
391  //
392  // This isn't an exact translation of the Aztec ILUT algorithm, for the
393  // following reasons:
394  // 1. Minor differences result from the fact that Aztec factors a MSR format
395  // matrix in place, while the code below factors an input CrsMatrix which
396  // remains untouched and stores the resulting factors in separate L and U
397  // CrsMatrix objects.
398  // Also, the Aztec code begins by shifting the matrix pointers back
399  // by one, and the pointer contents back by one, and then using 1-based
400  // Fortran-style indexing in the algorithm. This Ifpack2 code uses C-style
401  // 0-based indexing throughout.
402  // 2. Aztec stores the inverse of the diagonal of U. This Ifpack2 code
403  // stores the non-inverted diagonal in U.
404  // The triangular solves (in Ifpack2::ILUT::apply()) are performed by
405  // calling the Tpetra::CrsMatrix::solve method on the L and U objects, and
406  // this requires U to contain the non-inverted diagonal.
407  //
408  // ABW.
409  //--------------------------------------------------------------------------
410 
411  // Don't count initialization in the compute() time.
412  if (! isInitialized ()) {
413  initialize ();
414  }
415 
416  Teuchos::Time timer ("ILUT::compute");
417  { // Timer scope for timing compute()
418  Teuchos::TimeMonitor timeMon (timer, true);
419  const scalar_type zero = STS::zero ();
420  const scalar_type one = STS::one ();
421 
422  const local_ordinal_type myNumRows = A_->getNodeNumRows ();
423  L_ = rcp (new MatrixType (A_->getRowMap (), A_->getColMap (), 0));
424  U_ = rcp (new MatrixType (A_->getRowMap (), A_->getColMap (), 0));
425 
426  // CGB: note, this caching approach may not be necessary anymore
427  // We will store ArrayView objects that are views of the rows of U, so that
428  // we don't have to repeatedly retrieve the view for each row. These will
429  // be populated row by row as the factorization proceeds.
430  Array<ArrayView<const local_ordinal_type> > Uindices (myNumRows);
431  Array<ArrayView<const scalar_type> > Ucoefs (myNumRows);
432 
433  // If this macro is defined, files containing the L and U factors
434  // will be written. DON'T CHECK IN THE CODE WITH THIS MACRO ENABLED!!!
435  // #define IFPACK2_WRITE_FACTORS
436 #ifdef IFPACK2_WRITE_FACTORS
437  std::ofstream ofsL("L.tif.mtx", std::ios::out);
438  std::ofstream ofsU("U.tif.mtx", std::ios::out);
439 #endif
440 
441  // mfh 28 Nov 2012: The code here uses double for fill calculations.
442  // This really has nothing to do with magnitude_type. The point is
443  // to avoid rounding and overflow for integer calculations. That
444  // should work just fine for reasonably-sized integer values, so I'm
445  // leaving it. However, the fill level parameter is a
446  // magnitude_type, so I do need to cast to double. Typical values
447  // of the fill level are small, so this should not cause overflow.
448 
449  // Calculate how much fill will be allowed in addition to the space that
450  // corresponds to the input matrix entries.
451  double local_nnz = static_cast<double>(A_->getNodeNumEntries());
452  double fill;
453  {
454  const double fillLevel = as<double> (getLevelOfFill ());
455  fill = ((fillLevel - 1) * local_nnz) / (2 * myNumRows);
456  }
457 
458  // std::ceil gives the smallest integer larger than the argument.
459  // this may give a slightly different result than Aztec's fill value in
460  // some cases.
461  double fill_ceil=std::ceil(fill);
462 
463  // Similarly to Aztec, we will allow the same amount of fill for each
464  // row, half in L and half in U.
465  size_type fillL = static_cast<size_type>(fill_ceil);
466  size_type fillU = static_cast<size_type>(fill_ceil);
467 
468  Array<scalar_type> InvDiagU (myNumRows, zero);
469 
470  Array<local_ordinal_type> tmp_idx;
471  Array<scalar_type> tmpv;
472 
473  enum { UNUSED, ORIG, FILL };
474  local_ordinal_type max_col = myNumRows;
475 
476  Array<int> pattern(max_col, UNUSED);
477  Array<scalar_type> cur_row(max_col, zero);
478  Array<magnitude_type> unorm(max_col);
479  magnitude_type rownorm;
480  Array<local_ordinal_type> L_cols_heap;
481  Array<local_ordinal_type> U_cols;
482  Array<local_ordinal_type> L_vals_heap;
483  Array<local_ordinal_type> U_vals_heap;
484 
485  // A comparison object which will be used to create 'heaps' of indices
486  // that are ordered according to the corresponding values in the
487  // 'cur_row' array.
488  greater_indirect<scalar_type,local_ordinal_type> vals_comp(cur_row);
489 
490  // =================== //
491  // start factorization //
492  // =================== //
493 
494  ArrayRCP<local_ordinal_type> ColIndicesARCP;
495  ArrayRCP<scalar_type> ColValuesARCP;
496  if (! A_->supportsRowViews ()) {
497  const size_t maxnz = A_->getNodeMaxNumRowEntries ();
498  ColIndicesARCP.resize (maxnz);
499  ColValuesARCP.resize (maxnz);
500  }
501 
502  for (local_ordinal_type row_i = 0 ; row_i < myNumRows ; ++row_i) {
503  ArrayView<const local_ordinal_type> ColIndicesA;
504  ArrayView<const scalar_type> ColValuesA;
505  size_t RowNnz;
506 
507  if (A_->supportsRowViews ()) {
508  A_->getLocalRowView (row_i, ColIndicesA, ColValuesA);
509  RowNnz = ColIndicesA.size ();
510  }
511  else {
512  A_->getLocalRowCopy (row_i, ColIndicesARCP (), ColValuesARCP (), RowNnz);
513  ColIndicesA = ColIndicesARCP (0, RowNnz);
514  ColValuesA = ColValuesARCP (0, RowNnz);
515  }
516 
517  // Always include the diagonal in the U factor. The value should get
518  // set in the next loop below.
519  U_cols.push_back(row_i);
520  cur_row[row_i] = zero;
521  pattern[row_i] = ORIG;
522 
523  size_type L_cols_heaplen = 0;
524  rownorm = STM::zero ();
525  for (size_t i = 0; i < RowNnz; ++i) {
526  if (ColIndicesA[i] < myNumRows) {
527  if (ColIndicesA[i] < row_i) {
528  add_to_heap(ColIndicesA[i], L_cols_heap, L_cols_heaplen);
529  }
530  else if (ColIndicesA[i] > row_i) {
531  U_cols.push_back(ColIndicesA[i]);
532  }
533 
534  cur_row[ColIndicesA[i]] = ColValuesA[i];
535  pattern[ColIndicesA[i]] = ORIG;
536  rownorm += scalar_mag(ColValuesA[i]);
537  }
538  }
539 
540  // Alter the diagonal according to the absolute-threshold and
541  // relative-threshold values. If not set, those values default
542  // to zero and one respectively.
543  const magnitude_type rthresh = getRelativeThreshold();
544  const scalar_type& v = cur_row[row_i];
545  cur_row[row_i] = as<scalar_type> (getAbsoluteThreshold() * IFPACK2_SGN(v)) + rthresh*v;
546 
547  size_type orig_U_len = U_cols.size();
548  RowNnz = L_cols_heap.size() + orig_U_len;
549  rownorm = getDropTolerance() * rownorm/RowNnz;
550 
551  // The following while loop corresponds to the 'L30' goto's in Aztec.
552  size_type L_vals_heaplen = 0;
553  while (L_cols_heaplen > 0) {
554  local_ordinal_type row_k = L_cols_heap.front();
555 
556  scalar_type multiplier = cur_row[row_k] * InvDiagU[row_k];
557  cur_row[row_k] = multiplier;
558  magnitude_type mag_mult = scalar_mag(multiplier);
559  if (mag_mult*unorm[row_k] < rownorm) {
560  pattern[row_k] = UNUSED;
561  rm_heap_root(L_cols_heap, L_cols_heaplen);
562  continue;
563  }
564  if (pattern[row_k] != ORIG) {
565  if (L_vals_heaplen < fillL) {
566  add_to_heap(row_k, L_vals_heap, L_vals_heaplen, vals_comp);
567  }
568  else if (L_vals_heaplen==0 ||
569  mag_mult < scalar_mag(cur_row[L_vals_heap.front()])) {
570  pattern[row_k] = UNUSED;
571  rm_heap_root(L_cols_heap, L_cols_heaplen);
572  continue;
573  }
574  else {
575  pattern[L_vals_heap.front()] = UNUSED;
576  rm_heap_root(L_vals_heap, L_vals_heaplen, vals_comp);
577  add_to_heap(row_k, L_vals_heap, L_vals_heaplen, vals_comp);
578  }
579  }
580 
581  /* Reduce current row */
582 
583  ArrayView<const local_ordinal_type>& ColIndicesU = Uindices[row_k];
584  ArrayView<const scalar_type>& ColValuesU = Ucoefs[row_k];
585  size_type ColNnzU = ColIndicesU.size();
586 
587  for(size_type j=0; j<ColNnzU; ++j) {
588  if (ColIndicesU[j] > row_k) {
589  scalar_type tmp = multiplier * ColValuesU[j];
590  local_ordinal_type col_j = ColIndicesU[j];
591  if (pattern[col_j] != UNUSED) {
592  cur_row[col_j] -= tmp;
593  }
594  else if (scalar_mag(tmp) > rownorm) {
595  cur_row[col_j] = -tmp;
596  pattern[col_j] = FILL;
597  if (col_j > row_i) {
598  U_cols.push_back(col_j);
599  }
600  else {
601  add_to_heap(col_j, L_cols_heap, L_cols_heaplen);
602  }
603  }
604  }
605  }
606 
607  rm_heap_root(L_cols_heap, L_cols_heaplen);
608  }//end of while(L_cols_heaplen) loop
609 
610 
611  // Put indices and values for L into arrays and then into the L_ matrix.
612 
613  // first, the original entries from the L section of A:
614  for (size_type i = 0; i < ColIndicesA.size (); ++i) {
615  if (ColIndicesA[i] < row_i) {
616  tmp_idx.push_back(ColIndicesA[i]);
617  tmpv.push_back(cur_row[ColIndicesA[i]]);
618  pattern[ColIndicesA[i]] = UNUSED;
619  }
620  }
621 
622  // next, the L entries resulting from fill:
623  for (size_type j = 0; j < L_vals_heaplen; ++j) {
624  tmp_idx.push_back(L_vals_heap[j]);
625  tmpv.push_back(cur_row[L_vals_heap[j]]);
626  pattern[L_vals_heap[j]] = UNUSED;
627  }
628 
629  // L has a one on the diagonal, but we don't explicitly store it.
630  // If we don't store it, then the Tpetra/Kokkos kernel which performs
631  // the triangular solve can assume a unit diagonal, take a short-cut
632  // and perform faster.
633 
634  L_->insertLocalValues (row_i, tmp_idx (), tmpv ());
635 #ifdef IFPACK2_WRITE_FACTORS
636  for (size_type ii = 0; ii < tmp_idx.size (); ++ii) {
637  ofsL << row_i << " " << tmp_idx[ii] << " " << tmpv[ii] << std::endl;
638  }
639 #endif
640 
641  tmp_idx.clear();
642  tmpv.clear();
643 
644  // Pick out the diagonal element, store its reciprocal.
645  if (cur_row[row_i] == zero) {
646  std::cerr << "Ifpack2::ILUT::Compute: zero pivot encountered! Replacing with rownorm and continuing...(You may need to set the parameter 'fact: absolute threshold'.)" << std::endl;
647  cur_row[row_i] = rownorm;
648  }
649  InvDiagU[row_i] = one / cur_row[row_i];
650 
651  // Non-inverted diagonal is stored for U:
652  tmp_idx.push_back(row_i);
653  tmpv.push_back(cur_row[row_i]);
654  unorm[row_i] = scalar_mag(cur_row[row_i]);
655  pattern[row_i] = UNUSED;
656 
657  // Now put indices and values for U into arrays and then into the U_ matrix.
658  // The first entry in U_cols is the diagonal, which we just handled, so we'll
659  // start our loop at j=1.
660 
661  size_type U_vals_heaplen = 0;
662  for(size_type j=1; j<U_cols.size(); ++j) {
663  local_ordinal_type col = U_cols[j];
664  if (pattern[col] != ORIG) {
665  if (U_vals_heaplen < fillU) {
666  add_to_heap(col, U_vals_heap, U_vals_heaplen, vals_comp);
667  }
668  else if (U_vals_heaplen!=0 && scalar_mag(cur_row[col]) >
669  scalar_mag(cur_row[U_vals_heap.front()])) {
670  rm_heap_root(U_vals_heap, U_vals_heaplen, vals_comp);
671  add_to_heap(col, U_vals_heap, U_vals_heaplen, vals_comp);
672  }
673  }
674  else {
675  tmp_idx.push_back(col);
676  tmpv.push_back(cur_row[col]);
677  unorm[row_i] += scalar_mag(cur_row[col]);
678  }
679  pattern[col] = UNUSED;
680  }
681 
682  for(size_type j=0; j<U_vals_heaplen; ++j) {
683  tmp_idx.push_back(U_vals_heap[j]);
684  tmpv.push_back(cur_row[U_vals_heap[j]]);
685  unorm[row_i] += scalar_mag(cur_row[U_vals_heap[j]]);
686  }
687 
688  unorm[row_i] /= (orig_U_len + U_vals_heaplen);
689 
690  U_->insertLocalValues(row_i, tmp_idx(), tmpv() );
691 #ifdef IFPACK2_WRITE_FACTORS
692  for(int ii=0; ii<tmp_idx.size(); ++ii) {
693  ofsU <<row_i<< " " <<tmp_idx[ii]<< " " <<tmpv[ii]<< std::endl;
694  }
695 #endif
696  tmp_idx.clear();
697  tmpv.clear();
698 
699  U_->getLocalRowView(row_i, Uindices[row_i], Ucoefs[row_i] );
700 
701  L_cols_heap.clear();
702  U_cols.clear();
703  L_vals_heap.clear();
704  U_vals_heap.clear();
705  } // end of for(row_i) loop
706 
707  // FIXME (mfh 03 Apr 2013) Do we need to supply a domain and range Map?
708  L_->fillComplete();
709  U_->fillComplete();
710  }
711  ComputeTime_ += timer.totalElapsedTime ();
712  IsComputed_ = true;
713  ++NumCompute_;
714 }
715 
716 //==========================================================================
717 template <class MatrixType>
719  const Tpetra::MultiVector<typename MatrixType::scalar_type, typename MatrixType::local_ordinal_type, typename MatrixType::global_ordinal_type, typename MatrixType::node_type>& X,
720  Tpetra::MultiVector<typename MatrixType::scalar_type, typename MatrixType::local_ordinal_type, typename MatrixType::global_ordinal_type, typename MatrixType::node_type>& Y,
721  Teuchos::ETransp mode,
722  typename MatrixType::scalar_type alpha,
723  typename MatrixType::scalar_type beta) const
724 {
725  this->template applyTempl<scalar_type,scalar_type>(X, Y, mode, alpha, beta);
726 }
727 
728 //==========================================================================
729 template <class MatrixType>
730 template <class DomainScalar, class RangeScalar>
732  const Tpetra::MultiVector<DomainScalar, typename MatrixType::local_ordinal_type, typename MatrixType::global_ordinal_type, typename MatrixType::node_type>& X,
733  Tpetra::MultiVector<RangeScalar, typename MatrixType::local_ordinal_type, typename MatrixType::global_ordinal_type, typename MatrixType::node_type>& Y,
734  Teuchos::ETransp mode,
735  RangeScalar alpha,
736  RangeScalar beta) const
737 {
738  using Teuchos::RCP;
739  using Teuchos::rcp;
740  typedef Tpetra::MultiVector<DomainScalar, local_ordinal_type, global_ordinal_type, node_type> MV;
741 
742  Teuchos::Time timer ("ILUT::apply");
743  { // Timer scope for timing apply()
744  Teuchos::TimeMonitor timeMon (timer, true);
745 
747  ! isComputed (), std::runtime_error,
748  "Ifpack2::ILUT::apply: You must call compute() to compute the incomplete "
749  "factorization, before calling apply().");
750 
752  X.getNumVectors() != Y.getNumVectors(), std::runtime_error,
753  "Ifpack2::ILUT::apply: X and Y must have the same number of columns. "
754  "X has " << X.getNumVectors () << " columns, but Y has "
755  << Y.getNumVectors () << " columns.");
756 
758  beta != STS::zero (), std::logic_error,
759  "Ifpack2::ILUT::apply: This method does not currently work when beta != 0.");
760 
761  // If X and Y are pointing to the same memory location,
762  // we need to create an auxiliary vector, Xcopy
763  RCP<const MV> Xcopy;
764  if (X.getLocalMV ().getValues () == Y.getLocalMV ().getValues ()) {
765  Xcopy = rcp (new MV (X));
766  }
767  else {
768  Xcopy = rcpFromRef (X);
769  }
770 
771  if (mode == Teuchos::NO_TRANS) { // Solve L U Y = X
772  L_->template localSolve<RangeScalar,DomainScalar> (*Xcopy, Y, Teuchos::NO_TRANS);
773  U_->template localSolve<RangeScalar,RangeScalar> (Y, Y, Teuchos::NO_TRANS);
774  }
775  else { // Solve U^* L^* Y = X
776  U_->template localSolve<RangeScalar,DomainScalar> (*Xcopy, Y, mode);
777  L_->template localSolve<RangeScalar,RangeScalar> (Y, Y, mode);
778  }
779 
780  if (alpha != STS::one ()) {
781  Y.scale (alpha);
782  }
783  }
784  ++NumApply_;
785  ApplyTime_ += timer.totalElapsedTime ();
786 }
787 
788 //=============================================================================
789 template <class MatrixType>
790 std::string ILUT<MatrixType>::description() const {
791  std::ostringstream oss;
793  if (isInitialized()) {
794  if (isComputed()) {
795  oss << "{status: [initialized, computed]";
796  }
797  else {
798  oss << "{status: [initialized, not computed]";
799  }
800  }
801  else {
802  oss << "{status: [not initialized, not computed]";
803  }
804  oss << ", global number of rows: " << A_->getGlobalNumRows()
805  << ", global number of columns: " << A_->getGlobalNumCols()
806  << "}";
807  return oss.str();
808 }
809 
810 //=============================================================================
811 template <class MatrixType>
813  using std::endl;
814  using std::setw;
815  using Teuchos::VERB_DEFAULT;
816  using Teuchos::VERB_NONE;
817  using Teuchos::VERB_LOW;
818  using Teuchos::VERB_MEDIUM;
819  using Teuchos::VERB_HIGH;
820  using Teuchos::VERB_EXTREME;
821 
822  const Teuchos::EVerbosityLevel vl = (verbLevel == VERB_DEFAULT) ? VERB_LOW : verbLevel;
823  Teuchos::OSTab tab (out);
824  // none: print nothing
825  // low: print O(1) info from node 0
826  // medium:
827  // high:
828  // extreme:
829  if (vl != VERB_NONE && getComm ()->getRank () == 0) {
830  out << this->description() << endl;
831  out << endl;
832  out << "===============================================================================" << endl;
833  out << "Level-of-fill = " << getLevelOfFill() << endl;
834  out << "Absolute threshold = " << getAbsoluteThreshold() << endl;
835  out << "Relative threshold = " << getRelativeThreshold() << endl;
836  out << "Relax value = " << getRelaxValue() << endl;
837  if (Condest_ == -1.0) { out << "Condition number estimate = N/A" << endl; }
838  else { out << "Condition number estimate = " << Condest_ << endl; }
839  if (isComputed()) {
840  out << "Number of nonzeros in A = " << A_->getGlobalNumEntries() << endl;
841  out << "Number of nonzeros in L + U = " << getGlobalNumEntries()
842  << " ( = " << 100.0 * (double)getGlobalNumEntries() / (double)A_->getGlobalNumEntries() << " % of A)" << endl;
843  out << "nonzeros / rows = " << 1.0 * getGlobalNumEntries() / U_->getGlobalNumRows() << endl;
844  }
845  out << endl;
846  out << "Phase # calls Total Time (s) " << endl;
847  out << "------------ ------- ---------------" << endl;
848  out << "initialize() " << setw(7) << getNumInitialize() << " " << setw(15) << getInitializeTime() << endl;
849  out << "compute() " << setw(7) << getNumCompute() << " " << setw(15) << getComputeTime() << endl;
850  out << "apply() " << setw(7) << getNumApply() << " " << setw(15) << getApplyTime() << endl;
851  out << "===============================================================================" << endl;
852  out << endl;
853  }
854 }
855 
856 
857 }//namespace Ifpack2
858 
859 #endif /* IFPACK2_ILUT_DEF_HPP */
860 
CondestType
Ifpack2::CondestType: enum to define the type of condition number estimate.
Definition: Ifpack2_CondestType.hpp:50
bool hasTransposeApply() const
Whether this object&#39;s apply() method can apply the transpose (or conjugate transpose, if applicable).
Definition: Ifpack2_ILUT_def.hpp:277
virtual ~ILUT()
Destructor.
Definition: Ifpack2_ILUT_def.hpp:137
T & get(const std::string &name, T def_value)
const Teuchos::RCP< const Tpetra::Map< local_ordinal_type, global_ordinal_type, node_type > > & getDomainMap() const
Tpetra::Map representing the domain of this operator.
Definition: Ifpack2_ILUT_def.hpp:262
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Teuchos::ScalarTraits< scalar_type >::magnitudeType magnitude_type
The type of the magnitude (absolute value) of a matrix entry.
Definition: Ifpack2_ILUT_decl.hpp:135
global_size_t getGlobalNumEntries() const
Returns the number of nonzero entries in the global graph.
Definition: Ifpack2_ILUT_def.hpp:319
void initialize()
Clear any previously computed factors.
Definition: Ifpack2_ILUT_def.hpp:350
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print the object with some verbosity level to an FancyOStream object.
Definition: Ifpack2_ILUT_def.hpp:812
std::string description() const
Return a simple one-line description of this object.
Definition: Ifpack2_ILUT_def.hpp:790
size_t getNodeNumEntries() const
Returns the number of nonzero entries in the local graph.
Definition: Ifpack2_ILUT_def.hpp:325
ILUT(const Teuchos::RCP< const Tpetra::RowMatrix< scalar_type, local_ordinal_type, global_ordinal_type, node_type > > &A)
Constructor.
Definition: Ifpack2_ILUT_def.hpp:113
void rm_heap_root(Teuchos::Array< Ordinal > &heap, SizeType &heap_len)
Definition: Ifpack2_Heap.hpp:91
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Teuchos::RCP< const Tpetra::RowMatrix< scalar_type, local_ordinal_type, global_ordinal_type, node_type > > getMatrix() const
Returns a reference to the matrix to be preconditioned.
Definition: Ifpack2_ILUT_def.hpp:255
magnitude_type computeCondEst(CondestType CT=Cheap, local_ordinal_type MaxIters=1550, magnitude_type Tol=1e-9, const Teuchos::Ptr< const Tpetra::RowMatrix< scalar_type, local_ordinal_type, global_ordinal_type, node_type > > &Matrix_in=Teuchos::null)
Computes the estimated condition number and returns the value.
Definition: Ifpack2_ILUT_def.hpp:333
virtual std::string description() const
MatrixType::scalar_type scalar_type
The type of the entries of the input MatrixType.
Definition: Ifpack2_ILUT_decl.hpp:107
const Teuchos::RCP< const Teuchos::Comm< int > > & getComm() const
Returns the Tpetra::BlockMap object associated with the range of this matrix operator.
Definition: Ifpack2_ILUT_def.hpp:248
void applyTempl(const Tpetra::MultiVector< DomainScalar, local_ordinal_type, global_ordinal_type, node_type > &X, Tpetra::MultiVector< RangeScalar, local_ordinal_type, global_ordinal_type, node_type > &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, RangeScalar alpha=Teuchos::ScalarTraits< scalar_type >::one(), RangeScalar beta=Teuchos::ScalarTraits< scalar_type >::zero()) const
Returns the result of a ILUT forward/back solve on a Tpetra::MultiVector X in Y.
const Teuchos::RCP< const Tpetra::Map< local_ordinal_type, global_ordinal_type, node_type > > & getRangeMap() const
Tpetra::Map representing the range of this operator.
Definition: Ifpack2_ILUT_def.hpp:270
TEUCHOS_DEPRECATED void reduceAll(const Comm< Ordinal > &comm, const EReductionType reductType, const Packet &send, Packet *globalReduct)
void compute()
Compute factors L and U using the specified diagonal perturbation thresholds and relaxation parameter...
Definition: Ifpack2_ILUT_def.hpp:380
static magnitudeType magnitude(T a)
int getNumInitialize() const
Returns the number of calls to Initialize().
Definition: Ifpack2_ILUT_def.hpp:283
double getApplyTime() const
Returns the time spent in apply().
Definition: Ifpack2_ILUT_def.hpp:313
TypeTo as(const TypeFrom &t)
double totalElapsedTime(bool readCurrentTime=false) const
void add_to_heap(const Ordinal &idx, Teuchos::Array< Ordinal > &heap, SizeType &heap_len)
Definition: Ifpack2_Heap.hpp:69
double getInitializeTime() const
Returns the time spent in Initialize().
Definition: Ifpack2_ILUT_def.hpp:301
int getNumApply() const
Returns the number of calls to apply().
Definition: Ifpack2_ILUT_def.hpp:295
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
Returns the result of a ILUT forward/back solve on a Tpetra::MultiVector X in Y.
Definition: Ifpack2_ILUT_def.hpp:718
double getComputeTime() const
Returns the time spent in Compute().
Definition: Ifpack2_ILUT_def.hpp:307
void setParameters(const Teuchos::ParameterList &params)
Set parameters for the preconditioner.
Definition: Ifpack2_ILUT_def.hpp:142
int getNumCompute() const
Returns the number of calls to Compute().
Definition: Ifpack2_ILUT_def.hpp:289
MatrixType::local_ordinal_type local_ordinal_type
The type of local indices in the input MatrixType.
Definition: Ifpack2_ILUT_decl.hpp:114
bool is_null() const