5 #ifndef GINKGO_EXTENSIONS_KOKKOS_SPACES_HPP
6 #define GINKGO_EXTENSIONS_KOKKOS_SPACES_HPP
8 #include <Kokkos_Core.hpp>
10 #include <ginkgo/config.hpp>
11 #include <ginkgo/core/base/exception.hpp>
12 #include <ginkgo/core/base/exception_helpers.hpp>
13 #include <ginkgo/core/base/executor.hpp>
28 template <
typename MemorySpace,
typename ExecType>
29 struct compatible_space
30 : std::integral_constant<bool, Kokkos::has_shared_space ||
31 Kokkos::has_shared_host_pinned_space> {};
34 struct compatible_space<Kokkos::HostSpace, ReferenceExecutor> : std::true_type {
37 template <
typename MemorySpace>
38 struct compatible_space<MemorySpace, ReferenceExecutor> {
41 static constexpr
bool value =
42 Kokkos::SpaceAccessibility<Kokkos::HostSpace, MemorySpace>::accessible;
45 #ifdef KOKKOS_ENABLE_OPENMP
46 template <
typename MemorySpace>
47 struct compatible_space<MemorySpace, OmpExecutor>
48 : compatible_space<MemorySpace, ReferenceExecutor> {};
51 #ifdef KOKKOS_ENABLE_CUDA
52 template <
typename MemorySpace>
53 struct compatible_space<MemorySpace, CudaExecutor> {
54 static constexpr
bool value =
55 Kokkos::SpaceAccessibility<Kokkos::Cuda, MemorySpace>::accessible;
59 #ifdef KOKKOS_ENABLE_HIP
60 template <
typename MemorySpace>
61 struct compatible_space<MemorySpace, HipExecutor> {
62 static constexpr
bool value =
63 Kokkos::SpaceAccessibility<Kokkos::HIP, MemorySpace>::accessible;
67 #ifdef KOKKOS_ENABLE_SYCL
68 template <
typename MemorySpace>
69 struct compatible_space<MemorySpace, DpcppExecutor> {
70 static constexpr
bool value =
71 Kokkos::SpaceAccessibility<Kokkos::Experimental::SYCL,
72 MemorySpace>::accessible;
85 template <
typename MemorySpace,
typename ExecType>
86 inline bool check_compatibility(std::shared_ptr<const ExecType>)
88 return compatible_space<MemorySpace, ExecType>::value;
101 template <
typename MemorySpace>
102 inline bool check_compatibility(std::shared_ptr<const Executor> exec)
104 if (
auto p = std::dynamic_pointer_cast<const ReferenceExecutor>(exec)) {
105 return check_compatibility<MemorySpace>(p);
107 if (
auto p = std::dynamic_pointer_cast<const OmpExecutor>(exec)) {
108 return check_compatibility<MemorySpace>(p);
110 if (
auto p = std::dynamic_pointer_cast<const CudaExecutor>(exec)) {
111 return check_compatibility<MemorySpace>(p);
113 if (
auto p = std::dynamic_pointer_cast<const HipExecutor>(exec)) {
114 return check_compatibility<MemorySpace>(p);
116 if (
auto p = std::dynamic_pointer_cast<const DpcppExecutor>(exec)) {
117 return check_compatibility<MemorySpace>(p);
133 template <
typename MemorySpace,
typename T>
134 inline void assert_compatibility(T&& obj)
136 GKO_THROW_IF_INVALID(check_compatibility<MemorySpace>(obj.get_executor()),
137 "Executor type and memory space are incompatible");
151 inline std::shared_ptr<Executor> create_default_host_executor()
153 #ifdef KOKKOS_ENABLE_SERIAL
154 if constexpr (std::is_same_v<Kokkos::DefaultHostExecutionSpace,
156 return ReferenceExecutor::create();
159 #ifdef KOKKOS_ENABLE_OPENMP
160 if constexpr (std::is_same_v<Kokkos::DefaultHostExecutionSpace,
190 template <
typename ExecSpace,
191 typename MemorySpace =
typename ExecSpace::memory_space>
192 inline std::shared_ptr<Executor> create_executor(ExecSpace ex, MemorySpace = {})
195 Kokkos::SpaceAccessibility<ExecSpace, MemorySpace>::accessible);
196 #ifdef KOKKOS_ENABLE_SERIAL
197 if constexpr (std::is_same_v<ExecSpace, Kokkos::Serial>) {
198 return ReferenceExecutor::create();
201 #ifdef KOKKOS_ENABLE_OPENMP
202 if constexpr (std::is_same_v<ExecSpace, Kokkos::OpenMP>) {
206 #ifdef KOKKOS_ENABLE_CUDA
207 if constexpr (std::is_same_v<ExecSpace, Kokkos::Cuda>) {
208 if constexpr (std::is_same_v<MemorySpace, Kokkos::CudaSpace>) {
210 Kokkos::device_id(), create_default_host_executor(),
211 std::make_shared<CudaAllocator>(), ex.cuda_stream());
213 if constexpr (std::is_same_v<MemorySpace, Kokkos::CudaUVMSpace>) {
215 Kokkos::device_id(), create_default_host_executor(),
216 std::make_shared<CudaUnifiedAllocator>(Kokkos::device_id()),
219 if constexpr (std::is_same_v<MemorySpace,
220 Kokkos::CudaHostPinnedSpace>) {
222 Kokkos::device_id(), create_default_host_executor(),
223 std::make_shared<CudaHostAllocator>(Kokkos::device_id()),
228 #ifdef KOKKOS_ENABLE_HIP
229 if constexpr (std::is_same_v<ExecSpace, Kokkos::HIP>) {
230 if constexpr (std::is_same_v<MemorySpace, Kokkos::HIPSpace>) {
232 Kokkos::device_id(), create_default_host_executor(),
233 std::make_shared<HipAllocator>(), ex.hip_stream());
235 if constexpr (std::is_same_v<MemorySpace, Kokkos::HIPManagedSpace>) {
237 Kokkos::device_id(), create_default_host_executor(),
238 std::make_shared<HipUnifiedAllocator>(Kokkos::device_id()),
241 if constexpr (std::is_same_v<MemorySpace, Kokkos::HIPHostPinnedSpace>) {
243 Kokkos::device_id(), create_default_host_executor(),
244 std::make_shared<HipHostAllocator>(Kokkos::device_id()),
249 #ifdef KOKKOS_ENABLE_SYCL
250 if constexpr (std::is_same_v<ExecSpace, Kokkos::Experimental::SYCL>) {
252 std::is_same_v<MemorySpace, Kokkos::Experimental::SYCLSpace>,
253 "Ginkgo doesn't support shared memory space allocation for SYCL");
255 create_default_host_executor());
267 inline std::shared_ptr<Executor> create_default_executor(
268 Kokkos::DefaultExecutionSpace ex = {})
270 return create_executor(std::move(ex));
279 #endif // GINKGO_EXTENSIONS_KOKKOS_SPACES_HPP