Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/dnn/src/opencl/matvec_mul.cl
16337 views
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) 2017, Intel Corporation, all rights reserved.
14
// Third party copyrights are property of their respective owners.
15
//
16
// Redistribution and use in source and binary forms, with or without modification,
17
// are permitted provided that the following conditions are met:
18
//
19
// * Redistribution's of source code must retain the above copyright notice,
20
// this list of conditions and the following disclaimer.
21
//
22
// * Redistribution's in binary form must reproduce the above copyright notice,
23
// this list of conditions and the following disclaimer in the documentation
24
// and/or other materials provided with the distribution.
25
//
26
// * The name of the copyright holders may not be used to endorse or promote products
27
// derived from this software without specific prior written permission.
28
//
29
// This software is provided by the copyright holders and contributors "as is" and
30
// any express or implied warranties, including, but not limited to, the implied
31
// warranties of merchantability and fitness for a particular purpose are disclaimed.
32
// In no event shall the Intel Corporation or contributors be liable for any direct,
33
// indirect, incidental, special, exemplary, or consequential damages
34
// (including, but not limited to, procurement of substitute goods or services;
35
// loss of use, data, or profits; or business interruption) however caused
36
// and on any theory of liability, whether in contract, strict liability,
37
// or tort (including negligence or otherwise) arising in any way out of
38
// the use of this software, even if advised of the possibility of such damage.
39
//
40
//M*/
41
42
#if defined(cl_khr_fp16)
43
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
44
#endif
45
46
#define CONCAT(A,B) A##_##B
47
#define TEMPLATE(name,type) CONCAT(name,type)
48
#define KERNEL_ARG_DTYPE float
49
50
__kernel void TEMPLATE(matvec_mul4,Dtype)(
51
__global const Dtype * A,
52
int offA,
53
unsigned int A_col_size,
54
unsigned int trail_item,
55
__global const Dtype * v,
56
int offv,
57
KERNEL_ARG_DTYPE alpha,
58
KERNEL_ARG_DTYPE beta,
59
__global Dtype4* result,
60
int offr,
61
__local Dtype4* work)
62
{
63
unsigned int row_gid = get_group_id(0);
64
unsigned int lid = get_local_id(0);
65
const __global Dtype *src0_read = A + row_gid * 4 * A_col_size + offA;
66
const __global Dtype *src1_read = v + offv;
67
result = (__global Dtype4*)((__global Dtype*)result + offr);
68
Dtype4 dot0 = (Dtype4)(0.f);
69
Dtype4 dot1 = (Dtype4)(0.f);
70
Dtype4 dot2 = (Dtype4)(0.f);
71
Dtype4 dot3 = (Dtype4)(0.f);
72
73
unsigned int i = lid;
74
while( i < A_col_size / 4) {
75
const Dtype4 a0 = vload4(i, src0_read);
76
const Dtype4 a1 = vload4(i, src0_read + A_col_size);
77
const Dtype4 a2 = vload4(i, src0_read + 2 * A_col_size);
78
const Dtype4 a3 = vload4(i, src0_read + 3 * A_col_size);
79
80
const Dtype4 b0 = vload4(i, src1_read);
81
82
dot0 += a0 * b0;
83
dot1 += a1 * b0;
84
dot2 += a2 * b0;
85
dot3 += a3 * b0;
86
87
i += get_local_size(0);
88
}
89
90
work[lid].s0 = dot0.x + dot0.y + dot0.z + dot0.w;
91
work[lid].s1 = dot1.x + dot1.y + dot1.z + dot1.w;
92
work[lid].s2 = dot2.x + dot2.y + dot2.z + dot2.w;
93
work[lid].s3 = dot3.x + dot3.y + dot3.z + dot3.w;
94
95
if(i == A_col_size / 4)
96
{
97
if(trail_item != 0)
98
{
99
const __global Dtype *src0_trail = src0_read + i * 4;
100
const __global Dtype *src1_trail = src1_read + i * 4;
101
for(unsigned int i = 0; i < trail_item; ++i) {
102
const Dtype at0 = src0_trail[i];
103
const Dtype at1 = src0_trail[i + A_col_size];
104
const Dtype at2 = src0_trail[i + 2 * A_col_size];
105
const Dtype at3 = src0_trail[i + 3 * A_col_size];
106
107
const Dtype bt = src1_trail[i];
108
109
work[lid].s0 += at0 * bt;
110
work[lid].s1 += at1 * bt;
111
work[lid].s2 += at2 * bt;
112
work[lid].s3 += at3 * bt;
113
}
114
}
115
116
}
117
118
for(unsigned int stride=get_local_size(0)/2 ; stride>0 ; stride>>=1) {
119
barrier(CLK_LOCAL_MEM_FENCE);
120
if(lid < stride)
121
work[lid] += work[lid+stride];
122
}
123
if(lid == 0) {
124
if(beta == (Dtype)0)
125
result[row_gid] = convert_Dtype(alpha) * work[0];
126
else
127
result[row_gid] = convert_Dtype(alpha) * work[0] + convert_Dtype(beta) * result[row_gid];
128
}
129
}
130
131
/* This kernel used for the trailing rows when row_of_A %4 !=0 */
132
__kernel void TEMPLATE(matvec_mul1,Dtype)(
133
__global const Dtype * A,
134
int offA,
135
unsigned int A_col_size,
136
unsigned int row_offset,
137
unsigned int trail_item,
138
__global const Dtype * v,
139
int offv,
140
KERNEL_ARG_DTYPE alpha,
141
KERNEL_ARG_DTYPE beta,
142
__global Dtype * result,
143
int offr,
144
__local Dtype * work)
145
{
146
unsigned int row_gid = get_group_id(0);
147
unsigned int lid = get_local_id(0);
148
149
const __global Dtype *src0_read = A + (row_offset + row_gid) * A_col_size + offA;
150
const __global Dtype *src1_read = v + + offv;
151
result = result + offr;
152
Dtype4 dot0 = (Dtype4)(0.f);
153
154
unsigned int i = lid;
155
while( i < A_col_size / 4)
156
{
157
const Dtype4 a0 = vload4(i, src0_read);
158
const Dtype4 b0 = vload4(i, src1_read);
159
160
dot0 += a0 * b0;
161
i += get_local_size(0);
162
}
163
164
work[lid] = dot0.x + dot0.y + dot0.z + dot0.w;
165
166
if(i == A_col_size / 4)
167
{
168
if(trail_item != 0)
169
{
170
const __global Dtype *src0_trail = src0_read + i * 4;
171
const __global Dtype *src1_trail = src1_read + i * 4;
172
for(unsigned int i = 0; i < trail_item; ++i) {
173
const Dtype at0 = src0_trail[i];
174
const Dtype bt = src1_trail[i];
175
176
work[lid] += at0 * bt;
177
}
178
}
179
180
}
181
for(unsigned int stride=get_local_size(0)/2 ; stride>0 ; stride>>=1) {
182
barrier(CLK_LOCAL_MEM_FENCE);
183
if(lid < stride)
184
work[lid] += work[lid+stride];
185
}
186
187
if(lid == 0) {
188
if(beta == (Dtype)0) {
189
result[row_gid+row_offset] = convert_Dtype(alpha) * work[0];
190
} else {
191
result[row_gid+row_offset] *= convert_Dtype(beta);
192
result[row_gid+row_offset] += convert_Dtype(alpha) * work[0];
193
}
194
}
195
}
196
197