Debian Codegraph @

packages/kokkos-kernels/perf_test/batched/sparse/SPMV/KokkosBatched_SPMV_View.hpp

raw
1 //@HEADER
2 // ************************************************************************
3 //
4 // Kokkos v. 4.0
5 // Copyright (2022) National Technology & Engineering
6 // Solutions of Sandia, LLC (NTESS).
7 //
8 // Under the terms of Contract DE-NA0003525 with NTESS,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12 // See https://kokkos.org/LICENSE for license information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //@HEADER
16
17 template <class AMatrix, class IntView, class XVector, class YVector, int dobeta>
18 struct BSPMV_Functor_View {
19 typedef typename AMatrix::execution_space exec_space;
20 typedef typename AMatrix::non_const_value_type value_type;
21 typedef typename IntView::non_const_value_type ordinal_type;
22 typedef typename Kokkos::TeamPolicy<exec_space> team_policy;
23 typedef typename team_policy::member_type team_member;
24 typedef typename AMatrix::non_const_value_type entries_type;
25 typedef Kokkos::ArithTraits<value_type> ATV;
26
27 const value_type* alpha;
28 const AMatrix m_A_values;
29 const IntView m_A_row_ptr;
30 const IntView m_A_col_indices;
31 XVector m_x;
32 const value_type* beta;
33 YVector m_y;
34 const ordinal_type matrices_per_team;
35 const int N;
36 int implementation;
37
38 BSPMV_Functor_View(const value_type* alpha_, const AMatrix m_A_values_, const IntView m_A_row_ptr_,
39 const IntView m_A_col_indices_, const XVector m_x_, const value_type* beta_, const YVector m_y_,
40 const int matrices_per_team_, const int N_, const int implementation_ = 0)
41 : alpha(alpha_),
42 m_A_values(m_A_values_),
43 m_A_row_ptr(m_A_row_ptr_),
44 m_A_col_indices(m_A_col_indices_),
45 m_x(m_x_),
46 beta(beta_),
47 m_y(m_y_),
48 matrices_per_team(matrices_per_team_),
49 N(N_),
50 implementation(implementation_) {
51 static_assert(static_cast<int>(AMatrix::rank) == 2, "AMatrix must be a rank 2 View.");
52 static_assert(static_cast<int>(IntView::rank) == 1, "IntView must be a rank 1 View.");
53 static_assert(static_cast<int>(XVector::rank) == 2, "XVector must be a rank 2 View.");
54 static_assert(static_cast<int>(YVector::rank) == 2, "YVector must be a rank 2 View.");
55 }
56
57 KOKKOS_INLINE_FUNCTION void getIndices(const ordinal_type iTemp, const ordinal_type n_rows,
58 const ordinal_type n_matrices, ordinal_type& iRow,
59 ordinal_type& iMatrix) const {
60 if (std::is_same<typename AMatrix::array_layout, Kokkos::LayoutLeft>::value) {
61 iRow = iTemp / n_matrices;
62 iMatrix = iTemp % n_matrices;
63 } else {
64 iRow = iTemp % n_rows;
65 iMatrix = iTemp / n_rows;
66 }
67 }
68
69 KOKKOS_INLINE_FUNCTION void operator()(const team_member& dev) const {
70 if (implementation == 0) {
71 const int first_matrix = static_cast<int>(dev.league_rank()) * matrices_per_team;
72 const int last_matrix = static_cast<int>(dev.league_rank() + 1) * matrices_per_team < N
73 ? static_cast<int>(dev.league_rank() + 1) * matrices_per_team
74 : N;
75 const ordinal_type n_rows = m_A_row_ptr.extent(0) - 1;
76
77 for (int i_matrix = first_matrix; i_matrix < last_matrix; ++i_matrix) {
78 Kokkos::parallel_for(Kokkos::TeamThreadRange(dev, 0, n_rows), [&](const ordinal_type& iRow) {
79 const ordinal_type row_length = m_A_row_ptr(iRow + 1) - m_A_row_ptr(iRow);
80 value_type sum = 0;
81
82 Kokkos::parallel_reduce(
83 Kokkos::ThreadVectorRange(dev, row_length),
84 [&](const ordinal_type& iEntry, value_type& lsum) {
85 const value_type val = m_A_values(i_matrix, m_A_row_ptr(iRow) + iEntry);
86 lsum += val * m_x(i_matrix, m_A_col_indices(m_A_row_ptr(iRow) + iEntry));
87 },
88 sum);
89
90 Kokkos::single(Kokkos::PerThread(dev), [&]() {
91 sum *= alpha[i_matrix];
92
93 if (dobeta == 0) {
94 m_y(i_matrix, iRow) = sum;
95 } else {
96 m_y(i_matrix, iRow) = beta[i_matrix] * m_y(i_matrix, iRow) + sum;
97 }
98 });
99 });
100 }
101 }
102 if (implementation == 1) {
103 const int first_matrix = static_cast<int>(dev.league_rank()) * matrices_per_team;
104 const int last_matrix = static_cast<int>(dev.league_rank() + 1) * matrices_per_team < N
105 ? static_cast<int>(dev.league_rank() + 1) * matrices_per_team
106 : N;
107 const int n_matrices = last_matrix - first_matrix;
108 const ordinal_type n_rows = m_A_row_ptr.extent(0) - 1;
109
110 Kokkos::parallel_for(Kokkos::TeamVectorRange(dev, 0, n_rows * n_matrices), [&](const ordinal_type& iTemp) {
111 ordinal_type iRow, iMatrix;
112 this->getIndices(iTemp, n_rows, n_matrices, iRow, iMatrix);
113 const int iGlobalMatrix = first_matrix + iMatrix;
114
115 const ordinal_type row_length = m_A_row_ptr(iRow + 1) - m_A_row_ptr(iRow);
116 value_type sum = 0;
117
118 #if defined(KOKKOS_ENABLE_PRAGMA_UNROLL)
119 #pragma unroll
120 #endif
121 for (int iEntry = 0; iEntry < row_length; ++iEntry) {
122 sum += m_A_values(iGlobalMatrix, m_A_row_ptr(iRow) + iEntry) *
123 m_x(iGlobalMatrix, m_A_col_indices(m_A_row_ptr(iRow) + iEntry));
124 }
125
126 sum *= alpha[iGlobalMatrix];
127
128 if (dobeta == 0) {
129 m_y(iGlobalMatrix, iRow) = sum;
130 } else {
131 m_y(iGlobalMatrix, iRow) = beta[iGlobalMatrix] * m_y(iGlobalMatrix, iRow) + sum;
132 }
133 });
134 }
135 if (implementation == 2) {
136 using ScratchPadIntView = Kokkos::View<int*, Kokkos::DefaultExecutionSpace::scratch_memory_space>;
137
138 const ordinal_type n_rows = m_A_row_ptr.extent(0) - 1;
139 const ordinal_type nnz = m_A_col_indices.extent(0);
140
141 ScratchPadIntView cols(dev.team_scratch(0), nnz);
142 ScratchPadIntView row_map(dev.team_scratch(0), n_rows + 1);
143
144 Kokkos::parallel_for(Kokkos::TeamVectorRange(dev, 0, n_rows + 1),
145 [&](const ordinal_type& i) { row_map(i) = m_A_row_ptr(i); });
146
147 Kokkos::parallel_for(Kokkos::TeamVectorRange(dev, 0, nnz),
148 [&](const ordinal_type& i) { cols(i) = m_A_col_indices(i); });
149
150 dev.team_barrier();
151
152 const int first_matrix = static_cast<int>(dev.league_rank()) * matrices_per_team;
153 const int last_matrix = static_cast<int>(dev.league_rank() + 1) * matrices_per_team < N
154 ? static_cast<int>(dev.league_rank() + 1) * matrices_per_team
155 : N;
156 const int n_matrices = last_matrix - first_matrix;
157
158 Kokkos::parallel_for(Kokkos::TeamVectorRange(dev, 0, n_rows * n_matrices), [&](const ordinal_type& iTemp) {
159 ordinal_type iRow, iMatrix;
160 this->getIndices(iTemp, n_rows, n_matrices, iRow, iMatrix);
161 const int iGlobalMatrix = first_matrix + iMatrix;
162
163 const ordinal_type row_length = row_map(iRow + 1) - row_map(iRow);
164 value_type sum = 0;
165
166 #if defined(KOKKOS_ENABLE_PRAGMA_UNROLL)
167 #pragma unroll
168 #endif
169 for (int iEntry = 0; iEntry < row_length; ++iEntry) {
170 sum += m_A_values(iGlobalMatrix, row_map(iRow) + iEntry) * m_x(iGlobalMatrix, cols(row_map(iRow) + iEntry));
171 }
172
173 sum *= alpha[iGlobalMatrix];
174
175 if (dobeta == 0) {
176 m_y(iGlobalMatrix, iRow) = sum;
177 } else {
178 m_y(iGlobalMatrix, iRow) = beta[iGlobalMatrix] * m_y(iGlobalMatrix, iRow) + sum;
179 }
180 });
181 }
182 }
183 };