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>
29template <
typename MemorySpace,
typename ExecType>
30struct compatible_space
31 : std::integral_constant<bool, Kokkos::has_shared_space ||
32 Kokkos::has_shared_host_pinned_space> {};
35struct compatible_space<Kokkos::HostSpace, ReferenceExecutor> : std::true_type {
38template <
typename MemorySpace>
39struct compatible_space<MemorySpace, ReferenceExecutor> {
42 static constexpr bool value =
43 Kokkos::SpaceAccessibility<Kokkos::HostSpace, MemorySpace>::accessible;
46#ifdef KOKKOS_ENABLE_OPENMP
47template <
typename MemorySpace>
48struct compatible_space<MemorySpace, OmpExecutor>
49 : compatible_space<MemorySpace, ReferenceExecutor> {};
52#ifdef KOKKOS_ENABLE_CUDA
53template <
typename MemorySpace>
54struct compatible_space<MemorySpace, CudaExecutor> {
55 static constexpr bool value =
56 Kokkos::SpaceAccessibility<Kokkos::Cuda, MemorySpace>::accessible;
60#ifdef KOKKOS_ENABLE_HIP
61template <
typename MemorySpace>
62struct compatible_space<MemorySpace, HipExecutor> {
63 static constexpr bool value =
64 Kokkos::SpaceAccessibility<Kokkos::HIP, MemorySpace>::accessible;
68#ifdef KOKKOS_ENABLE_SYCL
69template <
typename MemorySpace>
70struct compatible_space<MemorySpace, DpcppExecutor> {
71 static constexpr bool value =
72 Kokkos::SpaceAccessibility<Kokkos::Experimental::SYCL,
73 MemorySpace>::accessible;
86template <
typename MemorySpace,
typename ExecType>
87inline bool check_compatibility(std::shared_ptr<const ExecType>)
89 return compatible_space<MemorySpace, ExecType>::value;
102template <
typename MemorySpace>
103inline 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);
134template <
typename MemorySpace,
typename T>
135inline void assert_compatibility(T&& obj)
137 GKO_THROW_IF_INVALID(check_compatibility<MemorySpace>(obj.get_executor()),
138 "Executor type and memory space are incompatible");
152inline 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,
191template <
typename ExecSpace,
192 typename MemorySpace =
typename ExecSpace::memory_space>
193inline 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());
268inline std::shared_ptr<Executor> create_default_executor(
269 Kokkos::DefaultExecutionSpace ex = {})
271 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:1345
The Ginkgo namespace.
Definition abstract_factory.hpp:20