|
| 1 | +/*M/////////////////////////////////////////////////////////////////////////////////////// |
| 2 | +// |
| 3 | +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. |
| 4 | +// |
| 5 | +// By downloading, copying, installing or using the software you agree to this license. |
| 6 | +// If you do not agree to this license, do not download, install, |
| 7 | +// copy or use the software. |
| 8 | +// |
| 9 | +// |
| 10 | +// License Agreement |
| 11 | +// For Open Source Computer Vision Library |
| 12 | +// |
| 13 | +// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. |
| 14 | +// Copyright (C) 2009, Willow Garage Inc., all rights reserved. |
| 15 | +// Third party copyrights are property of their respective owners. |
| 16 | +// |
| 17 | +// Redistribution and use in source and binary forms, with or without modification, |
| 18 | +// are permitted provided that the following conditions are met: |
| 19 | +// |
| 20 | +// * Redistribution's of source code must retain the above copyright notice, |
| 21 | +// this list of conditions and the following disclaimer. |
| 22 | +// |
| 23 | +// * Redistribution's in binary form must reproduce the above copyright notice, |
| 24 | +// this list of conditions and the following disclaimer in the documentation |
| 25 | +// and/or other materials provided with the distribution. |
| 26 | +// |
| 27 | +// * The name of the copyright holders may not be used to endorse or promote products |
| 28 | +// derived from this software without specific prior written permission. |
| 29 | +// |
| 30 | +// This software is provided by the copyright holders and contributors "as is" and |
| 31 | +// any express or implied warranties, including, but not limited to, the implied |
| 32 | +// warranties of merchantability and fitness for a particular purpose are disclaimed. |
| 33 | +// In no event shall the Intel Corporation or contributors be liable for any direct, |
| 34 | +// indirect, incidental, special, exemplary, or consequential damages |
| 35 | +// (including, but not limited to, procurement of substitute goods or services; |
| 36 | +// loss of use, data, or profits; or business interruption) however caused |
| 37 | +// and on any theory of liability, whether in contract, strict liability, |
| 38 | +// or tort (including negligence or otherwise) arising in any way out of |
| 39 | +// the use of this software, even if advised of the possibility of such damage. |
| 40 | +// |
| 41 | +//M*/ |
| 42 | + |
| 43 | +#include "test_precomp.hpp" |
| 44 | + |
| 45 | +#ifdef HAVE_CUDA |
| 46 | + |
| 47 | +#include <cuda_runtime.h> |
| 48 | + |
| 49 | +#include "opencv2/core/cuda.hpp" |
| 50 | +#include "opencv2/core/cuda_stream_accessor.hpp" |
| 51 | +#include "opencv2/ts/cuda_test.hpp" |
| 52 | + |
| 53 | +namespace opencv_test { namespace { |
| 54 | + |
| 55 | +struct AsyncEvent : testing::TestWithParam<cv::cuda::DeviceInfo> |
| 56 | +{ |
| 57 | + cv::cuda::HostMem src; |
| 58 | + cv::cuda::GpuMat d_src; |
| 59 | + |
| 60 | + cv::cuda::HostMem dst; |
| 61 | + cv::cuda::GpuMat d_dst; |
| 62 | + |
| 63 | + cv::cuda::Stream stream; |
| 64 | + |
| 65 | + virtual void SetUp() |
| 66 | + { |
| 67 | + cv::cuda::DeviceInfo devInfo = GetParam(); |
| 68 | + cv::cuda::setDevice(devInfo.deviceID()); |
| 69 | + |
| 70 | + src = cv::cuda::HostMem(cv::cuda::HostMem::PAGE_LOCKED); |
| 71 | + |
| 72 | + cv::Mat m = randomMat(cv::Size(128, 128), CV_8UC1); |
| 73 | + m.copyTo(src); |
| 74 | + } |
| 75 | +}; |
| 76 | + |
| 77 | +void deviceWork(void* userData) |
| 78 | +{ |
| 79 | + AsyncEvent* test = reinterpret_cast<AsyncEvent*>(userData); |
| 80 | + test->d_src.upload(test->src, test->stream); |
| 81 | + test->d_src.convertTo(test->d_dst, CV_32S, test->stream); |
| 82 | + test->d_dst.download(test->dst, test->stream); |
| 83 | +} |
| 84 | + |
| 85 | +CUDA_TEST_P(AsyncEvent, WrapEvent) |
| 86 | +{ |
| 87 | + cudaEvent_t cuda_event = NULL; |
| 88 | + ASSERT_EQ(cudaSuccess, cudaEventCreate(&cuda_event)); |
| 89 | + { |
| 90 | + cv::cuda::Event cudaEvent = cv::cuda::EventAccessor::wrapEvent(cuda_event); |
| 91 | + deviceWork(this); |
| 92 | + cudaEvent.record(stream); |
| 93 | + cudaEvent.waitForCompletion(); |
| 94 | + cv::Mat dst_gold; |
| 95 | + src.createMatHeader().convertTo(dst_gold, CV_32S); |
| 96 | + ASSERT_MAT_NEAR(dst_gold, dst, 0); |
| 97 | + } |
| 98 | + ASSERT_EQ(cudaSuccess, cudaEventDestroy(cuda_event)); |
| 99 | +} |
| 100 | + |
| 101 | +CUDA_TEST_P(AsyncEvent, WithFlags) |
| 102 | +{ |
| 103 | + cv::cuda::Event cudaEvent = cv::cuda::Event(cv::cuda::Event::CreateFlags::BLOCKING_SYNC | Event::CreateFlags::DISABLE_TIMING); |
| 104 | + deviceWork(this); |
| 105 | + cudaEvent.record(stream); |
| 106 | + cudaEvent.waitForCompletion(); |
| 107 | + cv::Mat dst_gold; |
| 108 | + src.createMatHeader().convertTo(dst_gold, CV_32S); |
| 109 | + ASSERT_MAT_NEAR(dst_gold, dst, 0); |
| 110 | +} |
| 111 | + |
| 112 | +CUDA_TEST_P(AsyncEvent, Timing) |
| 113 | +{ |
| 114 | + const std::vector<unsigned> eventFlags = { cv::cuda::Event::CreateFlags::BLOCKING_SYNC , cv::cuda::Event::CreateFlags::BLOCKING_SYNC | Event::CreateFlags::DISABLE_TIMING }; |
| 115 | + const std::vector<bool> shouldFail = { false, true }; |
| 116 | + for (int i = 0; i < eventFlags.size(); i++) { |
| 117 | + const auto& flags = eventFlags.at(i); |
| 118 | + cv::cuda::Event startEvent = cv::cuda::Event(flags); |
| 119 | + cv::cuda::Event stopEvent = cv::cuda::Event(flags); |
| 120 | + startEvent.record(stream); |
| 121 | + deviceWork(this); |
| 122 | + stopEvent.record(stream); |
| 123 | + stopEvent.waitForCompletion(); |
| 124 | + cv::Mat dst_gold; |
| 125 | + src.createMatHeader().convertTo(dst_gold, CV_32S); |
| 126 | + ASSERT_MAT_NEAR(dst_gold, dst, 0); |
| 127 | + double elTimeMs = -1; |
| 128 | + bool failed = false; |
| 129 | + try { |
| 130 | + const double elTimeMs = Event::elapsedTime(startEvent, stopEvent); |
| 131 | + ASSERT_GT(elTimeMs, 0); |
| 132 | + } |
| 133 | + catch (cv::Exception ex) { |
| 134 | + failed = true; |
| 135 | + } |
| 136 | + ASSERT_EQ(failed, shouldFail.at(i)); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +INSTANTIATE_TEST_CASE_P(CUDA_Event, AsyncEvent, ALL_DEVICES); |
| 141 | + |
| 142 | +}} // namespace |
| 143 | +#endif // HAVE_CUDA |
0 commit comments