cuda_stream.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-2025, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <rmm/cuda_stream_view.hpp>
20 #include <rmm/detail/export.hpp>
21 
22 #include <cuda_runtime_api.h>
23 
24 #include <functional>
25 #include <memory>
26 
27 namespace RMM_EXPORT rmm {
39 class cuda_stream {
40  public:
44  enum class flags : unsigned int {
45  sync_default = cudaStreamDefault,
46  non_blocking =
47  cudaStreamNonBlocking,
48  };
55  cuda_stream(cuda_stream&&) = default;
65  ~cuda_stream() = default;
66  cuda_stream(cuda_stream const&) = delete; // Copying disallowed: one stream one owner
67  cuda_stream& operator=(cuda_stream&) = delete;
68 
76  cuda_stream(cuda_stream::flags flags = cuda_stream::flags::sync_default);
77 
84  [[nodiscard]] bool is_valid() const;
85 
91  [[nodiscard]] cudaStream_t value() const;
92 
96  explicit operator cudaStream_t() const noexcept;
97 
103  [[nodiscard]] cuda_stream_view view() const;
104 
110  operator cuda_stream_view() const;
111 
119  void synchronize() const;
120 
126  void synchronize_no_throw() const noexcept;
127 
128  private:
129  std::unique_ptr<cudaStream_t, std::function<void(cudaStream_t*)>> stream_;
130 };
131  // end of group
133 } // namespace RMM_EXPORT rmm
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:39
Owning wrapper for a CUDA stream.
Definition: cuda_stream.hpp:39
bool is_valid() const
Returns true if the owned stream is non-null.
cuda_stream & operator=(cuda_stream &&)=default
Move copy assignment operator (default)
cudaStream_t value() const
Get the value of the wrapped CUDA stream.
flags
stream creation flags.
Definition: cuda_stream.hpp:44
cuda_stream(cuda_stream &&)=default
Move constructor (default)
cuda_stream(cuda_stream::flags flags=cuda_stream::flags::sync_default)
Construct a new cuda stream object.