Path: blob/master/modules/dnn/src/opencl/matvec_mul.cl
16337 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) 2017, Intel Corporation, all rights reserved.13// Third party copyrights are property of their respective owners.14//15// Redistribution and use in source and binary forms, with or without modification,16// are permitted provided that the following conditions are met:17//18// * Redistribution's of source code must retain the above copyright notice,19// this list of conditions and the following disclaimer.20//21// * Redistribution's in binary form must reproduce the above copyright notice,22// this list of conditions and the following disclaimer in the documentation23// and/or other materials provided with the distribution.24//25// * The name of the copyright holders may not be used to endorse or promote products26// derived from this software without specific prior written permission.27//28// This software is provided by the copyright holders and contributors "as is" and29// any express or implied warranties, including, but not limited to, the implied30// warranties of merchantability and fitness for a particular purpose are disclaimed.31// In no event shall the Intel Corporation or contributors be liable for any direct,32// indirect, incidental, special, exemplary, or consequential damages33// (including, but not limited to, procurement of substitute goods or services;34// loss of use, data, or profits; or business interruption) however caused35// and on any theory of liability, whether in contract, strict liability,36// or tort (including negligence or otherwise) arising in any way out of37// the use of this software, even if advised of the possibility of such damage.38//39//M*/4041#if defined(cl_khr_fp16)42#pragma OPENCL EXTENSION cl_khr_fp16 : enable43#endif4445#define CONCAT(A,B) A##_##B46#define TEMPLATE(name,type) CONCAT(name,type)47#define KERNEL_ARG_DTYPE float4849__kernel void TEMPLATE(matvec_mul4,Dtype)(50__global const Dtype * A,51int offA,52unsigned int A_col_size,53unsigned int trail_item,54__global const Dtype * v,55int offv,56KERNEL_ARG_DTYPE alpha,57KERNEL_ARG_DTYPE beta,58__global Dtype4* result,59int offr,60__local Dtype4* work)61{62unsigned int row_gid = get_group_id(0);63unsigned int lid = get_local_id(0);64const __global Dtype *src0_read = A + row_gid * 4 * A_col_size + offA;65const __global Dtype *src1_read = v + offv;66result = (__global Dtype4*)((__global Dtype*)result + offr);67Dtype4 dot0 = (Dtype4)(0.f);68Dtype4 dot1 = (Dtype4)(0.f);69Dtype4 dot2 = (Dtype4)(0.f);70Dtype4 dot3 = (Dtype4)(0.f);7172unsigned int i = lid;73while( i < A_col_size / 4) {74const Dtype4 a0 = vload4(i, src0_read);75const Dtype4 a1 = vload4(i, src0_read + A_col_size);76const Dtype4 a2 = vload4(i, src0_read + 2 * A_col_size);77const Dtype4 a3 = vload4(i, src0_read + 3 * A_col_size);7879const Dtype4 b0 = vload4(i, src1_read);8081dot0 += a0 * b0;82dot1 += a1 * b0;83dot2 += a2 * b0;84dot3 += a3 * b0;8586i += get_local_size(0);87}8889work[lid].s0 = dot0.x + dot0.y + dot0.z + dot0.w;90work[lid].s1 = dot1.x + dot1.y + dot1.z + dot1.w;91work[lid].s2 = dot2.x + dot2.y + dot2.z + dot2.w;92work[lid].s3 = dot3.x + dot3.y + dot3.z + dot3.w;9394if(i == A_col_size / 4)95{96if(trail_item != 0)97{98const __global Dtype *src0_trail = src0_read + i * 4;99const __global Dtype *src1_trail = src1_read + i * 4;100for(unsigned int i = 0; i < trail_item; ++i) {101const Dtype at0 = src0_trail[i];102const Dtype at1 = src0_trail[i + A_col_size];103const Dtype at2 = src0_trail[i + 2 * A_col_size];104const Dtype at3 = src0_trail[i + 3 * A_col_size];105106const Dtype bt = src1_trail[i];107108work[lid].s0 += at0 * bt;109work[lid].s1 += at1 * bt;110work[lid].s2 += at2 * bt;111work[lid].s3 += at3 * bt;112}113}114115}116117for(unsigned int stride=get_local_size(0)/2 ; stride>0 ; stride>>=1) {118barrier(CLK_LOCAL_MEM_FENCE);119if(lid < stride)120work[lid] += work[lid+stride];121}122if(lid == 0) {123if(beta == (Dtype)0)124result[row_gid] = convert_Dtype(alpha) * work[0];125else126result[row_gid] = convert_Dtype(alpha) * work[0] + convert_Dtype(beta) * result[row_gid];127}128}129130/* This kernel used for the trailing rows when row_of_A %4 !=0 */131__kernel void TEMPLATE(matvec_mul1,Dtype)(132__global const Dtype * A,133int offA,134unsigned int A_col_size,135unsigned int row_offset,136unsigned int trail_item,137__global const Dtype * v,138int offv,139KERNEL_ARG_DTYPE alpha,140KERNEL_ARG_DTYPE beta,141__global Dtype * result,142int offr,143__local Dtype * work)144{145unsigned int row_gid = get_group_id(0);146unsigned int lid = get_local_id(0);147148const __global Dtype *src0_read = A + (row_offset + row_gid) * A_col_size + offA;149const __global Dtype *src1_read = v + + offv;150result = result + offr;151Dtype4 dot0 = (Dtype4)(0.f);152153unsigned int i = lid;154while( i < A_col_size / 4)155{156const Dtype4 a0 = vload4(i, src0_read);157const Dtype4 b0 = vload4(i, src1_read);158159dot0 += a0 * b0;160i += get_local_size(0);161}162163work[lid] = dot0.x + dot0.y + dot0.z + dot0.w;164165if(i == A_col_size / 4)166{167if(trail_item != 0)168{169const __global Dtype *src0_trail = src0_read + i * 4;170const __global Dtype *src1_trail = src1_read + i * 4;171for(unsigned int i = 0; i < trail_item; ++i) {172const Dtype at0 = src0_trail[i];173const Dtype bt = src1_trail[i];174175work[lid] += at0 * bt;176}177}178179}180for(unsigned int stride=get_local_size(0)/2 ; stride>0 ; stride>>=1) {181barrier(CLK_LOCAL_MEM_FENCE);182if(lid < stride)183work[lid] += work[lid+stride];184}185186if(lid == 0) {187if(beta == (Dtype)0) {188result[row_gid+row_offset] = convert_Dtype(alpha) * work[0];189} else {190result[row_gid+row_offset] *= convert_Dtype(beta);191result[row_gid+row_offset] += convert_Dtype(alpha) * work[0];192}193}194}195196197