1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
5 #ifndef OPENCV_DNN_SRC_CUDA4DNN_CSL_STREAM_HPP
6 #define OPENCV_DNN_SRC_CUDA4DNN_CSL_STREAM_HPP
10 #include <opencv2/core.hpp>
11 #include <opencv2/core/utils/logger.hpp>
13 #include <cuda_runtime_api.h>
19 namespace cv { namespace dnn { namespace cuda4dnn { namespace csl {
21 /** @brief noncopyable smart CUDA stream
23 * UniqueStream is a smart non-sharable wrapper for CUDA stream handle which ensures that
24 * the handle is destroyed after use. Unless explicitly specified by a constructor argument,
25 * the stream object represents the default stream.
29 UniqueStream() noexcept : stream{ 0 } { }
30 UniqueStream(UniqueStream&) = delete;
31 UniqueStream(UniqueStream&& other) noexcept {
32 stream = other.stream;
36 UniqueStream(bool create) : stream{ 0 } {
38 CUDA4DNN_CHECK_CUDA(cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking));
45 CUDA4DNN_CHECK_CUDA(cudaStreamDestroy(stream));
46 } catch (const CUDAException& ex) {
47 std::ostringstream os;
48 os << "Asynchronous exception caught during CUDA stream destruction.\n";
50 os << "Exception will be ignored.\n";
51 CV_LOG_WARNING(0, os.str().c_str());
55 UniqueStream& operator=(const UniqueStream&) = delete;
56 UniqueStream& operator=(UniqueStream&& other) noexcept {
57 stream = other.stream;
62 /** returns the raw CUDA stream handle */
63 cudaStream_t get() const noexcept { return stream; }
65 void synchronize() const { CUDA4DNN_CHECK_CUDA(cudaStreamSynchronize(stream)); }
67 auto status = cudaStreamQuery(stream);
68 if (status == cudaErrorNotReady)
70 CUDA4DNN_CHECK_CUDA(status);
78 /** @brief sharable smart CUDA stream
80 * Stream is a smart sharable wrapper for CUDA stream handle which ensures that
81 * the handle is destroyed after use. Unless explicitly specified by a constructor argument,
82 * the stream object represents the default stream.
84 * @note Moving a Stream object to another invalidates the former
88 Stream() : stream(std::make_shared<UniqueStream>()) { }
89 Stream(const Stream&) = default;
90 Stream(Stream&&) = default;
92 /** if \p create is `true`, a new stream will be created instead of the otherwise default stream */
93 Stream(bool create) : stream(std::make_shared<UniqueStream>(create)) { }
95 Stream& operator=(const Stream&) = default;
96 Stream& operator=(Stream&&) = default;
98 /** blocks the caller thread until all operations in the stream are complete */
99 void synchronize() const { stream->synchronize(); }
101 /** returns true if there are operations pending in the stream */
102 bool busy() const { return stream->busy(); }
104 /** returns true if the stream is valid */
105 explicit operator bool() const noexcept { return static_cast<bool>(stream); }
107 cudaStream_t get() const noexcept {
109 return stream->get();
113 std::shared_ptr<UniqueStream> stream;
116 }}}} /* namespace cv::dnn::cuda4dnn::csl */
118 #endif /* OPENCV_DNN_SRC_CUDA4DNN_CSL_STREAM_HPP */