-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcaldgemm.cl
256 lines (237 loc) · 8.06 KB
/
caldgemm.cl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
* This file is part of the CALDGEMM library.
*
* Copyright 2015:
* - David Rohr ([email protected])
* - Matthias Bach ([email protected])
* - Matthias Kretz ([email protected])
*
* This file is part of CALDGEMM.
*
* CALDGEMM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CALDGEMM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with CALDGEMM. If not, see <http://www.gnu.org/licenses/>.
*/
#define qon_mstr(a) #a
#define qon_mxstr(a) qon_mstr(a)
#ifdef OCL_USE_SIMPLE_BUFFERS
#ifdef CALDGEMM_TRANSPOSED_B
const char *caldgemm_opencl::OCLKernelName =
OCL_KERNEL_PRE
"//KERNEL TRANSPOSED B SIMPLE BUFFERS\n"
"__kernel void oclkernel(__global double* C, __global const double* __restrict const A, __global const double* __restrict const B, int height1, int height2, int width, double alpha, double beta, int pitch, ulong offset)\n"
"{\n"
" int i, j, k;\n"
" for (i = get_global_id(1);i < height2;i += get_global_size(1))\n"
" {\n"
" for (j = get_global_id(0);j < height1;j += get_global_size(0))\n"
" {\n"
" double addval = 0.;\n"
#ifdef CALDGEMM_FORCE_K
" for (k = 0;k < " qon_mxstr(CALDGEMM_FORCE_K) ";k++)\n"
#else
" for (k = 0;k < width;k++)\n"
#endif
" {\n"
" addval += A[i * width + k] * B[j * width + k];\n"
" }\n"
#ifdef CALDGEMM_ALPHA1
" C[offset + i * pitch + j] = beta * C[offset + i * pitch + j] + addval;\n"
#else
" C[offset + i * pitch + j] = beta * C[offset + i * pitch + j] + alpha * addval;\n"
#endif
" }\n"
" }\n"
"}\n"
;
#else
const char *caldgemm_opencl::OCLKernelName =
OCL_KERNEL_PRE
"//KERNEL TRANSPOSED A SIMPLE BUFFERS\n"
"__kernel void oclkernel(__global double* C, __global const double* __restrict const A, __global const double* __restrict const B, int height1, int height2, int width, double alpha, double beta, int pitch, ulong offset)\n"
"{\n"
" int i, j, k;\n"
" for (i = get_global_id(1);i < height2;i += get_global_size(1))\n"
" {\n"
" for (j = get_global_id(0);j < height1;j += get_global_size(0))\n"
" {\n"
" double addval = 0.;\n"
#ifdef CALDGEMM_FORCE_K
" for (k = 0;k < " qon_mxstr(CALDGEMM_FORCE_K) ";k++)\n"
#else
" for (k = 0;k < width;k++)\n"
#endif
" {\n"
" addval += A[k * height2 + i] * B[k * height1 + j];\n"
" }\n"
#ifdef CALDGEMM_ALPHA1
" C[offset + i * pitch + j] = beta * C[offset + i * pitch + j] + addval;\n"
#else
" C[offset + i * pitch + j] = beta * C[offset + i * pitch + j] + alpha * addval;\n"
#endif
" }\n"
" }\n"
"}\n"
;
#endif
#else //OCL_USE_SIMPLE_BUFFERS
#ifdef CALDGEMM_TRANSPOSED_B
const char *caldgemm_opencl::OCLKernelName =
OCL_KERNEL_PRE
"//KERNEL TRANSPOSED B TEXTURE BUFFERS\n"
"union double_read {uint4 f; double2 d;};\n"
"__kernel void oclkernel(__global double* C, image2d_t A, image2d_t B, int height1, int height2, int width, double alpha, double beta, int pitch, ulong offset)\n"
"{\n"
" const sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_NONE | CLK_FILTER_NEAREST;\n"
" int i, j, k;\n"
" for (i = get_global_id(1);i < height2;i += get_global_size(1))\n"
" {\n"
" for (j = get_global_id(0);j < height1;j += get_global_size(0))\n"
" {\n"
" double addval = 0.;\n"
#ifdef CALDGEMM_FORCE_K
" for (k = 0;k < " qon_mxstr(CALDGEMM_FORCE_K) " / 2;k++)\n"
#else
" for (k = 0;k < width / 2;k++)\n"
#endif
" {\n"
" float2 coord;\n"
" union double_read tmp, tmp2;\n"
" coord.x = k;\n"
" coord.y = i;\n"
" tmp.f = read_imageui(A, sampler, coord);\n"
" coord.y = j;\n"
" tmp2.f = read_imageui(B, sampler, coord);\n"
" addval += tmp.d.x * tmp2.d.x + tmp.d.y * tmp2.d.y;\n"
" }\n"
#ifdef CALDGEMM_ALPHA1
" C[offset + i * pitch + j] = beta * C[offset + i * pitch + j] + addval;\n"
#else
" C[offset + i * pitch + j] = beta * C[offset + i * pitch + j] + alpha * addval;\n"
#endif
" }\n"
" }\n"
"}\n"
;
#elif defined(CALDGEMM_TRANSPOSED_A)
#ifndef OCL_TILED_KERNEL
const char *caldgemm_opencl::OCLKernelName =
OCL_KERNEL_PRE
"//KERNEL TRANSPOSED A TEXTURE BUFFERS\n"
"union double_read {uint4 f; double2 d;};\n"
"__kernel void oclkernel(__global double* C, image2d_t A, image2d_t B, int height1, int height2, int width, double alpha, double beta, int pitch, ulong offset)\n"
"{\n"
" const sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_NONE | CLK_FILTER_NEAREST;\n"
" int i, j, k;\n"
" for (i = get_global_id(1);i < height2;i += get_global_size(1))\n"
" {\n"
" for (j = get_global_id(0);j < height1;j += get_global_size(0))\n"
" {\n"
" double addval = 0.;\n"
#ifdef CALDGEMM_FORCE_K
" for (k = 0;k < " qon_mxstr(CALDGEMM_FORCE_K) ";k++)\n"
#else
" for (k = 0;k < width;k++)\n"
#endif
" {\n"
" float2 coord;\n"
" union double_read tmp, tmp2;\n"
" coord.x = i / 2;\n"
" coord.y = k;\n"
" tmp.f = read_imageui(A, sampler, coord);\n"
" coord.x = j / 2;\n"
" tmp2.f = read_imageui(B, sampler, coord);\n"
" double v1 = (i & 1) ? tmp.d.y : tmp.d.x, v2 = (j & 1) ? tmp2.d.y : tmp2.d.x;\n"
" addval += v1 * v2;\n"
" }\n"
#ifdef CALDGEMM_ALPHA1
" C[offset + i * pitch + j] = beta * C[offset + i * pitch + j] + addval;\n"
#else
" C[offset + i * pitch + j] = beta * C[offset + i * pitch + j] + alpha * addval;\n"
#endif
" }\n"
" }\n"
"}\n"
;
#else
const char *caldgemm_opencl::OCLKernelName =
OCL_KERNEL_PRE
"//KERNEL TRANSPOSED A TEXTURE BUFFERS TILED\n"
"//#pragma OPENCL EXTENSION CP_FP_FMA\n"
"union double_read {uint4 f; double2 d;};\n"
"#define OCL_TILING_X " qon_mxstr(OCL_TILING_X) "\n"
"#define OCL_TILING_Y " qon_mxstr(OCL_TILING_Y) "\n"
"__kernel void oclkernel(__global double* C, image2d_t A, image2d_t B, int height1, int height2, int width, double alpha, double beta, int pitch, ulong offset)\n"
"{\n"
" const sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_NONE | CLK_FILTER_NEAREST;\n"
" int i, j, k, l, m;\n"
" for (i = get_global_id(1) * OCL_TILING_Y;i < height2;i += get_global_size(1) * OCL_TILING_Y)\n"
" {\n"
" for (j = get_global_id(0) * OCL_TILING_X;j < height1;j += get_global_size(0) * OCL_TILING_X)\n"
" {\n"
" double addval[OCL_TILING_X][OCL_TILING_Y];\n"
"#pragma unroll\n"
" for (k = 0;k < OCL_TILING_X;k++) for (l = 0;l < OCL_TILING_Y;l++) addval[k][l] = 0.;\n"
"#pragma unroll 1\n"
#ifdef CALDGEMM_FORCE_K
" for (k = 0;k < " qon_mxstr(CALDGEMM_FORCE_K) ";k++)\n"
#else
" for (k = 0;k < width;k++)\n"
#endif
" {\n"
" float2 coord;\n"
" union double_read tmp[OCL_TILING_X / 2], tmp2[OCL_TILING_Y / 2];\n"
" coord.y = k;\n"
"#pragma unroll\n"
" for (l = 0;l < OCL_TILING_X / 2;l++)\n"
" {\n"
" coord.x = i / 2 + l;\n"
" tmp[l].f = read_imageui(A, sampler, coord);\n"
" }\n"
" for (l = 0;l < OCL_TILING_Y / 2;l++)\n"
" {\n"
" coord.x = j / 2 + l;\n"
" tmp2[l].f = read_imageui(B, sampler, coord);\n"
" }\n"
"#pragma unroll\n"
" for (l = 0;l < OCL_TILING_X / 2;l++)\n"
" {\n"
"#pragma unroll\n"
" for (m = 0;m < OCL_TILING_Y / 2;m++)\n"
" {\n"
" addval[2 * l][2 * m] = mad(tmp[l].d.x, tmp2[m].d.x, addval[2 * l][2 * m]);\n"
" addval[2 * l + 1][2 * m] = mad(tmp[l].d.y, tmp2[m].d.x, addval[2 * l + 1][2 * m]);\n"
" addval[2 * l][2 * m + 1] = mad(tmp[l].d.x, tmp2[m].d.y, addval[2 * l][2 * m + 1]);\n"
" addval[2 * l + 1][2 * m + 1] = mad(tmp[l].d.y, tmp2[m].d.y, addval[2 * l + 1][2 * m + 1]);\n"
" }\n"
" }\n"
" }\n"
"#pragma unroll\n"
" for (k = 0;k < OCL_TILING_X;k++)\n"
" {\n"
"#pragma unroll\n"
" for (l = 0;l < OCL_TILING_Y;l++)\n"
" {\n"
#ifdef CALDGEMM_ALPHA1
" C[offset + (i + k) * pitch + j + l] = beta * C[offset + (i + k) * pitch + j + l] + addval[k][l];\n"
#else
" C[offset + (i + k) * pitch + j + l] = beta * C[offset + (i + k) * pitch + j + l] + alpha * addval[k][l];\n"
#endif
" }\n"
" }\n"
" }\n"
" }\n"
"}\n"
;
#endif
#endif
#endif