Ginkgo Generated from branch based on master. Ginkgo version 1.8.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
types.hpp
1// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#ifndef GINKGO_EXTENSIONS_KOKKOS_TYPES_HPP
6#define GINKGO_EXTENSIONS_KOKKOS_TYPES_HPP
7
8#include <Kokkos_Complex.hpp>
9
10
11#include <Kokkos_Core.hpp>
12
13
14#include <ginkgo/config.hpp>
15#include <ginkgo/core/base/array.hpp>
16#include <ginkgo/core/matrix/dense.hpp>
17#include <ginkgo/extensions/kokkos/config.hpp>
18
19
20namespace gko {
21namespace ext {
22namespace kokkos {
23namespace detail {
24
25
31template <typename T>
32struct value_type_impl {
33 using type = T;
34};
35
36template <typename T>
37struct value_type_impl<const T> {
38 using type = const typename value_type_impl<T>::type;
39};
40
41template <typename T>
42struct value_type_impl<std::complex<T>> {
43 using type = Kokkos::complex<T>;
44};
45
46
47template <typename T>
48struct value_type {
49 using type = typename value_type_impl<T>::type;
50
51 static_assert(sizeof(std::decay_t<T>) == sizeof(std::decay_t<type>),
52 "Can't handle C++ data type and corresponding Kokkos "
53 "type with mismatching type sizes.");
54#if GINKGO_EXTENSION_KOKKOS_CHECK_TYPE_ALIGNMENT
55 static_assert(
56 alignof(std::decay_t<T>) == alignof(std::decay_t<type>),
57 "Can't handle C++ data type and corresponding Kokkos type with "
58 "mismatching alignments. If std::complex is used, please make sure "
59 "to configure Kokkos with `KOKKOS_ENABLE_COMPLEX_ALIGN=ON`.\n"
60 "Alternatively, disable this check by setting the CMake option "
61 "-DGINKGO_EXTENSION_KOKKOS_CHECK_TYPE_ALIGNMENT=OFF.");
62#endif
63};
64
65template <typename T>
66using value_type_t = typename value_type<T>::type;
67
68
80template <typename T, typename MemorySpace>
81struct mapper {
82 static auto map(T&);
83
84 static auto map(const T&);
85};
86
87
96template <typename ValueType, typename MemorySpace>
97struct mapper<array<ValueType>, MemorySpace> {
98 template <typename ValueType_c>
99 using type =
100 Kokkos::View<typename value_type<ValueType_c>::type*, MemorySpace,
101 Kokkos::MemoryTraits<Kokkos::Unmanaged>>;
102
112 template <typename ValueType_c>
113 static type<ValueType_c> map(ValueType_c* data, size_type size);
114
118 static type<ValueType> map(array<ValueType>& arr);
119
123 static type<const ValueType> map(const array<ValueType>& arr);
124
128 static type<const ValueType> map(
129 const ::gko::detail::const_array_view<ValueType>& arr);
130};
131
132template <typename ValueType, typename MemorySpace>
133template <typename ValueType_c>
134typename mapper<array<ValueType>, MemorySpace>::template type<ValueType_c>
135mapper<array<ValueType>, MemorySpace>::map(ValueType_c* data, size_type size)
136{
137 return type<ValueType_c>{reinterpret_cast<value_type_t<ValueType_c>*>(data),
138 size};
139}
140
141
142template <typename ValueType, typename MemorySpace>
143typename mapper<array<ValueType>, MemorySpace>::template type<ValueType>
144mapper<array<ValueType>, MemorySpace>::map(array<ValueType>& arr)
145{
146 assert_compatibility<MemorySpace>(arr);
147
148 return map(arr.get_data(), arr.get_size());
149}
150
151
152template <typename ValueType, typename MemorySpace>
153typename mapper<array<ValueType>, MemorySpace>::template type<const ValueType>
154mapper<array<ValueType>, MemorySpace>::map(const array<ValueType>& arr)
155{
156 assert_compatibility<MemorySpace>(arr);
157
158 return map(arr.get_const_data(), arr.get_size());
159}
160
161
162template <typename ValueType, typename MemorySpace>
163typename mapper<array<ValueType>, MemorySpace>::template type<const ValueType>
164mapper<array<ValueType>, MemorySpace>::map(
165 const ::gko::detail::const_array_view<ValueType>& arr)
166{
167 assert_compatibility<MemorySpace>(arr);
168
169 return map(arr.get_const_data(), arr.get_size());
170}
171
172
181template <typename ValueType, typename MemorySpace>
182struct mapper<matrix::Dense<ValueType>, MemorySpace> {
183 template <typename ValueType_c>
184 using type = Kokkos::View<typename value_type<ValueType_c>::type**,
185 Kokkos::LayoutStride, MemorySpace,
186 Kokkos::MemoryTraits<Kokkos::Unmanaged>>;
187
191 static type<ValueType> map(matrix::Dense<ValueType>& m);
192
196 static type<const ValueType> map(const matrix::Dense<ValueType>& m);
197};
198
199template <typename ValueType, typename MemorySpace>
200typename mapper<matrix::Dense<ValueType>, MemorySpace>::template type<ValueType>
201mapper<matrix::Dense<ValueType>, MemorySpace>::map(matrix::Dense<ValueType>& m)
202{
203 assert_compatibility<MemorySpace>(m);
204
205 auto size = m.get_size();
206
207 return type<ValueType>{
208 reinterpret_cast<value_type_t<ValueType>*>(m.get_values()),
209 Kokkos::LayoutStride{size[0], m.get_stride(), size[1], 1}};
210}
211
212
213template <typename ValueType, typename MemorySpace>
214typename mapper<matrix::Dense<ValueType>,
215 MemorySpace>::template type<const ValueType>
216mapper<matrix::Dense<ValueType>, MemorySpace>::map(
217 const matrix::Dense<ValueType>& m)
218{
219 assert_compatibility<MemorySpace>(m);
220
221 auto size = m.get_size();
222
223 return type<const ValueType>{
224 reinterpret_cast<const value_type_t<ValueType>*>(m.get_const_values()),
225 Kokkos::LayoutStride{size[0], m.get_stride(), size[1], 1}};
226}
227
228
229} // namespace detail
230
231
242template <typename MemorySpace = Kokkos::DefaultExecutionSpace::memory_space,
243 typename T>
244inline auto map_data(T&& data)
245{
246 return detail::mapper<std::decay_t<T>, MemorySpace>::map(
247 std::forward<T>(data));
248}
249
253template <typename MemorySpace = Kokkos::DefaultExecutionSpace::memory_space,
254 typename T>
255inline auto map_data(T* data)
256{
257 return map_data<MemorySpace>(*data);
258}
259
263template <typename MemorySpace = Kokkos::DefaultExecutionSpace::memory_space,
264 typename T>
265inline auto map_data(std::unique_ptr<T>& data)
266{
267 return map_data<MemorySpace>(*data);
268}
269
273template <typename MemorySpace = Kokkos::DefaultExecutionSpace::memory_space,
274 typename T>
275inline auto map_data(std::shared_ptr<T>& data)
276{
277 return map_data<MemorySpace>(*data);
278}
279
280
281} // namespace kokkos
282} // namespace ext
283} // namespace gko
284
285
286#endif // GINKGO_EXTENSIONS_KOKKOS_TYPES_HPP
The Ginkgo namespace.
Definition abstract_factory.hpp:20
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:86
@ array
The matrix should be written as dense matrix in column-major order.