~paparazzi-uav/paparazzi/v5.0-manual

« back to all changes in this revision

Viewing changes to sw/ext/opencv_bebop/opencv/modules/cudaarithm/test/test_stream.cpp

  • Committer: Paparazzi buildbot
  • Date: 2016-05-18 15:00:29 UTC
  • Revision ID: felix.ruess+docbot@gmail.com-20160518150029-e8lgzi5kvb4p7un9
Manual import commit 4b8bbb730080dac23cf816b98908dacfabe2a8ec from v5.0 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
using namespace cvtest;
 
54
 
 
55
struct Async : 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
    virtual void SetUp()
 
64
    {
 
65
        cv::cuda::DeviceInfo devInfo = GetParam();
 
66
        cv::cuda::setDevice(devInfo.deviceID());
 
67
 
 
68
        src = cv::cuda::HostMem(cv::cuda::HostMem::PAGE_LOCKED);
 
69
 
 
70
        cv::Mat m = randomMat(cv::Size(128, 128), CV_8UC1);
 
71
        m.copyTo(src);
 
72
    }
 
73
};
 
74
 
 
75
void checkMemSet(int status, void* userData)
 
76
{
 
77
    ASSERT_EQ(cudaSuccess, status);
 
78
 
 
79
    Async* test = reinterpret_cast<Async*>(userData);
 
80
 
 
81
    cv::cuda::HostMem src = test->src;
 
82
    cv::cuda::HostMem dst = test->dst;
 
83
 
 
84
    cv::Mat dst_gold = cv::Mat::zeros(src.size(), src.type());
 
85
 
 
86
    ASSERT_MAT_NEAR(dst_gold, dst, 0);
 
87
}
 
88
 
 
89
CUDA_TEST_P(Async, MemSet)
 
90
{
 
91
    cv::cuda::Stream stream;
 
92
 
 
93
    d_dst.upload(src);
 
94
 
 
95
    d_dst.setTo(cv::Scalar::all(0), stream);
 
96
    d_dst.download(dst, stream);
 
97
 
 
98
    Async* test = this;
 
99
    stream.enqueueHostCallback(checkMemSet, test);
 
100
 
 
101
    stream.waitForCompletion();
 
102
}
 
103
 
 
104
void checkConvert(int status, void* userData)
 
105
{
 
106
    ASSERT_EQ(cudaSuccess, status);
 
107
 
 
108
    Async* test = reinterpret_cast<Async*>(userData);
 
109
 
 
110
    cv::cuda::HostMem src = test->src;
 
111
    cv::cuda::HostMem dst = test->dst;
 
112
 
 
113
    cv::Mat dst_gold;
 
114
    src.createMatHeader().convertTo(dst_gold, CV_32S);
 
115
 
 
116
    ASSERT_MAT_NEAR(dst_gold, dst, 0);
 
117
}
 
118
 
 
119
CUDA_TEST_P(Async, Convert)
 
120
{
 
121
    cv::cuda::Stream stream;
 
122
 
 
123
    d_src.upload(src, stream);
 
124
    d_src.convertTo(d_dst, CV_32S, stream);
 
125
    d_dst.download(dst, stream);
 
126
 
 
127
    Async* test = this;
 
128
    stream.enqueueHostCallback(checkConvert, test);
 
129
 
 
130
    stream.waitForCompletion();
 
131
}
 
132
 
 
133
CUDA_TEST_P(Async, WrapStream)
 
134
{
 
135
    cudaStream_t cuda_stream = NULL;
 
136
    ASSERT_EQ(cudaSuccess, cudaStreamCreate(&cuda_stream));
 
137
 
 
138
    {
 
139
        cv::cuda::Stream stream = cv::cuda::StreamAccessor::wrapStream(cuda_stream);
 
140
 
 
141
        d_src.upload(src, stream);
 
142
        d_src.convertTo(d_dst, CV_32S, stream);
 
143
        d_dst.download(dst, stream);
 
144
 
 
145
        Async* test = this;
 
146
        stream.enqueueHostCallback(checkConvert, test);
 
147
 
 
148
        stream.waitForCompletion();
 
149
    }
 
150
 
 
151
    ASSERT_EQ(cudaSuccess, cudaStreamDestroy(cuda_stream));
 
152
}
 
153
 
 
154
CUDA_TEST_P(Async, HostMemAllocator)
 
155
{
 
156
    cv::cuda::Stream stream;
 
157
 
 
158
    cv::Mat h_dst;
 
159
    h_dst.allocator = cv::cuda::HostMem::getAllocator();
 
160
 
 
161
    d_src.upload(src, stream);
 
162
    d_src.convertTo(d_dst, CV_32S, stream);
 
163
    d_dst.download(h_dst, stream);
 
164
 
 
165
    stream.waitForCompletion();
 
166
 
 
167
    cv::Mat dst_gold;
 
168
    src.createMatHeader().convertTo(dst_gold, CV_32S);
 
169
 
 
170
    ASSERT_MAT_NEAR(dst_gold, h_dst, 0);
 
171
}
 
172
 
 
173
INSTANTIATE_TEST_CASE_P(CUDA_Stream, Async, ALL_DEVICES);
 
174
 
 
175
#endif // HAVE_CUDA