5 #ifndef GINKGO_EXTENSIONS_KOKKOS_TYPES_HPP
6 #define GINKGO_EXTENSIONS_KOKKOS_TYPES_HPP
8 #include <Kokkos_Complex.hpp>
9 #include <Kokkos_Core.hpp>
11 #include <ginkgo/config.hpp>
12 #include <ginkgo/core/base/array.hpp>
13 #include <ginkgo/core/matrix/dense.hpp>
14 #include <ginkgo/extensions/kokkos/config.hpp>
29 struct value_type_impl {
34 struct value_type_impl<const T> {
35 using type =
const typename value_type_impl<T>::type;
39 struct value_type_impl<std::complex<T>> {
40 using type = Kokkos::complex<T>;
46 using type =
typename value_type_impl<T>::type;
48 static_assert(
sizeof(std::decay_t<T>) ==
sizeof(std::decay_t<type>),
49 "Can't handle C++ data type and corresponding Kokkos "
50 "type with mismatching type sizes.");
51 #if GINKGO_EXTENSION_KOKKOS_CHECK_TYPE_ALIGNMENT
53 alignof(std::decay_t<T>) ==
alignof(std::decay_t<type>),
54 "Can't handle C++ data type and corresponding Kokkos type with "
55 "mismatching alignments. If std::complex is used, please make sure "
56 "to configure Kokkos with `KOKKOS_ENABLE_COMPLEX_ALIGN=ON`.\n"
57 "Alternatively, disable this check by setting the CMake option "
58 "-DGINKGO_EXTENSION_KOKKOS_CHECK_TYPE_ALIGNMENT=OFF.");
63 using value_type_t =
typename value_type<T>::type;
77 template <
typename T,
typename MemorySpace>
81 static auto map(
const T&);
93 template <
typename ValueType,
typename MemorySpace>
94 struct mapper<
array<ValueType>, MemorySpace> {
95 template <
typename ValueType_c>
97 Kokkos::View<typename value_type<ValueType_c>::type*, MemorySpace,
98 Kokkos::MemoryTraits<Kokkos::Unmanaged>>;
109 template <
typename ValueType_c>
110 static type<ValueType_c> map(ValueType_c* data,
size_type size);
115 static type<ValueType> map(array<ValueType>& arr);
120 static type<const ValueType> map(
const array<ValueType>& arr);
125 static type<const ValueType> map(
126 const ::gko::detail::const_array_view<ValueType>& arr);
129 template <
typename ValueType,
typename MemorySpace>
130 template <
typename ValueType_c>
131 typename mapper<array<ValueType>, MemorySpace>::template type<ValueType_c>
132 mapper<array<ValueType>, MemorySpace>::map(ValueType_c* data,
size_type size)
134 return type<ValueType_c>{
reinterpret_cast<value_type_t<ValueType_c>*
>(data),
139 template <
typename ValueType,
typename MemorySpace>
140 typename mapper<array<ValueType>, MemorySpace>::template type<ValueType>
141 mapper<array<ValueType>, MemorySpace>::map(array<ValueType>& arr)
143 assert_compatibility<MemorySpace>(arr);
145 return map(arr.get_data(), arr.get_size());
149 template <
typename ValueType,
typename MemorySpace>
150 typename mapper<array<ValueType>, MemorySpace>::template type<const ValueType>
151 mapper<array<ValueType>, MemorySpace>::map(
const array<ValueType>& arr)
153 assert_compatibility<MemorySpace>(arr);
155 return map(arr.get_const_data(), arr.get_size());
159 template <
typename ValueType,
typename MemorySpace>
160 typename mapper<array<ValueType>, MemorySpace>::template type<const ValueType>
161 mapper<array<ValueType>, MemorySpace>::map(
162 const ::gko::detail::const_array_view<ValueType>& arr)
164 assert_compatibility<MemorySpace>(arr);
166 return map(arr.get_const_data(), arr.get_size());
178 template <
typename ValueType,
typename MemorySpace>
179 struct mapper<matrix::Dense<ValueType>, MemorySpace> {
180 template <
typename ValueType_c>
181 using type = Kokkos::View<typename value_type<ValueType_c>::type**,
182 Kokkos::LayoutStride, MemorySpace,
183 Kokkos::MemoryTraits<Kokkos::Unmanaged>>;
188 static type<ValueType> map(matrix::Dense<ValueType>& m);
193 static type<const ValueType> map(
const matrix::Dense<ValueType>& m);
196 template <
typename ValueType,
typename MemorySpace>
197 typename mapper<matrix::Dense<ValueType>, MemorySpace>::template type<ValueType>
198 mapper<matrix::Dense<ValueType>, MemorySpace>::map(matrix::Dense<ValueType>& m)
200 assert_compatibility<MemorySpace>(m);
202 auto size = m.get_size();
204 return type<ValueType>{
205 reinterpret_cast<value_type_t<ValueType>*
>(m.get_values()),
206 Kokkos::LayoutStride{size[0], m.get_stride(), size[1], 1}};
210 template <
typename ValueType,
typename MemorySpace>
211 typename mapper<matrix::Dense<ValueType>,
212 MemorySpace>::template type<const ValueType>
213 mapper<matrix::Dense<ValueType>, MemorySpace>::map(
214 const matrix::Dense<ValueType>& m)
216 assert_compatibility<MemorySpace>(m);
218 auto size = m.get_size();
220 return type<const ValueType>{
221 reinterpret_cast<const value_type_t<ValueType>*
>(m.get_const_values()),
222 Kokkos::LayoutStride{size[0], m.get_stride(), size[1], 1}};
239 template <
typename MemorySpace = Kokkos::DefaultExecutionSpace::memory_space,
241 inline auto map_data(T&& data)
243 return detail::mapper<std::decay_t<T>, MemorySpace>::map(
244 std::forward<T>(data));
250 template <
typename MemorySpace = Kokkos::DefaultExecutionSpace::memory_space,
252 inline auto map_data(T* data)
254 return map_data<MemorySpace>(*data);
260 template <
typename MemorySpace = Kokkos::DefaultExecutionSpace::memory_space,
262 inline auto map_data(std::unique_ptr<T>& data)
264 return map_data<MemorySpace>(*data);
270 template <
typename MemorySpace = Kokkos::DefaultExecutionSpace::memory_space,
272 inline auto map_data(std::shared_ptr<T>& data)
274 return map_data<MemorySpace>(*data);
283 #endif // GINKGO_EXTENSIONS_KOKKOS_TYPES_HPP