-
Notifications
You must be signed in to change notification settings - Fork 5
/
mult_kernels.cu
345 lines (268 loc) · 10.2 KB
/
mult_kernels.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
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
#include <curand.h>
#include "mult_kernels.h"
#include "zero_kernels.h"
#include <math.h>
#include <stdio.h>
#include "transpose_kernel.h"
#include "gen_gpu.h"
/***************MatMulKernel*****************/
__global__ void MatMulKernel(TYPE *out, TYPE *in, TYPE *a, const int matrixHeight, const int matrixWidth) {
// get variables for loop
// copy section of b into shared mem
// go through the threads vertically and sum them into a variable
// atomic add these variables to the corresponding c index
// looping is happening horizontally on the matrix
// BLOCK_WIDTH is again horizontal
// BLOCK_HEIGHT is going vertical
// n / BLOCK_WIDTH blocks horizontally
// m / BLOCK_HEIGHT block vertically
// get variables for loop
// variable for loop length: blockEltHeight
__shared__ int blockElt;
__shared__ int blockxInd;
__shared__ int blockyInd;
if (threadIdx.x == 0) {
if ((blockIdx.x + 1) * BLOCK_WIDTH <= matrixWidth)
blockElt = BLOCK_WIDTH;
else blockElt = matrixWidth % BLOCK_WIDTH;
blockxInd = blockIdx.x * BLOCK_WIDTH;
blockyInd = blockIdx.y * BLOCK_HEIGHT;
}
__syncthreads();
// copy section of b into shared mem
// use the first BLOCK_WIDTH of thread
__shared__ TYPE b[BLOCK_WIDTH];
if (threadIdx.x < blockElt)
b[threadIdx.x] = in[blockxInd + threadIdx.x];
__syncthreads();
// summing variable
TYPE cSum = (TYPE) 0;
int threadyInd = blockyInd + threadIdx.x;
// make sure we are inside the matrix verticallly
if (threadyInd < matrixHeight) {
// go through the threads vertically and sum them into a variable
for (int i=0; i<blockElt; i++)
// A col index : blockIdx.x * BLOCK_WIDTH + i : blockxInd + i
// A row index : blockIdx.y * BLOCK_HEIGHT + threadIdx.x : blockyInd + threadIdx.x : threadyInd
// B index : b[i]
// cSum = B index * ( A col index * matrixHeight + A row index)
cSum += b[i] * a[(blockxInd + i) * (matrixHeight) + (threadyInd)];
//printf("csum = %f\n", cSum);
// atomic add these variables to the corresponding c index
atomicAdd(out + threadyInd, cSum);
}
}
__global__ void MatMulKernelT(TYPE *out, TYPE *in, TYPE *a, const int matrixHeight, const int matrixWidth) {
// get variables for loop
// copy section of b into shared mem
// go through the threads vertically and sum them into a variable
// atomic add these variables to the corresponding c index
// looping is happening vertically on the matrix
// BLOCK_WIDTH is going vertical
// BLOCK_HEIGHT is going horizontal
// m / BLOCK_WIDTH blocks vertically
// n / BLOCK_HEIGHT block horizontally
// get variables for loop
// variable for loop length: blockElt
__shared__ int blockElt;
__shared__ int blockxInd;
__shared__ int blockyInd;
if (threadIdx.x == 0) {
if ((blockIdx.y + 1) * BLOCK_WIDTH <= matrixHeight)
blockElt = BLOCK_WIDTH;
else blockElt = matrixHeight % BLOCK_WIDTH;
blockxInd = blockIdx.x * BLOCK_HEIGHT;
blockyInd = blockIdx.y * BLOCK_WIDTH;
}
__syncthreads();
// copy section of b into shared mem
// use the first BLOCK_WIDTH of thread
__shared__ TYPE b[BLOCK_WIDTH];
if (threadIdx.x < blockElt)
b[threadIdx.x] = in[blockyInd + threadIdx.x];
__syncthreads();
// summing variable
TYPE cSum = (TYPE) 0;
int threadxInd = blockxInd + threadIdx.x;
// make sure we are inside the array horizontally
if (threadxInd < matrixWidth) {
// go through the threads vertically and sum them into a variable
for (int i=0; i<blockElt; i++)
// A col index : blockIdx.x * BLOCK_HEIGHT + threadIdx.x : blockxInd + threadIdx.x : threadxInd
// A row index : blockIdx.y * BLOCK_WIDTH + i : blockyInd + i
// B index : b[i]
// cSum = B index * ( A col index * matrixHeight + A row index)
cSum += b[i] * a[(threadxInd) * (matrixHeight) + (blockyInd + i)];
// atomic add these variables to the corresponding c index
atomicAdd(out + threadxInd , cSum);
//printf("el[%d%d;%d] csum = %f tot = %f\n", blockIdx.x, blockIdx.y, threadIdx.x, cSum, *(out + blockIdx.x * BLOCK_HEIGHT + threadIdx.x));
}
}
/***********createRandomMatrix***************/
void createRandomMatrix(TYPE *A, int size, int seed) {
float *d_A;
float *h_A = (float *) malloc (size * sizeof(float));
curandGenerator_t gen;
size_t size_d_A = size * sizeof(TYPE);
cudaMalloc((void **) &d_A, size_d_A);
curandCreateGenerator(&gen, CURAND_RNG_PSEUDO_DEFAULT);
curandSetPseudoRandomGeneratorSeed(gen, seed);
curandGenerateUniform(gen, d_A, size);
cudaMemcpy(h_A, d_A, size_d_A, cudaMemcpyDeviceToHost);
// for (int j = 0; j < 10; j++)
// printf("h_A[%d] = %l=f\n", j, 10* h_A[j]);
for (int j = 0; j < size; j++)
A[j] = h_A[j] / sqrt (size);
curandDestroyGenerator(gen);
cudaFree(d_A);
free(h_A);
}
float matVecMul (float * out, float * in, float * A, const int m, const int n)
{
// set up threading and blocking variables
cudaDeviceProp dp;
cudaGetDeviceProperties(&dp,0);
unsigned int max_threads_per_block = dp.maxThreadsPerBlock;
int threads_perblockm = min(m, max_threads_per_block);
dim3 threadsPerBlockm(threads_perblockm);
int num_blocksm = (int)ceil((float)m/(float)threads_perblockm);
dim3 numBlocksm(num_blocksm);
int blockCols = (int) ceil(n / (double) BLOCK_WIDTH);
int blockRows = (int) ceil(m / (double) BLOCK_HEIGHT);
dim3 dimBlock(BLOCK_HEIGHT);
dim3 dimGrid(blockCols, blockRows);
int sharedMem = 3 * sizeof (int) + BLOCK_WIDTH * sizeof (float);
// set up timing
cudaEvent_t start, stop;
float time;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start,0);
// execute kernels
zero_vector_float<<<numBlocksm, threadsPerBlockm>>>(out, m);
MatMulKernel<<<dimGrid, dimBlock, sharedMem>>>(out, in, A, m, n);
cudaThreadSynchronize();
cudaEventRecord(stop,0);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&time, start, stop);
cudaEventDestroy(start);
cudaEventDestroy(stop);
return time;
}
float matVecMulT (float * out, float * in, float * A, const int m, const int n)
{
// set up threading and blocking variables
cudaDeviceProp dp;
cudaGetDeviceProperties(&dp,0);
unsigned int max_threads_per_block = dp.maxThreadsPerBlock;
int threads_perblockn = min(n, max_threads_per_block);
dim3 threadsPerBlockn(threads_perblockn);
int num_blocksn = (int)ceil((float)n/(float)threads_perblockn);
dim3 numBlocksn(num_blocksn);
int blockCols = (int) ceil(n / (double) BLOCK_HEIGHT);
int blockRows = (int) ceil(m / (double) BLOCK_WIDTH);
dim3 dimBlock(BLOCK_HEIGHT);
dim3 dimGrid(blockCols, blockRows);
int sharedMem = 3 * sizeof (int) + BLOCK_WIDTH * sizeof (float);
// set up timing
cudaEvent_t start, stop;
float time;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start,0);
// execute kernels
zero_vector_float<<<numBlocksn, threadsPerBlockn>>>(out, n);
MatMulKernelT<<<dimGrid, dimBlock, sharedMem>>>(out, in, A, m, n);
cudaThreadSynchronize();
cudaEventRecord(stop,0);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&time, start, stop);
cudaEventDestroy(start);
cudaEventDestroy(stop);
return time;
}
float matVecMulTransposed(float * out, float * in, float * A, float * AT, const int m, const int n)
{
// set up threading and blocking variables
cudaDeviceProp dp;
cudaGetDeviceProperties(&dp,0);
unsigned int max_threads_per_block = dp.maxThreadsPerBlock;
int threads_perblockn = min(n, max_threads_per_block);
dim3 threadsPerBlockn(threads_perblockn);
int num_blocksn = (int)ceil((float)n/(float)threads_perblockn);
dim3 numBlocksn(num_blocksn);
int blockCols = (int) ceil(n / (double) BLOCK_HEIGHT);
int blockRows = (int) ceil(m / (double) BLOCK_WIDTH);
dim3 dimBlock(BLOCK_HEIGHT);
dim3 dimGridt(blockRows, blockCols);
int sharedMem = 3 * sizeof (int) + BLOCK_WIDTH * sizeof (float);
dim3 blocks((int)ceil (m / (float)BLOCK_DIM), (int) ceil(n / (float)BLOCK_DIM));
dim3 threads(BLOCK_DIM, BLOCK_DIM);
// set up timing
cudaEvent_t start, stop;
float time;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start,0);
// execute kernels
zero_vector_float<<<numBlocksn, threadsPerBlockn>>>(out, n);
transpose<<<blocks, threads>>>(AT, A, m, n);
MatMulKernel<<<dimGridt, dimBlock, sharedMem>>>(out, in, AT, n, m);
cudaThreadSynchronize();
cudaEventRecord(stop,0);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&time, start, stop);
cudaEventDestroy(start);
cudaEventDestroy(stop);
return time;
}
float matVecNaive (float * out, float * in, float * A, const int m, const int n) {
// set up threading and blocking variables
cudaDeviceProp dp;
cudaGetDeviceProperties(&dp,0);
unsigned int max_threads_per_block = dp.maxThreadsPerBlock;
int threads_perblockm = min(m, max_threads_per_block);
dim3 threadsPerBlockm(threads_perblockm);
int num_blocksm = (int)ceil((float)m/(float)threads_perblockm);
dim3 numBlocksm(num_blocksm);
// set up timing
cudaEvent_t start, stop;
float time;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start,0);
// execute kernel
gen_matvec <<< numBlocksm, threadsPerBlockm >>>((float*)A, (float*)in, (float*)out, m, n);
cudaThreadSynchronize();
cudaEventRecord(stop,0);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&time, start, stop);
cudaEventDestroy(start);
cudaEventDestroy(stop);
return time;
}
float matVecNaiveTrans (float * out, float * in, float * A, const int m, const int n) {
// set up threading and blocking variables
cudaDeviceProp dp;
cudaGetDeviceProperties(&dp,0);
unsigned int max_threads_per_block = dp.maxThreadsPerBlock;
int threads_perblockn = min(n, max_threads_per_block);
dim3 threadsPerBlockn(threads_perblockn);
int num_blocksn = (int)ceil((float)n/(float)threads_perblockn);
dim3 numBlocksn(num_blocksn);
// set up timing
cudaEvent_t start, stop;
float time;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start,0);
// execute kernel
gen_matvecT <<< numBlocksn, threadsPerBlockn >>>((float*)A, (float*)out, (float*)in, m, n);
cudaThreadSynchronize();
cudaEventRecord(stop,0);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&time, start, stop);
cudaEventDestroy(start);
cudaEventDestroy(stop);
return time;
}