test_stream.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. #include "test_precomp.hpp"
  43. #ifdef HAVE_CUDA
  44. #include <cuda_runtime.h>
  45. #include "opencv2/core/cuda.hpp"
  46. #include "opencv2/core/cuda_stream_accessor.hpp"
  47. #include "opencv2/ts/cuda_test.hpp"
  48. namespace opencv_test { namespace {
  49. struct Async : testing::TestWithParam<cv::cuda::DeviceInfo>
  50. {
  51. cv::cuda::HostMem src;
  52. cv::cuda::GpuMat d_src;
  53. cv::cuda::HostMem dst;
  54. cv::cuda::GpuMat d_dst;
  55. virtual void SetUp()
  56. {
  57. cv::cuda::DeviceInfo devInfo = GetParam();
  58. cv::cuda::setDevice(devInfo.deviceID());
  59. src = cv::cuda::HostMem(cv::cuda::HostMem::PAGE_LOCKED);
  60. cv::Mat m = randomMat(cv::Size(128, 128), CV_8UC1);
  61. m.copyTo(src);
  62. }
  63. };
  64. void checkMemSet(int status, void* userData)
  65. {
  66. ASSERT_EQ(cudaSuccess, status);
  67. Async* test = reinterpret_cast<Async*>(userData);
  68. cv::cuda::HostMem src = test->src;
  69. cv::cuda::HostMem dst = test->dst;
  70. cv::Mat dst_gold = cv::Mat::zeros(src.size(), src.type());
  71. ASSERT_MAT_NEAR(dst_gold, dst, 0);
  72. }
  73. CUDA_TEST_P(Async, MemSet)
  74. {
  75. cv::cuda::Stream stream;
  76. d_dst.upload(src);
  77. d_dst.setTo(cv::Scalar::all(0), stream);
  78. d_dst.download(dst, stream);
  79. Async* test = this;
  80. stream.enqueueHostCallback(checkMemSet, test);
  81. stream.waitForCompletion();
  82. }
  83. void checkConvert(int status, void* userData)
  84. {
  85. ASSERT_EQ(cudaSuccess, status);
  86. Async* test = reinterpret_cast<Async*>(userData);
  87. cv::cuda::HostMem src = test->src;
  88. cv::cuda::HostMem dst = test->dst;
  89. cv::Mat dst_gold;
  90. src.createMatHeader().convertTo(dst_gold, CV_32S);
  91. ASSERT_MAT_NEAR(dst_gold, dst, 0);
  92. }
  93. CUDA_TEST_P(Async, Convert)
  94. {
  95. cv::cuda::Stream stream;
  96. d_src.upload(src, stream);
  97. d_src.convertTo(d_dst, CV_32S, stream);
  98. d_dst.download(dst, stream);
  99. Async* test = this;
  100. stream.enqueueHostCallback(checkConvert, test);
  101. stream.waitForCompletion();
  102. }
  103. CUDA_TEST_P(Async, WrapStream)
  104. {
  105. cudaStream_t cuda_stream = NULL;
  106. ASSERT_EQ(cudaSuccess, cudaStreamCreate(&cuda_stream));
  107. {
  108. cv::cuda::Stream stream = cv::cuda::StreamAccessor::wrapStream(cuda_stream);
  109. d_src.upload(src, stream);
  110. d_src.convertTo(d_dst, CV_32S, stream);
  111. d_dst.download(dst, stream);
  112. Async* test = this;
  113. stream.enqueueHostCallback(checkConvert, test);
  114. stream.waitForCompletion();
  115. }
  116. ASSERT_EQ(cudaSuccess, cudaStreamDestroy(cuda_stream));
  117. }
  118. CUDA_TEST_P(Async, HostMemAllocator)
  119. {
  120. cv::cuda::Stream stream;
  121. cv::Mat h_dst;
  122. h_dst.allocator = cv::cuda::HostMem::getAllocator();
  123. d_src.upload(src, stream);
  124. d_src.convertTo(d_dst, CV_32S, stream);
  125. d_dst.download(h_dst, stream);
  126. stream.waitForCompletion();
  127. cv::Mat dst_gold;
  128. src.createMatHeader().convertTo(dst_gold, CV_32S);
  129. ASSERT_MAT_NEAR(dst_gold, h_dst, 0);
  130. }
  131. INSTANTIATE_TEST_CASE_P(CUDA_Stream, Async, ALL_DEVICES);
  132. }} // namespace
  133. #endif // HAVE_CUDA