forked from quettabit/convolution_kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convolution.cu
286 lines (264 loc) · 9.61 KB
/
convolution.cu
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
#include <cuda_runtime.h>
#include <iostream>
#include <vector>
#include <utility>
#include <stdio.h>
#include <math.h>
using namespace std;
#define K 3
#define BLCH 8
#define BLCW 32
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
{
if (code != cudaSuccess)
{
fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) exit(code);
}
}
// declaration of constant memory where the fiter values are stored
__constant__ float cm[K*K];
__device__ void conv(const float* gm,
float* convolved,
int bh,
int bw,
int ih,
int iw,
int ch,
int cw,
int smH,
int smW,
int k,
float* sm,
int gID,
int tID,
int nT,
int rel_row,
int rel_col,
int nRows,
int stopPrefetchRowID,
int lastActiveThreadID) {
for(int i=k; i<=nRows; i++)
{
/*
----prefetch a pixel value from GM and store it in register----
all threads fetch the cell value immediately below to the current cell iteratively
last thread in the block would fetch k cells immediately below the current cell
boundary check would be needed for the blocks that act on the bottom most partition of the input image to prevent it from prefetching out of image values.
*/
float reg;
float regArr[K];
if(i <= stopPrefetchRowID){
reg = gm[i * iw + gID];
if(tID == lastActiveThreadID){
for(int j=1; j<=k-1; j++){
regArr[j] = gm[(i * iw) + gID + j];
}
}
}
// load k * k pixels above the current cell
float imgPixels[K*K];
for(int r=i-k; r<i; r++){
for(int c=0; c<k; c++){
/* translate the indices to [0,k] using r - (i-k) as imgPixels is of size k*k */
imgPixels[(r-i+k)*k + c] = sm[r * smW + tID + c];
}
}
/*multiply image pixel values with filter values (direct convolution) */
float convolvedCell = 0.0;
for(int c=0; c<k*k; c++){
convolvedCell += cm[c]*imgPixels[c];
}
//place the convolvedCell value into convolvedMatrix
int cID = ( ( (rel_row * bh) + (i-k) ) * cw )+( rel_col * nT )+tID;
if(cID < 0 || cID >= ch*cw ) {
printf("cID : %d, tID : %d, gID : %d\n", cID, tID, gID );
}
convolved[cID] = convolvedCell;
__syncthreads();
if(i <= stopPrefetchRowID){
sm[i * smW + tID] = reg;
if(tID == lastActiveThreadID){
for(int j=1; j<=k-1; j++){
int sID = i *smW + tID + j;
sm[sID] = regArr[j];
}
}
}
__syncthreads();
}
}
__global__ void conv_kernel(const float* gm,
float* convolved,
int bh,
int bw,
int ih,
int iw,
int ch,
int cw,
int smH,
int smW,
int k) {
int tID = threadIdx.x;
int bID = blockIdx.x;
int nT = blockDim.x;
int nB = gridDim.x;
int nBx = iw / nT;
//printf("num of blocks is %d\n", nB);
//printf("nB in a row is %d\n", nBx);
//check for right border or bottom border thread block
bool isBottomBorder = false;
bool isRightBorder = false;
// bottom border thread block
if(bID >= nB - nBx) {
//printf("bID : %d is bottom border\n", bID);
isBottomBorder = true;
}
// right border thread block
if((bID+1) % nBx == 0){
//printf("bID : %d is right border\n", bID);
isRightBorder = true;
}
// ---------------- Load k rows from GM into SM ----------------------
__shared__ float sm[ (BLCH + K - 1) * (BLCW + K - 1) ];
// rel_row and rel_col maps the Thread Block to appropriate position
int rel_row = bID / nBx;
int rel_col = bID % nBx;
// (rel_row * bh * iw) covers all the cells before row_ids bh, 2bh, 3bh ..
// gID finally maps threads to cells at rows 0, bh, 2bh, 3bh, ...
int gID = (rel_row * bh * iw) + (rel_col * nT) + tID;
for(int i=0; i<k; i++){
int sID = i * smW + tID;
sm[sID] = gm[i * iw + gID];
/* if last thread in the block, it should fetch additional k-1 pixels
in each row which are needed for computation of the convolution
*/
if(!isRightBorder && tID == nT-1){
for(int j=1; j<=k-1; j++){
sID = (i * smW) + tID + j;
sm[sID] = gm[i * iw + gID + j];
}
}
}
__syncthreads();
if( !isBottomBorder && !isRightBorder ){
int lastActiveThreadID = nT - 1;
int nRows = bh + k - 1;
int stopPrefetchRowID = nRows;
conv( gm, convolved, bh, bw,
ih, iw, ch, cw, smH, smW, k,
sm, gID, tID, nT, rel_row, rel_col,
nRows, stopPrefetchRowID, lastActiveThreadID );
}
else if( isBottomBorder && isRightBorder ){
/* make the last k-1 threads in the block to be idle. as there is no convolution needed for them */
if(tID < (nT - (k-1))){
int nRows = bh;
int stopPrefetchRowID = nRows - 1;
int lastActiveThreadID = nT - k;
conv( gm, convolved, bh, bw,
ih, iw, ch, cw, smH, smW, k,
sm, gID, tID, nT, rel_row, rel_col,
nRows, stopPrefetchRowID, lastActiveThreadID );
}
}
else if( isBottomBorder ){
int nRows = bh;
int stopPrefetchRowID = nRows-1;
int lastActiveThreadID = nT - 1;
conv( gm, convolved, bh, bw,
ih, iw, ch, cw, smH, smW, k,
sm, gID, tID, nT, rel_row, rel_col,
nRows, stopPrefetchRowID, lastActiveThreadID );
}
else if( isRightBorder ){
/* make the last k-1 threads in the block to be idle. as there is no convolution needed for them */
if(tID < (nT - (k-1))){
int nRows = bh + k - 1;
int stopPrefetchRowID = nRows;
int lastActiveThreadID = nT - k;
conv( gm, convolved, bh, bw,
ih, iw, ch, cw, smH, smW, k,
sm, gID, tID, nT, rel_row, rel_col,
nRows, stopPrefetchRowID, lastActiveThreadID );
}
}
}
int main(int argc, char **argv){
/* set values for image dimensions, block dimensions, filter size, stride ..
some of the constraints to keep in mind are
1. the value of k(filter size) should be less than blcH and blcW
2. stride value(s) should be 1
*/
int imgH = 2048;
int imgW = 2048;
int blcH = BLCH;
int blcW = BLCW;
int k = K;
int s = 1;
int nB = (imgH * imgW) / (blcH * blcW);
int nT = blcW;
int imgDims = imgH * imgW;
int imgSize = imgDims * sizeof(float);
// create host array that can hold pixel intensity values
float *h_img = new float[imgDims];
for(int i=0; i<imgDims; i++){
h_img[i] = 1.0;
}
// create device array that can hold pixel intensity values in GPU GM
float *d_img;
gpuErrchk(cudaMalloc((void **) &d_img, imgSize ));
gpuErrchk(cudaMemcpy(d_img, h_img, imgSize, cudaMemcpyHostToDevice));
// create filter and copy to constant memory
int filterDims = k * k;
int filterSize = filterDims * sizeof(float);
float *filter = new float[filterDims];
for(int i=0; i<filterDims; i++){
filter[i] = 0.5;
}
gpuErrchk(cudaMemcpyToSymbol(cm, filter, filterSize));
// create host and device array that holds the convoluted matrix
int convH = ( (imgH - k) / s ) + 1;
int convW = convH;
int convDims = convH * convW;
int convSize = convDims * sizeof(float);
float *h_convolved = new float[convDims];
for(int i=0; i<convDims; i++){
h_convolved[i] = 0;
}
float *d_convolved;
gpuErrchk(cudaMalloc((void **) &d_convolved, convSize));
gpuErrchk(cudaMemcpy(d_convolved, h_convolved,
convSize, cudaMemcpyHostToDevice));
// calculate shared memory dimensions
int smH = blcH + k - 1;
int smW = blcW + k - 1;
// call the kernel
conv_kernel<<<nB, nT>>>(d_img, d_convolved,
blcH, blcW,
imgH, imgW,
convH, convW,
smH, smW,
k);
gpuErrchk(cudaMemcpy(h_convolved, d_convolved,
convSize, cudaMemcpyDeviceToHost));
vector<pair<int,int> > miss;
for(int i=0; i<convH; i++){
for(int j=0; j<convW; j++){
//cout<<h_convolved[i*convW +j]<<" ";
if(h_convolved[i*convW +j] != 4.5){
miss.push_back(make_pair(i,j));
}
}
//cout<<"\n";
}
cout<<miss.size()<<"\n";
for(int i=0;i<miss.size();i++){
cout<<miss[i].first<<","<<miss[i].second<<"\n";
}
cudaDeviceReset();
delete h_img;
delete h_convolved;
return 0;
}