Ginkgo: ginkgo/extensions/kokkos/spaces.hpp Source File

| Ginkgo Generated from branch based on main. Ginkgo version 1.11.0 A numerical linear algebra library targeting many-core architectures

Loading...
Searching...
No Matches
spaces.hpp
1// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#ifndef GINKGO_EXTENSIONS_KOKKOS_SPACES_HPP
6#define GINKGO_EXTENSIONS_KOKKOS_SPACES_HPP
7
8#include <Kokkos_Core.hpp>
9
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>
14
15
16namespace gko {
17namespace ext {
18namespace kokkos {
19namespace detail {
20
21#ifdef KOKKOS_ENABLE_SYCL
22#if KOKKOS_VERSION >= 40500
23using KokkosSYCLExecSpace = Kokkos::SYCL;
24using KokkosSYCLMemorySpace = Kokkos::SYCLDeviceUSMSpace;
25#else
26using KokkosSYCLExecSpace = Kokkos::Experimental::SYCL;
27using KokkosSYCLMemorySpace = Kokkos::Experimental::SYCLDeviceUSMSpace;
28#endif
29#endif
30
31
39template <typename MemorySpace, typename ExecType>
40struct compatible_space
41 : std::integral_constant<bool, Kokkos::has_shared_space ||
42 Kokkos::has_shared_host_pinned_space> {};
43
44template <>
45struct compatible_space<Kokkos::HostSpace, ReferenceExecutor> : std::true_type {
46};
47
48template <typename MemorySpace>
49struct compatible_space<MemorySpace, ReferenceExecutor> {
50 // need manual implementation of std::integral_constant because,
51 // while compiling for cuda, somehow bool is replaced by __nv_bool
52 static constexpr bool value =
53 Kokkos::SpaceAccessibility<Kokkos::HostSpace, MemorySpace>::accessible;
54};
55
56#ifdef KOKKOS_ENABLE_OPENMP
57template <typename MemorySpace>
58struct compatible_space<MemorySpace, OmpExecutor>
59 : compatible_space<MemorySpace, ReferenceExecutor> {};
60#endif
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;
66};
67#endif
68
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;
74};
75#endif
76
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;
83};
84#endif
85
86
95template <typename MemorySpace, typename ExecType>
96inline bool check_compatibility(std::shared_ptr<const ExecType>)
97{
98 return compatible_space<MemorySpace, ExecType>::value;
99}
100
101
110
111template <typename MemorySpace>
112inline bool check_compatibility(std::shared_ptr<const Executor> exec)
113{
114 if (auto p = std::dynamic_pointer_cast<const ReferenceExecutor>(exec)) {
115 return check_compatibility<MemorySpace>(p);
116 }
117 if (auto p = std::dynamic_pointer_cast<const OmpExecutor>(exec)) {
118 return check_compatibility<MemorySpace>(p);
119 }
120 if (auto p = std::dynamic_pointer_cast<const CudaExecutor>(exec)) {
121 return check_compatibility<MemorySpace>(p);
122 }
123 if (auto p = std::dynamic_pointer_cast<const HipExecutor>(exec)) {
124 return check_compatibility<MemorySpace>(p);
125 }
126 if (auto p = std::dynamic_pointer_cast<const DpcppExecutor>(exec)) {
127 return check_compatibility<MemorySpace>(p);
128 }
129 GKO_NOT_IMPLEMENTED;
130}
131
132
143template <typename MemorySpace, typename T>
144inline void assert_compatibility(T&& obj)
145{
146 GKO_THROW_IF_INVALID(check_compatibility<MemorySpace>(obj.get_executor()),
147 "Executor type and memory space are incompatible");
148}
149
150
151} // namespace detail
152
153
161inline std::shared_ptr<Executor> create_default_host_executor()
162{
163#ifdef KOKKOS_ENABLE_SERIAL
164 if constexpr (std::is_same_v<Kokkos::DefaultHostExecutionSpace,
165 Kokkos::Serial>) {
166 return ReferenceExecutor::create();
167 }
168#endif
169#ifdef KOKKOS_ENABLE_THREADS
170 if constexpr (std::is_same_v<Kokkos::DefaultHostExecutionSpace,
171 Kokkos::Threads>) {
172 return ReferenceExecutor::create();
173 }
174#endif
175#ifdef KOKKOS_ENABLE_OPENMP
176 if constexpr (std::is_same_v<Kokkos::DefaultHostExecutionSpace,
177 Kokkos::OpenMP>) {
178 return OmpExecutor::create();
179 }
180#endif
181 GKO_NOT_IMPLEMENTED;
182}
183
184
206template <typename ExecSpace,
207 typename MemorySpace = typename ExecSpace::memory_space>
208inline std::shared_ptr<Executor> create_executor(ExecSpace ex, MemorySpace = {})
209{
210 static_assert(
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();
215 }
216#endif
217#ifdef KOKKOS_ENABLE_OPENMP
218 if constexpr (std::is_same_v<ExecSpace, Kokkos::OpenMP>) {
219 return OmpExecutor::create();
220 }
221#endif
222#ifdef KOKKOS_ENABLE_THREADS
223 if constexpr (std::is_same_v<ExecSpace, Kokkos::Threads>) {
224 return ReferenceExecutor::create();
225 }
226#endif
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>) {
230 return CudaExecutor::create(
231 Kokkos::device_id(), create_default_host_executor(),
232 std::make_shared<CudaAllocator>(), ex.cuda_stream());
233 }
234 if constexpr (std::is_same_v<MemorySpace, Kokkos::CudaUVMSpace>) {
235 return CudaExecutor::create(
236 Kokkos::device_id(), create_default_host_executor(),
237 std::make_shared<CudaUnifiedAllocator>(Kokkos::device_id()),
238 ex.cuda_stream());
239 }
240 if constexpr (std::is_same_v<MemorySpace,
241 Kokkos::CudaHostPinnedSpace>) {
242 return CudaExecutor::create(
243 Kokkos::device_id(), create_default_host_executor(),
244 std::make_shared<CudaHostAllocator>(Kokkos::device_id()),
245 ex.cuda_stream());
246 }
247 }
248#endif
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>) {
252 return HipExecutor::create(
253 Kokkos::device_id(), create_default_host_executor(),
254 std::make_shared<HipAllocator>(), ex.hip_stream());
255 }
256 if constexpr (std::is_same_v<MemorySpace, Kokkos::HIPManagedSpace>) {
257 return HipExecutor::create(
258 Kokkos::device_id(), create_default_host_executor(),
259 std::make_shared<HipUnifiedAllocator>(Kokkos::device_id()),
260 ex.hip_stream());
261 }
262 if constexpr (std::is_same_v<MemorySpace, Kokkos::HIPHostPinnedSpace>) {
263 return HipExecutor::create(
264 Kokkos::device_id(), create_default_host_executor(),
265 std::make_shared<HipHostAllocator>(Kokkos::device_id()),
266 ex.hip_stream());
267 }
268 }
269#endif
270#ifdef KOKKOS_ENABLE_SYCL
271 if constexpr (std::is_same_v<ExecSpace, detail::KokkosSYCLExecSpace>) {
272 static_assert(
273 std::is_same_v<MemorySpace, detail::KokkosSYCLMemorySpace>,
274 "Ginkgo doesn't support shared memory space allocation for SYCL");
275 return DpcppExecutor::create(Kokkos::device_id(),
276 create_default_host_executor());
277 }
278#endif
279 GKO_NOT_IMPLEMENTED;
280}
281
282
288inline std::shared_ptr<Executor> create_default_executor(
289 Kokkos::DefaultExecutionSpace ex = {})
290{
291 return create_executor(std::move(ex));
292}
293
294
295} // namespace kokkos
296} // namespace ext
297} // namespace gko
298
299
300#endif // GINKGO_EXTENSIONS_KOKKOS_SPACES_HPP
gko::CudaExecutor::create
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.
gko::DpcppExecutor::create
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.
gko::HipExecutor::create
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.
gko::OmpExecutor::create
static std::shared_ptr< OmpExecutor > create(std::shared_ptr< CpuAllocatorBase > alloc=std::make_shared< CpuAllocator >())
Creates a new OmpExecutor.
Definition executor.hpp:1396
gko
The Ginkgo namespace.
Definition abstract_factory.hpp:20