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>
21#ifdef KOKKOS_ENABLE_SYCL
22#if KOKKOS_VERSION >= 40500
23using KokkosSYCLExecSpace = Kokkos::SYCL;
24using KokkosSYCLMemorySpace = Kokkos::SYCLDeviceUSMSpace;
26using KokkosSYCLExecSpace = Kokkos::Experimental::SYCL;
27using KokkosSYCLMemorySpace = Kokkos::Experimental::SYCLDeviceUSMSpace;
39template <
typename MemorySpace,
typename ExecType>
40struct compatible_space
41 : std::integral_constant<bool, Kokkos::has_shared_space ||
42 Kokkos::has_shared_host_pinned_space> {};
45struct compatible_space<Kokkos::HostSpace, ReferenceExecutor> : std::true_type {
48template <
typename MemorySpace>
49struct compatible_space<MemorySpace, ReferenceExecutor> {
52 static constexpr bool value =
53 Kokkos::SpaceAccessibility<Kokkos::HostSpace, MemorySpace>::accessible;
56#ifdef KOKKOS_ENABLE_OPENMP
57template <
typename MemorySpace>
58struct compatible_space<MemorySpace, OmpExecutor>
59 : compatible_space<MemorySpace, ReferenceExecutor> {};
61#ifdef KOKKOS_ENABLE_CUDA
62template <
typename MemorySpace>
63struct compatible_space<MemorySpace, CudaExecutor> {
64 static constexpr bool value =
65 Kokkos::SpaceAccessibility<Kokkos::Cuda, MemorySpace>::accessible;
69#ifdef KOKKOS_ENABLE_HIP
70template <
typename MemorySpace>
71struct compatible_space<MemorySpace, HipExecutor> {
72 static constexpr bool value =
73 Kokkos::SpaceAccessibility<Kokkos::HIP, MemorySpace>::accessible;
77#ifdef KOKKOS_ENABLE_SYCL
78template <
typename MemorySpace>
79struct compatible_space<MemorySpace, DpcppExecutor> {
80 static constexpr bool value =
81 Kokkos::SpaceAccessibility<KokkosSYCLExecSpace,
82 MemorySpace>::accessible;
95template <
typename MemorySpace,
typename ExecType>
96inline bool check_compatibility(std::shared_ptr<const ExecType>)
98 return compatible_space<MemorySpace, ExecType>::value;
111template <
typename MemorySpace>
112inline bool check_compatibility(std::shared_ptr<const Executor> exec)
114 if (
auto p = std::dynamic_pointer_cast<const ReferenceExecutor>(exec)) {
115 return check_compatibility<MemorySpace>(p);
117 if (
auto p = std::dynamic_pointer_cast<const OmpExecutor>(exec)) {
118 return check_compatibility<MemorySpace>(p);
120 if (
auto p = std::dynamic_pointer_cast<const CudaExecutor>(exec)) {
121 return check_compatibility<MemorySpace>(p);
123 if (
auto p = std::dynamic_pointer_cast<const HipExecutor>(exec)) {
124 return check_compatibility<MemorySpace>(p);
126 if (
auto p = std::dynamic_pointer_cast<const DpcppExecutor>(exec)) {
127 return check_compatibility<MemorySpace>(p);
143template <
typename MemorySpace,
typename T>
144inline void assert_compatibility(T&& obj)
146 GKO_THROW_IF_INVALID(check_compatibility<MemorySpace>(obj.get_executor()),
147 "Executor type and memory space are incompatible");
161inline std::shared_ptr<Executor> create_default_host_executor()
163#ifdef KOKKOS_ENABLE_SERIAL
164 if constexpr (std::is_same_v<Kokkos::DefaultHostExecutionSpace,
166 return ReferenceExecutor::create();
169#ifdef KOKKOS_ENABLE_THREADS
170 if constexpr (std::is_same_v<Kokkos::DefaultHostExecutionSpace,
172 return ReferenceExecutor::create();
175#ifdef KOKKOS_ENABLE_OPENMP
176 if constexpr (std::is_same_v<Kokkos::DefaultHostExecutionSpace,
206template <
typename ExecSpace,
207 typename MemorySpace =
typename ExecSpace::memory_space>
208inline std::shared_ptr<Executor> create_executor(ExecSpace ex, MemorySpace = {})
211 Kokkos::SpaceAccessibility<ExecSpace, MemorySpace>::accessible);
212#ifdef KOKKOS_ENABLE_SERIAL
213 if constexpr (std::is_same_v<ExecSpace, Kokkos::Serial>) {
214 return ReferenceExecutor::create();
217#ifdef KOKKOS_ENABLE_OPENMP
218 if constexpr (std::is_same_v<ExecSpace, Kokkos::OpenMP>) {
222#ifdef KOKKOS_ENABLE_THREADS
223 if constexpr (std::is_same_v<ExecSpace, Kokkos::Threads>) {
224 return ReferenceExecutor::create();
227#ifdef KOKKOS_ENABLE_CUDA
228 if constexpr (std::is_same_v<ExecSpace, Kokkos::Cuda>) {
229 if constexpr (std::is_same_v<MemorySpace, Kokkos::CudaSpace>) {
231 Kokkos::device_id(), create_default_host_executor(),
232 std::make_shared<CudaAllocator>(), ex.cuda_stream());
234 if constexpr (std::is_same_v<MemorySpace, Kokkos::CudaUVMSpace>) {
236 Kokkos::device_id(), create_default_host_executor(),
237 std::make_shared<CudaUnifiedAllocator>(Kokkos::device_id()),
240 if constexpr (std::is_same_v<MemorySpace,
241 Kokkos::CudaHostPinnedSpace>) {
243 Kokkos::device_id(), create_default_host_executor(),
244 std::make_shared<CudaHostAllocator>(Kokkos::device_id()),
249#ifdef KOKKOS_ENABLE_HIP
250 if constexpr (std::is_same_v<ExecSpace, Kokkos::HIP>) {
251 if constexpr (std::is_same_v<MemorySpace, Kokkos::HIPSpace>) {
253 Kokkos::device_id(), create_default_host_executor(),
254 std::make_shared<HipAllocator>(), ex.hip_stream());
256 if constexpr (std::is_same_v<MemorySpace, Kokkos::HIPManagedSpace>) {
258 Kokkos::device_id(), create_default_host_executor(),
259 std::make_shared<HipUnifiedAllocator>(Kokkos::device_id()),
262 if constexpr (std::is_same_v<MemorySpace, Kokkos::HIPHostPinnedSpace>) {
264 Kokkos::device_id(), create_default_host_executor(),
265 std::make_shared<HipHostAllocator>(Kokkos::device_id()),
270#ifdef KOKKOS_ENABLE_SYCL
271 if constexpr (std::is_same_v<ExecSpace, detail::KokkosSYCLExecSpace>) {
273 std::is_same_v<MemorySpace, detail::KokkosSYCLMemorySpace>,
274 "Ginkgo doesn't support shared memory space allocation for SYCL");
276 create_default_host_executor());
288inline std::shared_ptr<Executor> create_default_executor(
289 Kokkos::DefaultExecutionSpace ex = {})
291 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