cuda_stream.hpp
1 /*
2  * Copyright (c) 2020, 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/error.hpp>
21 
22 #include <cuda_runtime_api.h>
23 
24 #include <memory>
25 
26 namespace rmm {
27 
33 class cuda_stream {
34  public:
41  cuda_stream(cuda_stream&&) = default;
49  ~cuda_stream() = default;
50  cuda_stream(cuda_stream const&) = delete; // Copying disallowed: one stream one owner
51  cuda_stream& operator=(cuda_stream&) = delete;
52 
59  : stream_{[]() {
60  cudaStream_t* s = new cudaStream_t;
61  RMM_CUDA_TRY(cudaStreamCreate(s));
62  return s;
63  }(),
64  [](cudaStream_t* s) {
65  RMM_ASSERT_CUDA_SUCCESS(cudaStreamDestroy(*s));
66  delete s;
67  }}
68  {
69  }
70 
77  bool is_valid() const { return stream_ != nullptr; }
78 
84  cudaStream_t value() const
85  {
86  RMM_LOGGING_ASSERT(is_valid());
87  return *stream_;
88  }
89 
93  explicit operator cudaStream_t() const noexcept { return value(); }
94 
101 
107  operator cuda_stream_view() const { return view(); }
108 
116  void synchronize() const { RMM_CUDA_TRY(cudaStreamSynchronize(value())); }
117 
123  void synchronize_no_throw() const noexcept
124  {
125  RMM_ASSERT_CUDA_SUCCESS(cudaStreamSynchronize(value()));
126  }
127 
128  private:
129  std::unique_ptr<cudaStream_t, std::function<void(cudaStream_t*)>> stream_;
130 };
131 
132 } // namespace rmm
rmm::cuda_stream::synchronize
void synchronize() const
Synchronize the owned CUDA stream.
Definition: cuda_stream.hpp:116
rmm::cuda_stream_view
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:34
rmm::cuda_stream::is_valid
bool is_valid() const
Returns true if the owned stream is non-null.
Definition: cuda_stream.hpp:77
rmm::cuda_stream::operator=
cuda_stream & operator=(cuda_stream &&)=default
Move copy assignment operator (default)
rmm::cuda_stream::cuda_stream
cuda_stream()
Construct a new cuda stream object.
Definition: cuda_stream.hpp:58
rmm::cuda_stream::view
cuda_stream_view view() const
Creates an immutable, non-owning view of the wrapped CUDA stream.
Definition: cuda_stream.hpp:100
rmm::cuda_stream::cuda_stream
cuda_stream(cuda_stream &&)=default
Move constructor (default)
rmm::cuda_stream
Owning wrapper for a CUDA stream.
Definition: cuda_stream.hpp:33
rmm::cuda_stream::value
cudaStream_t value() const
Get the value of the wrapped CUDA stream.
Definition: cuda_stream.hpp:84
rmm::cuda_stream::synchronize_no_throw
void synchronize_no_throw() const noexcept
Synchronize the owned CUDA stream. Does not throw if there is an error.
Definition: cuda_stream.hpp:123