|
| 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. |
| 4 | + |
| 5 | +#include "test_precomp.hpp" |
| 6 | + |
| 7 | +#ifdef HAVE_CUDA |
| 8 | + |
| 9 | +#include <cuda_runtime.h> |
| 10 | + |
| 11 | +#include "opencv2/core/cuda.hpp" |
| 12 | +#include "opencv2/core/cuda_stream_accessor.hpp" |
| 13 | +#include "opencv2/ts/cuda_test.hpp" |
| 14 | + |
| 15 | +namespace opencv_test { namespace { |
| 16 | + |
| 17 | +struct AsyncEvent : testing::TestWithParam<cv::cuda::DeviceInfo> |
| 18 | +{ |
| 19 | + cv::cuda::HostMem src; |
| 20 | + cv::cuda::GpuMat d_src; |
| 21 | + |
| 22 | + cv::cuda::HostMem dst; |
| 23 | + cv::cuda::GpuMat d_dst; |
| 24 | + |
| 25 | + cv::cuda::Stream stream; |
| 26 | + |
| 27 | + virtual void SetUp() |
| 28 | + { |
| 29 | + cv::cuda::DeviceInfo devInfo = GetParam(); |
| 30 | + cv::cuda::setDevice(devInfo.deviceID()); |
| 31 | + |
| 32 | + src = cv::cuda::HostMem(cv::cuda::HostMem::PAGE_LOCKED); |
| 33 | + |
| 34 | + cv::Mat m = randomMat(cv::Size(128, 128), CV_8UC1); |
| 35 | + m.copyTo(src); |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +void deviceWork(void* userData) |
| 40 | +{ |
| 41 | + AsyncEvent* test = reinterpret_cast<AsyncEvent*>(userData); |
| 42 | + test->d_src.upload(test->src, test->stream); |
| 43 | + test->d_src.convertTo(test->d_dst, CV_32S, test->stream); |
| 44 | + test->d_dst.download(test->dst, test->stream); |
| 45 | +} |
| 46 | + |
| 47 | +CUDA_TEST_P(AsyncEvent, WrapEvent) |
| 48 | +{ |
| 49 | + cudaEvent_t cuda_event = NULL; |
| 50 | + ASSERT_EQ(cudaSuccess, cudaEventCreate(&cuda_event)); |
| 51 | + { |
| 52 | + cv::cuda::Event cudaEvent = cv::cuda::EventAccessor::wrapEvent(cuda_event); |
| 53 | + deviceWork(this); |
| 54 | + cudaEvent.record(stream); |
| 55 | + cudaEvent.waitForCompletion(); |
| 56 | + cv::Mat dst_gold; |
| 57 | + src.createMatHeader().convertTo(dst_gold, CV_32S); |
| 58 | + ASSERT_MAT_NEAR(dst_gold, dst, 0); |
| 59 | + } |
| 60 | + ASSERT_EQ(cudaSuccess, cudaEventDestroy(cuda_event)); |
| 61 | +} |
| 62 | + |
| 63 | +CUDA_TEST_P(AsyncEvent, WithFlags) |
| 64 | +{ |
| 65 | + cv::cuda::Event cudaEvent = cv::cuda::Event(cv::cuda::Event::CreateFlags::BLOCKING_SYNC); |
| 66 | + deviceWork(this); |
| 67 | + cudaEvent.record(stream); |
| 68 | + cudaEvent.waitForCompletion(); |
| 69 | + cv::Mat dst_gold; |
| 70 | + src.createMatHeader().convertTo(dst_gold, CV_32S); |
| 71 | + ASSERT_MAT_NEAR(dst_gold, dst, 0); |
| 72 | +} |
| 73 | + |
| 74 | +CUDA_TEST_P(AsyncEvent, Timing) |
| 75 | +{ |
| 76 | + const std::vector<cv::cuda::Event::CreateFlags> eventFlags = { cv::cuda::Event::CreateFlags::BLOCKING_SYNC , cv::cuda::Event::CreateFlags::BLOCKING_SYNC | Event::CreateFlags::DISABLE_TIMING }; |
| 77 | + const std::vector<bool> shouldFail = { false, true }; |
| 78 | + for (size_t i = 0; i < eventFlags.size(); i++) { |
| 79 | + const auto& flags = eventFlags.at(i); |
| 80 | + cv::cuda::Event startEvent = cv::cuda::Event(flags); |
| 81 | + cv::cuda::Event stopEvent = cv::cuda::Event(flags); |
| 82 | + startEvent.record(stream); |
| 83 | + deviceWork(this); |
| 84 | + stopEvent.record(stream); |
| 85 | + stopEvent.waitForCompletion(); |
| 86 | + cv::Mat dst_gold; |
| 87 | + src.createMatHeader().convertTo(dst_gold, CV_32S); |
| 88 | + ASSERT_MAT_NEAR(dst_gold, dst, 0); |
| 89 | + bool failed = false; |
| 90 | + try { |
| 91 | + const double elTimeMs = Event::elapsedTime(startEvent, stopEvent); |
| 92 | + ASSERT_GT(elTimeMs, 0); |
| 93 | + } |
| 94 | + catch (cv::Exception ex) { |
| 95 | + failed = true; |
| 96 | + } |
| 97 | + ASSERT_EQ(failed, shouldFail.at(i)); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +INSTANTIATE_TEST_CASE_P(CUDA_Event, AsyncEvent, ALL_DEVICES); |
| 102 | + |
| 103 | +}} // namespace |
| 104 | +#endif // HAVE_CUDA |
0 commit comments