-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcodeGen_convolution.sh
executable file
·386 lines (358 loc) · 16.7 KB
/
codeGen_convolution.sh
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
echo '#include "cuda.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <opencv2/opencv.hpp>
#include "convolution.h"
//#include "helpers.h"
using namespace std;
//using namespace cv;
texture<float, 2, cudaReadModeElementType> tex;
static inline __device__ int clamp(int x, int l, int r)
{
//max and min are gpu intrinsics
return max(l, min(x,r));
}
static inline __device__ int clamp_addr(int x, int y, int mx, int my)
{
int xaddr = clamp(x, 0, (mx-1));
int yaddr = clamp(y, 0, (my-1));
return (yaddr*mx) + xaddr;
}'
#load2x2 up to load7x7.
# user inputs the startX and startY.
# using the standard anchor, startX = globalX - kernelSize/2
echo "//Load from DRAM to register"
for((amountToLoad=2; amountToLoad<8; amountToLoad++)) do
echo "__device__ void load_global_register_size${amountToLoad}x${amountToLoad}(const float* in, float registers[$amountToLoad][$amountToLoad], int startX, int startY, int width, int height)
{
for(int i=0; i<$amountToLoad; i++)
{"
for((j=0; j<$amountToLoad; j++)) do
echo " registers[i][$j] = in[clamp_addr(startX+$j, startY+i, width, height)];"
done
echo " }
}"
done
for((amountToLoad=2; amountToLoad<8; amountToLoad++)) do
echo "__device__ void load_texCache_register_size${amountToLoad}x${amountToLoad}(const float* in, float registers[$amountToLoad][$amountToLoad], int startX, int startY, int width, int height)
{
for(int i=0; i<$amountToLoad; i++)
{"
for((j=0; j<$amountToLoad; j++)) do
echo " registers[i][$j] = tex2D(tex, float(startX+$j)+0.5, float(startY+i)+0.5);"
done
echo " }
}"
done
echo "//Store from register to DRAM"
for((amountToLoad=1; amountToLoad<8; amountToLoad++)) do
echo "
__device__ void store_register_global_size${amountToLoad}x${amountToLoad}(float* out, float registers[$amountToLoad][$amountToLoad], int startX, int startY, int width, int height)
{
for(int i=0; i<$amountToLoad; i++)
{"
for((j=0; j<$amountToLoad; j++)) do
echo " out[(startY+i)*width + startX+$j] = registers[i][$j];"
done
echo " }
}"
done
filters[2]=.2500; filters[3]=0.1111; filters[4]=0.0625; filters[5]=0.0400; filters[6]=0.0278; filters[7]=0.0204; #1/(kernelSize*kernelSize)
echo "//Convolution -- hard-coded 2D arrays"
for((inDim=2; inDim<8; inDim++)) do
for((kernelSize=2; kernelSize<8; kernelSize++)) do #convolution filter dim
if [ $inDim -ge $kernelSize ]
then
echo "
//@param height,width = dims of 'in' array
__device__ void convolutionDevice_size${inDim}x${inDim}_kernel${kernelSize}x${kernelSize}(float in[$inDim][$inDim], float *out, int startX, int startY)
{
const float filter = ${filters[${kernelSize}]}; // 1/(kernelSize 2D)
float tmp="
for((y=0; y<$kernelSize; y++)) do
for((x=0; x<$kernelSize; x++)) do
echo " in[startY+$y][startX+$x] * filter +"
done
echo ""
done
echo " 0;
*out = tmp;
}"
fi
done
done
#notes:
# const int convsPerThread = $amountToLoad - (ghost zone)
# ghost zone = convSize/2 (possibly +1)
echo "//Kernels that load, convolve, and store."
for memoryScheme in "global_register" "texCache_register"
do
for((kernelSize=2; kernelSize<=7; kernelSize++)) do
for((amountToLoad=2; amountToLoad<=7; amountToLoad++)) do
sqrtConvsPerThread=$(($amountToLoad-$kernelSize+1)) #square of this is total number of convs per thread [TODO: check correctness]
if [ $sqrtConvsPerThread -ge 1 ]
then
echo "__global__ void convolutionKernel_${memoryScheme}_size${amountToLoad}x${amountToLoad}_kernel${kernelSize}x${kernelSize}(const float* in, float* out, const int width, const int height)
{
int globalX = $sqrtConvsPerThread*(blockIdx.x*blockDim.x + threadIdx.x);
int globalY = $sqrtConvsPerThread*(blockIdx.y*blockDim.y + threadIdx.y);
float registers[$amountToLoad][$amountToLoad];
float outRegisters[$sqrtConvsPerThread][$sqrtConvsPerThread];
if(globalX < width && globalY < height)
{
load_${memoryScheme}_size${amountToLoad}x${amountToLoad}(in, registers, globalX-$((($kernelSize+1)/2-1)), globalY-$((($kernelSize+1)/2-1)), width, height); //includes offset so ghost zone is loaded
for(int y=0; y<$sqrtConvsPerThread; y++) //(int, y) = top left of region to convolve
{"
for((x=0; x<$sqrtConvsPerThread; x++)) do
echo " convolutionDevice_size${amountToLoad}x${amountToLoad}_kernel${kernelSize}x${kernelSize}(registers, &outRegisters[y][$x], $x, y);"
done
echo " }
store_register_global_size${sqrtConvsPerThread}x${sqrtConvsPerThread}(out, outRegisters, globalX, globalY, width, height);
}
}"
fi
done
done
done
#Benchmark the time used to do the *actual compute work*
# (not for "production" use)
echo "//Kernels that load, convolve, and store."
for memoryScheme in "global_register" "texCache_register"
do
for((kernelSize=2; kernelSize<=7; kernelSize++)) do
amountToLoad=$kernelSize
sqrtConvsPerThread=1
if [ $sqrtConvsPerThread -ge 1 ]
then
echo "__global__ void convolutionKernel_${memoryScheme}_BenchmarkComputeTime_size${amountToLoad}x${amountToLoad}_kernel${kernelSize}x${kernelSize}(const float* in, float* out, const int width, const int height, float* fpuTime)
{
int globalX = $sqrtConvsPerThread*(blockIdx.x*blockDim.x + threadIdx.x);
int globalY = $sqrtConvsPerThread*(blockIdx.y*blockDim.y + threadIdx.y);
float registers[$amountToLoad][$amountToLoad];
float outRegisters[$sqrtConvsPerThread][$sqrtConvsPerThread];
if(globalX < width && globalY < height)
{
load_${memoryScheme}_size${amountToLoad}x${amountToLoad}(in, registers, globalX-$((($kernelSize+1)/2-1)), globalY-$((($kernelSize+1)/2-1)), width, height); //includes offset so ghost zone is loaded
__syncthreads();
double start = clock64();
//for(int y=0; y<$sqrtConvsPerThread; y++) //(int, y) = top left of region to convolve
int y=0;
for(int iter=0; iter<5; iter++)
{"
for((x=0; x<$sqrtConvsPerThread; x++)) do
echo " convolutionDevice_size${amountToLoad}x${amountToLoad}_kernel${kernelSize}x${kernelSize}(registers, &outRegisters[y][$x], $x, y);"
done
echo " }
__syncthreads();
double myTime = clock64() - start;
store_register_global_size${sqrtConvsPerThread}x${sqrtConvsPerThread}(out, outRegisters, globalX, globalY, width, height);
fpuTime[globalY*width + globalX] = myTime;
//fpuTime[globalY*width + globalX] = globalY*width + globalX; //test
}
}"
fi
done
done
#global only kernel, hard-coded bounds
memoryScheme="global_only"
for((kernelSize=2; kernelSize<8; kernelSize++)) do
startOffset=$((($kernelSize+1)/2-1))
for((sqrtConvsPerThread=1; sqrtConvsPerThread<8; sqrtConvsPerThread++)) do
amountToLoad=$(($sqrtConvsPerThread+$kernelSize-1))
echo "
//@param height,width = dims of 'in' array
__global__ void convolutionKernel_${memoryScheme}_size${amountToLoad}x${amountToLoad}_kernel${kernelSize}x${kernelSize}(const float* in, float* out, const int width, const int height)
{
int globalX = $sqrtConvsPerThread*(blockIdx.x*blockDim.x + threadIdx.x);
int globalY = $sqrtConvsPerThread*(blockIdx.y*blockDim.y + threadIdx.y);
int outIdxX;
int outIdxY;
if(globalX < width && globalY < height)
{
const float filter = ${filters[${kernelSize}]}; // 1/(kernelSize 2D)
for(int yy=0; yy < $sqrtConvsPerThread; yy++)
{"
#for(int outIdxX=globalX; outIdxX < globalX+$sqrtConvsPerThread; outIdxX++) //TODO: unroll this in script.
for((xx=0; xx<$sqrtConvsPerThread; xx++)) do
echo "
outIdxX = globalX+$xx;
outIdxY = globalY+yy;
out[outIdxY*width + outIdxX]="
for((y=-$startOffset; y<$(($kernelSize-$startOffset)); y++)) do
for((x=-$startOffset; x<$(($kernelSize-$startOffset)); x++)) do
echo " in[clamp_addr(outIdxX+$x, outIdxY+$y, width, height)] * filter +" #TODO: use currX and currY
done
echo ""
done
echo " 0;"
done
echo "
}
}
}"
done
done
#texCache only kernel, hard-coded bounds
memoryScheme="texCache_only"
for((kernelSize=2; kernelSize<8; kernelSize++)) do
amountToLoad=$kernelSize
echo "
//@param height,width = dims of 'in' array
__global__ void convolutionKernel_${memoryScheme}_size${amountToLoad}x${amountToLoad}_kernel${kernelSize}x${kernelSize}(const float* in, float* out, const int width, const int height)
{
int globalX = (blockIdx.x*blockDim.x + threadIdx.x);
int globalY = (blockIdx.y*blockDim.y + threadIdx.y);
int startX = globalX - $((($kernelSize+1)/2-1));
int startY = globalY - $((($kernelSize+1)/2-1));
if(globalX < width && globalY < height)
{
const float filter = ${filters[${kernelSize}]}; // 1/(kernelSize 2D)
float tmp="
for((y=0; y<$kernelSize; y++)) do
for((x=0; x<$kernelSize; x++)) do
echo " tex2D(tex, float(startX+$x)+0.5, float(startY+$y)+0.5) * filter +"
done
echo ""
done
echo " 0;
out[globalY*width + globalX] = tmp;
}
}"
done
echo "//configuration directly pulled from simpleTexture in nvidia sdk
void setTexCacheParams()
{
tex.addressMode[0] = cudaAddressModeClamp;
tex.addressMode[1] = cudaAddressModeClamp;
tex.filterMode = cudaFilterModeLinear;
tex.normalized = false;
}"
echo "float convolutionWrapper(float* hImg, const int width, const int height, int amountToLoad, int kernelSize, string memoryScheme, bool outputImgFlag, string outFilename)
{
dim3 grid;
dim3 block;
block.x = 16;
block.y = 16;
int sqrtConvsPerThread = amountToLoad-kernelSize+1;
int nx = width / (block.x*sqrtConvsPerThread); //magic number is for persistant kernels (e.g. if persistant6x6, divide by 4)
int ny = height / (block.y*sqrtConvsPerThread);
grid.x = (width % block.x == 0) ? nx : nx+1;
grid.y = (height % block.y == 0) ? ny : ny+1;
float *dImg;
CHECK_CUDART(cudaMalloc((void**)&dImg, sizeof(float)*width*height)); //for input data (possibly use texture cache?)
CHECK_CUDART(cudaMemcpy(dImg, hImg, sizeof(float)*width*height, cudaMemcpyHostToDevice));
float* dResult; //device memory for output
CHECK_CUDART(cudaMalloc((void**)&dResult, sizeof(float)*width*height));
float* dFpuTime;
CHECK_CUDART(cudaMalloc((void**)&dFpuTime, sizeof(float)*width*height));
CHECK_CUDART(cudaMemset(dFpuTime, 0, sizeof(float)*width*height));
double start = read_timer();"
memoryScheme="global_only"
for((kernelSize=2; kernelSize<8; kernelSize++)) do
for((sqrtConvsPerThread=1; sqrtConvsPerThread<8; sqrtConvsPerThread++)) do
amountToLoad=$(($sqrtConvsPerThread+$kernelSize-1))
echo " if(amountToLoad == $amountToLoad && kernelSize == $kernelSize && memoryScheme == \"${memoryScheme}\") {
cudaFuncSetCacheConfig(convolutionKernel_${memoryScheme}_size${amountToLoad}x${amountToLoad}_kernel${kernelSize}x${kernelSize}, cudaFuncCachePreferL1);
convolutionKernel_${memoryScheme}_size${amountToLoad}x${amountToLoad}_kernel${kernelSize}x${kernelSize}<<<grid, block>>>(dImg, dResult, width, height);
}"
done
done
memoryScheme="global_register_BenchmarkComputeTime"
for((kernelSize=2; kernelSize<8; kernelSize++)) do
amountToLoad=$kernelSize
echo " if(amountToLoad == $amountToLoad && kernelSize == $kernelSize && memoryScheme == \"${memoryScheme}\") {
cudaFuncSetCacheConfig(convolutionKernel_${memoryScheme}_size${amountToLoad}x${amountToLoad}_kernel${kernelSize}x${kernelSize}, cudaFuncCachePreferL1);
convolutionKernel_${memoryScheme}_size${amountToLoad}x${amountToLoad}_kernel${kernelSize}x${kernelSize}<<<grid, block>>>(dImg, dResult, width, height, dFpuTime);
}"
done
memoryScheme="global_register" #TODO: if I do global_shared_register, I should probably set 'prefer shared'
for((kernelSize=2; kernelSize<8; kernelSize++)) do
for((amountToLoad=$kernelSize; amountToLoad<8; amountToLoad++)) do
echo " if(amountToLoad == $amountToLoad && kernelSize == $kernelSize && memoryScheme == \"${memoryScheme}\") {
cudaFuncSetCacheConfig(convolutionKernel_${memoryScheme}_size${amountToLoad}x${amountToLoad}_kernel${kernelSize}x${kernelSize}, cudaFuncCachePreferL1);
convolutionKernel_${memoryScheme}_size${amountToLoad}x${amountToLoad}_kernel${kernelSize}x${kernelSize}<<<grid, block>>>(dImg, dResult, width, height);
}"
#echo " if(amountToLoad == $amountToLoad && memoryScheme == \"global_shared_register\"){
# cudaFuncSetCacheConfig(convolutionKernel_global_shared_register_persist${amountToLoad}x${amountToLoad}_kernel3x3, cudaFuncCachePreferShared);
# convolutionKernel_global_shared_register_persist${amountToLoad}x${amountToLoad}_kernel3x3 <<< grid, block >>>(dImg, dResult, width, height);
# }"
done
done
echo ' cudaDeviceSynchronize();
double responseTime = read_timer() - start;
CHECK_CUDART(cudaFree(dImg));
if(outputImgFlag)
{
float* hResult = (float*)malloc(sizeof(float)*width*height);
CHECK_CUDART(cudaMemcpy(hResult, dResult, sizeof(float)*width*height, cudaMemcpyDeviceToHost));
outputProcessedImageFloat(hResult, width, height, outFilename);
free(hResult);
}
if(memoryScheme.find("BenchmarkComputeTime") != string::npos)
{
float* hFpuTime = (float*)malloc(sizeof(float)*width*height);
CHECK_CUDART(cudaMemcpy(hFpuTime, dFpuTime, sizeof(float)*width*height, cudaMemcpyDeviceToHost));
printf("COMPUTE time on GPU, %dx%d filter: \n", kernelSize, kernelSize);
for(int i=0; i<(width*height); i++)
{
printf("%f\n", hFpuTime[i]);
}
}
CHECK_CUDART(cudaFree(dResult));
CHECK_CUDART(cudaFree(dFpuTime));
return responseTime;
}'
echo "float convolutionWrapper_texCache(float* hImg, const int width, const int height, int amountToLoad, int kernelSize, string memoryScheme, bool outputImgFlag, string outFilename)
{
dim3 grid;
dim3 block;
block.x = 16;
block.y = 16;
int sqrtConvsPerThread = amountToLoad-kernelSize+1;
int nx = width / (block.x*sqrtConvsPerThread); //magic number is for persistant kernels (e.g. if persistant6x6, divide by 4)
int ny = height / (block.y*sqrtConvsPerThread);
grid.x = (width % block.x == 0) ? nx : nx+1;
grid.y = (height % block.y == 0) ? ny : ny+1;
cudaArray *dImg; //cudaArray*, not float*
cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat);
CHECK_CUDART(cudaMallocArray(&dImg, &channelDesc, width, height));
CHECK_CUDART(cudaMemcpyToArray(dImg, 0, 0, hImg, width*height*sizeof(float), cudaMemcpyHostToDevice));
setTexCacheParams();
CHECK_CUDART(cudaBindTextureToArray(tex, dImg, channelDesc));
float* dummy = NULL; //my texCache code doesn't need input data ptr, but it was easier to have one in code generation anyway.
float* dResult; //device memory for output
CHECK_CUDART(cudaMalloc((void**)&dResult, sizeof(float)*width*height));
double start = read_timer();"
memoryScheme="texCache_register" #TODO if I do texCache_shared_register, I should probably set 'prefer shared'
for((kernelSize=2; kernelSize<8; kernelSize++)) do
for((amountToLoad=$kernelSize; amountToLoad<8; amountToLoad++)) do
echo " if(amountToLoad == $amountToLoad && kernelSize == $kernelSize && memoryScheme == \"${memoryScheme}\") {
cudaFuncSetCacheConfig(convolutionKernel_${memoryScheme}_size${amountToLoad}x${amountToLoad}_kernel${kernelSize}x${kernelSize}, cudaFuncCachePreferL1);
convolutionKernel_${memoryScheme}_size${amountToLoad}x${amountToLoad}_kernel${kernelSize}x${kernelSize}<<<grid, block>>>(dummy, dResult, width, height);
}"
done
done
memoryScheme="texCache_only" #TODO if I do texCache_shared_register, I should probably set 'prefer shared'
for((kernelSize=2; kernelSize<8; kernelSize++)) do
amountToLoad=$kernelSize
echo " if(amountToLoad == $amountToLoad && kernelSize == $kernelSize && memoryScheme == \"${memoryScheme}\") {
cudaFuncSetCacheConfig(convolutionKernel_${memoryScheme}_size${amountToLoad}x${amountToLoad}_kernel${kernelSize}x${kernelSize}, cudaFuncCachePreferL1);
convolutionKernel_${memoryScheme}_size${amountToLoad}x${amountToLoad}_kernel${kernelSize}x${kernelSize}<<<grid, block>>>(dummy, dResult, width, height);
}"
done
echo '
cudaDeviceSynchronize();
double responseTime = read_timer() - start;
CHECK_CUDART(cudaFreeArray(dImg));
if(outputImgFlag)
{
float* hResult = (float*)malloc(sizeof(float)*width*height);
CHECK_CUDART(cudaMemcpy(hResult, dResult, sizeof(float)*width*height, cudaMemcpyDeviceToHost));
outputProcessedImageFloat(hResult, width, height, outFilename);
free(hResult);
}
CHECK_CUDART(cudaFree(dResult));
return responseTime;
}'