test_stream.cpp 4.5 KB
Newer Older
wester committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#include "test_precomp.hpp"

#ifdef HAVE_CUDA

wester committed
47
#include "opencv2/gpu/stream_accessor.hpp"
wester committed
48 49 50

using namespace cvtest;

wester committed
51 52 53
#if CUDART_VERSION >= 5000

struct Async : testing::TestWithParam<cv::gpu::DeviceInfo>
wester committed
54
{
wester committed
55 56
    cv::gpu::CudaMem src;
    cv::gpu::GpuMat d_src;
wester committed
57

wester committed
58 59
    cv::gpu::CudaMem dst;
    cv::gpu::GpuMat d_dst;
wester committed
60 61 62

    virtual void SetUp()
    {
wester committed
63 64
        cv::gpu::DeviceInfo devInfo = GetParam();
        cv::gpu::setDevice(devInfo.deviceID());
wester committed
65 66

        cv::Mat m = randomMat(cv::Size(128, 128), CV_8UC1);
wester committed
67 68
        src.create(m.size(), m.type(), cv::gpu::CudaMem::ALLOC_PAGE_LOCKED);
        m.copyTo(src.createMatHeader());
wester committed
69 70 71
    }
};

wester committed
72
void checkMemSet(cv::gpu::Stream&, int status, void* userData)
wester committed
73 74 75 76 77
{
    ASSERT_EQ(cudaSuccess, status);

    Async* test = reinterpret_cast<Async*>(userData);

wester committed
78 79
    cv::Mat src = test->src;
    cv::Mat dst = test->dst;
wester committed
80 81 82 83 84 85

    cv::Mat dst_gold = cv::Mat::zeros(src.size(), src.type());

    ASSERT_MAT_NEAR(dst_gold, dst, 0);
}

wester committed
86
GPU_TEST_P(Async, MemSet)
wester committed
87
{
wester committed
88
    cv::gpu::Stream stream;
wester committed
89 90 91

    d_dst.upload(src);

wester committed
92 93
    stream.enqueueMemSet(d_dst, cv::Scalar::all(0));
    stream.enqueueDownload(d_dst, dst);
wester committed
94 95 96 97 98 99 100

    Async* test = this;
    stream.enqueueHostCallback(checkMemSet, test);

    stream.waitForCompletion();
}

wester committed
101
void checkConvert(cv::gpu::Stream&, int status, void* userData)
wester committed
102 103 104 105 106
{
    ASSERT_EQ(cudaSuccess, status);

    Async* test = reinterpret_cast<Async*>(userData);

wester committed
107 108
    cv::Mat src = test->src;
    cv::Mat dst = test->dst;
wester committed
109 110

    cv::Mat dst_gold;
wester committed
111
    src.convertTo(dst_gold, CV_32S);
wester committed
112 113 114 115

    ASSERT_MAT_NEAR(dst_gold, dst, 0);
}

wester committed
116
GPU_TEST_P(Async, Convert)
wester committed
117
{
wester committed
118
    cv::gpu::Stream stream;
wester committed
119

wester committed
120 121 122
    stream.enqueueUpload(src, d_src);
    stream.enqueueConvert(d_src, d_dst, CV_32S);
    stream.enqueueDownload(d_dst, dst);
wester committed
123 124 125 126 127 128 129

    Async* test = this;
    stream.enqueueHostCallback(checkConvert, test);

    stream.waitForCompletion();
}

wester committed
130
GPU_TEST_P(Async, WrapStream)
wester committed
131 132 133 134
{
    cudaStream_t cuda_stream = NULL;
    ASSERT_EQ(cudaSuccess, cudaStreamCreate(&cuda_stream));

wester committed
135
    cv::gpu::Stream stream = cv::gpu::StreamAccessor::wrapStream(cuda_stream);
wester committed
136

wester committed
137 138 139
    stream.enqueueUpload(src, d_src);
    stream.enqueueConvert(d_src, d_dst, CV_32S);
    stream.enqueueDownload(d_dst, dst);
wester committed
140

wester committed
141 142
    Async* test = this;
    stream.enqueueHostCallback(checkConvert, test);
wester committed
143

wester committed
144
    stream.waitForCompletion();
wester committed
145 146 147 148

    ASSERT_EQ(cudaSuccess, cudaStreamDestroy(cuda_stream));
}

wester committed
149
INSTANTIATE_TEST_CASE_P(GPU_Stream, Async, ALL_DEVICES);
wester committed
150

wester committed
151
#endif
wester committed
152 153

#endif // HAVE_CUDA