5 #ifndef GINKGO_EXTENSIONS_KOKKOS_TYPES_HPP
6 #define GINKGO_EXTENSIONS_KOKKOS_TYPES_HPP
8 #include <Kokkos_Complex.hpp>
11 #include <Kokkos_Core.hpp>
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>
32 struct value_type_impl {
37 struct value_type_impl<const T> {
38 using type =
const typename value_type_impl<T>::type;
42 struct value_type_impl<std::complex<T>> {
43 using type = Kokkos::complex<T>;
49 using type =
typename value_type_impl<T>::type;
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
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.");
66 using value_type_t =
typename value_type<T>::type;
80 template <
typename T,
typename MemorySpace>
84 static auto map(
const T&);
96 template <
typename ValueType,
typename MemorySpace>
97 struct mapper<
array<ValueType>, MemorySpace> {
98 template <
typename ValueType_c>
100 Kokkos::View<typename value_type<ValueType_c>::type*, MemorySpace,
101 Kokkos::MemoryTraits<Kokkos::Unmanaged>>;
112 template <
typename ValueType_c>
113 static type<ValueType_c> map(ValueType_c* data,
size_type size);
118 static type<ValueType> map(array<ValueType>& arr);
123 static type<const ValueType> map(
const array<ValueType>& arr);
128 static type<const ValueType> map(
129 const ::gko::detail::const_array_view<ValueType>& arr);
132 template <
typename ValueType,
typename MemorySpace>
133 template <
typename ValueType_c>
134 typename mapper<array<ValueType>, MemorySpace>::template type<ValueType_c>
135 mapper<array<ValueType>, MemorySpace>::map(ValueType_c* data,
size_type size)
137 return type<ValueType_c>{
reinterpret_cast<value_type_t<ValueType_c>*
>(data),
142 template <
typename ValueType,
typename MemorySpace>
143 typename mapper<array<ValueType>, MemorySpace>::template type<ValueType>
144 mapper<array<ValueType>, MemorySpace>::map(array<ValueType>& arr)
146 assert_compatibility<MemorySpace>(arr);
148 return map(arr.get_data(), arr.get_size());
152 template <
typename ValueType,
typename MemorySpace>
153 typename mapper<array<ValueType>, MemorySpace>::template type<const ValueType>
154 mapper<array<ValueType>, MemorySpace>::map(
const array<ValueType>& arr)
156 assert_compatibility<MemorySpace>(arr);
158 return map(arr.get_const_data(), arr.get_size());
162 template <
typename ValueType,
typename MemorySpace>
163 typename mapper<array<ValueType>, MemorySpace>::template type<const ValueType>
164 mapper<array<ValueType>, MemorySpace>::map(
165 const ::gko::detail::const_array_view<ValueType>& arr)
167 assert_compatibility<MemorySpace>(arr);
169 return map(arr.get_const_data(), arr.get_size());
181 template <
typename ValueType,
typename MemorySpace>
182 struct 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>>;
191 static type<ValueType> map(matrix::Dense<ValueType>& m);
196 static type<const ValueType> map(
const matrix::Dense<ValueType>& m);
199 template <
typename ValueType,
typename MemorySpace>
200 typename mapper<matrix::Dense<ValueType>, MemorySpace>::template type<ValueType>
201 mapper<matrix::Dense<ValueType>, MemorySpace>::map(matrix::Dense<ValueType>& m)
203 assert_compatibility<MemorySpace>(m);
205 auto size = m.get_size();
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}};
213 template <
typename ValueType,
typename MemorySpace>
214 typename mapper<matrix::Dense<ValueType>,
215 MemorySpace>::template type<const ValueType>
216 mapper<matrix::Dense<ValueType>, MemorySpace>::map(
217 const matrix::Dense<ValueType>& m)
219 assert_compatibility<MemorySpace>(m);
221 auto size = m.get_size();
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}};
242 template <
typename MemorySpace = Kokkos::DefaultExecutionSpace::memory_space,
244 inline auto map_data(T&& data)
246 return detail::mapper<std::decay_t<T>, MemorySpace>::map(
247 std::forward<T>(data));
253 template <
typename MemorySpace = Kokkos::DefaultExecutionSpace::memory_space,
255 inline auto map_data(T* data)
257 return map_data<MemorySpace>(*data);
263 template <
typename MemorySpace = Kokkos::DefaultExecutionSpace::memory_space,
265 inline auto map_data(std::unique_ptr<T>& data)
267 return map_data<MemorySpace>(*data);
273 template <
typename MemorySpace = Kokkos::DefaultExecutionSpace::memory_space,
275 inline auto map_data(std::shared_ptr<T>& data)
277 return map_data<MemorySpace>(*data);
286 #endif // GINKGO_EXTENSIONS_KOKKOS_TYPES_HPP