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_EVENT_HPP
6 #define OPENCV_DNN_SRC_CUDA4DNN_CSL_EVENT_HPP
11 #include <opencv2/core/utils/logger.hpp>
13 #include <cuda_runtime_api.h>
15 namespace cv { namespace dnn { namespace cuda4dnn { namespace csl {
17 /** @brief sharable CUDA event
19 * Event is a smart sharable wrapper for CUDA event handle which ensures that
20 * the handle is destroyed after use.
22 * @note Moving an Event object to another invalidates the former
26 Event() noexcept : event{ nullptr } { }
27 Event(const Event&) = delete;
28 Event(Event&& other) noexcept
29 : event{ other.event } {
30 other.event = nullptr;
33 /** if \p create is `true`, a new event will be created; otherwise, an empty event object is created */
34 Event(bool create, bool timing_event = false) : event{nullptr} {
36 unsigned int flags = cudaEventBlockingSync | (timing_event ? 0 : cudaEventDisableTiming);
37 CUDA4DNN_CHECK_CUDA(cudaEventCreateWithFlags(&event, flags));
44 CUDA4DNN_CHECK_CUDA(cudaEventDestroy(event));
45 } catch (const CUDAException& ex) {
46 std::ostringstream os;
47 os << "Asynchronous exception caught during CUDA event destruction.\n";
49 os << "Exception will be ignored.\n";
50 CV_LOG_WARNING(0, os.str().c_str());
54 Event& operator=(const Event&) noexcept = delete;
55 Event& operator=(Event&& other) noexcept {
57 other.event = nullptr;
61 /** mark a point in \p stream */
62 void record(const Stream& stream) {
63 CUDA4DNN_CHECK_CUDA(cudaEventRecord(event, stream.get()));
66 /** blocks the caller thread until all operations before the event finish */
67 void synchronize() const { CUDA4DNN_CHECK_CUDA(cudaEventSynchronize(event)); }
69 /** returns true if there are operations pending before the event completes */
71 auto status = cudaEventQuery(event);
72 if (status == cudaErrorNotReady)
74 CUDA4DNN_CHECK_CUDA(status);
78 cudaEvent_t get() const noexcept { return event; }
80 /** returns true if the event is valid */
81 explicit operator bool() const noexcept { return event; }
87 /** makes a stream wait on an event */
88 void StreamWaitOnEvent(const Stream& stream, const Event& event) {
89 CUDA4DNN_CHECK_CUDA(cudaStreamWaitEvent(stream.get(), event.get(), 0));
92 /** returns the time elapsed between two events in milliseconds */
93 float TimeElapsedBetweenEvents(const Event& start, const Event& end) {
95 CUDA4DNN_CHECK_CUDA(cudaEventElapsedTime(&temp, start.get(), end.get()));
99 }}}} /* namespace cv::dnn::cuda4dnn::csl */
101 #endif /* OPENCV_DNN_SRC_CUDA4DNN_CSL_EVENT_HPP */