| 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 <typename DeviceType, typename ValuesViewType, typename IntView, typename VectorViewType, |
| 18 |
typename KrylovHandleType, bool UsePrec> |
| 19 |
struct Functor_TestBatchedTeamVectorGMRES_3 { |
| 20 |
const ValuesViewType _D; |
| 21 |
const ValuesViewType _diag; |
| 22 |
const IntView _r; |
| 23 |
const IntView _c; |
| 24 |
const VectorViewType _X; |
| 25 |
const VectorViewType _B; |
| 26 |
const int _N_team, _team_size, _vector_length; |
| 27 |
const int _N_iteration; |
| 28 |
const double _tol; |
| 29 |
const int _ortho_strategy; |
| 30 |
const int _arnoldi_level; |
| 31 |
const int _other_level; |
| 32 |
KrylovHandleType _handle; |
| 33 |
|
| 34 |
KOKKOS_INLINE_FUNCTION |
| 35 |
Functor_TestBatchedTeamVectorGMRES_3(const ValuesViewType &D, const IntView &r, const IntView &c, |
| 36 |
const VectorViewType &X, const VectorViewType &B, const int N_team, |
| 37 |
const int team_size, const int vector_length, const int N_iteration, |
| 38 |
const double tol, const int ortho_strategy, const int arnoldi_level, |
| 39 |
const int other_level, KrylovHandleType &handle) |
| 40 |
: _D(D), |
| 41 |
_r(r), |
| 42 |
_c(c), |
| 43 |
_X(X), |
| 44 |
_B(B), |
| 45 |
_N_team(N_team), |
| 46 |
_team_size(team_size), |
| 47 |
_vector_length(vector_length), |
| 48 |
_N_iteration(N_iteration), |
| 49 |
_tol(tol), |
| 50 |
_ortho_strategy(ortho_strategy), |
| 51 |
_arnoldi_level(arnoldi_level), |
| 52 |
_other_level(other_level), |
| 53 |
_handle(handle) {} |
| 54 |
|
| 55 |
KOKKOS_INLINE_FUNCTION |
| 56 |
Functor_TestBatchedTeamVectorGMRES_3(const ValuesViewType &D, const ValuesViewType &diag, const IntView &r, |
| 57 |
const IntView &c, const VectorViewType &X, const VectorViewType &B, |
| 58 |
const int N_team, const int team_size, const int vector_length, |
| 59 |
const int N_iteration, const double tol, int ortho_strategy, |
| 60 |
const int arnoldi_level, const int other_level, KrylovHandleType &handle) |
| 61 |
: _D(D), |
| 62 |
_diag(diag), |
| 63 |
_r(r), |
| 64 |
_c(c), |
| 65 |
_X(X), |
| 66 |
_B(B), |
| 67 |
_N_team(N_team), |
| 68 |
_team_size(team_size), |
| 69 |
_vector_length(vector_length), |
| 70 |
_N_iteration(N_iteration), |
| 71 |
_tol(tol), |
| 72 |
_ortho_strategy(ortho_strategy), |
| 73 |
_arnoldi_level(arnoldi_level), |
| 74 |
_other_level(other_level), |
| 75 |
_handle(handle) {} |
| 76 |
|
| 77 |
template <typename MemberType> |
| 78 |
KOKKOS_INLINE_FUNCTION void operator()(const MemberType &member) const { |
| 79 |
const int first_matrix = _handle.first_index(member.league_rank()); |
| 80 |
const int last_matrix = _handle.last_index(member.league_rank()); |
| 81 |
|
| 82 |
using TeamVectorCopy1D = KokkosBatched::TeamVectorCopy<MemberType, KokkosBatched::Trans::NoTranspose, 1>; |
| 83 |
|
| 84 |
auto d = Kokkos::subview(_D, Kokkos::make_pair(first_matrix, last_matrix), Kokkos::ALL); |
| 85 |
auto x = Kokkos::subview(_X, Kokkos::make_pair(first_matrix, last_matrix), Kokkos::ALL); |
| 86 |
auto b = Kokkos::subview(_B, Kokkos::make_pair(first_matrix, last_matrix), Kokkos::ALL); |
| 87 |
|
| 88 |
using ScratchPadIntViewType = Kokkos::View<typename IntView::non_const_value_type *, typename IntView::array_layout, |
| 89 |
typename IntView::execution_space::scratch_memory_space>; |
| 90 |
using ScratchPadValuesViewType = |
| 91 |
Kokkos::View<typename ValuesViewType::non_const_value_type **, typename ValuesViewType::array_layout, |
| 92 |
typename ValuesViewType::execution_space::scratch_memory_space>; |
| 93 |
using Operator = KokkosBatched::CrsMatrix<ValuesViewType, ScratchPadIntViewType>; |
| 94 |
|
| 95 |
ScratchPadIntViewType tmp_1D_int(member.team_scratch(0), _r.extent(0) + _c.extent(0)); |
| 96 |
|
| 97 |
auto r = Kokkos::subview(tmp_1D_int, Kokkos::make_pair(0, (int)_r.extent(0))); |
| 98 |
auto c = Kokkos::subview(tmp_1D_int, Kokkos::make_pair((int)_r.extent(0), (int)tmp_1D_int.extent(0))); |
| 99 |
|
| 100 |
TeamVectorCopy1D::invoke(member, _r, r); |
| 101 |
TeamVectorCopy1D::invoke(member, _c, c); |
| 102 |
Operator A(d, r, c); |
| 103 |
|
| 104 |
if (UsePrec) { |
| 105 |
ScratchPadValuesViewType diag(member.team_scratch(0), last_matrix - first_matrix, _diag.extent(1)); |
| 106 |
using PrecOperator = KokkosBatched::JacobiPrec<ScratchPadValuesViewType>; |
| 107 |
|
| 108 |
KokkosBatched::TeamVectorCopy<MemberType>::invoke( |
| 109 |
member, Kokkos::subview(_diag, Kokkos::make_pair(first_matrix, last_matrix), Kokkos::ALL), diag); |
| 110 |
PrecOperator P(diag); |
| 111 |
P.setComputedInverse(); |
| 112 |
|
| 113 |
KokkosBatched::TeamVectorGMRES<MemberType>::template invoke<Operator, VectorViewType, PrecOperator, |
| 114 |
KrylovHandleType>(member, A, b, x, P, _handle); |
| 115 |
} else { |
| 116 |
KokkosBatched::TeamVectorGMRES<MemberType>::template invoke<Operator, VectorViewType>(member, A, b, x, _handle); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
inline double run() { |
| 121 |
std::string name("KokkosBatched::Test::TeamVectorGMRES"); |
| 122 |
Kokkos::Timer timer; |
| 123 |
Kokkos::Profiling::pushRegion(name.c_str()); |
| 124 |
|
| 125 |
Kokkos::TeamPolicy<DeviceType> auto_policy(_handle.get_number_of_teams(), Kokkos::AUTO(), Kokkos::AUTO()); |
| 126 |
Kokkos::TeamPolicy<DeviceType> tuned_policy(_handle.get_number_of_teams(), _team_size, _vector_length); |
| 127 |
Kokkos::TeamPolicy<DeviceType> policy; |
| 128 |
|
| 129 |
if (_team_size < 1) |
| 130 |
policy = auto_policy; |
| 131 |
else |
| 132 |
policy = tuned_policy; |
| 133 |
|
| 134 |
_handle.set_memory_strategy(0); |
| 135 |
|
| 136 |
int maximum_iteration = _handle.get_max_iteration(); |
| 137 |
|
| 138 |
using ScalarType = typename ValuesViewType::non_const_value_type; |
| 139 |
using Layout = typename ValuesViewType::array_layout; |
| 140 |
using EXSP = typename ValuesViewType::execution_space; |
| 141 |
|
| 142 |
using ViewType2D = Kokkos::View<ScalarType **, Layout, EXSP>; |
| 143 |
|
| 144 |
size_t bytes_1D = ViewType2D::shmem_size(_N_team, 1); |
| 145 |
size_t bytes_row_ptr = IntView::shmem_size(_r.extent(0)); |
| 146 |
size_t bytes_col_idc = IntView::shmem_size(_c.extent(0)); |
| 147 |
size_t bytes_2D_1 = ViewType2D::shmem_size(_N_team, _X.extent(1)); |
| 148 |
size_t bytes_2D_2 = ViewType2D::shmem_size(_N_team, maximum_iteration + 1); |
| 149 |
|
| 150 |
size_t bytes_int = bytes_row_ptr + bytes_col_idc; |
| 151 |
size_t bytes_diag = bytes_2D_1; |
| 152 |
size_t bytes_tmp = 2 * bytes_2D_1 + 2 * bytes_1D + bytes_2D_2; |
| 153 |
|
| 154 |
policy.set_scratch_size(0, Kokkos::PerTeam(bytes_tmp + bytes_diag + bytes_int)); |
| 155 |
|
| 156 |
exec_space().fence(); |
| 157 |
timer.reset(); |
| 158 |
Kokkos::parallel_for(name.c_str(), policy, *this); |
| 159 |
exec_space().fence(); |
| 160 |
double sec = timer.seconds(); |
| 161 |
Kokkos::Profiling::popRegion(); |
| 162 |
|
| 163 |
return sec; |
| 164 |
} |
| 165 |
}; |