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>
29struct value_type_impl {
34struct value_type_impl<const T> {
35 using type =
const typename value_type_impl<T>::type;
39struct 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.");
63using value_type_t =
typename value_type<T>::type;
77template <
typename T,
typename MemorySpace>
81 static auto map(
const T&);
93template <
typename ValueType,
typename MemorySpace>
94struct 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);
125 static type<const ValueType> map(
126 const ::gko::detail::const_array_view<ValueType>& arr);
129template <
typename ValueType,
typename MemorySpace>
130template <
typename ValueType_c>
131typename mapper<array<ValueType>, MemorySpace>::template type<ValueType_c>
132mapper<array<ValueType>, MemorySpace>::map(ValueType_c* data,
size_type size)
134 return type<ValueType_c>{
reinterpret_cast<value_type_t<ValueType_c>*
>(data),
139template <
typename ValueType,
typename MemorySpace>
140typename mapper<array<ValueType>, MemorySpace>::template type<ValueType>
143 assert_compatibility<MemorySpace>(arr);
145 return map(arr.get_data(), arr.get_size());
149template <
typename ValueType,
typename MemorySpace>
150typename mapper<array<ValueType>, MemorySpace>::template type<const ValueType>
153 assert_compatibility<MemorySpace>(arr);
155 return map(arr.get_const_data(), arr.get_size());
159template <
typename ValueType,
typename MemorySpace>
160typename mapper<array<ValueType>, MemorySpace>::template type<const ValueType>
161mapper<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());
178template <
typename ValueType,
typename MemorySpace>
179struct 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);
196template <
typename ValueType,
typename MemorySpace>
197typename mapper<matrix::Dense<ValueType>, MemorySpace>::template type<ValueType>
198mapper<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}};
210template <
typename ValueType,
typename MemorySpace>
211typename mapper<matrix::Dense<ValueType>,
212 MemorySpace>::template type<const ValueType>
213mapper<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}};
239template <
typename MemorySpace = Kokkos::DefaultExecutionSpace::memory_space,
241inline auto map_data(T&& data)
243 return detail::mapper<std::decay_t<T>, MemorySpace>::map(
244 std::forward<T>(data));
250template <
typename MemorySpace = Kokkos::DefaultExecutionSpace::memory_space,
252inline auto map_data(T* data)
254 return map_data<MemorySpace>(*data);
260template <
typename MemorySpace = Kokkos::DefaultExecutionSpace::memory_space,
262inline auto map_data(std::unique_ptr<T>& data)
264 return map_data<MemorySpace>(*data);
270template <
typename MemorySpace = Kokkos::DefaultExecutionSpace::memory_space,
272inline auto map_data(std::shared_ptr<T>& data)
274 return map_data<MemorySpace>(*data);
The Ginkgo namespace.
Definition abstract_factory.hpp:20
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:89
@ array
The matrix should be written as dense matrix in column-major order.
Definition mtx_io.hpp:96