1#ifndef TPETRA_DETAILS_EXECUTIONSPACES_HPP
2#define TPETRA_DETAILS_EXECUTIONSPACES_HPP
8#include <Kokkos_Core.hpp>
10#include <Teuchos_RCP.hpp>
31#define TPETRA_DETAILS_SPACES_THROW(x) \
33 std::stringstream ss; \
34 ss << __FILE__ << ":" << __LINE__ << ": " << x; \
35 throw std::runtime_error(ss.str()); \
54#if defined(KOKKOS_ENABLE_CUDA)
61 throw std::runtime_error(
ss.str());
64#define TPETRA_DETAILS_SPACES_CUDA_RUNTIME(x) \
65 Tpetra::Details::Spaces::success_or_throw((x), __FILE__, __LINE__)
76#if defined(KOKKOS_ENABLE_CUDA)
82 cudaEvent_t execSpaceWaitEvent_;
85 ~CudaInfo() =
default;
86 CudaInfo(
const CudaInfo &other) =
delete;
87 CudaInfo(CudaInfo &&other) =
delete;
89extern CudaInfo cudaInfo;
93#if defined(KOKKOS_ENABLE_CUDA)
94template <
typename Space>
95using IsCuda = std::enable_if_t<std::is_same_v<Space, Kokkos::Cuda>,
bool>;
96template <
typename Space>
97using NotCuda = std::enable_if_t<!std::is_same_v<Space, Kokkos::Cuda>,
bool>;
98template <
typename S1,
typename S2>
99using BothCuda = std::enable_if_t<
100 std::is_same_v<S1, Kokkos::Cuda> && std::is_same_v<S2, Kokkos::Cuda>,
bool>;
101template <
typename S1,
typename S2>
102using NotBothCuda = std::enable_if_t<!std::is_same_v<S1, Kokkos::Cuda> ||
103 !std::is_same_v<S2, Kokkos::Cuda>,
107#if defined(KOKKOS_ENABLE_SERIAL)
109template <
typename Space>
110using IsSerial = std::enable_if_t<std::is_same_v<Space, Kokkos::Serial>,
bool>;
113#if defined(KOKKOS_ENABLE_OPENMP)
115template <
typename Space>
116using IsOpenMP = std::enable_if_t<std::is_same_v<Space, Kokkos::OpenMP>,
bool>;
119#if defined(KOKKOS_ENABLE_HIP)
121template <
typename Space>
122using IsHIP = std::enable_if_t<std::is_same_v<Space, Kokkos::HIP>,
bool>;
125#if defined(KOKKOS_ENABLE_SYCL)
127template <
typename Space>
128using IsSYCL = std::enable_if_t<std::is_same_v<Space, Kokkos::Experimental::SYCL>,
bool>;
136template <
typename ExecSpace,
Priority priority = Priority::medium
137#if defined(KOKKOS_ENABLE_CUDA)
139 NotCuda<ExecSpace> =
true
152#if defined(KOKKOS_ENABLE_CUDA)
155Kokkos::Cuda make_instance() {
163 case Priority::medium:
170 throw std::runtime_error(
"unexpected static Tpetra Space priority");
174 return Kokkos::Cuda(
stream,
true );
187 case Priority::medium:
192 throw std::runtime_error(
"unexpected dynamic Tpetra Space priority");
210 using rcp_type = Teuchos::RCP<const execution_space>;
218 template <Priority priority = Priority::medium>
221 "Tpetra::Details::Spaces::space_instance");
223 constexpr int p =
static_cast<int>(
priority);
224 static_assert(
p <
sizeof(instances) /
sizeof(instances[0]),
225 "Spaces::Priority enum error");
228 TPETRA_DETAILS_SPACES_THROW(
"requested instance id " <<
i <<
" (< 0)");
231 TPETRA_DETAILS_SPACES_THROW(
232 "requested instance id "
234 <<
") set by TPETRA_SPACES_ID_WARN_LIMT");
239 while (
size_t(
i) >= instances[
p].
size()) {
240 instances[
p].push_back(Teuchos::ENull());
255 rcp_type
r = Teuchos::RCP<const execution_space>(
259 instances[
p][
i] =
r.create_weak();
265 auto r = instances[
p][
i].create_strong();
274 for (
const rcp_type &
rcp : instances[
i]) {
275 if (
rcp.is_valid_ptr() && !
rcp.is_null()) {
278 <<
" execution space instance survived to "
279 "~InstanceLifetimeManager. strong_count() = "
280 <<
rcp.strong_count()
281 <<
". Did a Tpetra object live past Kokkos::finalize()?"
290 std::vector<rcp_type>
291 instances[
static_cast<int>(Spaces::Priority::NUM_LEVELS)];
294#if defined(KOKKOS_ENABLE_CUDA)
297#if defined(KOKKOS_ENABLE_SERIAL)
300#if defined(KOKKOS_ENABLE_OPENMP)
303#if defined(KOKKOS_ENABLE_HIP)
306#if defined(KOKKOS_ENABLE_SYCL)
310#if defined(KOKKOS_ENABLE_CUDA)
317Teuchos::RCP<const ExecSpace> space_instance(
int i = 0) {
322#if defined(KOKKOS_ENABLE_SERIAL)
328Teuchos::RCP<const ExecSpace> space_instance(
int i = 0) {
333#if defined(KOKKOS_ENABLE_OPENMP)
337template <
typename ExecSpace, Priority priority = Priority::medium,
338 IsOpenMP<ExecSpace> =
true>
339Teuchos::RCP<const ExecSpace> space_instance(
int i = 0) {
340 return openMPSpaces.space_instance<priority>(i);
344#if defined(KOKKOS_ENABLE_HIP)
347template <
typename ExecSpace,
Priority priority = Priority::medium,
348 IsHIP<ExecSpace> =
true>
350 return HIPSpaces.space_instance<priority>(i);
353#if defined(KOKKOS_ENABLE_SYCL)
357template <
typename ExecSpace,
Priority priority = Priority::medium,
358 IsSYCL<ExecSpace> =
true>
360 return SYCLSpaces.space_instance<priority>(i);
369template <
typename ExecSpace>
375 case Priority::medium:
380 throw std::runtime_error(
381 "unexpected dynamic Tpetra Space priority in space_instance");
398template <
typename S1,
typename S2
399#if defined(KOKKOS_ENABLE_CUDA)
406 "Tpetra::Details::Spaces::exec_space_wait");
411#if defined(KOKKOS_ENABLE_CUDA)
412template <
typename S1,
typename S2, BothCuda<S1, S2> = true>
415 "Tpetra::Details::Spaces::exec_space_wait");
419 if (
waitee.impl_instance_id() !=
421 .impl_instance_id()) {
434template <
typename S1,
typename S2>
435void exec_space_wait(
const S1 &waitee,
const S2 &waiter) {
437 "Tpetra::Details::Spaces::exec_space_wait");
439 exec_space_wait(
"anonymous", waitee, waiter);
442template <
typename ExecutionSpace>
443constexpr KOKKOS_INLINE_FUNCTION
bool is_gpu_exec_space() {
447#if defined(KOKKOS_ENABLE_CUDA)
449constexpr KOKKOS_INLINE_FUNCTION
bool is_gpu_exec_space<Kokkos::Cuda>() {
454#if defined(KOKKOS_ENABLE_HIP)
456constexpr KOKKOS_INLINE_FUNCTION
bool
457is_gpu_exec_space<Kokkos::Experimental::HIP>() {
462#if defined(KOKKOS_ENABLE_SYCL)
464constexpr KOKKOS_INLINE_FUNCTION
bool
465is_gpu_exec_space<Kokkos::Experimental::SYCL>() {
474#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.