1
/*M///////////////////////////////////////////////////////////////////////////////////////
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11
// For Open Source Computer Vision Library
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.
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
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.
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.
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.
43
#include "test_precomp.hpp"
47
#include <cuda_runtime.h>
49
#include "opencv2/core/cuda.hpp"
50
#include "opencv2/core/cuda_stream_accessor.hpp"
51
#include "opencv2/ts/cuda_test.hpp"
53
using namespace cvtest;
55
struct Async : testing::TestWithParam<cv::cuda::DeviceInfo>
57
cv::cuda::HostMem src;
58
cv::cuda::GpuMat d_src;
60
cv::cuda::HostMem dst;
61
cv::cuda::GpuMat d_dst;
65
cv::cuda::DeviceInfo devInfo = GetParam();
66
cv::cuda::setDevice(devInfo.deviceID());
68
src = cv::cuda::HostMem(cv::cuda::HostMem::PAGE_LOCKED);
70
cv::Mat m = randomMat(cv::Size(128, 128), CV_8UC1);
75
void checkMemSet(int status, void* userData)
77
ASSERT_EQ(cudaSuccess, status);
79
Async* test = reinterpret_cast<Async*>(userData);
81
cv::cuda::HostMem src = test->src;
82
cv::cuda::HostMem dst = test->dst;
84
cv::Mat dst_gold = cv::Mat::zeros(src.size(), src.type());
86
ASSERT_MAT_NEAR(dst_gold, dst, 0);
89
CUDA_TEST_P(Async, MemSet)
91
cv::cuda::Stream stream;
95
d_dst.setTo(cv::Scalar::all(0), stream);
96
d_dst.download(dst, stream);
99
stream.enqueueHostCallback(checkMemSet, test);
101
stream.waitForCompletion();
104
void checkConvert(int status, void* userData)
106
ASSERT_EQ(cudaSuccess, status);
108
Async* test = reinterpret_cast<Async*>(userData);
110
cv::cuda::HostMem src = test->src;
111
cv::cuda::HostMem dst = test->dst;
114
src.createMatHeader().convertTo(dst_gold, CV_32S);
116
ASSERT_MAT_NEAR(dst_gold, dst, 0);
119
CUDA_TEST_P(Async, Convert)
121
cv::cuda::Stream stream;
123
d_src.upload(src, stream);
124
d_src.convertTo(d_dst, CV_32S, stream);
125
d_dst.download(dst, stream);
128
stream.enqueueHostCallback(checkConvert, test);
130
stream.waitForCompletion();
133
CUDA_TEST_P(Async, WrapStream)
135
cudaStream_t cuda_stream = NULL;
136
ASSERT_EQ(cudaSuccess, cudaStreamCreate(&cuda_stream));
139
cv::cuda::Stream stream = cv::cuda::StreamAccessor::wrapStream(cuda_stream);
141
d_src.upload(src, stream);
142
d_src.convertTo(d_dst, CV_32S, stream);
143
d_dst.download(dst, stream);
146
stream.enqueueHostCallback(checkConvert, test);
148
stream.waitForCompletion();
151
ASSERT_EQ(cudaSuccess, cudaStreamDestroy(cuda_stream));
154
CUDA_TEST_P(Async, HostMemAllocator)
156
cv::cuda::Stream stream;
159
h_dst.allocator = cv::cuda::HostMem::getAllocator();
161
d_src.upload(src, stream);
162
d_src.convertTo(d_dst, CV_32S, stream);
163
d_dst.download(h_dst, stream);
165
stream.waitForCompletion();
168
src.createMatHeader().convertTo(dst_gold, CV_32S);
170
ASSERT_MAT_NEAR(dst_gold, h_dst, 0);
173
INSTANTIATE_TEST_CASE_P(CUDA_Stream, Async, ALL_DEVICES);