Path: blob/master/modules/core/test/ocl/test_gemm.cpp
16339 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.13// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// @Authors17// Peng Xiao, [email protected]18// Redistribution and use in source and binary forms, with or without modification,19// are permitted provided that the following conditions are met:20//21// * Redistribution's of source code must retain the above copyright notice,22// this list of conditions and the following disclaimer.23//24// * Redistribution's in binary form must reproduce the above copyright notice,25// this list of conditions and the following disclaimer in the documentation26// and/or other materials provided with the distribution.27//28// * The name of the copyright holders may not be used to endorse or promote products29// derived from this software without specific prior written permission.30//31// This software is provided by the copyright holders and contributors as is and32// any express or implied warranties, including, but not limited to, the implied33// warranties of merchantability and fitness for a particular purpose are disclaimed.34// In no event shall the Intel Corporation or contributors be liable for any direct,35// indirect, incidental, special, exemplary, or consequential damages36// (including, but not limited to, procurement of substitute goods or services;37// loss of use, data, or profits; or business interruption) however caused38// and on any theory of liability, whether in contract, strict liability,39// or tort (including negligence or otherwise) arising in any way out of40// the use of this software, even if advised of the possibility of such damage.41//42//M*/4344#include "../test_precomp.hpp"45#include "opencv2/ts/ocl_test.hpp"4647#ifdef HAVE_OPENCL4849namespace opencv_test {50namespace ocl {5152////////////////////////////////////////////////////////////////////////////53// GEMM5455PARAM_TEST_CASE(Gemm,56MatType,57bool, // GEMM_1_T58bool, // GEMM_2_T59bool, // GEMM_3_T60bool // ROI61)62{63bool use_roi;64int type, flags;65bool atrans, btrans, ctrans;6667double alpha, beta;6869TEST_DECLARE_INPUT_PARAMETER(A);70TEST_DECLARE_INPUT_PARAMETER(B);71TEST_DECLARE_INPUT_PARAMETER(C);72TEST_DECLARE_OUTPUT_PARAMETER(D);7374virtual void SetUp()75{76atrans = btrans = ctrans = false;7778type = GET_PARAM(0);79use_roi = GET_PARAM(4);8081flags = 0;82if (GET_PARAM(1))83flags |= GEMM_1_T, atrans = true;84if (GET_PARAM(2))85flags |= GEMM_2_T, btrans = true;86if (GET_PARAM(3))87flags |= GEMM_3_T, ctrans = true;88}8990void generateTestData()91{92// set minimum size to 20, since testing less sizes doesn't make sense93Size ARoiSize = randomSize(20, MAX_VALUE);94Border ABorder = randomBorder(0, use_roi ? MAX_VALUE : 0);95randomSubMat(A, A_roi, ARoiSize, ABorder, type, -11, 11);9697if (atrans)98ARoiSize = Size(ARoiSize.height, ARoiSize.width);99100Size BRoiSize = randomSize(20, MAX_VALUE);101if (btrans)102BRoiSize.width = ARoiSize.width;103else104BRoiSize.height = ARoiSize.width;105106Border BBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);107randomSubMat(B, B_roi, BRoiSize, BBorder, type, -11, 11);108109if (btrans)110BRoiSize = Size(BRoiSize.height, BRoiSize.width);111112Size DRoiSize = Size(BRoiSize.width, ARoiSize.height), CRoiSizeT(DRoiSize.height, DRoiSize.width);113Border CBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);114randomSubMat(C, C_roi, ctrans ? CRoiSizeT : DRoiSize, CBorder, type, -11, 11);115116Border DBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);117randomSubMat(D, D_roi, DRoiSize, DBorder, type, -11, 11);118119alpha = randomDouble(-4, 4);120beta = randomDouble(-4, 4);121122UMAT_UPLOAD_INPUT_PARAMETER(A);123UMAT_UPLOAD_INPUT_PARAMETER(B);124UMAT_UPLOAD_INPUT_PARAMETER(C);125UMAT_UPLOAD_OUTPUT_PARAMETER(D);126}127};128129OCL_TEST_P(Gemm, Accuracy)130{131for (int i = 0; i < test_loop_times; ++i)132{133generateTestData();134135OCL_OFF(cv::gemm(A_roi, B_roi, alpha, C_roi, beta, D_roi, flags));136OCL_ON(cv::gemm(uA_roi, uB_roi, alpha, uC_roi, beta, uD_roi, flags));137138double eps = D_roi.size().area() * 1e-4;139OCL_EXPECT_MATS_NEAR(D, eps);140}141}142143OCL_INSTANTIATE_TEST_CASE_P(Core, Gemm, ::testing::Combine(144testing::Values(CV_32FC1, CV_32FC2, CV_64FC1, CV_64FC2),145Bool(), Bool(), Bool(), Bool()));146147// Test for non-Intel GPUs to check CL_INVALID_WORK_GROUP_SIZE when localsize > globalsize148OCL_TEST(Gemm, small)149{150UMat A(2, 3, CV_32F), B(4, 3, CV_32F), uC(2, 4, CV_32F);151Mat C(2, 4, CV_32F);152153randu(A, -1, 1);154randu(B, -1, 1);155156OCL_OFF(cv::gemm(A, B, 1, noArray(), 0, C, GEMM_2_T));157OCL_ON(cv::gemm(A, B, 1, noArray(), 0, uC, GEMM_2_T));158159EXPECT_LE(cvtest::norm(C, uC, cv::NORM_INF), 1e-5);160}161162} } // namespace opencv_test::ocl163164#endif // HAVE_OPENCL165166167