forked from howardjchen/Sparse_CNN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcnnConvLayer.cu
More file actions
309 lines (252 loc) · 8.2 KB
/
cnnConvLayer.cu
File metadata and controls
309 lines (252 loc) · 8.2 KB
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
// This program executes a typical convolutional layer in regular CNNs
#include <iostream>
#include "cnnConvLayer.h"
#include <stdio.h>
#include <unistd.h>
using namespace std;
#define xDim 512
#define yDim 32
#define zDim 32
#define xThreadDim 4
#define yThreadDim 16
#define zThreadDim 16
#define Pool_xDim 512
#define Pool_yDim 16
#define Pool_zDim 16
int outputsize = 512*16*16;
int Outputsize = xDim*yDim*zDim;
int *devoutNeu;
int *devPooling;
short *devFilt;
short *devinNeu;
int *devGlobalBarrier;
int *outResult = new int[outputsize]();
int *outResult_neu = new int[Outputsize]();
int *outGlobalBarrier = new int[Outputsize]();
// This is the CPU version, please don't modify it
void convLayerCPU()
{
// declarations for bunch of indexing parameters
int fn, sli, fmy, fmx, y, x;
int sum, ifmy, ifmx, ofmy, ofmx;
int filtIdx, inNeuIdx, outNeuIdx, outIdx;
int filtVol = FMDEPTH * FILTSIZE * FILTSIZE;
int filtArea = FILTSIZE * FILTSIZE;
int fmArea = FMSIZE *FMSIZE;
int outArea = FMSIZE/2 * FMSIZE/2;
cout << "convolutioning..." << endl;
// Convolution
for(fn = 0; fn < FILTNUM; fn++) //512
{
for(fmy = 0; fmy < FMSIZE; fmy += STRIDE) //32
{
for(fmx = 0; fmx < FMSIZE; fmx += STRIDE) //32
{
sum = 0;
for(sli = 0; sli < FMDEPTH; sli++) //512
{
for(y = 0; y < FILTSIZE; y++) //3
{
for(x = 0; x < FILTSIZE; x++) //3
{
ifmy = fmy - FILTSIZE / 2 + y; //no dependancy
ifmx = fmx - FILTSIZE / 2 + x; //no dependancy
filtIdx = (fn * filtVol) + (sli * filtArea) + (y * FILTSIZE) + x; //no dependancy
inNeuIdx = sli*fmArea + ifmy*FMSIZE + ifmx; //no dependancy
if(ifmy >= 0 && ifmy < FMSIZE && ifmx >= 0 && ifmx < FMSIZE)
sum += filt[filtIdx] * inNeu[inNeuIdx];
}
}
}
// Activation - ReLU
outNeuIdx = fn*fmArea + fmy*FMSIZE + fmx;
if(sum <= 0)
outNeu[outNeuIdx] = 0;
else
outNeu[outNeuIdx] = sum;
}
}
}
cout << "Pooling....." << endl;
// Max Pooling with Window Size 2x2
int max, tmpVal;
for(sli = 0; sli < FILTNUM; sli++)
{
for(fmy = 0; fmy < FMSIZE/2 ; fmy += 1)
{
for(fmx = 0; fmx < FMSIZE/2 ; fmx += 1)
{
outNeuIdx = sli*fmArea + fmy*2*FMSIZE + fmx*2;
max = outNeu[outNeuIdx];
for(y = 0; y < 2; y++)
{
for(x = 0; x < 2; x++)
{
ofmy = fmy*2 + y;
ofmx = fmx*2 + x;
outNeuIdx = sli*fmArea + ofmy*FMSIZE + ofmx;
tmpVal = outNeu[outNeuIdx];
if(tmpVal > max)
max = tmpVal;
}
}
outIdx = sli*outArea + fmy*FMSIZE/2 + fmx;
outCPU[outIdx] = max;
}
}
}
}
void initGPU()
{
int outNeuVol = FILTNUM * FMSIZE * FMSIZE; //512x32x32
int outPolVol = FILTNUM * FMSIZE/2 * FMSIZE/2; //512x16x16
int filtTensorVol = sizeof(short)*FILTNUM*FMDEPTH*FILTSIZE*FILTSIZE; //512x512x3x3
int inNeuVol = sizeof(short)*FMDEPTH*FMSIZE*FMSIZE; //512x32x32
cudaMalloc(&devoutNeu, sizeof(int)*outNeuVol);
cudaMalloc(&devPooling, sizeof(int)*outPolVol);
cudaMalloc(&devFilt, filtTensorVol);
cudaMalloc(&devinNeu, inNeuVol);
cudaMalloc(&devGlobalBarrier,sizeof(int)*outNeuVol);
cudaMemcpy(devFilt, filt, filtTensorVol, cudaMemcpyHostToDevice);
cudaMemcpy(devinNeu, inNeu, inNeuVol, cudaMemcpyHostToDevice);
//cudaMemcpy(devoutNeu, outNeu,sizeof(int)*outNeuVol, cudaMemcpyHostToDevice ); // debug for race outNeu
}
/*** Implement your CUDA Kernel here ***/
__global__
void convLayerGPU(short *FILT, short *InNeu, int *GlobalBarrier, int *outNeural, int *outPooling)
{
int threadX = threadIdx.x + blockIdx.x * blockDim.x;
int threadY = threadIdx.y + blockIdx.y * blockDim.y;
int threadZ = threadIdx.z + blockIdx.z * blockDim.z;
//int xall = blockDim.x * gridDim.x;
//int yall = blockDim.y * gridDim.y;
//int GlobalThreadId = threadX + threadY * xall + threadZ * xall * yall;
//int GlobalBlockId = blockIdx.x + blockIdx.y * gridDim.x + blockIdx.z * gridDim.y * gridDim.x;
int sli,y, x;
int ifmy, ifmx;
int filtIdx, inNeuIdx, outNeuIdx;
int filtVol = 4608; //512x3x3
int filtArea = 9; //3x3
int fmArea = 1024; //32x32
int outArea = 256; //32/2*32/2
int sum = 0;
for(sli = 0; sli < 512; sli++) //512
{
for(y = 0; y < 3; y++) //3
{
for(x = 0; x < 3; x++) //3
{
ifmy = threadY - 3 / 2 + y; //no dependancy
ifmx = threadZ - 3 / 2 + x; //no dependancy
filtIdx = (threadX * filtVol) + (sli * filtArea) + (y * 3) + x; //no dependancy
inNeuIdx = sli * fmArea + ifmy * 32 + ifmx; //no dependancy
if(ifmy >= 0 && ifmy < 32 && ifmx >= 0 && ifmx < 32)
sum += FILT[filtIdx] * InNeu[inNeuIdx];
}
}
}
// Activation - ReLU
outNeuIdx = threadX * fmArea + threadY*32 + threadZ;
//GlobalBarrier[outNeuIdx] = 0;
if(sum <= 0)
outNeural[outNeuIdx] = 0;
else
outNeural[outNeuIdx] = sum;
}
__global__
void MaxPoolingGPU(int *outNeural, int *outPooling) // Max Pooling with Window Size 2x2
{
int threadX = threadIdx.x + blockIdx.x * blockDim.x;
int threadY = threadIdx.y + blockIdx.y * blockDim.y;
int threadZ = threadIdx.z + blockIdx.z * blockDim.z;
//int xall = blockDim.x * gridDim.x;
//int yall = blockDim.y * gridDim.y;
//int GlobalThreadId = threadX + threadY * xall + threadZ * xall * yall;
//int GlobalBlockId = blockIdx.x + blockIdx.y * gridDim.x + blockIdx.z * gridDim.y * gridDim.x;
int max, tmpVal, outNeuIdx, x, y;
int fmArea = 1024;
int outArea = 256;
int ofmy, ofmx, outIdx; // pooling varable
outNeuIdx = threadX*fmArea + threadY*2*32 + threadZ*2;
max = outNeural[outNeuIdx];
for(y = 0; y < 2; y++)
{
for(x = 0; x < 2; x++)
{
ofmy = threadY*2 + y;
ofmx = threadZ*2 + x;
outNeuIdx = threadX*fmArea + ofmy*32 + ofmx;
tmpVal = outNeural[outNeuIdx];
if(tmpVal > max)
max = tmpVal;
}
}
outIdx = threadX*outArea + threadY*32/2 + threadZ;
outPooling[outIdx] = max;
}
int main()
{
float convLayerCPUExecTime, convLayerGPUExecTime;
init();
initCoo();
timespec time_begin, time_end;
clock_gettime(CLOCK_REALTIME, &time_begin);
convLayerCPU();
clock_gettime(CLOCK_REALTIME, &time_end);
convLayerCPUExecTime = timespec_diff_us(time_begin, time_end);
cout << " ================ Result ===================" << endl;
cout << "CPU time for executing a typical convolutional layer = " << convLayerCPUExecTime / 1000 << "ms" << endl;
initGPU();
dim3 threadPerBlock(xThreadDim, yThreadDim, zThreadDim);
dim3 numBlocks(xDim/xThreadDim, yDim/yThreadDim, zDim/zThreadDim);
dim3 Pool_threadPerBlock(xThreadDim, yThreadDim, zThreadDim);
dim3 Pool_numBlocks(Pool_xDim/xThreadDim, Pool_yDim/yThreadDim, Pool_zDim/zThreadDim);
clock_gettime(CLOCK_REALTIME, &time_begin);
convLayerGPU<<<numBlocks,threadPerBlock>>>(devFilt, devinNeu, devGlobalBarrier, devoutNeu, devPooling);
//cudaDeviceSynchronize();
MaxPoolingGPU<<<Pool_numBlocks , Pool_threadPerBlock>>>(devoutNeu, devPooling);
cudaDeviceSynchronize();
clock_gettime(CLOCK_REALTIME, &time_end);
convLayerGPUExecTime = timespec_diff_us(time_begin, time_end);
cout << "GPU time for executing a typical convolutional layer = " << convLayerGPUExecTime / 1000 << "ms" << endl;
int outSize = sizeof(int)*outputsize;
int OutSize = sizeof(int)*Outputsize;
cudaMemcpy(outGPU, devPooling, outSize, cudaMemcpyDeviceToHost);
cudaMemcpy(outGlobalBarrier, devGlobalBarrier,OutSize, cudaMemcpyDeviceToHost );
//int OutSize = sizeof(int)*Outputsize;
//cudaMemcpy(outResult_neu, devoutNeu, OutSize, cudaMemcpyDeviceToHost);
//printf("BarrierSum = %d\n",outGlobalBarrier[0] );
//printf("count = %d\n",outGlobalBarrier[1] );
// check the Output of Neu
/*for (int i = 0; i < 512*32*32; ++i)
if (outNeu[i] == outResult_neu[i])
{
printf("wrong at = %d \n", i);
break;
}
printf("PASS!!!\n");*/
// check the Output of GPU
/*for (int i = 0; i < 512*16*16; ++i)
if (outCPU[i] != outGPU[i])
{
printf("wrong at = %d \n", i);
break;
}
printf("PASS!!!\n");*/
if(checker())
{
cout << "Congratulations! You pass the check." << endl;
cout << "Speedup: " << (float)convLayerCPUExecTime / convLayerGPUExecTime << endl;
}
else
cout << "Sorry! Your result is wrong." << endl;
cout << "=====================================================" << endl;
cudaFree(&devoutNeu);
cudaFree(&devPooling);
cudaFree(&devFilt);
cudaFree(&devinNeu);
delete [] outResult;
delete [] outResult_neu;
ending();
return 0;
}