Home | History | Annotate | Download | only in test
      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/ts/cuda_test.hpp"
     51 
     52 using namespace cvtest;
     53 
     54 struct Async : testing::TestWithParam<cv::cuda::DeviceInfo>
     55 {
     56     cv::cuda::HostMem src;
     57     cv::cuda::GpuMat d_src;
     58 
     59     cv::cuda::HostMem dst;
     60     cv::cuda::GpuMat d_dst;
     61 
     62     virtual void SetUp()
     63     {
     64         cv::cuda::DeviceInfo devInfo = GetParam();
     65         cv::cuda::setDevice(devInfo.deviceID());
     66 
     67         src = cv::cuda::HostMem(cv::cuda::HostMem::PAGE_LOCKED);
     68 
     69         cv::Mat m = randomMat(cv::Size(128, 128), CV_8UC1);
     70         m.copyTo(src);
     71     }
     72 };
     73 
     74 void checkMemSet(int status, void* userData)
     75 {
     76     ASSERT_EQ(cudaSuccess, status);
     77 
     78     Async* test = reinterpret_cast<Async*>(userData);
     79 
     80     cv::cuda::HostMem src = test->src;
     81     cv::cuda::HostMem dst = test->dst;
     82 
     83     cv::Mat dst_gold = cv::Mat::zeros(src.size(), src.type());
     84 
     85     ASSERT_MAT_NEAR(dst_gold, dst, 0);
     86 }
     87 
     88 CUDA_TEST_P(Async, MemSet)
     89 {
     90     cv::cuda::Stream stream;
     91 
     92     d_dst.upload(src);
     93 
     94     d_dst.setTo(cv::Scalar::all(0), stream);
     95     d_dst.download(dst, stream);
     96 
     97     Async* test = this;
     98     stream.enqueueHostCallback(checkMemSet, test);
     99 
    100     stream.waitForCompletion();
    101 }
    102 
    103 void checkConvert(int status, void* userData)
    104 {
    105     ASSERT_EQ(cudaSuccess, status);
    106 
    107     Async* test = reinterpret_cast<Async*>(userData);
    108 
    109     cv::cuda::HostMem src = test->src;
    110     cv::cuda::HostMem dst = test->dst;
    111 
    112     cv::Mat dst_gold;
    113     src.createMatHeader().convertTo(dst_gold, CV_32S);
    114 
    115     ASSERT_MAT_NEAR(dst_gold, dst, 0);
    116 }
    117 
    118 CUDA_TEST_P(Async, Convert)
    119 {
    120     cv::cuda::Stream stream;
    121 
    122     d_src.upload(src, stream);
    123     d_src.convertTo(d_dst, CV_32S, stream);
    124     d_dst.download(dst, stream);
    125 
    126     Async* test = this;
    127     stream.enqueueHostCallback(checkConvert, test);
    128 
    129     stream.waitForCompletion();
    130 }
    131 
    132 CUDA_TEST_P(Async, HostMemAllocator)
    133 {
    134     cv::cuda::Stream stream;
    135 
    136     cv::Mat h_dst;
    137     h_dst.allocator = cv::cuda::HostMem::getAllocator();
    138 
    139     d_src.upload(src, stream);
    140     d_src.convertTo(d_dst, CV_32S, stream);
    141     d_dst.download(h_dst, stream);
    142 
    143     stream.waitForCompletion();
    144 
    145     cv::Mat dst_gold;
    146     src.createMatHeader().convertTo(dst_gold, CV_32S);
    147 
    148     ASSERT_MAT_NEAR(dst_gold, h_dst, 0);
    149 }
    150 
    151 INSTANTIATE_TEST_CASE_P(CUDA_Stream, Async, ALL_DEVICES);
    152 
    153 #endif // HAVE_CUDA
    154