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>
28template <
typename MemorySpace,
typename ExecType>
29struct compatible_space
30 : std::integral_constant<bool, Kokkos::has_shared_space ||
31 Kokkos::has_shared_host_pinned_space> {};
34struct compatible_space<Kokkos::HostSpace, ReferenceExecutor> : std::true_type {
37template <
typename MemorySpace>
38struct compatible_space<MemorySpace, ReferenceExecutor> {
41 static constexpr bool value =
42 Kokkos::SpaceAccessibility<Kokkos::HostSpace, MemorySpace>::accessible;
45#ifdef KOKKOS_ENABLE_OPENMP
46template <
typename MemorySpace>
47struct compatible_space<MemorySpace, OmpExecutor>
48 : compatible_space<MemorySpace, ReferenceExecutor> {};
51#ifdef KOKKOS_ENABLE_CUDA
52template <
typename MemorySpace>
53struct compatible_space<MemorySpace, CudaExecutor> {
54 static constexpr bool value =
55 Kokkos::SpaceAccessibility<Kokkos::Cuda, MemorySpace>::accessible;
59#ifdef KOKKOS_ENABLE_HIP
60template <
typename MemorySpace>
61struct compatible_space<MemorySpace, HipExecutor> {
62 static constexpr bool value =
63 Kokkos::SpaceAccessibility<Kokkos::HIP, MemorySpace>::accessible;
67#ifdef KOKKOS_ENABLE_SYCL
68template <
typename MemorySpace>
69struct compatible_space<MemorySpace, DpcppExecutor> {
70 static constexpr bool value =
71 Kokkos::SpaceAccessibility<Kokkos::Experimental::SYCL,
72 MemorySpace>::accessible;
85template <
typename MemorySpace,
typename ExecType>
86inline bool check_compatibility(std::shared_ptr<const ExecType>)
88 return compatible_space<MemorySpace, ExecType>::value;
101template <
typename MemorySpace>
102inline 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);
133template <
typename MemorySpace,
typename T>
134inline void assert_compatibility(T&& obj)
136 GKO_THROW_IF_INVALID(check_compatibility<MemorySpace>(obj.get_executor()),
137 "Executor type and memory space are incompatible");
151inline 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,
190template <
typename ExecSpace,
191 typename MemorySpace =
typename ExecSpace::memory_space>
192inline 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());
267inline std::shared_ptr<Executor> create_default_executor(
268 Kokkos::DefaultExecutionSpace ex = {})
270 return create_executor(std::move(ex));
static std::shared_ptr< CudaExecutor > create(int device_id, std::shared_ptr< Executor > master, bool device_reset, allocation_mode alloc_mode=default_cuda_alloc_mode, CUstream_st *stream=nullptr)
Creates a new CudaExecutor.
static std::shared_ptr< DpcppExecutor > create(int device_id, std::shared_ptr< Executor > master, std::string device_type="all", dpcpp_queue_property property=dpcpp_queue_property::in_order)
Creates a new DpcppExecutor.
static std::shared_ptr< HipExecutor > create(int device_id, std::shared_ptr< Executor > master, bool device_reset, allocation_mode alloc_mode=default_hip_alloc_mode, CUstream_st *stream=nullptr)
Creates a new HipExecutor.
static std::shared_ptr< OmpExecutor > create(std::shared_ptr< CpuAllocatorBase > alloc=std::make_shared< CpuAllocator >())
Creates a new OmpExecutor.
Definition executor.hpp:1396
The Ginkgo namespace.
Definition abstract_factory.hpp:20