|
| 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 | +namespace KokkosSparse { |
| 18 | +namespace Impl { |
| 19 | + |
| 20 | +template <class execution_space, class matrix_type, class functor_type> |
| 21 | +struct crsmatrix_traversal_functor { |
| 22 | + using size_type = typename matrix_type::non_const_size_type; |
| 23 | + using ordinal_type = typename matrix_type::non_const_ordinal_type; |
| 24 | + using value_type = typename matrix_type::non_const_value_type; |
| 25 | + |
| 26 | + using team_policy_type = Kokkos::TeamPolicy<execution_space>; |
| 27 | + using team_member_type = typename team_policy_type::member_type; |
| 28 | + |
| 29 | + matrix_type A; |
| 30 | + functor_type func; |
| 31 | + ordinal_type rows_per_team; |
| 32 | + |
| 33 | + crsmatrix_traversal_functor(const matrix_type& A_, const functor_type& func_, |
| 34 | + const ordinal_type rows_per_team_) |
| 35 | + : A(A_), func(func_), rows_per_team(rows_per_team_) {} |
| 36 | + |
| 37 | + // RangePolicy overload |
| 38 | + KOKKOS_INLINE_FUNCTION void operator()(const ordinal_type rowIdx) const { |
| 39 | + for (size_type entryIdx = A.graph.row_map(rowIdx); |
| 40 | + entryIdx < A.graph.row_map(rowIdx + 1); ++entryIdx) { |
| 41 | + const ordinal_type colIdx = A.graph.entries(entryIdx); |
| 42 | + const value_type value = A.values(entryIdx); |
| 43 | + |
| 44 | + func(rowIdx, entryIdx, colIdx, value); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // TeamPolicy overload |
| 49 | + KOKKOS_INLINE_FUNCTION void operator()(const team_member_type& dev) const { |
| 50 | + const ordinal_type teamWork = dev.league_rank() * rows_per_team; |
| 51 | + Kokkos::parallel_for( |
| 52 | + Kokkos::TeamThreadRange(dev, rows_per_team), [&](ordinal_type loop) { |
| 53 | + // iRow represents a row of the matrix, so its correct type is |
| 54 | + // ordinal_type. |
| 55 | + const ordinal_type rowIdx = teamWork + loop; |
| 56 | + if (rowIdx >= A.numRows()) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + const ordinal_type row_length = |
| 61 | + A.graph.row_map(rowIdx + 1) - A.graph.row_map(rowIdx); |
| 62 | + Kokkos::parallel_for( |
| 63 | + Kokkos::ThreadVectorRange(dev, row_length), |
| 64 | + [&](ordinal_type rowEntryIdx) { |
| 65 | + const size_type entryIdx = A.graph.row_map(rowIdx) + |
| 66 | + static_cast<size_type>(rowEntryIdx); |
| 67 | + const ordinal_type colIdx = A.graph.entries(entryIdx); |
| 68 | + const value_type value = A.values(entryIdx); |
| 69 | + |
| 70 | + func(rowIdx, entryIdx, colIdx, value); |
| 71 | + }); |
| 72 | + }); |
| 73 | + } |
| 74 | +}; |
| 75 | + |
| 76 | +template <class execution_space> |
| 77 | +int64_t crsmatrix_traversal_launch_parameters(int64_t numRows, int64_t nnz, |
| 78 | + int64_t rows_per_thread, |
| 79 | + int& team_size, |
| 80 | + int& vector_length) { |
| 81 | + int64_t rows_per_team; |
| 82 | + int64_t nnz_per_row = nnz / numRows; |
| 83 | + |
| 84 | + if (nnz_per_row < 1) nnz_per_row = 1; |
| 85 | + |
| 86 | + int max_vector_length = 1; |
| 87 | +#ifdef KOKKOS_ENABLE_CUDA |
| 88 | + if (std::is_same<execution_space, Kokkos::Cuda>::value) |
| 89 | + max_vector_length = 32; |
| 90 | +#endif |
| 91 | +#ifdef KOKKOS_ENABLE_HIP |
| 92 | + if (std::is_same<execution_space, Kokkos::Experimental::HIP>::value) |
| 93 | + max_vector_length = 64; |
| 94 | +#endif |
| 95 | + |
| 96 | + if (vector_length < 1) { |
| 97 | + vector_length = 1; |
| 98 | + while (vector_length < max_vector_length && vector_length * 6 < nnz_per_row) |
| 99 | + vector_length *= 2; |
| 100 | + } |
| 101 | + |
| 102 | + // Determine rows per thread |
| 103 | + if (rows_per_thread < 1) { |
| 104 | + if (KokkosKernels::Impl::kk_is_gpu_exec_space<execution_space>()) |
| 105 | + rows_per_thread = 1; |
| 106 | + else { |
| 107 | + if (nnz_per_row < 20 && nnz > 5000000) { |
| 108 | + rows_per_thread = 256; |
| 109 | + } else |
| 110 | + rows_per_thread = 64; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if (team_size < 1) { |
| 115 | + if (KokkosKernels::Impl::kk_is_gpu_exec_space<execution_space>()) { |
| 116 | + team_size = 256 / vector_length; |
| 117 | + } else { |
| 118 | + team_size = 1; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + rows_per_team = rows_per_thread * team_size; |
| 123 | + |
| 124 | + if (rows_per_team < 0) { |
| 125 | + int64_t nnz_per_team = 4096; |
| 126 | + int64_t conc = execution_space().concurrency(); |
| 127 | + while ((conc * nnz_per_team * 4 > nnz) && (nnz_per_team > 256)) |
| 128 | + nnz_per_team /= 2; |
| 129 | + rows_per_team = (nnz_per_team + nnz_per_row - 1) / nnz_per_row; |
| 130 | + } |
| 131 | + |
| 132 | + return rows_per_team; |
| 133 | +} |
| 134 | + |
| 135 | +template <class execution_space, class crsmatrix_type, class functor_type> |
| 136 | +void crsmatrix_traversal_on_host(const execution_space& space, |
| 137 | + const crsmatrix_type& A, |
| 138 | + const functor_type& func) { |
| 139 | + // Wrap user functor with crsmatrix_traversal_functor |
| 140 | + crsmatrix_traversal_functor<execution_space, crsmatrix_type, functor_type> |
| 141 | + traversal_func(A, func, -1); |
| 142 | + |
| 143 | + // Launch traversal kernel |
| 144 | + Kokkos::parallel_for( |
| 145 | + "KokkosSparse::crsmatrix_traversal", |
| 146 | + Kokkos::RangePolicy<execution_space>(space, 0, A.numRows()), |
| 147 | + traversal_func); |
| 148 | +} |
| 149 | + |
| 150 | +template <class execution_space, class crsmatrix_type, class functor_type> |
| 151 | +void crsmatrix_traversal_on_gpu(const execution_space& space, |
| 152 | + const crsmatrix_type& A, |
| 153 | + const functor_type& func) { |
| 154 | + // Wrap user functor with crsmatrix_traversal_functor |
| 155 | + int64_t rows_per_thread = 0; |
| 156 | + int team_size = 0, vector_length = 0; |
| 157 | + const int64_t rows_per_team = |
| 158 | + crsmatrix_traversal_launch_parameters<execution_space>( |
| 159 | + A.numRows(), A.nnz(), rows_per_thread, team_size, vector_length); |
| 160 | + const int nteams = |
| 161 | + (static_cast<int>(A.numRows()) + rows_per_team - 1) / rows_per_team; |
| 162 | + crsmatrix_traversal_functor<execution_space, crsmatrix_type, functor_type> |
| 163 | + traversal_func(A, func, rows_per_team); |
| 164 | + |
| 165 | + // Launch traversal kernel |
| 166 | + Kokkos::parallel_for("KokkosSparse::crsmatrix_traversal", |
| 167 | + Kokkos::TeamPolicy<execution_space>( |
| 168 | + space, nteams, team_size, vector_length), |
| 169 | + traversal_func); |
| 170 | +} |
| 171 | + |
| 172 | +} // namespace Impl |
| 173 | +} // namespace KokkosSparse |
0 commit comments