10#ifndef TPETRA_DETAILS_EXECUTIONSPACES_HPP
11#define TPETRA_DETAILS_EXECUTIONSPACES_HPP
17#include <Kokkos_Core.hpp>
19#include <Teuchos_RCP.hpp>
40#define TPETRA_DETAILS_SPACES_THROW(x) \
42 std::stringstream ss; \
43 ss << __FILE__ << ":" << __LINE__ << ": " << x; \
44 throw std::runtime_error(ss.str()); \
63#if defined(KOKKOS_ENABLE_CUDA)
70 throw std::runtime_error(
ss.str());
73#define TPETRA_DETAILS_SPACES_CUDA_RUNTIME(x) \
74 Tpetra::Details::Spaces::success_or_throw((x), __FILE__, __LINE__)
85#if defined(KOKKOS_ENABLE_CUDA)
91 cudaEvent_t execSpaceWaitEvent_;
94 ~CudaInfo() =
default;
95 CudaInfo(
const CudaInfo &other) =
delete;
96 CudaInfo(CudaInfo &&other) =
delete;
98extern CudaInfo cudaInfo;
102#if defined(KOKKOS_ENABLE_CUDA)
103template <
typename Space>
104using IsCuda = std::enable_if_t<std::is_same_v<Space, Kokkos::Cuda>,
bool>;
105template <
typename Space>
106using NotCuda = std::enable_if_t<!std::is_same_v<Space, Kokkos::Cuda>,
bool>;
107template <
typename S1,
typename S2>
108using BothCuda = std::enable_if_t<
109 std::is_same_v<S1, Kokkos::Cuda> && std::is_same_v<S2, Kokkos::Cuda>,
bool>;
110template <
typename S1,
typename S2>
111using NotBothCuda = std::enable_if_t<!std::is_same_v<S1, Kokkos::Cuda> ||
112 !std::is_same_v<S2, Kokkos::Cuda>,
116#if defined(KOKKOS_ENABLE_SERIAL)
118template <
typename Space>
119using IsSerial = std::enable_if_t<std::is_same_v<Space, Kokkos::Serial>,
bool>;
122#if defined(KOKKOS_ENABLE_OPENMP)
124template <
typename Space>
125using IsOpenMP = std::enable_if_t<std::is_same_v<Space, Kokkos::OpenMP>,
bool>;
128#if defined(KOKKOS_ENABLE_HIP)
130template <
typename Space>
131using IsHIP = std::enable_if_t<std::is_same_v<Space, Kokkos::HIP>,
bool>;
134#if defined(KOKKOS_ENABLE_SYCL)
136template <
typename Space>
137using IsSYCL = std::enable_if_t<std::is_same_v<Space, Kokkos::Experimental::SYCL>,
bool>;
145template <
typename ExecSpace,
Priority priority = Priority::medium
146#if defined(KOKKOS_ENABLE_CUDA)
148 NotCuda<ExecSpace> =
true
161#if defined(KOKKOS_ENABLE_CUDA)
164Kokkos::Cuda make_instance() {
172 case Priority::medium:
179 throw std::runtime_error(
"unexpected static Tpetra Space priority");
183 return Kokkos::Cuda(
stream,
true );
196 case Priority::medium:
201 throw std::runtime_error(
"unexpected dynamic Tpetra Space priority");
219 using rcp_type = Teuchos::RCP<const execution_space>;
227 template <Priority priority = Priority::medium>
230 "Tpetra::Details::Spaces::space_instance");
232 constexpr int p =
static_cast<int>(
priority);
233 static_assert(
p <
sizeof(instances) /
sizeof(instances[0]),
234 "Spaces::Priority enum error");
237 TPETRA_DETAILS_SPACES_THROW(
"requested instance id " <<
i <<
" (< 0)");
240 TPETRA_DETAILS_SPACES_THROW(
241 "requested instance id "
243 <<
") set by TPETRA_SPACES_ID_WARN_LIMIT");
248 while (
size_t(
i) >= instances[
p].
size()) {
249 instances[
p].push_back(Teuchos::ENull());
264 rcp_type
r = Teuchos::RCP<const execution_space>(
268 instances[
p][
i] =
r.create_weak();
274 auto r = instances[
p][
i].create_strong();
283 for (
const rcp_type &
rcp : instances[
i]) {
284 if (
rcp.is_valid_ptr() && !
rcp.is_null()) {
287 <<
" execution space instance survived to "
288 "~InstanceLifetimeManager. strong_count() = "
289 <<
rcp.strong_count()
290 <<
". Did a Tpetra object live past Kokkos::finalize()?"
299 std::vector<rcp_type>
300 instances[
static_cast<int>(Spaces::Priority::NUM_LEVELS)];
303#if defined(KOKKOS_ENABLE_CUDA)
306#if defined(KOKKOS_ENABLE_SERIAL)
309#if defined(KOKKOS_ENABLE_OPENMP)
312#if defined(KOKKOS_ENABLE_HIP)
315#if defined(KOKKOS_ENABLE_SYCL)
319#if defined(KOKKOS_ENABLE_CUDA)
326Teuchos::RCP<const ExecSpace> space_instance(
int i = 0) {
331#if defined(KOKKOS_ENABLE_SERIAL)
337Teuchos::RCP<const ExecSpace> space_instance(
int i = 0) {
342#if defined(KOKKOS_ENABLE_OPENMP)
346template <
typename ExecSpace, Priority priority = Priority::medium,
347 IsOpenMP<ExecSpace> =
true>
348Teuchos::RCP<const ExecSpace> space_instance(
int i = 0) {
349 return openMPSpaces.space_instance<priority>(i);
353#if defined(KOKKOS_ENABLE_HIP)
356template <
typename ExecSpace,
Priority priority = Priority::medium,
357 IsHIP<ExecSpace> =
true>
359 return HIPSpaces.space_instance<priority>(i);
362#if defined(KOKKOS_ENABLE_SYCL)
366template <
typename ExecSpace,
Priority priority = Priority::medium,
367 IsSYCL<ExecSpace> =
true>
369 return SYCLSpaces.space_instance<priority>(i);
378template <
typename ExecSpace>
384 case Priority::medium:
389 throw std::runtime_error(
390 "unexpected dynamic Tpetra Space priority in space_instance");
407template <
typename S1,
typename S2
408#if defined(KOKKOS_ENABLE_CUDA)
415 "Tpetra::Details::Spaces::exec_space_wait");
420#if defined(KOKKOS_ENABLE_CUDA)
421template <
typename S1,
typename S2, BothCuda<S1, S2> = true>
424 "Tpetra::Details::Spaces::exec_space_wait");
428 if (
waitee.impl_instance_id() !=
430 .impl_instance_id()) {
443template <
typename S1,
typename S2>
444void exec_space_wait(
const S1 &waitee,
const S2 &waiter) {
446 "Tpetra::Details::Spaces::exec_space_wait");
448 exec_space_wait(
"anonymous", waitee, waiter);
451template <
typename ExecutionSpace>
452constexpr KOKKOS_INLINE_FUNCTION
bool is_gpu_exec_space() {
456#if defined(KOKKOS_ENABLE_CUDA)
458constexpr KOKKOS_INLINE_FUNCTION
bool is_gpu_exec_space<Kokkos::Cuda>() {
463#if defined(KOKKOS_ENABLE_HIP)
465constexpr KOKKOS_INLINE_FUNCTION
bool
466is_gpu_exec_space<Kokkos::HIP>() {
471#if defined(KOKKOS_ENABLE_SYCL)
473constexpr KOKKOS_INLINE_FUNCTION
bool
474is_gpu_exec_space<Kokkos::Experimental::SYCL>() {
483#undef TPETRA_DETAILS_SPACES_THROW
Declaration of Tpetra::Details::Behavior, a class that describes Tpetra's behavior.
void exec_space_wait(const char *msg, const S1 &waitee, const S2 &)
cause future work submitted to waiter to wait for the current work in waitee to finish
ExecSpace make_instance()
Construct a Kokkos execution space instance with the following priority.
Teuchos::RCP< const ExecSpace > space_instance(const Priority &priority, int i=0)
get a strong Teuchos::RCP to Tpetra-managed Kokkos execution space instance i
Priority
Priority interface for Tpetra's managed execution spaces.
Declaration of Tpetra::Details::Profiling, a scope guard for Kokkos Profiling.
Struct that holds views of the contents of a CrsMatrix.
static size_t spacesIdWarnLimit()
Warn if more than this many Kokkos spaces are accessed.
Provides reusable Kokkos execution space instances.
~InstanceLifetimeManager()
Issue a warning if any Tpetra-managed execution space instances survive to the end of static lifetime...
rcp_type space_instance(int i=0)
Retrieve a strong Teuchos::RCP<const ExecSpace> to instance i
Implementation details of Tpetra.
Namespace Tpetra contains the class and methods constituting the Tpetra library.