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 
45 #include "../test_precomp.hpp"
46 #include "opencv2/ts/ocl_test.hpp"
47 
48 #ifdef HAVE_OPENCL
49 
50 namespace opencv_test {
51 namespace ocl {
52 
53 ////////////////////////////////////////////////////////////////////////////
54 // GEMM
55 
PARAM_TEST_CASE(Gemm,MatType,bool,bool,bool,bool)56 PARAM_TEST_CASE(Gemm,
57                 MatType,
58                 bool, // GEMM_1_T
59                 bool, // GEMM_2_T
60                 bool, // GEMM_3_T
61                 bool // ROI
62                 )
63 {
64     bool use_roi;
65     int type, flags;
66     bool atrans, btrans, ctrans;
67 
68     double alpha, beta;
69 
70     TEST_DECLARE_INPUT_PARAMETER(A);
71     TEST_DECLARE_INPUT_PARAMETER(B);
72     TEST_DECLARE_INPUT_PARAMETER(C);
73     TEST_DECLARE_OUTPUT_PARAMETER(D);
74 
75     virtual void SetUp()
76     {
77         atrans = btrans = ctrans = false;
78 
79         type = GET_PARAM(0);
80         use_roi = GET_PARAM(4);
81 
82         flags = 0;
83         if (GET_PARAM(1))
84             flags |= GEMM_1_T, atrans = true;
85         if (GET_PARAM(2))
86             flags |= GEMM_2_T, btrans = true;
87         if (GET_PARAM(3))
88             flags |= GEMM_3_T, ctrans = true;
89     }
90 
91     void generateTestData()
92     {
93         // set minimum size to 20, since testing less sizes doesn't make sense
94         Size ARoiSize = randomSize(20, MAX_VALUE);
95         Border ABorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
96         randomSubMat(A, A_roi, ARoiSize, ABorder, type, -11, 11);
97 
98         if (atrans)
99             ARoiSize = Size(ARoiSize.height, ARoiSize.width);
100 
101         Size BRoiSize = randomSize(20, MAX_VALUE);
102         if (btrans)
103             BRoiSize.width = ARoiSize.width;
104         else
105             BRoiSize.height = ARoiSize.width;
106 
107         Border BBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
108         randomSubMat(B, B_roi, BRoiSize, BBorder, type, -11, 11);
109 
110         if (btrans)
111             BRoiSize = Size(BRoiSize.height, BRoiSize.width);
112 
113         Size DRoiSize = Size(BRoiSize.width, ARoiSize.height), CRoiSizeT(DRoiSize.height, DRoiSize.width);
114         Border CBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
115         randomSubMat(C, C_roi, ctrans ? CRoiSizeT : DRoiSize, CBorder, type, -11, 11);
116 
117         Border DBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
118         randomSubMat(D, D_roi, DRoiSize, DBorder, type, -11, 11);
119 
120         alpha = randomDouble(-4, 4);
121         beta = randomDouble(-4, 4);
122 
123         UMAT_UPLOAD_INPUT_PARAMETER(A);
124         UMAT_UPLOAD_INPUT_PARAMETER(B);
125         UMAT_UPLOAD_INPUT_PARAMETER(C);
126         UMAT_UPLOAD_OUTPUT_PARAMETER(D);
127     }
128 };
129 
OCL_TEST_P(Gemm,Accuracy)130 OCL_TEST_P(Gemm, Accuracy)
131 {
132     for (int i = 0; i < test_loop_times; ++i)
133     {
134         generateTestData();
135 
136         OCL_OFF(cv::gemm(A_roi, B_roi, alpha, C_roi, beta, D_roi, flags));
137         OCL_ON(cv::gemm(uA_roi, uB_roi, alpha, uC_roi, beta, uD_roi, flags));
138 
139         double eps = D_roi.size().area() * 1e-4;
140         OCL_EXPECT_MATS_NEAR(D, eps);
141     }
142 }
143 
144 OCL_INSTANTIATE_TEST_CASE_P(Core, Gemm, ::testing::Combine(
145                             testing::Values(CV_32FC1, CV_32FC2, CV_64FC1, CV_64FC2),
146                             Bool(), Bool(), Bool(), Bool()));
147 
148 // Test for non-Intel GPUs to check CL_INVALID_WORK_GROUP_SIZE when localsize > globalsize
OCL_TEST(Gemm,small)149 OCL_TEST(Gemm, small)
150 {
151     UMat A(2, 3, CV_32F), B(4, 3, CV_32F), uC(2, 4, CV_32F);
152     Mat C(2, 4, CV_32F);
153 
154     randu(A, -1, 1);
155     randu(B, -1, 1);
156 
157     OCL_OFF(cv::gemm(A, B, 1, noArray(), 0, C, GEMM_2_T));
158     OCL_ON(cv::gemm(A, B, 1, noArray(), 0, uC, GEMM_2_T));
159 
160     EXPECT_LE(cvtest::norm(C, uC, cv::NORM_INF), 1e-5);
161 }
162 
163 } } // namespace opencv_test::ocl
164 
165 #endif // HAVE_OPENCL
166