-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconv_cuda.cu
More file actions
299 lines (251 loc) · 9.97 KB
/
conv_cuda.cu
File metadata and controls
299 lines (251 loc) · 9.97 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
#include <cuda.h>
#include <math.h>
#include <iostream>
using namespace std;
__global__
void convStandard(uint32_t* d_MATDIM, uint32_t* d_KERDIM, double* mat, double* ker, double* res) {
uint32_t MATDIM = d_MATDIM[0];
uint32_t KERDIM = d_KERDIM[0];
uint32_t threadID = blockIdx.x * blockDim.x + threadIdx.x;
if (threadID < (MATDIM-KERDIM+1)*(MATDIM-KERDIM+1)) {
uint32_t index = KERDIM/2 + (KERDIM/2) * MATDIM + (threadID / (MATDIM-KERDIM+1)) * MATDIM + threadID % (MATDIM-KERDIM+1);
double sum = 0.0;
for(int i = -((int32_t) KERDIM)/2; i < ((int32_t) KERDIM)/2+1; i++){
for(int j = -((int32_t) KERDIM)/2; j < ((int32_t) KERDIM)/2+1; j++){
sum += mat[index + i*MATDIM + j] * ker[(i+KERDIM/2) * KERDIM + (j+KERDIM/2)];
}
}
res[threadID] = sum;
}
}
__global__
void convBinW(uint32_t* d_MATDIM, uint32_t* d_KERDIM, double* mat, unsigned char* ker, double* res) {
uint32_t MATDIM = d_MATDIM[0];
uint32_t KERDIM = d_KERDIM[0];
uint32_t threadID = blockIdx.x * blockDim.x + threadIdx.x;
if (threadID < (MATDIM-KERDIM+1)*(MATDIM-KERDIM+1)) {
uint32_t index_mat = KERDIM/2 + (KERDIM/2) * MATDIM + (threadID / (MATDIM-KERDIM+1)) * MATDIM + threadID % (MATDIM-KERDIM+1);
double sum = 0.0;
for(int i = -((int32_t) KERDIM)/2; i < ((int32_t) KERDIM)/2+1; i++){
for(int j = -((int32_t) KERDIM)/2; j < ((int32_t) KERDIM)/2+1; j++){
uint32_t index_ker = (i+KERDIM/2) * KERDIM + (j+KERDIM/2);
if ((unsigned char)((unsigned char)(ker[index_ker/8] << (index_ker % 8)) >> 7) == 1)
sum += mat[index_mat + i*MATDIM + j];
else
sum -= mat[index_mat + i*MATDIM + j];
}
}
res[threadID] = sum;
}
}
__global__
void convBinWBinI(uint32_t* d_MATDIM, uint32_t* d_KERDIM, unsigned char* mat, unsigned char* ker, unsigned char* res) {
uint32_t MATDIM = d_MATDIM[0];
uint32_t KERDIM = d_KERDIM[0];
uint32_t xnor_number = 0;
unsigned char bit_counter = 0;
unsigned char pop_count = 0;
uint32_t threadID = blockIdx.x * blockDim.x + threadIdx.x;
if (threadID < (MATDIM-KERDIM+1)*(MATDIM-KERDIM+1)) {
uint32_t index_mat = KERDIM/2 + (KERDIM/2) * MATDIM + (threadID / (MATDIM-KERDIM+1)) * MATDIM + threadID % (MATDIM-KERDIM+1);
for(int i = -((int32_t) KERDIM)/2; i < ((int32_t) KERDIM)/2+1; i++){
for(int j = -((int32_t) KERDIM)/2; j < ((int32_t) KERDIM)/2+1; j++){
uint32_t index_ker = (i+(KERDIM >> 1)) * KERDIM + (j+(KERDIM >> 1));
uint32_t index_mat_bin = index_mat + i*MATDIM + j;
if (bit_counter == 32) {
pop_count += (unsigned char) __popc((unsigned int) xnor_number);
bit_counter = 0;
xnor_number = 0;
} else {
if (((ker[index_ker >> 3] << (index_ker % 8)) >> 7) == ((mat[index_mat_bin >> 3] << (index_mat_bin % 8)) >> 7))
xnor_number |= 1 << bit_counter;
bit_counter++;
}
}
}
pop_count += (unsigned char) __popc((unsigned int) xnor_number);
res[threadID] = pop_count;
}
}
void initMat(uint32_t dim, double* mat) {
for (uint32_t i = 0; i < dim; i++) {
for (uint32_t j = 0; j < dim; j++) {
mat[i*dim+j] = (double) rand() / RAND_MAX * 2.0 - 1.0;
}
}
}
void convertToBinary(uint32_t dim, double* mat, uint32_t size_bin, unsigned char* mat_bin) {
uint32_t pos_bit = 0;
uint32_t pos_byte = 0;
unsigned char sum = 0;
for (uint32_t i = 0; i < dim; i++) {
for (uint32_t j = 0; j < dim; j++) {
if (mat[i*dim+j] >= 0) {
sum += pow(2, 7-pos_bit);
}
if (pos_bit == 7) {
mat_bin[pos_byte] = sum;
pos_byte++;
sum = 0;
pos_bit = 0;
} else {
pos_bit++;
}
}
}
if (dim*dim % 8 != 0) {
mat_bin[pos_byte] = sum;
}
}
void printMatrix(uint32_t dim, double* mat) {
cout << "dim: " << dim << "x" << dim << "\n{\n";
for (uint32_t i = 0; i < dim; i++) {
for (uint32_t j = 0; j < dim; j++) {
cout << mat[i*dim+j] << ", ";
}
cout << '\n';
}
cout << "}\n";
}
void printBinary(uint32_t dim, uint32_t size_bin, unsigned char* mat) {
unsigned char rest;
for (uint32_t i = 0; i < size_bin; i++) {
rest = mat[i];
for (uint32_t j = 0; j < 8; j++) {
if (i * 8 + j == dim*dim) {
cout << "\n";
break;
}
if(rest - pow(2,7-j) >= 0) {
rest = rest - pow(2,7-j);
cout << "1 ";
} else {
cout << "0 ";
}
if((i * 8 + j + 1) % dim == 0) {
cout << "\n";
}
}
}
}
int main(int argc, char* argv[]) {
if (argc != 4) {
cout << "Usage: srun out <int: dimension of input matrix> <int: dimension of kernel> <blocksize>\n";
return 0;
}
uint32_t MATDIM = strtol(argv[1], NULL, 10);
uint32_t KERDIM = strtol(argv[2], NULL, 10);
uint32_t N = strtol(argv[3], NULL, 10);
uint32_t h_MATDIM[1];
h_MATDIM[0] = MATDIM;
uint32_t h_KERDIM[1];
h_KERDIM[0] = KERDIM;
struct timespec tstart={0,0}, tend={0,0};
double elapsed;
// Matrix (double)
double h_mat[MATDIM*MATDIM];
// Kernel (double)
double h_ker[KERDIM*KERDIM];
// Matrix (bits)
unsigned char h_mat_bin[(uint32_t) ceil(MATDIM*MATDIM/8.0)];
// Kernel (bits)
unsigned char h_ker_bin[(uint32_t) ceil(KERDIM*KERDIM/8.0)];
// Result of standard convolution
double h_res_standard[(MATDIM-KERDIM+1)*(MATDIM-KERDIM+1)];
// Result of convolution with binary weights
double h_res_binW[(MATDIM-KERDIM+1)*(MATDIM-KERDIM+1)];
// Result of convolution with binary weights and binary inputs
unsigned char h_res_binWbinI[(MATDIM-KERDIM+1)*(MATDIM-KERDIM+1)];
uint32_t mat_size = MATDIM*MATDIM * sizeof(double);
uint32_t ker_size = KERDIM*KERDIM * sizeof(double);
uint32_t mat_bin_size = (uint32_t) ceil(MATDIM*MATDIM/8.0) * sizeof(unsigned char);
uint32_t ker_bin_size = (uint32_t) ceil(KERDIM*KERDIM/8.0) * sizeof(unsigned char);
uint32_t res_standard_size = (MATDIM-KERDIM+1)*(MATDIM-KERDIM+1) * sizeof(double);
uint32_t res_binW_size = (MATDIM-KERDIM+1)*(MATDIM-KERDIM+1) * sizeof(double);
uint32_t res_binWbinI_size = (MATDIM-KERDIM+1)*(MATDIM-KERDIM+1) * sizeof(unsigned char);
// Pointers for allocation on device
uint32_t *d_MATDIM, *d_KERDIM;
double *d_mat, *d_ker, *d_res_standard, *d_res_binW;
unsigned char *d_mat_bin, *d_ker_bin, *d_res_binWbinI;
// Allocate all matrices on device (cudaFree later!)
cudaMalloc((void**) &d_mat, mat_size);
cudaMalloc((void**) &d_ker, ker_size);
cudaMalloc((void**) &d_mat_bin, mat_bin_size);
cudaMalloc((void**) &d_ker_bin, ker_bin_size);
cudaMalloc((void**) &d_res_standard, res_standard_size);
cudaMalloc((void**) &d_res_binW, res_binW_size);
cudaMalloc((void**) &d_res_binWbinI, res_binWbinI_size);
cudaMalloc((void**) &d_MATDIM, sizeof(uint32_t));
cudaMalloc((void**) &d_KERDIM, sizeof(uint32_t));
// Seed for random number generation
srand(time(NULL));
// Randomize the values of the double matrix with values -1.0 ... 1.0
initMat(MATDIM, h_mat);
// Convert the double matrix into binary (0 = -1, 1 = 1)
convertToBinary(MATDIM, h_mat, (uint32_t) ceil(MATDIM*MATDIM/8.0), h_mat_bin);
// TODO DEBUG: Print the double matrix.
printMatrix(MATDIM, h_mat);
// TODO DEBUG: Print the binary matrix.
printBinary(MATDIM, (uint32_t) ceil(MATDIM*MATDIM/8.0), h_mat_bin);
initMat(KERDIM, h_ker);
// Convert the double matrix into binary
convertToBinary(KERDIM, h_ker, (uint32_t) ceil(KERDIM*KERDIM/8.0), h_ker_bin);
// TODO DEBUG: Print the double matrix.
printMatrix(KERDIM, h_ker);
// TODO DEBUG: Print the binary matrix.
printBinary(KERDIM, (uint32_t) ceil(KERDIM*KERDIM/8.0), h_ker_bin);
// Copy all the matrices to the device (except the result matrices)
cudaMemcpy(d_mat, h_mat, mat_size, cudaMemcpyHostToDevice);
cudaMemcpy(d_ker, h_ker, ker_size, cudaMemcpyHostToDevice);
cudaMemcpy(d_mat_bin, h_mat_bin, mat_bin_size, cudaMemcpyHostToDevice);
cudaMemcpy(d_ker_bin, h_ker_bin, ker_bin_size, cudaMemcpyHostToDevice);
cudaMemcpy(d_MATDIM, h_MATDIM, sizeof(uint32_t), cudaMemcpyHostToDevice);
cudaMemcpy(d_KERDIM, h_KERDIM, sizeof(uint32_t), cudaMemcpyHostToDevice);
uint32_t grid_size = ceil((MATDIM-KERDIM+1) * (MATDIM-KERDIM+1) / ((double) N));
// Compute the different modes of convolution
clock_gettime(CLOCK_MONOTONIC, &tstart);
convStandard<<<grid_size, N>>>(d_MATDIM, d_KERDIM, d_mat, d_ker, d_res_standard);
clock_gettime(CLOCK_MONOTONIC, &tend);
elapsed = ((double)tend.tv_sec + 1.0e-9*tend.tv_nsec) - ((double)tstart.tv_sec + 1.0e-9*tstart.tv_nsec);
cout << "Standard convolution took " << elapsed << " seconds.\n";
clock_gettime(CLOCK_MONOTONIC, &tstart);
convBinW<<<grid_size, N>>>(d_MATDIM, d_KERDIM, d_mat, d_ker_bin, d_res_binW);
clock_gettime(CLOCK_MONOTONIC, &tend);
elapsed = ((double)tend.tv_sec + 1.0e-9*tend.tv_nsec) - ((double)tstart.tv_sec + 1.0e-9*tstart.tv_nsec);
cout << "Binary weights took " << elapsed << " nanoseconds.\n";
clock_gettime(CLOCK_MONOTONIC, &tstart);
convBinWBinI<<<grid_size, N>>>(d_MATDIM, d_KERDIM, d_mat_bin, d_ker_bin, d_res_binWbinI);
clock_gettime(CLOCK_MONOTONIC, &tend);
elapsed = ((double)tend.tv_sec + 1.0e-9*tend.tv_nsec) - ((double)tstart.tv_sec + 1.0e-9*tstart.tv_nsec);
cout << "Binary inputs and binary weights took " << elapsed << " nanoseconds.\n";
cout << elapsed << "\n";
// Fetch the results from device
cudaMemcpy(h_res_standard, d_res_standard, res_standard_size, cudaMemcpyDeviceToHost);
cudaMemcpy(h_res_binW, d_res_binW, res_binW_size, cudaMemcpyDeviceToHost);
cudaMemcpy(h_res_binWbinI, d_res_binWbinI, res_binWbinI_size, cudaMemcpyDeviceToHost);
// TODO DEBUG: Print the results
cout << "Standard convolution DOUBLExDOUBLE\n";
printMatrix(MATDIM-KERDIM+1, h_res_standard);
cout << "Binary weight convolution DOUBLExBITS\n";
printMatrix(MATDIM-KERDIM+1, h_res_binW);
cout << "Binary weights and binary inputs BITSxBITS\n";
printMatrix(MATDIM-KERDIM+1, (double*) h_res_binWbinI);
cout << "dim: " << MATDIM-KERDIM+1 << "x" << MATDIM-KERDIM+1 << "\n{\n";
for (uint32_t i = 0; i < MATDIM-KERDIM+1; i++) {
for (uint32_t j = 0; j < MATDIM-KERDIM+1; j++) {
cout << (uint32_t) h_res_binWbinI[i*(MATDIM-KERDIM+1)+j] << ", ";
}
cout << '\n';
}
cout << "}\n";
cudaFree(d_mat);
cudaFree(d_ker);
cudaFree(d_mat_bin);
cudaFree(d_ker_bin);
cudaFree(d_res_standard);
cudaFree(d_res_binW);
cudaFree(d_res_binWbinI);
cudaFree(d_MATDIM);
cudaFree(d_KERDIM);
return 0;
}