Ifpack2 Templated Preconditioning Package  Version 1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Ifpack2_LocalSparseTriangularSolver_def.hpp
1 /*@HEADER
2 // ***********************************************************************
3 //
4 // Ifpack2: Templated 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_LOCALSPARSETRIANGULARSOLVER_DEF_HPP
44 #define IFPACK2_LOCALSPARSETRIANGULARSOLVER_DEF_HPP
45 
46 #include "Tpetra_CrsMatrix.hpp"
47 #include "Tpetra_Core.hpp"
48 #include "Teuchos_StandardParameterEntryValidators.hpp"
49 #include "Tpetra_Details_determineLocalTriangularStructure.hpp"
50 
51 #ifdef HAVE_IFPACK2_SHYLU_NODEHTS
52 # include "shylu_hts.hpp"
53 #endif
54 
55 namespace Ifpack2 {
56 
57 namespace Details {
58 struct TrisolverType {
59  enum Enum {
60  Internal,
61  HTS
62  };
63 
64  static void loadPLTypeOption (Teuchos::Array<std::string>& type_strs, Teuchos::Array<Enum>& type_enums) {
65  type_strs.resize(2);
66  type_strs[0] = "Internal";
67  type_strs[1] = "HTS";
68  type_enums.resize(2);
69  type_enums[0] = Internal;
70  type_enums[1] = HTS;
71  }
72 };
73 }
74 
75 template<class MatrixType>
76 class LocalSparseTriangularSolver<MatrixType>::HtsImpl {
77 public:
78  typedef Tpetra::CrsMatrix<scalar_type, local_ordinal_type, global_ordinal_type, node_type> crs_matrix_type;
79 
80  void reset () {
81 #ifdef HAVE_IFPACK2_SHYLU_NODEHTS
82  Timpl_ = Teuchos::null;
83  levelset_block_size_ = 1;
84 #endif
85  }
86 
87  void setParameters (const Teuchos::ParameterList& pl) {
88  (void)pl;
89 #ifdef HAVE_IFPACK2_SHYLU_NODEHTS
90  const char* block_size_s = "trisolver: block size";
91  if (pl.isParameter(block_size_s)) {
92  TEUCHOS_TEST_FOR_EXCEPT_MSG( ! pl.isType<int>(block_size_s),
93  "The parameter \"" << block_size_s << "\" must be of type int.");
94  levelset_block_size_ = pl.get<int>(block_size_s);
95  }
96  if (levelset_block_size_ < 1)
97  levelset_block_size_ = 1;
98 #endif
99  }
100 
101  // HTS has the phases symbolic+numeric, numeric, and apply. Hence the first
102  // call to compute() will trigger the symbolic+numeric phase, and subsequent
103  // calls (with the same Timpl_) will trigger the numeric phase. In the call to
104  // initialize(), essentially nothing happens.
105  void initialize (const crs_matrix_type& /* unused */) {
106 #ifdef HAVE_IFPACK2_SHYLU_NODEHTS
107  reset();
108  transpose_ = conjugate_ = false;
109 #endif
110  }
111 
112  void compute (const crs_matrix_type& T_in, const Teuchos::RCP<Teuchos::FancyOStream>& out) {
113  (void)T_in;
114  (void)out;
115 #ifdef HAVE_IFPACK2_SHYLU_NODEHTS
116  using Teuchos::ArrayRCP;
117 
121  T_in.getAllValues(rowptr, colidx, val);
122 
123  Teuchos::RCP<HtsCrsMatrix> T_hts = Teuchos::rcpWithDealloc(
124  HTST::make_CrsMatrix(rowptr.size() - 1,
125  rowptr.getRawPtr(), colidx.getRawPtr(), val.getRawPtr(),
126  transpose_, conjugate_),
127  HtsCrsMatrixDeleter());
128 
129  if (Teuchos::nonnull(Timpl_)) {
130  // Reuse the nonzero pattern.
131  HTST::reprocess_numeric(Timpl_.get(), T_hts.get());
132  } else {
133  // Build from scratch.
134  if (T_in.getCrsGraph().is_null()) {
135  if (Teuchos::nonnull(out))
136  *out << "HTS compute failed because T_in.getCrsGraph().is_null().\n";
137  return;
138  }
139  if ( ! T_in.getCrsGraph()->isSorted()) {
140  if (Teuchos::nonnull(out))
141  *out << "HTS compute failed because ! T_in.getCrsGraph().isSorted().\n";
142  return;
143  }
144  if ( ! T_in.isStorageOptimized()) {
145  if (Teuchos::nonnull(out))
146  *out << "HTS compute failed because ! T_in.isStorageOptimized().\n";
147  return;
148  }
149 
150  typename HTST::PreprocessArgs args;
151  args.T = T_hts.get();
152  args.max_nrhs = 1;
153 #ifdef _OPENMP
154  args.nthreads = omp_get_max_threads();
155 #else
156  args.nthreads = 1;
157 #endif
158  args.save_for_reprocess = true;
159  typename HTST::Options opts;
160  opts.levelset_block_size = levelset_block_size_;
161  args.options = &opts;
162 
163  try {
164  Timpl_ = Teuchos::rcpWithDealloc(HTST::preprocess(args), TImplDeleter());
165  } catch (const std::exception& e) {
166  if (Teuchos::nonnull(out))
167  *out << "HTS preprocess threw: " << e.what() << "\n";
168  }
169  }
170 #endif
171  }
172 
173  // HTS may not be able to handle a matrix, so query whether compute()
174  // succeeded.
175  bool isComputed () {
176 #ifdef HAVE_IFPACK2_SHYLU_NODEHTS
177  return Teuchos::nonnull(Timpl_);
178 #else
179  return false;
180 #endif
181  }
182 
183  // Y := beta * Y + alpha * (M * X)
184  void localApply (const MV& X, MV& Y,
185  const Teuchos::ETransp /* mode */,
186  const scalar_type& alpha, const scalar_type& beta) const {
187  (void)X;
188  (void)Y;
189  (void)alpha;
190  (void)beta;
191 #ifdef HAVE_IFPACK2_SHYLU_NODEHTS
192 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
193  const auto& X_view = X.template getLocalView<Kokkos::HostSpace> ();
194  const auto& Y_view = Y.template getLocalView<Kokkos::HostSpace> ();
195 #else
196  const auto& X_view = X.getLocalViewHost ();
197  const auto& Y_view = Y.getLocalViewHost ();
198 #endif
199  // Only does something if #rhs > current capacity.
200  HTST::reset_max_nrhs(Timpl_.get(), X_view.extent(1));
201  // Switch alpha and beta because of HTS's opposite convention.
202  HTST::solve_omp(Timpl_.get(),
203  // For std/Kokkos::complex.
204  reinterpret_cast<const scalar_type*>(X_view.data()),
205  X_view.extent(1),
206  // For std/Kokkos::complex.
207  reinterpret_cast<scalar_type*>(Y_view.data()),
208  beta, alpha);
209 #endif
210  }
211 
212 private:
213 #ifdef HAVE_IFPACK2_SHYLU_NODEHTS
214  typedef ::Experimental::HTS<local_ordinal_type, size_t, scalar_type> HTST;
215  typedef typename HTST::Impl TImpl;
216  typedef typename HTST::CrsMatrix HtsCrsMatrix;
217 
218  struct TImplDeleter {
219  void free (TImpl* impl) {
220  HTST::delete_Impl(impl);
221  }
222  };
223 
224  struct HtsCrsMatrixDeleter {
225  void free (HtsCrsMatrix* T) {
226  HTST::delete_CrsMatrix(T);
227  }
228  };
229 
230  Teuchos::RCP<TImpl> Timpl_;
231  bool transpose_, conjugate_;
232  int levelset_block_size_;
233 #endif
234 };
235 
236 template<class MatrixType>
239  A_ (A)
240 {
241  initializeState();
242  typedef typename Tpetra::CrsMatrix<scalar_type, local_ordinal_type,
244  if (! A.is_null ()) {
246  Teuchos::rcp_dynamic_cast<const crs_matrix_type> (A);
248  (A_crs.is_null (), std::invalid_argument,
249  "Ifpack2::LocalSparseTriangularSolver constructor: "
250  "The input matrix A is not a Tpetra::CrsMatrix.");
251  A_crs_ = A_crs;
252  }
253 }
254 
255 template<class MatrixType>
259  A_ (A),
260  out_ (out)
261 {
262  initializeState();
263  if (! out_.is_null ()) {
264  *out_ << ">>> DEBUG Ifpack2::LocalSparseTriangularSolver constructor"
265  << std::endl;
266  }
267  typedef typename Tpetra::CrsMatrix<scalar_type, local_ordinal_type,
269  if (! A.is_null ()) {
271  Teuchos::rcp_dynamic_cast<const crs_matrix_type> (A);
273  (A_crs.is_null (), std::invalid_argument,
274  "Ifpack2::LocalSparseTriangularSolver constructor: "
275  "The input matrix A is not a Tpetra::CrsMatrix.");
276  A_crs_ = A_crs;
277  }
278 }
279 
280 template<class MatrixType>
283 {
284  initializeState();
285 }
286 
287 template<class MatrixType>
290  out_ (out)
291 {
292  initializeState();
293  if (! out_.is_null ()) {
294  *out_ << ">>> DEBUG Ifpack2::LocalSparseTriangularSolver constructor"
295  << std::endl;
296  }
297 }
298 
299 template<class MatrixType>
301 {
302  isInitialized_ = false;
303  isComputed_ = false;
304  reverseStorage_ = false;
305  isInternallyChanged_ = false;
306  numInitialize_ = 0;
307  numCompute_ = 0;
308  numApply_ = 0;
309  initializeTime_ = 0.0;
310  computeTime_ = 0.0;
311  applyTime_ = 0.0;
312  uplo_ = "N";
313  diag_ = "N";
314 }
315 
316 template<class MatrixType>
319 {}
320 
321 template<class MatrixType>
322 void
325 {
326  using Teuchos::RCP;
328  using Teuchos::Array;
329 
330  Details::TrisolverType::Enum trisolverType = Details::TrisolverType::Internal;
331  do {
332  static const char typeName[] = "trisolver: type";
333 
334  if ( ! pl.isType<std::string>(typeName)) break;
335 
336  // Map std::string <-> TrisolverType::Enum.
337  Array<std::string> trisolverTypeStrs;
338  Array<Details::TrisolverType::Enum> trisolverTypeEnums;
339  Details::TrisolverType::loadPLTypeOption (trisolverTypeStrs, trisolverTypeEnums);
341  s2i(trisolverTypeStrs (), trisolverTypeEnums (), typeName, false);
342 
343  trisolverType = s2i.getIntegralValue(pl.get<std::string>(typeName));
344  } while (0);
345 
346  if (trisolverType == Details::TrisolverType::HTS) {
347  htsImpl_ = Teuchos::rcp (new HtsImpl ());
348  htsImpl_->setParameters (pl);
349  }
350 
351  if (pl.isParameter("trisolver: reverse U"))
352  reverseStorage_ = pl.get<bool>("trisolver: reverse U");
353 
355  (reverseStorage_ && trisolverType == Details::TrisolverType::HTS,
356  std::logic_error, "Ifpack2::LocalSparseTriangularSolver::setParameters: "
357  "You are not allowed to enable both HTS and the \"trisolver: reverse U\" "
358  "options. See GitHub issue #2647.");
359 }
360 
361 template<class MatrixType>
362 void
365 {
366  using Tpetra::Details::determineLocalTriangularStructure;
367  using crs_matrix_type = Tpetra::CrsMatrix<scalar_type, local_ordinal_type,
369  using local_matrix_type = typename crs_matrix_type::local_matrix_type;
370  using LO = local_ordinal_type;
371 
372  const char prefix[] = "Ifpack2::LocalSparseTriangularSolver::initialize: ";
373  if (! out_.is_null ()) {
374  *out_ << ">>> DEBUG " << prefix << std::endl;
375  }
376 
378  (A_.is_null (), std::runtime_error, prefix << "You must call "
379  "setMatrix() with a nonnull input matrix before you may call "
380  "initialize() or compute().");
381  if (A_crs_.is_null ()) {
382  auto A_crs = Teuchos::rcp_dynamic_cast<const crs_matrix_type> (A_);
384  (A_crs.get () == nullptr, std::invalid_argument,
385  prefix << "The input matrix A is not a Tpetra::CrsMatrix.");
386  A_crs_ = A_crs;
387  }
388  auto G = A_crs_->getGraph ();
390  (G.is_null (), std::logic_error, prefix << "A_ and A_crs_ are nonnull, "
391  "but A_crs_'s RowGraph G is null. "
392  "Please report this bug to the Ifpack2 developers.");
393  // At this point, the graph MUST be fillComplete. The "initialize"
394  // (symbolic) part of setup only depends on the graph structure, so
395  // the matrix itself need not be fill complete.
397  (! G->isFillComplete (), std::runtime_error, "If you call this method, "
398  "the matrix's graph must be fill complete. It is not.");
399 
400  // mfh 30 Apr 2018: See GitHub Issue #2658.
401  constexpr bool ignoreMapsForTriStructure = true;
402  auto lclTriStructure = [&] {
403  auto lclMatrix = A_crs_->getLocalMatrix ();
404  auto lclRowMap = A_crs_->getRowMap ()->getLocalMap ();
405  auto lclColMap = A_crs_->getColMap ()->getLocalMap ();
406  auto lclTriStruct =
407  determineLocalTriangularStructure (lclMatrix.graph,
408  lclRowMap,
409  lclColMap,
410  ignoreMapsForTriStructure);
411  const LO lclNumRows = lclRowMap.getNodeNumElements ();
412  this->diag_ = (lclTriStruct.diagCount < lclNumRows) ? "U" : "N";
413  this->uplo_ = lclTriStruct.couldBeLowerTriangular ? "L" :
414  (lclTriStruct.couldBeUpperTriangular ? "U" : "N");
415  return lclTriStruct;
416  } ();
417 
418  if (reverseStorage_ && lclTriStructure.couldBeUpperTriangular &&
419  htsImpl_.is_null ()) {
420  // Reverse the storage for an upper triangular matrix
421  auto Alocal = A_crs_->getLocalMatrix();
422  auto ptr = Alocal.graph.row_map;
423  auto ind = Alocal.graph.entries;
424  auto val = Alocal.values;
425 
426  auto numRows = Alocal.numRows();
427  auto numCols = Alocal.numCols();
428  auto numNnz = Alocal.nnz();
429 
430  typename decltype(ptr)::non_const_type newptr ("ptr", ptr.extent (0));
431  typename decltype(ind)::non_const_type newind ("ind", ind.extent (0));
432  decltype(val) newval ("val", val.extent (0));
433 
434  // FIXME: The code below assumes UVM
435  crs_matrix_type::execution_space::fence();
436  newptr(0) = 0;
437  for (local_ordinal_type row = 0, rowStart = 0; row < numRows; ++row) {
438  auto A_r = Alocal.row(numRows-1 - row);
439 
440  auto numEnt = A_r.length;
441  for (local_ordinal_type k = 0; k < numEnt; ++k) {
442  newind(rowStart + k) = numCols-1 - A_r.colidx(numEnt-1 - k);
443  newval(rowStart + k) = A_r.value (numEnt-1 - k);
444  }
445  rowStart += numEnt;
446  newptr(row+1) = rowStart;
447  }
448  crs_matrix_type::execution_space::fence();
449 
450  // Reverse maps
451  using map_type = typename crs_matrix_type::map_type;
452  Teuchos::RCP<map_type> newRowMap, newColMap;
453  {
454  // Reverse row map
455  auto rowMap = A_->getRowMap();
456  auto numElems = rowMap->getNodeNumElements();
457  auto rowElems = rowMap->getNodeElementList();
458 
459  Teuchos::Array<global_ordinal_type> newRowElems(rowElems.size());
460  for (size_t i = 0; i < numElems; i++)
461  newRowElems[i] = rowElems[numElems-1 - i];
462 
463  newRowMap = Teuchos::rcp(new map_type(rowMap->getGlobalNumElements(), newRowElems, rowMap->getIndexBase(), rowMap->getComm()));
464  }
465  {
466  // Reverse column map
467  auto colMap = A_->getColMap();
468  auto numElems = colMap->getNodeNumElements();
469  auto colElems = colMap->getNodeElementList();
470 
471  Teuchos::Array<global_ordinal_type> newColElems(colElems.size());
472  for (size_t i = 0; i < numElems; i++)
473  newColElems[i] = colElems[numElems-1 - i];
474 
475  newColMap = Teuchos::rcp(new map_type(colMap->getGlobalNumElements(), newColElems, colMap->getIndexBase(), colMap->getComm()));
476  }
477 
478  // Construct new matrix
479  local_matrix_type newLocalMatrix("Upermuted", numRows, numCols, numNnz, newval, newptr, newind);
480 
481  A_crs_ = Teuchos::rcp(new crs_matrix_type(newLocalMatrix, newRowMap, newColMap, A_crs_->getDomainMap(), A_crs_->getRangeMap()));
482 
483  isInternallyChanged_ = true;
484 
485  // FIXME (mfh 18 Apr 2019) Recomputing this is unnecessary, but I
486  // didn't want to break any invariants, especially considering
487  // that this branch is likely poorly tested.
488  auto newLclTriStructure =
489  determineLocalTriangularStructure (newLocalMatrix.graph,
490  newRowMap->getLocalMap (),
491  newColMap->getLocalMap (),
492  ignoreMapsForTriStructure);
493  const LO newLclNumRows = newRowMap->getNodeNumElements ();
494  this->diag_ = (newLclTriStructure.diagCount < newLclNumRows) ? "U" : "N";
495  this->uplo_ = newLclTriStructure.couldBeLowerTriangular ? "L" :
496  (newLclTriStructure.couldBeUpperTriangular ? "U" : "N");
497  }
498 
499  if (Teuchos::nonnull (htsImpl_))
500  {
501  htsImpl_->initialize (*A_crs_);
502  isInternallyChanged_ = true;
503  }
504 
505  isInitialized_ = true;
506  ++numInitialize_;
507 }
508 
509 template<class MatrixType>
510 void
513 {
514  const char prefix[] = "Ifpack2::LocalSparseTriangularSolver::compute: ";
515  if (! out_.is_null ()) {
516  *out_ << ">>> DEBUG " << prefix << std::endl;
517  }
518 
520  (A_.is_null (), std::runtime_error, prefix << "You must call "
521  "setMatrix() with a nonnull input matrix before you may call "
522  "initialize() or compute().");
524  (A_crs_.is_null (), std::logic_error, prefix << "A_ is nonnull, but "
525  "A_crs_ is null. Please report this bug to the Ifpack2 developers.");
526  // At this point, the matrix MUST be fillComplete.
528  (! A_crs_->isFillComplete (), std::runtime_error, "If you call this "
529  "method, the matrix must be fill complete. It is not.");
530 
531  if (! isInitialized_) {
532  initialize ();
533  }
535  (! isInitialized_, std::logic_error, prefix << "initialize() should have "
536  "been called by this point, but isInitialized_ is false. "
537  "Please report this bug to the Ifpack2 developers.");
538 
539  if (Teuchos::nonnull (htsImpl_))
540  htsImpl_->compute (*A_crs_, out_);
541 
542  isComputed_ = true;
543  ++numCompute_;
544 }
545 
546 template<class MatrixType>
548 apply (const Tpetra::MultiVector<scalar_type, local_ordinal_type,
550  Tpetra::MultiVector<scalar_type, local_ordinal_type,
552  Teuchos::ETransp mode,
553  scalar_type alpha,
554  scalar_type beta) const
555 {
556  using Teuchos::RCP;
557  using Teuchos::rcp;
558  using Teuchos::rcpFromRef;
559  typedef scalar_type ST;
560  typedef Teuchos::ScalarTraits<ST> STS;
561  const char prefix[] = "Ifpack2::LocalSparseTriangularSolver::apply: ";
562  if (! out_.is_null ()) {
563  *out_ << ">>> DEBUG " << prefix;
564  if (A_crs_.is_null ()) {
565  *out_ << "A_crs_ is null!" << std::endl;
566  }
567  else {
569  Teuchos::rcp_dynamic_cast<const crs_matrix_type> (A_);
570  const std::string uplo = this->uplo_;
571  const std::string trans = (mode == Teuchos::CONJ_TRANS) ? "C" :
572  (mode == Teuchos::TRANS ? "T" : "N");
573  const std::string diag = this->diag_;
574  *out_ << "uplo=\"" << uplo
575  << "\", trans=\"" << trans
576  << "\", diag=\"" << diag << "\"" << std::endl;
577  }
578  }
579 
581  (! isComputed (), std::runtime_error, prefix << "If compute() has not yet "
582  "been called, or if you have changed the matrix via setMatrix(), you must "
583  "call compute() before you may call this method.");
584  // If isComputed() is true, it's impossible for the matrix to be
585  // null, or for it not to be a Tpetra::CrsMatrix.
587  (A_.is_null (), std::logic_error, prefix << "A_ is null. "
588  "Please report this bug to the Ifpack2 developers.");
590  (A_crs_.is_null (), std::logic_error, prefix << "A_crs_ is null. "
591  "Please report this bug to the Ifpack2 developers.");
592  // However, it _is_ possible that the user called resumeFill() on
593  // the matrix, after calling compute(). This is NOT allowed.
595  (! A_crs_->isFillComplete (), std::runtime_error, "If you call this "
596  "method, the matrix must be fill complete. It is not. This means that "
597  " you must have called resumeFill() on the matrix before calling apply(). "
598  "This is NOT allowed. Note that this class may use the matrix's data in "
599  "place without copying it. Thus, you cannot change the matrix and expect "
600  "the solver to stay the same. If you have changed the matrix, first call "
601  "fillComplete() on it, then call compute() on this object, before you call"
602  " apply(). You do NOT need to call setMatrix, as long as the matrix "
603  "itself (that is, its address in memory) is the same.");
604 
605  auto G = A_crs_->getGraph ();
607  (G.is_null (), std::logic_error, prefix << "A_ and A_crs_ are nonnull, "
608  "but A_crs_'s RowGraph G is null. "
609  "Please report this bug to the Ifpack2 developers.");
610  auto importer = G->getImporter ();
611  auto exporter = G->getExporter ();
612 
613  if (! importer.is_null ()) {
614  if (X_colMap_.is_null () || X_colMap_->getNumVectors () != X.getNumVectors ()) {
615  X_colMap_ = rcp (new MV (importer->getTargetMap (), X.getNumVectors ()));
616  }
617  else {
618  X_colMap_->putScalar (STS::zero ());
619  }
620  // See discussion of Github Issue #672 for why the Import needs to
621  // use the ZERO CombineMode. The case where the Export is
622  // nontrivial is likely never exercised.
623  X_colMap_->doImport (X, *importer, Tpetra::ZERO);
624  }
625  RCP<const MV> X_cur = importer.is_null () ? rcpFromRef (X) :
626  Teuchos::rcp_const_cast<const MV> (X_colMap_);
627 
628  if (! exporter.is_null ()) {
629  if (Y_rowMap_.is_null () || Y_rowMap_->getNumVectors () != Y.getNumVectors ()) {
630  Y_rowMap_ = rcp (new MV (exporter->getSourceMap (), Y.getNumVectors ()));
631  }
632  else {
633  Y_rowMap_->putScalar (STS::zero ());
634  }
635  Y_rowMap_->doExport (Y, *importer, Tpetra::ADD);
636  }
637  RCP<MV> Y_cur = exporter.is_null () ? rcpFromRef (Y) : Y_rowMap_;
638 
639  localApply (*X_cur, *Y_cur, mode, alpha, beta);
640 
641  if (! exporter.is_null ()) {
642  Y.putScalar (STS::zero ());
643  Y.doExport (*Y_cur, *exporter, Tpetra::ADD);
644  }
645 
646  ++numApply_;
647 }
648 
649 template<class MatrixType>
650 void
652 localTriangularSolve (const MV& Y,
653  MV& X,
654  const Teuchos::ETransp mode) const
655 {
656  using Teuchos::CONJ_TRANS;
657  using Teuchos::NO_TRANS;
658  using Teuchos::TRANS;
659  const char tfecfFuncName[] = "localTriangularSolve: ";
660 
662  (! A_crs_->isFillComplete (), std::runtime_error,
663  "The matrix is not fill complete.");
665  (! X.isConstantStride () || ! Y.isConstantStride (), std::invalid_argument,
666  "X and Y must be constant stride.");
668  ( A_crs_->getNodeNumRows() > 0 && this->uplo_ == "N", std::runtime_error,
669  "The matrix is neither upper triangular or lower triangular. "
670  "You may only call this method if the matrix is triangular. "
671  "Remember that this is a local (per MPI process) property, and that "
672  "Tpetra only knows how to do a local (per process) triangular solve.");
675  (STS::isComplex && mode == TRANS, std::logic_error, "This method does "
676  "not currently support non-conjugated transposed solve (mode == "
677  "Teuchos::TRANS) for complex scalar types.");
678 
679  // FIXME (mfh 19 May 2016) This makes some Ifpack2 tests fail.
680  //
681  // TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
682  // (Y.template need_sync<device_type> () && !
683  // Y.template need_sync<Kokkos::HostSpace> (), std::runtime_error,
684  // "Y must be sync'd to device memory before you may call this method.");
685 
686  const std::string uplo = this->uplo_;
687  const std::string trans = (mode == Teuchos::CONJ_TRANS) ? "C" :
688  (mode == Teuchos::TRANS ? "T" : "N");
689  const std::string diag = this->diag_;
690  auto A_lcl = this->A_crs_->getLocalMatrix ();
691 
692  // NOTE (mfh 20 Aug 2017): KokkosSparse::trsv currently is a
693  // sequential, host-only code. See
694  // https://github.com/kokkos/kokkos-kernels/issues/48. This
695  // means that we need to sync to host, then sync back to device
696  // when done.
697  X.sync_host ();
698  const_cast<MV&> (Y).sync_host ();
699  X.modify_host (); // we will write to X
700 
701  if (X.isConstantStride () && Y.isConstantStride ()) {
702  auto X_lcl = X.getLocalViewHost ();
703  auto Y_lcl = Y.getLocalViewHost ();
704  KokkosSparse::trsv (uplo.c_str (), trans.c_str (), diag.c_str (),
705  A_lcl, Y_lcl, X_lcl);
706  }
707  else {
708  const size_t numVecs =
709  std::min (X.getNumVectors (), Y.getNumVectors ());
710  for (size_t j = 0; j < numVecs; ++j) {
711  auto X_j = X.getVector (j);
712  auto Y_j = X.getVector (j);
713  auto X_lcl = X_j->getLocalViewHost ();
714  auto Y_lcl = Y_j->getLocalViewHost ();
715  KokkosSparse::trsv (uplo.c_str (), trans.c_str (),
716  diag.c_str (), A_lcl, Y_lcl, X_lcl);
717  }
718  }
719 
720  X.sync_device ();
721  const_cast<MV&> (Y).sync_device ();
722 }
723 
724 template<class MatrixType>
725 void
726 LocalSparseTriangularSolver<MatrixType>::
727 localApply (const MV& X,
728  MV& Y,
729  const Teuchos::ETransp mode,
730  const scalar_type& alpha,
731  const scalar_type& beta) const
732 {
733  if (mode == Teuchos::NO_TRANS && Teuchos::nonnull (htsImpl_) &&
734  htsImpl_->isComputed ()) {
735  htsImpl_->localApply (X, Y, mode, alpha, beta);
736  return;
737  }
738 
739  using Teuchos::RCP;
740  typedef scalar_type ST;
741  typedef Teuchos::ScalarTraits<ST> STS;
742 
743  if (beta == STS::zero ()) {
744  if (alpha == STS::zero ()) {
745  Y.putScalar (STS::zero ()); // Y := 0 * Y (ignore contents of Y)
746  }
747  else { // alpha != 0
748  this->localTriangularSolve (X, Y, mode);
749  if (alpha != STS::one ()) {
750  Y.scale (alpha);
751  }
752  }
753  }
754  else { // beta != 0
755  if (alpha == STS::zero ()) {
756  Y.scale (beta); // Y := beta * Y
757  }
758  else { // alpha != 0
759  MV Y_tmp (Y, Teuchos::Copy);
760  this->localTriangularSolve (X, Y_tmp, mode); // Y_tmp := M * X
761  Y.update (alpha, Y_tmp, beta); // Y := beta * Y + alpha * Y_tmp
762  }
763  }
764 }
765 
766 
767 template <class MatrixType>
768 int
771  return numInitialize_;
772 }
773 
774 template <class MatrixType>
775 int
777 getNumCompute () const {
778  return numCompute_;
779 }
780 
781 template <class MatrixType>
782 int
784 getNumApply () const {
785  return numApply_;
786 }
787 
788 template <class MatrixType>
789 double
792  return initializeTime_;
793 }
794 
795 template<class MatrixType>
796 double
798 getComputeTime () const {
799  return computeTime_;
800 }
801 
802 template<class MatrixType>
803 double
805 getApplyTime() const {
806  return applyTime_;
807 }
808 
809 template <class MatrixType>
810 std::string
812 description () const
813 {
814  std::ostringstream os;
815 
816  // Output is a valid YAML dictionary in flow style. If you don't
817  // like everything on a single line, you should call describe()
818  // instead.
819  os << "\"Ifpack2::LocalSparseTriangularSolver\": {";
820  if (this->getObjectLabel () != "") {
821  os << "Label: \"" << this->getObjectLabel () << "\", ";
822  }
823  os << "Initialized: " << (isInitialized () ? "true" : "false") << ", "
824  << "Computed: " << (isComputed () ? "true" : "false") << ", ";
825 
826  if (A_.is_null ()) {
827  os << "Matrix: null";
828  }
829  else {
830  os << "Matrix: not null"
831  << ", Global matrix dimensions: ["
832  << A_->getGlobalNumRows () << ", "
833  << A_->getGlobalNumCols () << "]";
834  }
835 
836  if (Teuchos::nonnull (htsImpl_))
837  os << ", HTS computed: " << (htsImpl_->isComputed () ? "true" : "false");
838 
839  os << "}";
840  return os.str ();
841 }
842 
843 template <class MatrixType>
846  const Teuchos::EVerbosityLevel verbLevel) const
847 {
848  using std::endl;
849  // Default verbosity level is VERB_LOW, which prints only on Process
850  // 0 of the matrix's communicator.
851  const Teuchos::EVerbosityLevel vl
852  = (verbLevel == Teuchos::VERB_DEFAULT) ? Teuchos::VERB_LOW : verbLevel;
853 
854  if (vl != Teuchos::VERB_NONE) {
855  // Print only on Process 0 in the matrix's communicator. If the
856  // matrix is null, though, we have to get the communicator from
857  // somewhere, so we ask Tpetra for its default communicator. If
858  // MPI is enabled, this wraps MPI_COMM_WORLD or a clone thereof.
859  auto comm = A_.is_null () ?
860  Tpetra::getDefaultComm () :
861  A_->getComm ();
862 
863  // Users aren't supposed to do anything with the matrix on
864  // processes where its communicator is null.
865  if (! comm.is_null () && comm->getRank () == 0) {
866  // By convention, describe() should always begin with a tab.
867  Teuchos::OSTab tab0 (out);
868  // Output is in YAML format. We have to escape the class name,
869  // because it has a colon.
870  out << "\"Ifpack2::LocalSparseTriangularSolver\":" << endl;
871  Teuchos::OSTab tab1 (out);
872  out << "Scalar: " << Teuchos::TypeNameTraits<scalar_type>::name () << endl
873  << "LocalOrdinal: " << Teuchos::TypeNameTraits<local_ordinal_type>::name () << endl
874  << "GlobalOrdinal: " << Teuchos::TypeNameTraits<global_ordinal_type>::name () << endl
875  << "Node: " << Teuchos::TypeNameTraits<node_type>::name () << endl;
876  }
877  }
878 }
879 
880 template <class MatrixType>
883 getDomainMap () const
884 {
886  (A_.is_null (), std::runtime_error,
887  "Ifpack2::LocalSparseTriangularSolver::getDomainMap: "
888  "The matrix is null. Please call setMatrix() with a nonnull input "
889  "before calling this method.");
890  return A_->getDomainMap ();
891 }
892 
893 template <class MatrixType>
896 getRangeMap () const
897 {
899  (A_.is_null (), std::runtime_error,
900  "Ifpack2::LocalSparseTriangularSolver::getRangeMap: "
901  "The matrix is null. Please call setMatrix() with a nonnull input "
902  "before calling this method.");
903  return A_->getRangeMap ();
904 }
905 
906 template<class MatrixType>
909 {
910  const char prefix[] = "Ifpack2::LocalSparseTriangularSolver::setMatrix: ";
911 
912  // If the pointer didn't change, do nothing. This is reasonable
913  // because users are supposed to call this method with the same
914  // object over all participating processes, and pointer identity
915  // implies object identity.
916  if (A.getRawPtr () != A_.getRawPtr () || isInternallyChanged_) {
917  // Check in serial or one-process mode if the matrix is square.
919  (! A.is_null () && A->getComm ()->getSize () == 1 &&
920  A->getNodeNumRows () != A->getNodeNumCols (),
921  std::runtime_error, prefix << "If A's communicator only contains one "
922  "process, then A must be square. Instead, you provided a matrix A with "
923  << A->getNodeNumRows () << " rows and " << A->getNodeNumCols ()
924  << " columns.");
925 
926  // It's legal for A to be null; in that case, you may not call
927  // initialize() until calling setMatrix() with a nonnull input.
928  // Regardless, setting the matrix invalidates the preconditioner.
929  isInitialized_ = false;
930  isComputed_ = false;
931 
932  typedef typename Tpetra::CrsMatrix<scalar_type, local_ordinal_type,
934  if (A.is_null ()) {
935  A_crs_ = Teuchos::null;
936  A_ = Teuchos::null;
937  }
938  else { // A is not null
940  Teuchos::rcp_dynamic_cast<const crs_matrix_type> (A);
942  (A_crs.is_null (), std::invalid_argument, prefix <<
943  "The input matrix A is not a Tpetra::CrsMatrix.");
944  A_crs_ = A_crs;
945  A_ = A;
946  }
947 
948  if (Teuchos::nonnull (htsImpl_))
949  htsImpl_->reset ();
950  } // pointers are not the same
951 }
952 
953 } // namespace Ifpack2
954 
955 #define IFPACK2_LOCALSPARSETRIANGULARSOLVER_INSTANT(S,LO,GO,N) \
956  template class Ifpack2::LocalSparseTriangularSolver< Tpetra::RowMatrix<S, LO, GO, N> >;
957 
958 #endif // IFPACK2_LOCALSPARSETRIANGULARSOLVER_DEF_HPP
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 preconditioner to X, and put the result in Y.
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:548
int getNumCompute() const
Return the number of calls to compute().
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:777
std::string description() const
A one-line description of this object.
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:812
T & get(const std::string &name, T def_value)
bool nonnull(const std::shared_ptr< T > &p)
#define TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(throw_exception_test, Exception, msg)
void compute()
&quot;Numeric&quot; phase of setup
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:512
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
MatrixType::global_ordinal_type global_ordinal_type
Type of the global indices of the input matrix.
Definition: Ifpack2_LocalSparseTriangularSolver_decl.hpp:93
T * getRawPtr() const
bool isComputed() const
Return true if compute() has been called.
Definition: Ifpack2_LocalSparseTriangularSolver_decl.hpp:209
T * get() const
double getApplyTime() const
Return the time spent in apply().
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:805
size_type size() const
double getComputeTime() const
Return the time spent in compute().
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:798
double getInitializeTime() const
Return the time spent in initialize().
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:791
MatrixType::node_type node_type
Node type of the input matrix.
Definition: Ifpack2_LocalSparseTriangularSolver_decl.hpp:95
bool isParameter(const std::string &name) const
T * getRawPtr() const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
#define TEUCHOS_TEST_FOR_EXCEPT_MSG(throw_exception_test, msg)
Teuchos::RCP< const map_type > getDomainMap() const
The domain of this operator.
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:883
&quot;Preconditioner&quot; that solves local sparse triangular systems.
Definition: Ifpack2_LocalSparseTriangularSolver_decl.hpp:77
void initialize()
&quot;Symbolic&quot; phase of setup
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:364
int getNumInitialize() const
Return the number of calls to initialize().
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:770
Tpetra::Map< local_ordinal_type, global_ordinal_type, node_type > map_type
Specialization of Tpetra::Map used by this class.
Definition: Ifpack2_LocalSparseTriangularSolver_decl.hpp:100
virtual void setMatrix(const Teuchos::RCP< const row_matrix_type > &A)
Set this preconditioner&#39;s matrix.
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:908
void resize(size_type new_size, const value_type &x=value_type())
IntegralType getIntegralValue(const std::string &str, const std::string &paramName="", const std::string &sublistName="") const
int getNumApply() const
Return the number of calls to apply().
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:784
MatrixType::local_ordinal_type local_ordinal_type
Type of the local indices of the input matrix.
Definition: Ifpack2_LocalSparseTriangularSolver_decl.hpp:91
Teuchos::RCP< const map_type > getRangeMap() const
The range of this operator.
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:896
bool isType(const std::string &name) const
Tpetra::CrsMatrix< scalar_type, local_ordinal_type, global_ordinal_type, node_type > crs_matrix_type
Specialization of Tpetra::CrsMatrix used by this class.
Definition: Ifpack2_LocalSparseTriangularSolver_decl.hpp:106
void setParameters(const Teuchos::ParameterList &params)
Set this object&#39;s parameters.
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:324
MatrixType::scalar_type scalar_type
Type of the entries of the input matrix.
Definition: Ifpack2_LocalSparseTriangularSolver_decl.hpp:89
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print this object with given verbosity to the given output stream.
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:845
virtual ~LocalSparseTriangularSolver()
Destructor (virtual for memory safety).
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:318
LocalSparseTriangularSolver()
Constructor that takes no input matrix.
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:282
static std::string name()
std::string typeName(const T &t)
bool is_null() const