Debian Codegraph @

packages/kokkos-kernels/batched/dense/impl/KokkosBatched_Copy_Impl.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_COPY_IMPL_HPP
17 #define KOKKOSBATCHED_COPY_IMPL_HPP
18
19 /// \author Kyungjoo Kim ([email protected])
20
21 #include "KokkosBatched_Util.hpp"
22 #include "KokkosBatched_Copy_Internal.hpp"
23
24 namespace KokkosBatched {
25
26 ///
27 /// Serial Impl
28 /// ===========
29
30 template <>
31 template <typename AViewType, typename BViewType>
32 KOKKOS_INLINE_FUNCTION int SerialCopy<Trans::NoTranspose, 1>::invoke(const AViewType &A, const BViewType &B) {
33 return SerialCopyInternal::invoke(A.extent(0), A.data(), A.stride_0(), B.data(), B.stride_0());
34 }
35
36 template <>
37 template <typename AViewType, typename BViewType>
38 KOKKOS_INLINE_FUNCTION int SerialCopy<Trans::Transpose, 1>::invoke(const AViewType &A, const BViewType &B) {
39 return SerialCopyInternal::invoke(A.extent(0), A.data(), A.stride_0(), B.data(), B.stride_0());
40 }
41
42 template <>
43 template <typename AViewType, typename BViewType>
44 KOKKOS_INLINE_FUNCTION int SerialCopy<Trans::NoTranspose, 2>::invoke(const AViewType &A, const BViewType &B) {
45 #if (KOKKOSKERNELS_DEBUG_LEVEL > 0)
46 static_assert(Kokkos::is_view<AViewType>::value, "KokkosBatched::copy: AViewType is not a Kokkos::View.");
47 static_assert(Kokkos::is_view<BViewType>::value, "KokkosBatched::copy: BViewType is not a Kokkos::View.");
48 static_assert(AViewType::rank == 2, "KokkosBatched::copy: AViewType must have rank 2.");
49 static_assert(BViewType::rank == 2, "KokkosBatched::copy: BViewType must have rank 2.");
50
51 // Check compatibility of dimensions at run time.
52 if (A.extent(0) != B.extent(0) || A.extent(1) != B.extent(1)) {
53 Kokkos::printf(
54 "KokkosBatched::copy: Dimensions of A and B do not match: A: %d x %d, "
55 "B: %d x %d\n",
56 (int)A.extent(0), (int)A.extent(1), (int)B.extent(0), (int)B.extent(1));
57 return 1;
58 }
59 #endif
60 return SerialCopyInternal::invoke(A.extent(0), A.extent(1), A.data(), A.stride_0(), A.stride_1(), B.data(),
61 B.stride_0(), B.stride_1());
62 }
63
64 template <>
65 template <typename AViewType, typename BViewType>
66 KOKKOS_INLINE_FUNCTION int SerialCopy<Trans::Transpose, 2>::invoke(const AViewType &A, const BViewType &B) {
67 #if (KOKKOSKERNELS_DEBUG_LEVEL > 0)
68 static_assert(Kokkos::is_view<AViewType>::value, "KokkosBatched::copy: AViewType is not a Kokkos::View.");
69 static_assert(Kokkos::is_view<BViewType>::value, "KokkosBatched::copy: BViewType is not a Kokkos::View.");
70 static_assert(AViewType::rank == 2, "KokkosBatched::copy: AViewType must have rank 2.");
71 static_assert(BViewType::rank == 2, "KokkosBatched::copy: BViewType must have rank 2.");
72
73 // Check compatibility of dimensions at run time.
74 if (A.extent(0) != B.extent(0) || A.extent(1) != B.extent(1)) {
75 Kokkos::printf(
76 "KokkosBatched::copy: Dimensions of A and B do not match: A: %d x %d, "
77 "B: %d x %d\n",
78 (int)A.extent(0), (int)A.extent(1), (int)B.extent(0), (int)B.extent(1));
79 return 1;
80 }
81 #endif
82 return SerialCopyInternal::invoke(A.extent(1), A.extent(0), A.data(), A.stride_1(), A.stride_0(), B.data(),
83 B.stride_0(), B.stride_1());
84 }
85
86 ///
87 /// Team Impl
88 /// =========
89
90 template <typename MemberType>
91 struct TeamCopy<MemberType, Trans::NoTranspose, 1> {
92 template <typename AViewType, typename BViewType>
93 KOKKOS_INLINE_FUNCTION static int invoke(const MemberType &member, const AViewType &A, const BViewType &B) {
94 return TeamCopyInternal::invoke(member, A.extent(0), A.data(), A.stride_0(), B.data(), B.stride_0());
95 }
96 };
97
98 template <typename MemberType>
99 struct TeamCopy<MemberType, Trans::Transpose, 1> {
100 template <typename AViewType, typename BViewType>
101 KOKKOS_INLINE_FUNCTION static int invoke(const MemberType &member, const AViewType &A, const BViewType &B) {
102 return TeamCopyInternal::invoke(member, A.extent(0), A.data(), A.stride_0(), B.data(), B.stride_0());
103 }
104 };
105
106 template <typename MemberType>
107 struct TeamCopy<MemberType, Trans::NoTranspose, 2> {
108 template <typename AViewType, typename BViewType>
109 KOKKOS_INLINE_FUNCTION static int invoke(const MemberType &member, const AViewType &A, const BViewType &B) {
110 #if (KOKKOSKERNELS_DEBUG_LEVEL > 0)
111 static_assert(Kokkos::is_view<AViewType>::value, "KokkosBatched::copy: AViewType is not a Kokkos::View.");
112 static_assert(Kokkos::is_view<BViewType>::value, "KokkosBatched::copy: BViewType is not a Kokkos::View.");
113 static_assert(AViewType::rank == 2, "KokkosBatched::copy: AViewType must have rank 2.");
114 static_assert(BViewType::rank == 2, "KokkosBatched::copy: BViewType must have rank 2.");
115
116 // Check compatibility of dimensions at run time.
117 if (A.extent(0) != B.extent(0) || A.extent(1) != B.extent(1)) {
118 Kokkos::printf(
119 "KokkosBatched::copy: Dimensions of A and B do not match: A: %d x "
120 "%d, "
121 "B: %d x %d\n",
122 (int)A.extent(0), (int)A.extent(1), (int)B.extent(0), (int)B.extent(1));
123 return 1;
124 }
125 #endif
126 if (A.extent(0) == 1) {
127 return TeamCopy<MemberType, Trans::NoTranspose, 1>::invoke(member, Kokkos::subview(A, 0, Kokkos::ALL),
128 Kokkos::subview(B, 0, Kokkos::ALL));
129 }
130 return TeamCopyInternal::invoke(member, A.extent(0), A.extent(1), A.data(), A.stride_0(), A.stride_1(), B.data(),
131 B.stride_0(), B.stride_1());
132 }
133 };
134
135 template <typename MemberType>
136 struct TeamCopy<MemberType, Trans::Transpose, 2> {
137 template <typename AViewType, typename BViewType>
138 KOKKOS_INLINE_FUNCTION static int invoke(const MemberType &member, const AViewType &A, const BViewType &B) {
139 #if (KOKKOSKERNELS_DEBUG_LEVEL > 0)
140 static_assert(Kokkos::is_view<AViewType>::value, "KokkosBatched::copy: AViewType is not a Kokkos::View.");
141 static_assert(Kokkos::is_view<BViewType>::value, "KokkosBatched::copy: BViewType is not a Kokkos::View.");
142 static_assert(AViewType::rank == 2, "KokkosBatched::copy: AViewType must have rank 2.");
143 static_assert(BViewType::rank == 2, "KokkosBatched::copy: BViewType must have rank 2.");
144
145 // Check compatibility of dimensions at run time.
146 if (A.extent(0) != B.extent(0) || A.extent(1) != B.extent(1)) {
147 Kokkos::printf(
148 "KokkosBatched::copy: Dimensions of A and B do not match: A: %d x "
149 "%d, "
150 "B: %d x %d\n",
151 (int)A.extent(0), (int)A.extent(1), (int)B.extent(0), (int)B.extent(1));
152 return 1;
153 }
154 #endif
155 if (A.extent(1) == 1) {
156 return TeamCopy<MemberType, Trans::Transpose, 1>::invoke(member, Kokkos::subview(A, Kokkos::ALL, 0),
157 Kokkos::subview(B, Kokkos::ALL, 0));
158 }
159 return TeamCopyInternal::invoke(member, A.extent(1), A.extent(0), A.data(), A.stride_1(), A.stride_0(), B.data(),
160 B.stride_0(), B.stride_1());
161 }
162 };
163
164 ///
165 /// TeamVector Impl
166 /// =========
167
168 template <typename MemberType>
169 struct TeamVectorCopy<MemberType, Trans::NoTranspose, 1> {
170 template <typename AViewType, typename BViewType>
171 KOKKOS_INLINE_FUNCTION static int invoke(const MemberType &member, const AViewType &A, const BViewType &B) {
172 return TeamVectorCopyInternal::invoke(member, A.extent(0), A.data(), A.stride_0(), B.data(), B.stride_0());
173 }
174 };
175
176 template <typename MemberType>
177 struct TeamVectorCopy<MemberType, Trans::Transpose, 1> {
178 template <typename AViewType, typename BViewType>
179 KOKKOS_INLINE_FUNCTION static int invoke(const MemberType &member, const AViewType &A, const BViewType &B) {
180 return TeamVectorCopyInternal::invoke(member, A.extent(0), A.data(), A.stride_0(), B.data(), B.stride_0());
181 }
182 };
183
184 template <typename MemberType>
185 struct TeamVectorCopy<MemberType, Trans::NoTranspose, 2> {
186 template <typename AViewType, typename BViewType>
187 KOKKOS_INLINE_FUNCTION static int invoke(const MemberType &member, const AViewType &A, const BViewType &B) {
188 #if (KOKKOSKERNELS_DEBUG_LEVEL > 0)
189 static_assert(Kokkos::is_view<AViewType>::value, "KokkosBatched::copy: AViewType is not a Kokkos::View.");
190 static_assert(Kokkos::is_view<BViewType>::value, "KokkosBatched::copy: BViewType is not a Kokkos::View.");
191 static_assert(AViewType::rank == 2, "KokkosBatched::copy: AViewType must have rank 2.");
192 static_assert(BViewType::rank == 2, "KokkosBatched::copy: BViewType must have rank 2.");
193
194 // Check compatibility of dimensions at run time.
195 if (A.extent(0) != B.extent(0) || A.extent(1) != B.extent(1)) {
196 Kokkos::printf(
197 "KokkosBatched::copy: Dimensions of A and B do not match: A: %d x "
198 "%d, "
199 "B: %d x %d\n",
200 (int)A.extent(0), (int)A.extent(1), (int)B.extent(0), (int)B.extent(1));
201 return 1;
202 }
203 #endif
204 if (A.extent(0) == 1) {
205 return TeamVectorCopy<MemberType, Trans::NoTranspose, 1>::invoke(member, Kokkos::subview(A, 0, Kokkos::ALL),
206 Kokkos::subview(B, 0, Kokkos::ALL));
207 }
208 return TeamVectorCopyInternal::invoke(member, A.extent(0), A.extent(1), A.data(), A.stride_0(), A.stride_1(),
209 B.data(), B.stride_0(), B.stride_1());
210 }
211 };
212
213 template <typename MemberType>
214 struct TeamVectorCopy<MemberType, Trans::Transpose, 2> {
215 template <typename AViewType, typename BViewType>
216 KOKKOS_INLINE_FUNCTION static int invoke(const MemberType &member, const AViewType &A, const BViewType &B) {
217 #if (KOKKOSKERNELS_DEBUG_LEVEL > 0)
218 static_assert(Kokkos::is_view<AViewType>::value, "KokkosBatched::copy: AViewType is not a Kokkos::View.");
219 static_assert(Kokkos::is_view<BViewType>::value, "KokkosBatched::copy: BViewType is not a Kokkos::View.");
220 static_assert(AViewType::rank == 2, "KokkosBatched::copy: AViewType must have rank 2.");
221 static_assert(BViewType::rank == 2, "KokkosBatched::copy: BViewType must have rank 2.");
222
223 // Check compatibility of dimensions at run time.
224 if (A.extent(0) != B.extent(0) || A.extent(1) != B.extent(1)) {
225 Kokkos::printf(
226 "KokkosBatched::copy: Dimensions of A and B do not match: A: %d x "
227 "%d, "
228 "B: %d x %d\n",
229 (int)A.extent(0), (int)A.extent(1), (int)B.extent(0), (int)B.extent(1));
230 return 1;
231 }
232 #endif
233 if (A.extent(1) == 1) {
234 return TeamVectorCopy<MemberType, Trans::NoTranspose, 1>::invoke(member, Kokkos::subview(A, Kokkos::ALL, 0),
235 Kokkos::subview(B, Kokkos::ALL, 0));
236 }
237 return TeamVectorCopyInternal::invoke(member, A.extent(1), A.extent(0), A.data(), A.stride_1(), A.stride_0(),
238 B.data(), B.stride_0(), B.stride_1());
239 }
240 };
241
242 } // end namespace KokkosBatched
243
244 #endif