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)
64 inline void success_or_throw(cudaError_t err,
const char *file,
66 if (err != cudaSuccess) {
68 ss << file <<
":" << line <<
": ";
69 ss << cudaGetErrorString(err);
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__)
75 #endif // KOKKOS_ENABLE_CUDA
85 #if defined(KOKKOS_ENABLE_CUDA)
91 cudaEvent_t execSpaceWaitEvent_;
94 ~CudaInfo() =
default;
95 CudaInfo(
const CudaInfo &other) =
delete;
96 CudaInfo(CudaInfo &&other) =
delete;
98 extern CudaInfo cudaInfo;
99 #endif // KOKKOS_ENABLE_CUDA
102 #if defined(KOKKOS_ENABLE_CUDA)
103 template <
typename Space>
104 using IsCuda = std::enable_if_t<std::is_same_v<Space, Kokkos::Cuda>,
bool>;
105 template <
typename Space>
106 using NotCuda = std::enable_if_t<!std::is_same_v<Space, Kokkos::Cuda>,
bool>;
107 template <
typename S1,
typename S2>
108 using BothCuda = std::enable_if_t<
109 std::is_same_v<S1, Kokkos::Cuda> && std::is_same_v<S2, Kokkos::Cuda>,
bool>;
110 template <
typename S1,
typename S2>
111 using NotBothCuda = std::enable_if_t<!std::is_same_v<S1, Kokkos::Cuda> ||
112 !std::is_same_v<S2, Kokkos::Cuda>,
114 #endif // KOKKOS_ENABLE_CUDA
116 #if defined(KOKKOS_ENABLE_SERIAL)
117 template <
typename Space>
119 using IsSerial = std::enable_if_t<std::is_same_v<Space, Kokkos::Serial>,
bool>;
120 #endif // KOKKOS_ENABLE_SERIAL
122 #if defined(KOKKOS_ENABLE_OPENMP)
123 template <
typename Space>
125 using IsOpenMP = std::enable_if_t<std::is_same_v<Space, Kokkos::OpenMP>,
bool>;
126 #endif // KOKKOS_ENABLE_OPENMP
128 #if defined(KOKKOS_ENABLE_HIP)
129 template <
typename Space>
131 using IsHIP = std::enable_if_t<std::is_same_v<Space, Kokkos::HIP>,
bool>;
132 #endif // KOKKOS_ENABLE_HIP
134 #if defined(KOKKOS_ENABLE_SYCL)
135 template <
typename Space>
137 using IsSYCL = std::enable_if_t<std::is_same_v<Space, Kokkos::Experimental::SYCL>,
bool>;
138 #endif // KOKKOS_ENABLE_SYCL
145 template <
typename ExecSpace, Priority priority = Priority::medium
146 #if defined(KOKKOS_ENABLE_CUDA)
148 NotCuda<ExecSpace> =
true
149 #endif // KOKKOS_ENABLE_CUDA
151 ExecSpace make_instance() {
161 #if defined(KOKKOS_ENABLE_CUDA)
162 template <
typename ExecSpace, Priority priority = Priority::medium,
163 IsCuda<ExecSpace> =
true>
164 Kokkos::Cuda make_instance() {
170 prio = cudaInfo.highPrio_;
172 case Priority::medium:
173 prio = cudaInfo.mediumPrio_;
176 prio = cudaInfo.lowPrio_;
179 throw std::runtime_error(
"unexpected static Tpetra Space priority");
181 TPETRA_DETAILS_SPACES_CUDA_RUNTIME(
182 cudaStreamCreateWithPriority(&stream, cudaStreamNonBlocking, prio));
183 return Kokkos::Cuda(stream,
true );
185 #endif // KOKKOS_ENABLE_CUDA
192 template <
typename ExecSpace> ExecSpace make_instance(
const Priority &prio) {
195 return make_instance<ExecSpace, Priority::high>();
196 case Priority::medium:
197 return make_instance<ExecSpace, Priority::medium>();
199 return make_instance<ExecSpace, Priority::low>();
201 throw std::runtime_error(
"unexpected dynamic Tpetra Space priority");
218 using execution_space = ExecSpace;
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());
261 if (instances[p][i].is_null() || !instances[p][i].is_valid_ptr()) {
264 rcp_type r = Teuchos::RCP<const execution_space>(
265 new ExecSpace(make_instance<ExecSpace, priority>()));
268 instances[p][i] = r.create_weak();
274 auto r = instances[p][i].create_strong();
282 for (
int i = 0; i < static_cast<int>(Spaces::Priority::NUM_LEVELS); ++i) {
283 for (
const rcp_type &rcp : instances[i]) {
284 if (rcp.is_valid_ptr() && !rcp.is_null()) {
286 std::cerr << __FILE__ <<
":" << __LINE__
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)
304 extern InstanceLifetimeManager<Kokkos::Cuda> cudaSpaces;
306 #if defined(KOKKOS_ENABLE_SERIAL)
307 extern InstanceLifetimeManager<Kokkos::Serial> serialSpaces;
309 #if defined(KOKKOS_ENABLE_OPENMP)
310 extern InstanceLifetimeManager<Kokkos::OpenMP> openMPSpaces;
312 #if defined(KOKKOS_ENABLE_HIP)
313 extern InstanceLifetimeManager<Kokkos::HIP> HIPSpaces;
315 #if defined(KOKKOS_ENABLE_SYCL)
316 extern InstanceLifetimeManager<Kokkos::Experimental::SYCL> SYCLSpaces;
319 #if defined(KOKKOS_ENABLE_CUDA)
324 template <
typename ExecSpace, Priority priority = Priority::medium,
325 IsCuda<ExecSpace> =
true>
326 Teuchos::RCP<const ExecSpace> space_instance(
int i = 0) {
327 return cudaSpaces.space_instance<priority>(i);
331 #if defined(KOKKOS_ENABLE_SERIAL)
335 template <
typename ExecSpace, Priority priority = Priority::medium,
336 IsSerial<ExecSpace> =
true>
337 Teuchos::RCP<const ExecSpace> space_instance(
int i = 0) {
338 return serialSpaces.space_instance<priority>(i);
342 #if defined(KOKKOS_ENABLE_OPENMP)
346 template <
typename ExecSpace, Priority priority = Priority::medium,
347 IsOpenMP<ExecSpace> =
true>
348 Teuchos::RCP<const ExecSpace> space_instance(
int i = 0) {
349 return openMPSpaces.space_instance<priority>(i);
353 #if defined(KOKKOS_ENABLE_HIP)
356 template <
typename ExecSpace, Priority priority = Priority::medium,
357 IsHIP<ExecSpace> =
true>
358 Teuchos::RCP<const ExecSpace> space_instance(
int i = 0) {
359 return HIPSpaces.space_instance<priority>(i);
362 #if defined(KOKKOS_ENABLE_SYCL)
366 template <
typename ExecSpace, Priority priority = Priority::medium,
367 IsSYCL<ExecSpace> =
true>
368 Teuchos::RCP<const ExecSpace> space_instance(
int i = 0) {
369 return SYCLSpaces.space_instance<priority>(i);
378 template <
typename ExecSpace>
379 Teuchos::RCP<const ExecSpace> space_instance(
const Priority &priority,
383 return space_instance<ExecSpace, Priority::high>(i);
384 case Priority::medium:
385 return space_instance<ExecSpace, Priority::medium>(i);
387 return space_instance<ExecSpace, Priority::low>(i);
389 throw std::runtime_error(
390 "unexpected dynamic Tpetra Space priority in space_instance");
407 template <
typename S1,
typename S2
408 #if defined(KOKKOS_ENABLE_CUDA)
410 NotBothCuda<S1, S2> =
true
413 void exec_space_wait(
const char *msg,
const S1 &waitee,
const S2 & ) {
415 "Tpetra::Details::Spaces::exec_space_wait");
420 #if defined(KOKKOS_ENABLE_CUDA)
421 template <
typename S1,
typename S2, BothCuda<S1, S2> = true>
422 void exec_space_wait(
const char *msg,
const S1 &waitee,
const S2 &waiter) {
424 "Tpetra::Details::Spaces::exec_space_wait");
428 if (waitee.impl_instance_id() !=
430 .impl_instance_id()) {
435 TPETRA_DETAILS_SPACES_CUDA_RUNTIME(
436 cudaEventRecord(cudaInfo.execSpaceWaitEvent_, waitee.cuda_stream()));
437 TPETRA_DETAILS_SPACES_CUDA_RUNTIME(cudaStreamWaitEvent(
438 waiter.cuda_stream(), cudaInfo.execSpaceWaitEvent_, 0 ));
443 template <
typename S1,
typename S2>
444 void exec_space_wait(
const S1 &waitee,
const S2 &waiter) {
446 "Tpetra::Details::Spaces::exec_space_wait");
448 exec_space_wait(
"anonymous", waitee, waiter);
451 template <
typename ExecutionSpace>
452 constexpr KOKKOS_INLINE_FUNCTION
bool is_gpu_exec_space() {
456 #if defined(KOKKOS_ENABLE_CUDA)
458 constexpr KOKKOS_INLINE_FUNCTION
bool is_gpu_exec_space<Kokkos::Cuda>() {
463 #if defined(KOKKOS_ENABLE_HIP)
465 constexpr KOKKOS_INLINE_FUNCTION
bool
466 is_gpu_exec_space<Kokkos::HIP>() {
471 #if defined(KOKKOS_ENABLE_SYCL)
473 constexpr KOKKOS_INLINE_FUNCTION
bool
474 is_gpu_exec_space<Kokkos::Experimental::SYCL>() {
483 #undef TPETRA_DETAILS_SPACES_THROW
485 #endif // TPETRA_DETAILS_EXECUTIONSPACES_HPP
Declaration of Tpetra::Details::Profiling, a scope guard for Kokkos Profiling.
static size_t spacesIdWarnLimit()
Warn if more than this many Kokkos spaces are accessed.
rcp_type space_instance(int i=0)
Retrieve a strong Teuchos::RCP<const ExecSpace> to instance i
~InstanceLifetimeManager()
Issue a warning if any Tpetra-managed execution space instances survive to the end of static lifetime...
Provides reusable Kokkos execution space instances.
Declaration of Tpetra::Details::Behavior, a class that describes Tpetra's behavior.