test_event.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #include "test_precomp.hpp"
  5. #ifdef HAVE_CUDA
  6. #include <cuda_runtime.h>
  7. #include "opencv2/core/cuda.hpp"
  8. #include "opencv2/core/cuda_stream_accessor.hpp"
  9. #include "opencv2/ts/cuda_test.hpp"
  10. namespace opencv_test { namespace {
  11. struct AsyncEvent : testing::TestWithParam<cv::cuda::DeviceInfo>
  12. {
  13. cv::cuda::HostMem src;
  14. cv::cuda::GpuMat d_src;
  15. cv::cuda::HostMem dst;
  16. cv::cuda::GpuMat d_dst;
  17. cv::cuda::Stream stream;
  18. virtual void SetUp()
  19. {
  20. cv::cuda::DeviceInfo devInfo = GetParam();
  21. cv::cuda::setDevice(devInfo.deviceID());
  22. src = cv::cuda::HostMem(cv::cuda::HostMem::PAGE_LOCKED);
  23. cv::Mat m = randomMat(cv::Size(128, 128), CV_8UC1);
  24. m.copyTo(src);
  25. }
  26. };
  27. void deviceWork(void* userData)
  28. {
  29. AsyncEvent* test = reinterpret_cast<AsyncEvent*>(userData);
  30. test->d_src.upload(test->src, test->stream);
  31. test->d_src.convertTo(test->d_dst, CV_32S, test->stream);
  32. test->d_dst.download(test->dst, test->stream);
  33. }
  34. CUDA_TEST_P(AsyncEvent, WrapEvent)
  35. {
  36. cudaEvent_t cuda_event = NULL;
  37. ASSERT_EQ(cudaSuccess, cudaEventCreate(&cuda_event));
  38. {
  39. cv::cuda::Event cudaEvent = cv::cuda::EventAccessor::wrapEvent(cuda_event);
  40. deviceWork(this);
  41. cudaEvent.record(stream);
  42. cudaEvent.waitForCompletion();
  43. cv::Mat dst_gold;
  44. src.createMatHeader().convertTo(dst_gold, CV_32S);
  45. ASSERT_MAT_NEAR(dst_gold, dst, 0);
  46. }
  47. ASSERT_EQ(cudaSuccess, cudaEventDestroy(cuda_event));
  48. }
  49. CUDA_TEST_P(AsyncEvent, WithFlags)
  50. {
  51. cv::cuda::Event cudaEvent = cv::cuda::Event(cv::cuda::Event::CreateFlags::BLOCKING_SYNC);
  52. deviceWork(this);
  53. cudaEvent.record(stream);
  54. cudaEvent.waitForCompletion();
  55. cv::Mat dst_gold;
  56. src.createMatHeader().convertTo(dst_gold, CV_32S);
  57. ASSERT_MAT_NEAR(dst_gold, dst, 0);
  58. }
  59. CUDA_TEST_P(AsyncEvent, Timing)
  60. {
  61. const std::vector<cv::cuda::Event::CreateFlags> eventFlags = { cv::cuda::Event::CreateFlags::BLOCKING_SYNC , cv::cuda::Event::CreateFlags::BLOCKING_SYNC | Event::CreateFlags::DISABLE_TIMING };
  62. const std::vector<bool> shouldFail = { false, true };
  63. for (size_t i = 0; i < eventFlags.size(); i++) {
  64. const auto& flags = eventFlags.at(i);
  65. cv::cuda::Event startEvent = cv::cuda::Event(flags);
  66. cv::cuda::Event stopEvent = cv::cuda::Event(flags);
  67. startEvent.record(stream);
  68. deviceWork(this);
  69. stopEvent.record(stream);
  70. stopEvent.waitForCompletion();
  71. cv::Mat dst_gold;
  72. src.createMatHeader().convertTo(dst_gold, CV_32S);
  73. ASSERT_MAT_NEAR(dst_gold, dst, 0);
  74. bool failed = false;
  75. try {
  76. const double elTimeMs = Event::elapsedTime(startEvent, stopEvent);
  77. ASSERT_GT(elTimeMs, 0);
  78. }
  79. catch (cv::Exception ex) {
  80. failed = true;
  81. }
  82. ASSERT_EQ(failed, shouldFail.at(i));
  83. }
  84. }
  85. INSTANTIATE_TEST_CASE_P(CUDA_Event, AsyncEvent, ALL_DEVICES);
  86. }} // namespace
  87. #endif // HAVE_CUDA