Debian Codegraph @

packages/kokkos-kernels/batched/dense/impl/KokkosBatched_FindAmax_Internal.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 #ifndef KOKKOSBATCHED_FIND_AMAX_INTERNAL_HPP
17 #define KOKKOSBATCHED_FIND_AMAX_INTERNAL_HPP
18
19 /// \author Kyungjoo Kim ([email protected])
20
21 #include "KokkosBatched_Util.hpp"
22
23 namespace KokkosBatched {
24
25 ///
26 /// Serial Internal Impl
27 /// =====================
28 struct SerialFindAmaxInternal {
29 template <typename ValueType, typename IntType>
30 KOKKOS_INLINE_FUNCTION static int invoke(const int m, const ValueType *KOKKOS_RESTRICT A, const int as0,
31 /**/ IntType *KOKKOS_RESTRICT idx) {
32 ValueType max_val(A[0]);
33 IntType val_loc(0);
34 for (int i = 1; i < m; ++i) {
35 const int idx_a = i * as0;
36 if (A[idx_a] > max_val) {
37 max_val = A[idx_a];
38 val_loc = i;
39 }
40 }
41 *idx = val_loc;
42 return 0;
43 }
44 };
45
46 ///
47 /// TeamVector Internal Impl
48 /// ========================
49 struct TeamVectorFindAmaxInternal {
50 template <typename MemberType, typename ValueType, typename IntType>
51 KOKKOS_INLINE_FUNCTION static int invoke(const MemberType &member, const int m, const ValueType *KOKKOS_RESTRICT A,
52 const int as0,
53 /**/ IntType *KOKKOS_RESTRICT idx) {
54 if (m > 0) {
55 using reducer_value_type = typename Kokkos::MaxLoc<ValueType, IntType>::value_type;
56 reducer_value_type value{};
57 Kokkos::MaxLoc<ValueType, IntType> reducer_value(value);
58 Kokkos::parallel_reduce(
59 Kokkos::TeamVectorRange(member, m),
60 [&](const int &i, reducer_value_type &update) {
61 const int idx_a = i * as0;
62 if (A[idx_a] > update.val) {
63 update.val = A[idx_a];
64 update.loc = i;
65 }
66 },
67 reducer_value);
68 Kokkos::single(Kokkos::PerTeam(member), [&]() { *idx = value.loc; });
69 } else {
70 Kokkos::single(Kokkos::PerTeam(member), [&]() { *idx = 0; });
71 }
72 return 0;
73 }
74 };
75
76 } // namespace KokkosBatched
77
78 #endif