cuda_stream_view.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2020-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #pragma once
7 
8 #include <rmm/detail/export.hpp>
9 
10 #include <cuda/stream>
11 #include <cuda_runtime_api.h>
12 
13 #include <concepts>
14 #include <cstddef>
15 #include <ostream>
16 
17 RMM_NAMESPACE_BEGIN
31 class [[deprecated("Use cuda::stream_ref instead.")]] cuda_stream_view {
32  public:
33  cuda_stream_view() = default;
34  ~cuda_stream_view() = default;
35  cuda_stream_view(cuda_stream_view const&) = default;
38  default;
40  default;
41 
42  // Disable construction from literal 0
43  cuda_stream_view(int) = delete; //< Prevent cast from 0
44  cuda_stream_view(std::nullptr_t) = delete; //< Prevent cast from nullptr
45 
51  cuda_stream_view(cudaStream_t stream) noexcept;
52 
58  cuda_stream_view(cuda::stream_ref stream) noexcept;
59 
65  [[nodiscard]] cudaStream_t value() const noexcept;
66 
72  [[nodiscard]] cudaStream_t get() const noexcept;
73 
79  operator cudaStream_t() const noexcept;
80 
86  operator cuda::stream_ref() const noexcept;
87 
91  [[nodiscard]] bool is_per_thread_default() const noexcept;
92 
96  [[nodiscard]] bool is_default() const noexcept;
97 
105  void synchronize() const;
106 
114  void sync() const;
115 
121  void synchronize_no_throw() const noexcept;
122 
123  private:
124  cudaStream_t stream_{};
125 };
126 
132 [[deprecated("Use cuda::stream_ref{cudaStream_t{cudaStreamDefault}} instead.")]]
133 static constexpr cuda::stream_ref cuda_stream_default{cudaStream_t{nullptr}};
134 
140 [[deprecated("Use cuda::stream_ref{cudaStreamLegacy} instead.")]]
141 static const cuda::stream_ref cuda_stream_legacy{cudaStream_t{cudaStreamLegacy}};
142 
148 [[deprecated("Use cuda::stream_ref{cudaStreamPerThread} instead.")]]
149 static const cuda::stream_ref cuda_stream_per_thread{cudaStream_t{cudaStreamPerThread}};
150 
151 #if defined(__GNUC__) || defined(__clang__)
152 #pragma GCC diagnostic push
153 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
154 #endif
155 
164 
166 template <std::same_as<cuda::stream_ref> StreamRef>
167 bool operator==(cuda_stream_view lhs, StreamRef const& rhs)
168 {
169  return lhs.value() == rhs.get();
170 }
171 
173 template <std::same_as<cuda::stream_ref> StreamRef>
174 bool operator==(StreamRef const& lhs, cuda_stream_view rhs)
175 {
176  return lhs.get() == rhs.value();
177 }
178 
187 
189 template <std::same_as<cuda::stream_ref> StreamRef>
190 bool operator!=(cuda_stream_view lhs, StreamRef const& rhs)
191 {
192  return lhs.value() != rhs.get();
193 }
194 
196 template <std::same_as<cuda::stream_ref> StreamRef>
197 bool operator!=(StreamRef const& lhs, cuda_stream_view rhs)
198 {
199  return lhs.get() != rhs.value();
200 }
201 
209 std::ostream& operator<<(std::ostream& os, cuda_stream_view stream);
210 
211 #if defined(__GNUC__) || defined(__clang__)
212 #pragma GCC diagnostic pop
213 #endif
214  // end of group
216 RMM_NAMESPACE_END
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:31
cuda_stream_view(cuda_stream_view &&)=default
Default move constructor.
cuda_stream_view & operator=(cuda_stream_view &&)=default
Default move assignment operator.
cuda_stream_view & operator=(cuda_stream_view const &)=default
Default copy assignment operator.
cuda_stream_view(cuda_stream_view const &)=default
Default copy constructor.
cuda_stream_view(cudaStream_t stream) noexcept
Constructor from a cudaStream_t.
cudaStream_t value() const noexcept
Get the wrapped stream.
cuda_stream_view(cuda::stream_ref stream) noexcept
Implicit conversion from stream_ref.
static const cuda::stream_ref cuda_stream_legacy
Static cuda::stream_ref of cudaStreamLegacy, for convenience.
Definition: cuda_stream_view.hpp:141
static const cuda::stream_ref cuda_stream_per_thread
Static cuda::stream_ref of cudaStreamPerThread, for convenience.
Definition: cuda_stream_view.hpp:149
bool operator!=(StreamRef const &lhs, cuda_stream_view rhs)
Inequality comparison operator for streams.
Definition: cuda_stream_view.hpp:197
std::ostream & operator<<(std::ostream &os, cuda_stream_view stream)
Output stream operator for printing / logging streams.
static constexpr cuda::stream_ref cuda_stream_default
Static cuda::stream_ref of the default stream (stream 0), for convenience.
Definition: cuda_stream_view.hpp:133
bool operator==(StreamRef const &lhs, cuda_stream_view rhs)
Equality comparison operator for streams.
Definition: cuda_stream_view.hpp:174