test_gemm.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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) 2010-2012, Multicoreware, Inc., all rights reserved.
  14. // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // @Authors
  18. // Peng Xiao, pengxiao@multicorewareinc.com
  19. // Redistribution and use in source and binary forms, with or without modification,
  20. // are permitted provided that the following conditions are met:
  21. //
  22. // * Redistribution's of source code must retain the above copyright notice,
  23. // this list of conditions and the following disclaimer.
  24. //
  25. // * Redistribution's in binary form must reproduce the above copyright notice,
  26. // this list of conditions and the following disclaimer in the documentation
  27. // and/or other materials provided with the distribution.
  28. //
  29. // * The name of the copyright holders may not be used to endorse or promote products
  30. // derived from this software without specific prior written permission.
  31. //
  32. // This software is provided by the copyright holders and contributors as is and
  33. // any express or implied warranties, including, but not limited to, the implied
  34. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  35. // In no event shall the Intel Corporation or contributors be liable for any direct,
  36. // indirect, incidental, special, exemplary, or consequential damages
  37. // (including, but not limited to, procurement of substitute goods or services;
  38. // loss of use, data, or profits; or business interruption) however caused
  39. // and on any theory of liability, whether in contract, strict liability,
  40. // or tort (including negligence or otherwise) arising in any way out of
  41. // the use of this software, even if advised of the possibility of such damage.
  42. //
  43. //M*/
  44. #include "../test_precomp.hpp"
  45. #include "opencv2/ts/ocl_test.hpp"
  46. #ifdef HAVE_OPENCL
  47. namespace opencv_test {
  48. namespace ocl {
  49. ////////////////////////////////////////////////////////////////////////////
  50. // GEMM
  51. PARAM_TEST_CASE(Gemm,
  52. MatType,
  53. bool, // GEMM_1_T
  54. bool, // GEMM_2_T
  55. bool, // GEMM_3_T
  56. bool // ROI
  57. )
  58. {
  59. bool use_roi;
  60. int type, flags;
  61. bool atrans, btrans, ctrans;
  62. double alpha, beta;
  63. int M, N, K;
  64. TEST_DECLARE_INPUT_PARAMETER(A);
  65. TEST_DECLARE_INPUT_PARAMETER(B);
  66. TEST_DECLARE_INPUT_PARAMETER(C);
  67. TEST_DECLARE_OUTPUT_PARAMETER(D);
  68. virtual void SetUp()
  69. {
  70. atrans = btrans = ctrans = false;
  71. type = GET_PARAM(0);
  72. use_roi = GET_PARAM(4);
  73. flags = 0;
  74. if (GET_PARAM(1))
  75. flags |= GEMM_1_T, atrans = true;
  76. if (GET_PARAM(2))
  77. flags |= GEMM_2_T, btrans = true;
  78. if (GET_PARAM(3))
  79. flags |= GEMM_3_T, ctrans = true;
  80. }
  81. void generateTestData()
  82. {
  83. M = (int)randomDoubleLog(1, 100);
  84. N = (int)randomDoubleLog(1, 100);
  85. K = (int)randomDoubleLog(1, 1200);
  86. M = roundUp(M, 1);
  87. N = roundUp(N, 1);
  88. K = roundUp(K, 1);
  89. Size ARoiSize = (atrans) ? Size(M, K) : Size(K, M);
  90. Border ABorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
  91. randomSubMat(A, A_roi, ARoiSize, ABorder, type, -11, 11);
  92. Size BRoiSize = (btrans) ? Size(K, N) : Size(N, K);
  93. Border BBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
  94. randomSubMat(B, B_roi, BRoiSize, BBorder, type, -11, 11);
  95. Size CRoiSize = (ctrans) ? Size(M, N) : Size(N, M);
  96. Border CBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
  97. randomSubMat(C, C_roi, CRoiSize, CBorder, type, -11, 11);
  98. Size DRoiSize = Size(N, M);
  99. Border DBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
  100. randomSubMat(D, D_roi, DRoiSize, DBorder, type, -11, 11);
  101. alpha = randomDouble(-4, 4);
  102. beta = randomDouble(-4, 4);
  103. UMAT_UPLOAD_INPUT_PARAMETER(A);
  104. UMAT_UPLOAD_INPUT_PARAMETER(B);
  105. UMAT_UPLOAD_INPUT_PARAMETER(C);
  106. UMAT_UPLOAD_OUTPUT_PARAMETER(D);
  107. }
  108. };
  109. OCL_TEST_P(Gemm, Accuracy)
  110. {
  111. for (int i = 0; i < test_loop_times; ++i)
  112. {
  113. generateTestData();
  114. SCOPED_TRACE(cv::format("i=%d: M=%d N=%d K=%d", i, M, N, K));
  115. OCL_OFF(cv::gemm(A_roi, B_roi, alpha, C_roi, beta, D_roi, flags));
  116. OCL_ON(cv::gemm(uA_roi, uB_roi, alpha, uC_roi, beta, uD_roi, flags));
  117. double eps = D_roi.size().area() * (1e-5 * K);
  118. OCL_EXPECT_MATS_NEAR(D, eps);
  119. }
  120. }
  121. OCL_INSTANTIATE_TEST_CASE_P(Core, Gemm, ::testing::Combine(
  122. testing::Values(CV_32FC1, CV_32FC2, CV_64FC1, CV_64FC2),
  123. Bool(), Bool(), Bool(), Bool()));
  124. // Test for non-Intel GPUs to check CL_INVALID_WORK_GROUP_SIZE when localsize > globalsize
  125. OCL_TEST(Gemm, small)
  126. {
  127. UMat A(2, 3, CV_32F), B(4, 3, CV_32F), uC(2, 4, CV_32F);
  128. Mat C(2, 4, CV_32F);
  129. randu(A, -1, 1);
  130. randu(B, -1, 1);
  131. OCL_OFF(cv::gemm(A, B, 1, noArray(), 0, C, GEMM_2_T));
  132. OCL_ON(cv::gemm(A, B, 1, noArray(), 0, uC, GEMM_2_T));
  133. EXPECT_LE(cvtest::norm(C, uC, cv::NORM_INF), 1e-5);
  134. }
  135. } } // namespace opencv_test::ocl
  136. #endif // HAVE_OPENCL