Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Tpetra_LocalCrsMatrixOperator_def.hpp
1 // @HEADER
2 // *****************************************************************************
3 // Tpetra: Templated Linear Algebra Services Package
4 //
5 // Copyright 2008 NTESS and the Tpetra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef TPETRA_LOCALCRSMATRIXOPERATOR_DEF_HPP
11 #define TPETRA_LOCALCRSMATRIXOPERATOR_DEF_HPP
12 
13 #include "Tpetra_LocalOperator.hpp"
15 #include "KokkosSparse.hpp"
16 #include "Teuchos_TestForException.hpp"
17 #include "Teuchos_OrdinalTraits.hpp"
18 
19 namespace Tpetra {
20 
21 template<class MultiVectorScalar, class MatrixScalar, class Device>
22 LocalCrsMatrixOperator<MultiVectorScalar, MatrixScalar, Device>::
23 LocalCrsMatrixOperator (const std::shared_ptr<local_matrix_device_type>& A)
24  : A_ (A), have_A_cusparse(false)
25 {
26  const char tfecfFuncName[] = "LocalCrsMatrixOperator: ";
27  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
28  (A_.get () == nullptr, std::invalid_argument,
29  "Input matrix A is null.");
30 }
31 
32 template<class MultiVectorScalar, class MatrixScalar, class Device>
33 LocalCrsMatrixOperator<MultiVectorScalar, MatrixScalar, Device>::
34 LocalCrsMatrixOperator (const std::shared_ptr<local_matrix_device_type>& A, const ordinal_view_type& A_ordinal_rowptrs) :
35  A_ (A),
36  A_cusparse("LocalCrsMatrixOperator_cuSPARSE", A->numRows(), A->numCols(), A->nnz(),
37  A->values, A_ordinal_rowptrs, A->graph.entries),
38  have_A_cusparse(true)
39 {
40  const char tfecfFuncName[] = "LocalCrsMatrixOperator: ";
41  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
42  (A_.get () == nullptr, std::invalid_argument,
43  "Input matrix A is null.");
44 }
45 
46 template<class MultiVectorScalar, class MatrixScalar, class Device>
47 bool
48 LocalCrsMatrixOperator<MultiVectorScalar, MatrixScalar, Device>::
49 hasTransposeApply () const
50 {
51  return true;
52 }
53 
54 template<class MultiVectorScalar, class MatrixScalar, class Device>
55 void
56 LocalCrsMatrixOperator<MultiVectorScalar, MatrixScalar, Device>::
57 apply (Kokkos::View<const mv_scalar_type**, array_layout,
58  device_type, Kokkos::MemoryTraits<Kokkos::Unmanaged> > X,
59  Kokkos::View<mv_scalar_type**, array_layout,
60  device_type, Kokkos::MemoryTraits<Kokkos::Unmanaged> > Y,
61  const Teuchos::ETransp mode,
62  const mv_scalar_type alpha,
63  const mv_scalar_type beta) const
64 {
65  const bool conjugate = (mode == Teuchos::CONJ_TRANS);
66  const bool transpose = (mode != Teuchos::NO_TRANS);
67 
68 #ifdef HAVE_TPETRA_DEBUG
69  const char tfecfFuncName[] = "apply: ";
70 
71  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
72  (X.extent (1) != Y.extent (1), std::runtime_error,
73  "X.extent(1) = " << X.extent (1) << " != Y.extent(1) = "
74  << Y.extent (1) << ".");
75  // If the two pointers are NULL, then they don't alias one
76  // another, even though they are equal.
77  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
78  (X.data () == Y.data () && X.data () != nullptr,
79  std::runtime_error, "X and Y may not alias one another.");
80 #endif // HAVE_TPETRA_DEBUG
81 
82  const auto op = transpose ?
83  (conjugate ? KokkosSparse::ConjugateTranspose :
84  KokkosSparse::Transpose) : KokkosSparse::NoTranspose;
85  if(have_A_cusparse)
86  {
87  KokkosSparse::spmv (op, alpha, A_cusparse, X, beta, Y);
88  }
89  else
90  {
91  KokkosSparse::spmv (op, alpha, *A_, X, beta, Y);
92  }
93 }
94 
97 template<class MultiVectorScalar, class MatrixScalar, class Device>
98 void
101  Kokkos::View<const mv_scalar_type**, array_layout,
102  device_type, Kokkos::MemoryTraits<Kokkos::Unmanaged> > X,
103  Kokkos::View<mv_scalar_type**, array_layout,
104  device_type, Kokkos::MemoryTraits<Kokkos::Unmanaged> > Y,
105  const Teuchos::ETransp mode,
106  const mv_scalar_type alpha,
107  const mv_scalar_type beta) const
108 {
109  apply(X, Y, mode, alpha, beta);
110 }
111 
112 template<class MultiVectorScalar, class MatrixScalar, class Device>
113 const typename LocalCrsMatrixOperator<MultiVectorScalar, MatrixScalar, Device>::local_matrix_device_type&
115 getLocalMatrixDevice () const
116 {
117  return *A_;
118 }
119 
120 } // namespace Tpetra
121 
122 //
123 // Explicit instantiation macro
124 //
125 // Must be expanded from within the Tpetra namespace!
126 //
127 
128 // We only explicitly instantiate for MultiVectorScalar ==
129 // MatrixScalar, which is what CrsMatrix needs.
130 
131 #define TPETRA_LOCALCRSMATRIXOPERATOR_INSTANT(SC,NT) \
132  template class LocalCrsMatrixOperator< SC, SC, NT::device_type >;
133 
134 // If we want mixed versions, we use this macro.
135 
136 #define TPETRA_LOCALCRSMATRIXOPERATOR_MIXED_INSTANT(SC,MATSC,LO,GO,NT) \
137  template class LocalCrsMatrixOperator< SC, MATSC, NT::device_type >;
138 
139 #endif // TPETRA_LOCALCRSMATRIXOPERATOR_DEF_HPP
Abstract interface for local operators (e.g., matrices and preconditioners).
void applyImbalancedRows(Kokkos::View< const mv_scalar_type **, array_layout, device_type, Kokkos::MemoryTraits< Kokkos::Unmanaged > > X, Kokkos::View< mv_scalar_type **, array_layout, device_type, Kokkos::MemoryTraits< Kokkos::Unmanaged > > Y, const Teuchos::ETransp mode, const mv_scalar_type alpha, const mv_scalar_type beta) const
Same behavior as apply() above, except give KokkosKernels a hint to use an SPMV algorithm that can ef...
Declaration of Tpetra::Details::Behavior, a class that describes Tpetra&#39;s behavior.