Debian Codegraph @

packages/kokkos-kernels/batched/dense/impl/KokkosBatched_Normalize_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_NORMALIZE_INTERNAL_HPP
17 #define KOKKOSBATCHED_NORMALIZE_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 SerialNormalizeInternal {
29 template <typename ValueType>
30 KOKKOS_INLINE_FUNCTION static int invoke(const int m,
31 /* */ ValueType *KOKKOS_RESTRICT v, const int vs) {
32 typedef ValueType value_type;
33 typedef Kokkos::ArithTraits<value_type> ats;
34 typedef typename ats::mag_type mag_type;
35
36 mag_type norm(0);
37 #if defined(KOKKOS_ENABLE_PRAGMA_UNROLL)
38 #pragma unroll
39 #endif
40 for (int i = 0; i < m; ++i) {
41 const auto v_at_i = v[i * vs];
42 norm += ats::real(v_at_i * ats::conj(v_at_i));
43 }
44 norm = Kokkos::ArithTraits<mag_type>::sqrt(norm);
45 #if defined(KOKKOS_ENABLE_PRAGMA_UNROLL)
46 #pragma unroll
47 #endif
48 for (int i = 0; i < m; ++i) v[i * vs] /= norm;
49
50 return 0;
51 }
52
53 template <typename RealType>
54 KOKKOS_INLINE_FUNCTION static int invoke(const int m,
55 /* */ RealType *KOKKOS_RESTRICT vr, const int vrs,
56 /* */ RealType *KOKKOS_RESTRICT vi, const int vis) {
57 typedef RealType real_type;
58 typedef Kokkos::ArithTraits<real_type> ats;
59 typedef typename ats::mag_type mag_type;
60
61 mag_type norm(0);
62 #if defined(KOKKOS_ENABLE_PRAGMA_UNROLL)
63 #pragma unroll
64 #endif
65 for (int i = 0; i < m; ++i) {
66 const auto vr_at_i = vr[i * vrs];
67 const auto vi_at_i = vi[i * vis];
68 norm += vr_at_i * vr_at_i + vi_at_i * vi_at_i;
69 }
70 norm = Kokkos::ArithTraits<mag_type>::sqrt(norm);
71 #if defined(KOKKOS_ENABLE_PRAGMA_UNROLL)
72 #pragma unroll
73 #endif
74 for (int i = 0; i < m; ++i) {
75 vr[i * vrs] /= norm;
76 vi[i * vis] /= norm;
77 }
78
79 return 0;
80 }
81 };
82
83 } // end namespace KokkosBatched
84
85 #endif