5 #ifndef GINKGO_EXTENSIONS_KOKKOS_SPACES_HPP
6 #define GINKGO_EXTENSIONS_KOKKOS_SPACES_HPP
8 #include <Kokkos_Core.hpp>
11 #include <ginkgo/config.hpp>
12 #include <ginkgo/core/base/exception.hpp>
13 #include <ginkgo/core/base/exception_helpers.hpp>
14 #include <ginkgo/core/base/executor.hpp>
29 template <
typename MemorySpace,
typename ExecType>
30 struct compatible_space
31 : std::integral_constant<bool, Kokkos::has_shared_space ||
32 Kokkos::has_shared_host_pinned_space> {};
35 struct compatible_space<Kokkos::HostSpace, ReferenceExecutor> : std::true_type {
38 template <
typename MemorySpace>
39 struct compatible_space<MemorySpace, ReferenceExecutor> {
42 static constexpr
bool value =
43 Kokkos::SpaceAccessibility<Kokkos::HostSpace, MemorySpace>::accessible;
46 #ifdef KOKKOS_ENABLE_OPENMP
47 template <
typename MemorySpace>
48 struct compatible_space<MemorySpace, OmpExecutor>
49 : compatible_space<MemorySpace, ReferenceExecutor> {};
52 #ifdef KOKKOS_ENABLE_CUDA
53 template <
typename MemorySpace>
54 struct compatible_space<MemorySpace, CudaExecutor> {
55 static constexpr
bool value =
56 Kokkos::SpaceAccessibility<Kokkos::Cuda, MemorySpace>::accessible;
60 #ifdef KOKKOS_ENABLE_HIP
61 template <
typename MemorySpace>
62 struct compatible_space<MemorySpace, HipExecutor> {
63 static constexpr
bool value =
64 Kokkos::SpaceAccessibility<Kokkos::HIP, MemorySpace>::accessible;
68 #ifdef KOKKOS_ENABLE_SYCL
69 template <
typename MemorySpace>
70 struct compatible_space<MemorySpace, DpcppExecutor> {
71 static constexpr
bool value =
72 Kokkos::SpaceAccessibility<Kokkos::Experimental::SYCL,
73 MemorySpace>::accessible;
86 template <
typename MemorySpace,
typename ExecType>
87 inline bool check_compatibility(std::shared_ptr<const ExecType>)
89 return compatible_space<MemorySpace, ExecType>::value;
102 template <
typename MemorySpace>
103 inline bool check_compatibility(std::shared_ptr<const Executor> exec)
105 if (
auto p = std::dynamic_pointer_cast<const ReferenceExecutor>(exec)) {
106 return check_compatibility<MemorySpace>(p);
108 if (
auto p = std::dynamic_pointer_cast<const OmpExecutor>(exec)) {
109 return check_compatibility<MemorySpace>(p);
111 if (
auto p = std::dynamic_pointer_cast<const CudaExecutor>(exec)) {
112 return check_compatibility<MemorySpace>(p);
114 if (
auto p = std::dynamic_pointer_cast<const HipExecutor>(exec)) {
115 return check_compatibility<MemorySpace>(p);
117 if (
auto p = std::dynamic_pointer_cast<const DpcppExecutor>(exec)) {
118 return check_compatibility<MemorySpace>(p);
134 template <
typename MemorySpace,
typename T>
135 inline void assert_compatibility(T&& obj)
137 GKO_THROW_IF_INVALID(check_compatibility<MemorySpace>(obj.get_executor()),
138 "Executor type and memory space are incompatible");
152 inline std::shared_ptr<Executor> create_default_host_executor()
154 #ifdef KOKKOS_ENABLE_SERIAL
155 if constexpr (std::is_same_v<Kokkos::DefaultHostExecutionSpace,
157 return ReferenceExecutor::create();
160 #ifdef KOKKOS_ENABLE_OPENMP
161 if constexpr (std::is_same_v<Kokkos::DefaultHostExecutionSpace,
191 template <
typename ExecSpace,
192 typename MemorySpace =
typename ExecSpace::memory_space>
193 inline std::shared_ptr<Executor> create_executor(ExecSpace ex, MemorySpace = {})
196 Kokkos::SpaceAccessibility<ExecSpace, MemorySpace>::accessible);
197 #ifdef KOKKOS_ENABLE_SERIAL
198 if constexpr (std::is_same_v<ExecSpace, Kokkos::Serial>) {
199 return ReferenceExecutor::create();
202 #ifdef KOKKOS_ENABLE_OPENMP
203 if constexpr (std::is_same_v<ExecSpace, Kokkos::OpenMP>) {
207 #ifdef KOKKOS_ENABLE_CUDA
208 if constexpr (std::is_same_v<ExecSpace, Kokkos::Cuda>) {
209 if constexpr (std::is_same_v<MemorySpace, Kokkos::CudaSpace>) {
211 Kokkos::device_id(), create_default_host_executor(),
212 std::make_shared<CudaAllocator>(), ex.cuda_stream());
214 if constexpr (std::is_same_v<MemorySpace, Kokkos::CudaUVMSpace>) {
216 Kokkos::device_id(), create_default_host_executor(),
217 std::make_shared<CudaUnifiedAllocator>(Kokkos::device_id()),
220 if constexpr (std::is_same_v<MemorySpace,
221 Kokkos::CudaHostPinnedSpace>) {
223 Kokkos::device_id(), create_default_host_executor(),
224 std::make_shared<CudaHostAllocator>(Kokkos::device_id()),
229 #ifdef KOKKOS_ENABLE_HIP
230 if constexpr (std::is_same_v<ExecSpace, Kokkos::HIP>) {
231 if constexpr (std::is_same_v<MemorySpace, Kokkos::HIPSpace>) {
233 Kokkos::device_id(), create_default_host_executor(),
234 std::make_shared<HipAllocator>(), ex.hip_stream());
236 if constexpr (std::is_same_v<MemorySpace, Kokkos::HIPManagedSpace>) {
238 Kokkos::device_id(), create_default_host_executor(),
239 std::make_shared<HipUnifiedAllocator>(Kokkos::device_id()),
242 if constexpr (std::is_same_v<MemorySpace, Kokkos::HIPHostPinnedSpace>) {
244 Kokkos::device_id(), create_default_host_executor(),
245 std::make_shared<HipHostAllocator>(Kokkos::device_id()),
250 #ifdef KOKKOS_ENABLE_SYCL
251 if constexpr (std::is_same_v<ExecSpace, Kokkos::Experimental::SYCL>) {
253 std::is_same_v<MemorySpace, Kokkos::Experimental::SYCLSpace>,
254 "Ginkgo doesn't support shared memory space allocation for SYCL");
256 create_default_host_executor());
268 inline std::shared_ptr<Executor> create_default_executor(
269 Kokkos::DefaultExecutionSpace ex = {})
271 return create_executor(std::move(ex));
280 #endif // GINKGO_EXTENSIONS_KOKKOS_SPACES_HPP