Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Tpetra_Details_MatrixApplyHelper.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Tpetra: Templated Linear Algebra Services Package
5 // Copyright (2008) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
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 // ************************************************************************
38 // @HEADER
39 
40 #ifndef TPETRA_MATRIX_APPLY_HELPER_HPP
41 #define TPETRA_MATRIX_APPLY_HELPER_HPP
42 
43 #include "Kokkos_Core.hpp"
44 #include "KokkosSparse_spmv_handle.hpp"
45 
46 #if KOKKOSKERNELS_VERSION < 40299
47 #error "Tpetra::Details::MatrixApplyHelper can only be included with KokkosKernels 4.3+"
48 #endif
49 
50 namespace Tpetra {
51 namespace Details {
52 
60 template<typename LocalMatrix, typename IntLocalMatrix, typename MultiVectorLocalView>
62 {
63  using Rowptrs = typename LocalMatrix::row_map_type;
64  using IntRowptrs = typename IntLocalMatrix::row_map_type;
65  using XVectorType = typename MultiVectorLocalView::const_type;
66  using YVectorType = MultiVectorLocalView;
67  using SPMVHandle = KokkosSparse::SPMVHandle<typename LocalMatrix::device_type, LocalMatrix, XVectorType, YVectorType>;
68  using SPMVHandleInt = KokkosSparse::SPMVHandle<typename LocalMatrix::device_type, IntLocalMatrix, XVectorType, YVectorType>;
69 
70  MatrixApplyHelper(size_t nnz_, const typename LocalMatrix::row_map_type& rowptrs, KokkosSparse::SPMVAlgorithm algo = KokkosSparse::SPMV_DEFAULT)
71  : nnz(nnz_), handle(algo), handle_int(algo)
72  {
73  if(shouldUseIntRowptrs())
74  fillRowptrsInt(rowptrs);
75  // otherwise, leave rowptrs_int empty
76  }
77 
78  bool shouldUseIntRowptrs() const
79  {
80  // Use int-valued rowptrs if:
81  // - number of entries doesn't overflow int
82  // - LocalMatrix's rowptrs is not already int-typed
83  return nnz <= size_t(INT_MAX) && !std::is_same_v<int, typename Rowptrs::non_const_value_type>;
84  }
85 
86  // Given the local matrix, return a version with int-typed rowptrs
87  IntLocalMatrix getIntRowptrMatrix(const LocalMatrix& A)
88  {
89  if constexpr(KokkosSparse::is_crs_matrix_v<LocalMatrix>)
90  {
91  return IntLocalMatrix("", A.numRows(), A.numCols(), A.nnz(),
92  A.values, rowptrs_int, A.graph.entries);
93  }
94  else
95  {
96  return IntLocalMatrix("", A.numRows(), A.numCols(), A.nnz(),
97  A.values, rowptrs_int, A.graph.entries, A.blockDim());
98  }
99  }
100 
101  void fillRowptrsInt(const Rowptrs& rowptrs)
102  {
103  // Populate rowptrs_int as a deep copy of rowptrs, converted to int.
104  // This needs to be in its own public function because KOKKOS_LAMBDA cannot be used inside constructor or protected/private
105  typename IntRowptrs::non_const_type rowptrs_temp(
106  Kokkos::view_alloc(Kokkos::WithoutInitializing, "rowptrs_int"), rowptrs.extent(0));
107  Kokkos::parallel_for(Kokkos::RangePolicy<typename LocalMatrix::execution_space>(0, rowptrs.extent(0)),
108  KOKKOS_LAMBDA(int i)
109  {
110  rowptrs_temp(i) = int(rowptrs(i));
111  });
112  rowptrs_int = rowptrs_temp;
113  }
114 
115  size_t nnz;
116  // SPMVHandles are lazily initialized by actual spmv calls.
117  // We declare both here because we don't know until runtime (based on nnz) which should be used.
118  SPMVHandle handle;
119  SPMVHandleInt handle_int;
120  IntRowptrs rowptrs_int;
121 };
122 }
123 }
124 
125 #endif // TPETRA_MATRIX_APPLY_HELPER_HPP
126