forked from upenn-acg/cis6010
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcugemm.cu
More file actions
566 lines (479 loc) · 19.2 KB
/
cugemm.cu
File metadata and controls
566 lines (479 loc) · 19.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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/*TODO: before you submit on Canvas, include here:
1) GPU Using: GeForce RTX 2060
2) Final performance: Average elapsed time: (0.050167) s, performance: ( 342.45) GFLOPS. size: (2048).
--------------------------------------------------HOMEWORK 1: ------------------------------------------------
In Homework 1, even though I fixed coalesced memory access, the performance was still low. I followed the
instruction in transpose.cu and used a shared memory bank but still the performance was lower than expected in
homework 1 hint. Here are the specification of my GPU:
Name: NVIDIA GeForce RTX 2060
Compute capability: 7.5
MultiProcessor (SM) count: 30
Warp size: 32
Max threads per block: 1024
Max threads per SM: 1024
Max threads dim: (1024, 1024, 64)
Max grid size: (2147483647, 65535, 65535)
From Nsight Compute, I located the Global Load & Store Sectors/Request (ld) for coalesced implementation and the value is 4.0, in comparison
the value for uncoalesced implementation is 16.5. So I believe the memory access is coalesced.
--------------------------------------------------HOMEWORK 2: ------------------------------------------------
I just realized that I have done the shared memory part in homework 1. I copied the code from above and repeated
the experiment, the final performance I got is listed below:
>>>> Average elapsed time: (0.044725) s, performance: ( 384.12) GFLOPS. size: (2048).
This time, I checked my DRAM bandwidth from nvidia-smi. It returned the following:
>>>> NVIDIA GeForce RTX 2060, 405 MHz, 5501 MHz
We can use the equation to calculate an approximated theoretical max performance:
Bandwidth = (busWidth(bits)/8) * memClock(Hz) * 2E-9 = 264GB/s
Theoretical FLOPS = (4 * F^2) * BW = 4 * 32^2 * 264E-9 = 1.07 TFLOPS
My performance is still lower than theoretical max performance, so I tried a bigger matrix side:
>>>> Average elapsed time: (0.179142) s, performance: ( 767.21) GFLOPS. size: (4096).
Therefore, I suspect that matrix size of 2048 is not big enough to amortize kernel or per-tile overhead.
*/
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <random>
#include <cublas_v2.h>
#include <cuda_runtime.h>
#include <cuda_fp16.h>
// from https://github.com/jarro2783/cxxopts
#include "cxxopts.hpp"
#define cudaCheck(err) (cudaErrorCheck(err, __FILE__, __LINE__))
#define cublasCheck(err) (cublasErrorCheck(err, __FILE__, __LINE__))
#define ROUND_UP_TO_NEAREST(M, N) (((M) + (N)-1) / (N))
enum Algo
{
cublas = 0,
basic,
gmem_coalesced,
smem,
smem_multioutput,
numAlgos
};
const char *algo2str(Algo a)
{
switch (a)
{
case cublas:
return "cublas";
case basic:
return "basic";
case gmem_coalesced:
return "gmem_coalesced";
case smem:
return "sharedmem";
case smem_multioutput:
return "sharedmem_multioutput";
default:
return "INVALID";
}
}
void cudaErrorCheck(cudaError_t error, const char *file, int line);
void cublasErrorCheck(cublasStatus_t status, const char *file, int line);
void randomize_matrix(float *mat, int N);
void const_init_matrix(float *mat, int N, float F);
bool verify_matrix(float *expected, float *actual, int M, int N);
void print_matrix(const float *A, int M, int N, std::ostream &outs);
void runAlgo(Algo algo, cublasHandle_t handle, int M, int N, int K, float alpha, float *A, float *B, float beta, float *C);
void runCublas(cublasHandle_t handle, int M, int N, int K, float alpha, float *A, float *B, float beta, float *C);
const std::string errLogFile = "gemmValidationFailure.txt";
// NB: must use a single generator to avoid duplicates
std::default_random_engine generator(2);
std::uniform_real_distribution<float> distribution(0, 1);
// Variables defined while completing homework questions
const int TILE_N = 64; // columns per block
const int BLOCK_ROWS = 16; // rows per block
int main(int argc, char **argv)
{
// command-line flags
cxxopts::Options options("gemm.cu", "CUDA GEMM kernels");
options.add_options()("size", "matrix size (N x N)", cxxopts::value<uint16_t>()->default_value("128")) //
("reps", "repeat GEMM this many times", cxxopts::value<uint16_t>()->default_value("1")) //
("algo", "GEMM algorithm to use, a number in [0,4], 0 is cuBLAS", cxxopts::value<uint16_t>()->default_value("0")) //
("validate", "Validate output against cuBLAS", cxxopts::value<bool>()->default_value("true")) //
("rngseed", "PRNG seed", cxxopts::value<uint>()->default_value("2")) //
("h,help", "Print usage");
auto clFlags = options.parse(argc, argv);
if (clFlags.count("help"))
{
std::cout << options.help() << std::endl;
exit(0);
}
const uint16_t SIZE = clFlags["size"].as<uint16_t>();
if (SIZE % 32 != 0)
{
//std::cout << "--size must be a multiple of 32" << std::endl;
//exit(EXIT_FAILURE);
}
const uint16_t REPS = clFlags["reps"].as<uint16_t>();
const Algo ALGO = static_cast<Algo>(clFlags["algo"].as<uint16_t>());
if (ALGO >= numAlgos)
{
printf("Invalid algorithm: %d\n", ALGO);
exit(EXIT_FAILURE);
}
const bool VALIDATE = clFlags["validate"].as<bool>();
const uint SEED = clFlags["rngseed"].as<uint>();
generator.seed(SEED);
printf("Multiplying two %u x %u matrices with %u trials using %s algorithm\n", SIZE, SIZE, REPS, algo2str(ALGO));
cudaCheck(cudaSetDevice(0));
// Setup cublas
cublasHandle_t handle;
cublasCheck(cublasCreate(&handle));
// Using cudaEvent for gpu stream timing, cudaEvent is equivalent to
// publishing event tasks in the target stream
cudaEvent_t beg, end;
cudaCheck(cudaEventCreate(&beg));
cudaCheck(cudaEventCreate(&end));
uint16_t m = SIZE, n = SIZE, k = SIZE;
// GEMM computes C = α*AB+β*C
// just do pure A*B (for simpler debugging)
float alpha = 1.0, beta = 1.0, initC = 1.0;
float *A = nullptr, *B = nullptr, *C = nullptr, *C_ref = nullptr; // host matrices
float *dA = nullptr, *dB = nullptr, *dC = nullptr, *dC_ref = nullptr; // device matrices
A = (float *)malloc(sizeof(float) * SIZE * SIZE);
B = (float *)malloc(sizeof(float) * SIZE * SIZE);
C = (float *)malloc(sizeof(float) * SIZE * SIZE);
C_ref = (float *)malloc(sizeof(float) * SIZE * SIZE);
randomize_matrix(A, SIZE * SIZE);
randomize_matrix(B, SIZE * SIZE);
randomize_matrix(C, SIZE * SIZE);
const_init_matrix(C, SIZE * SIZE, initC);
// print_matrix(A, SIZE, SIZE, std::cout);
// print_matrix(B, SIZE, SIZE, std::cout);
// print_matrix(C, SIZE, SIZE, std::cout);
cudaCheck(cudaMalloc((void **)&dA, sizeof(float) * SIZE * SIZE));
cudaCheck(cudaMalloc((void **)&dB, sizeof(float) * SIZE * SIZE));
cudaCheck(cudaMalloc((void **)&dC, sizeof(float) * SIZE * SIZE));
cudaCheck(cudaMalloc((void **)&dC_ref, sizeof(float) * SIZE * SIZE));
cudaCheck(cudaMemcpy(dA, A, sizeof(float) * SIZE * SIZE, cudaMemcpyHostToDevice));
cudaCheck(cudaMemcpy(dB, B, sizeof(float) * SIZE * SIZE, cudaMemcpyHostToDevice));
cudaCheck(cudaMemcpy(dC, C, sizeof(float) * SIZE * SIZE, cudaMemcpyHostToDevice));
cudaCheck(cudaMemcpy(dC_ref, C, sizeof(float) * SIZE * SIZE, cudaMemcpyHostToDevice));
printf("dimensions(m=n=k) %u, alpha: %f, beta: %f\n", m, alpha, beta);
// Verify the correctness of the calculation, and execute it once before the
// kernel function timing to avoid cold start errors
if (!VALIDATE)
{
printf("disabled validation\n");
}
else
{
// run cublas to get correct answer in dC_ref
runCublas(handle, m, n, k, alpha, dA, dB, beta, dC_ref);
// run user's algorithm, filling in dC
runAlgo(ALGO, handle, m, n, k, alpha, dA, dB, beta, dC);
cudaCheck(cudaDeviceSynchronize());
// copy both results back to host
cudaMemcpy(C, dC, sizeof(float) * m * n, cudaMemcpyDeviceToHost);
cudaMemcpy(C_ref, dC_ref, sizeof(float) * m * n, cudaMemcpyDeviceToHost);
if (verify_matrix(C_ref, C, n, m))
{
printf("Validated successfully!\n");
}
else
{
printf("Failed validation against NVIDIA cuBLAS.\n");
std::cout << " Logging faulty output into " << errLogFile << "\n";
std::ofstream fs;
fs.open(errLogFile, std::ios::out | std::ios::trunc);
fs << "α=" << alpha << " β=" << beta << std::endl;
fs << "C matrix initialized to " << initC << std::endl << std::endl;
fs << "A:" << std::endl;
print_matrix(A, m, n, fs);
fs << "B:" << std::endl;
print_matrix(B, m, n, fs);
fs << "C:" << std::endl;
print_matrix(C, m, n, fs);
fs << "Expected:" << std::endl;
print_matrix(C_ref, m, n, fs);
fs.close();
exit(EXIT_FAILURE);
}
}
// timing run(s)
cudaEventRecord(beg);
for (int j = 0; j < REPS; j++)
{
// We don't reset dC between runs to save time
runAlgo(ALGO, handle, m, n, k, alpha, dA, dB, beta, dC);
cudaCheck(cudaDeviceSynchronize());
}
// TODO: measure timing without memory transfers?
cudaCheck(cudaEventRecord(end));
cudaCheck(cudaEventSynchronize(beg));
cudaCheck(cudaEventSynchronize(end));
float elapsed_time;
cudaCheck(cudaEventElapsedTime(&elapsed_time, beg, end));
elapsed_time /= 1000.; // Convert to seconds
double flops = (double)2 * m * n * k;
printf(
"Average elapsed time: (%7.6f) s, performance: (%7.2f) GFLOPS. size: (%u).\n",
elapsed_time / REPS,
(REPS * flops * 1e-9) / elapsed_time,
m);
// free CPU and GPU memory
free(A);
free(B);
free(C);
free(C_ref);
cudaCheck(cudaFree(dA));
cudaCheck(cudaFree(dB));
cudaCheck(cudaFree(dC));
cudaCheck(cudaFree(dC_ref));
cublasCheck(cublasDestroy(handle));
return 0;
}
/** Function to check for errors in CUDA API calls */
void cudaErrorCheck(cudaError_t error, const char *file, int line)
{
if (error != cudaSuccess)
{
printf("[CUDA ERROR] at file %s:%d:\n%s: %s\n", file, line,
cudaGetErrorName(error), cudaGetErrorString(error));
exit(EXIT_FAILURE);
}
};
void cublasErrorCheck(cublasStatus_t status, const char *file, int line)
{
if (status != CUBLAS_STATUS_SUCCESS)
{
printf("[CUDA ERROR] at file %s:%d:\n %s: %s\n", file, line,
cublasGetStatusName(status), cublasGetStatusString(status));
exit(EXIT_FAILURE);
}
}
/** Initialize the given matrix `mat` which has `N` contiguous values. Contents of `mat` are set to random values. */
void randomize_matrix(float *mat, int N)
{
for (int i = 0; i < N; i++)
{
mat[i] = distribution(generator);
}
}
void const_init_matrix(float *mat, int N, float F)
{
for (int i = 0; i < N; i++)
{
mat[i] = F;
}
}
/** Print the given MxN matrix `mat` to the provided output stream. */
void print_matrix(const float *A, int M, int N, std::ostream &outs)
{
outs << "[";
for (int i = 0; i < M * N; i++)
{
if ((i + 1) % N == 0)
{
outs << std::fixed << std::setprecision(3) << A[i];
}
else
{
outs << std::fixed << std::setprecision(3) << A[i] << ", ";
}
if ((i + 1) % N == 0)
{
if (i + 1 < M * N)
outs << ";" << std::endl;
}
}
outs << "]" << std::endl << std::endl;
}
bool verify_matrix(float *expected, float *actual, int M, int N)
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
float fexp = (expected[(i * N) + j]);
float fact = (actual[(i * N) + j]);
double diff = std::fabs(fexp - fact);
if (diff > 0.002)
{
printf("Divergence! Should be %5.3f, is %5.3f (diff %5.3f) at [%d,%d]\n",
fexp, fact, diff, i, j);
return false;
}
}
}
return true;
}
void runCublas(cublasHandle_t handle, int M, int N, int K, float alpha,
float *A, float *B, float beta, float *C)
{
// cuBLAS uses *column-major* order. So we change the order of our row-major A &
// B, since (B^T*A^T)^T = (A*B)
// cublasStatus_t ok = cublasGemmEx(handle, CUBLAS_OP_N, CUBLAS_OP_N, N, M, K, &alpha, B, CUDA_R_16F,
// N, A, CUDA_R_16F, K, &beta, C, CUDA_R_16F, N, /*CUBLAS_COMPUTE_16F*/ CUBLAS_COMPUTE_16F_PEDANTIC,
// CUBLAS_GEMM_DEFAULT);
cublasStatus_t ok = cublasSgemm(handle, CUBLAS_OP_N, CUBLAS_OP_N, N, M, K, &alpha, B, N, A, K, &beta, C, N);
cublasCheck(ok);
}
__global__ void runBasic(int M, int N, int K, float alpha, float *A, float *B, float beta, float *C)
{
const unsigned x = blockIdx.x * blockDim.x + threadIdx.x;
const unsigned y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < M && y < N)
{
float tmp = 0.0;
// C = α*(AxB)+β*C
for (int i = 0; i < K; ++i)
{
// tmp += __A__[x][i] * __B__[i][y]
tmp += A[(x * K) + i] * B[(i * N) + y];
}
// __C__[x][y]
C[(x * N) + y] = (alpha * tmp) + (beta * C[x * N + y]);
}
}
__global__ void runGmemCoalesced(int M, int N, int K, float alpha, float *A, float *B, float beta, float *C)
{
// HW1 TODO: copy runBasic() code here and update to avoid uncoalesced accesses to global memory.
// Note, you are also free to change the grid dimensions in the kernel launch below.
// Create a shared memory to hold input tiles
__shared__ float As[BLOCK_ROWS][TILE_N + 1];
__shared__ float Bs[TILE_N][TILE_N + 1];
const int col = blockIdx.x * TILE_N + threadIdx.x;
const int row = blockIdx.y * BLOCK_ROWS + threadIdx.y;
if (row >= M || col >= N) return;
float acc = 0;
// Tile K in chunks of TILE_N
for (int k0 = 0; k0 < K; k0 += TILE_N) {
// Threads to load multiple rows of A and B
int aCol = k0 + threadIdx.x;
if (aCol < K) {
As[threadIdx.y][threadIdx.x] = A[row * K + aCol];
} else {
As[threadIdx.y][threadIdx.x] = 0;
}
for (int kk = threadIdx.y; kk < TILE_N; kk += BLOCK_ROWS) {
int bRow = k0 + kk;
if (bRow < K) {
Bs[kk][threadIdx.x] = B[bRow * N + col];
} else {
Bs[kk][threadIdx.x] = 0;
}
}
__syncthreads();
// Compute partial product
for (int k = 0; k < TILE_N; ++k) {
acc += As[threadIdx.y][k] * Bs[k][threadIdx.x];
}
__syncthreads();
}
// Write back
const int idx = row * N + col;
C[idx] = alpha * acc + beta * C[idx];
}
const uint F = 32;
__global__ void runSharedMem(int M, int N, int K, float alpha, float *A, float *B, float beta, float *C)
{
// HW2 TODO: Use shared memory to cache square FxF tiles of the A and B matrices in shared memory
// (SA and SB, respectively, provided below). Each thread should compute the result for one cell
// of the output matrix C.
// Note, you will also need to change the grid dimensions in the kernel launch below to take into account the value
// of F (which is a constant, defined above). You should experiment with different values of F to see how it
// affects performance.
__shared__ float SA[F][F];
__shared__ float SB[F][F];
const int col = blockIdx.x * F + threadIdx.x;
const int row = blockIdx.y * F + threadIdx.y;
if (row >= M || col >= N) return;
float acc = 0;
for (int k0 = 0; k0 < K; k0 += F) {
// Threads to load multiple rows of A and B
int aCol = k0 + threadIdx.x;
if (aCol < K) {
SA[threadIdx.y][threadIdx.x] = A[row * K + aCol];
} else {
SA[threadIdx.y][threadIdx.x] = 0;
}
for (int kk = threadIdx.y; kk < F; kk += F) {
int bRow = k0 + kk;
if (bRow < K) {
SB[kk][threadIdx.x] = B[bRow * N + col];
} else {
SB[kk][threadIdx.x] = 0;
}
}
__syncthreads();
// Compute partial product
for (int k = 0; k < F; ++k) {
acc += SA[threadIdx.y][k] * SB[k][threadIdx.x];
}
__syncthreads();
}
// Write back
const int idx = row * N + col;
C[idx] = alpha * acc + beta * C[idx];
}
const uint G = 4;
__global__ void runSharedMemMultiOutput(int M, int N, int K, float alpha, float *A, float *B, float beta, float *C)
{
// HW3 TODO: Copy your runSharedMem() code here and update it so that each thread computes the result for GxG cells
// of the output matrix C. Each thread should accumulate temporary results in the local LC matrix, provided below,
// before writing them to C in global memory.
// Note, you will also need to change the grid dimensions in the kernel launch below. You should experiment
// with different values of F and G to see how they affect performance.
__shared__ float SA[F][F];
__shared__ float SB[F][F];
float LC[G][G] = {0.0};
}
void runAlgo(Algo algo, cublasHandle_t handle, int M, int N, int K, float alpha,
float *A, float *B, float beta, float *C)
{
switch (algo)
{
case cublas:
runCublas(handle, M, N, K, alpha, A, B, beta, C);
break;
case basic:
{
dim3 gridDim(ROUND_UP_TO_NEAREST(M, 32), ROUND_UP_TO_NEAREST(N, 32));
dim3 blockDim(32, 32);
runBasic<<<gridDim, blockDim>>>(M, N, K, alpha, A, B, beta, C);
break;
}
case gmem_coalesced:
{
dim3 threads(TILE_N, BLOCK_ROWS);
dim3 blocks((N + TILE_N - 1) / TILE_N,
(M + BLOCK_ROWS - 1) / BLOCK_ROWS);
runGmemCoalesced<<<blocks, threads>>>(M, N, K, alpha, A, B, beta, C);
break;
}
case smem:
{
assert(0 == M % F);
assert(0 == N % F);
assert(0 == K % F);
// TODO: update your grid here
dim3 threads(F, F);
dim3 blocks((N + F - 1) / F,
(M + F - 1) / F);
runSharedMem<<<blocks, threads>>>(M, N, K, alpha, A, B, beta, C);
break;
}
case smem_multioutput:
{
assert(0 == M % F);
assert(0 == N % F);
assert(0 == K % F);
assert(0 == F % G);
assert((F*F) / (G*G) >= F);
// TODO: update your grid here
dim3 gridDim(ROUND_UP_TO_NEAREST(M, 32), ROUND_UP_TO_NEAREST(N, 32));
dim3 blockDim(32, 32);
runSharedMemMultiOutput<<<gridDim, blockDim>>>(M, N, K, alpha, A, B, beta, C);
break;
}
default:
printf("Invalid algorithm: %d\n", algo);
exit(EXIT_FAILURE);
}
cudaCheck(cudaDeviceSynchronize()); // wait for kernel to finish
cudaCheck(cudaGetLastError()); // check for errors from kernel run
}