forked from upenn-acg/cis6010
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcugemm-hw4.cu
More file actions
481 lines (420 loc) · 16.3 KB
/
cugemm-hw4.cu
File metadata and controls
481 lines (420 loc) · 16.3 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
// TODO: before you submit on Canvas, include here:
// 1) which GPU you used and
// 2) what performance improvement you obtained over previous homework(s)
#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,
smem_multioutput_1stream,
smem_multioutput_multistream,
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";
case smem_multioutput_1stream:
return "sharedmem_multioutput_1stream";
case smem_multioutput_multistream:
return "sharedmem_multioutput_multistream";
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, uint NUM_STREAMS, float* hA, float* hB, float* hC);
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);
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,6], 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")) //
("streams", "number of CUDA streams to use", cxxopts::value<uint>()->default_value("1")) //
("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 uint NUM_STREAMS = clFlags["streams"].as<uint>();
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
cudaMallocHost(&A, sizeof(float) * SIZE * SIZE);
cudaMallocHost(&B, sizeof(float) * SIZE * SIZE);
cudaMallocHost(&C, sizeof(float) * SIZE * SIZE);
cudaMallocHost(&C_ref, 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, NUM_STREAMS, A, B, C);
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, NUM_STREAMS, A, B, C);
cudaCheck(cudaDeviceSynchronize());
}
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
cudaFreeHost(A);
cudaFreeHost(B);
cudaFreeHost(C);
cudaFreeHost(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.
}
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 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,
uint NUM_STREAMS, float *hA, float* hB, float* hC)
{
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 gridDim(ROUND_UP_TO_NEAREST(M, 32), ROUND_UP_TO_NEAREST(N, 32));
dim3 blockDim(32, 32);
runGmemCoalesced<<<gridDim, blockDim>>>(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 gridDim(ROUND_UP_TO_NEAREST(M, 32), ROUND_UP_TO_NEAREST(N, 32));
dim3 blockDim(32, 32);
runSharedMem<<<gridDim, blockDim>>>(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;
}
case smem_multioutput_1stream:
{
assert(0 == M % F);
assert(0 == N % F);
assert(0 == K % F);
assert(0 == F % G);
assert((F*F) / (G*G) >= F);
cudaCheck(cudaMemcpy(A, hA, sizeof(float) * M * K, cudaMemcpyHostToDevice));
cudaCheck(cudaMemcpy(B, hB, sizeof(float) * K * N, cudaMemcpyHostToDevice));
cudaCheck(cudaMemcpy(C, hC, sizeof(float) * M * N, cudaMemcpyHostToDevice));
// TODO: HW4: use same grid & kernel launch as HW3
cudaMemcpy(hC, C, sizeof(float) * M * N, cudaMemcpyDeviceToHost);
break;
}
case smem_multioutput_multistream:
{
assert(0 == M % F);
assert(0 == N % F);
assert(0 == K % F);
assert(0 == F % G);
assert((F*F) / (G*G) >= F);
assert(0 == (N/F) % NUM_STREAMS);
cudaStream_t streams[NUM_STREAMS];
for (int i = 0; i < NUM_STREAMS; ++i) {
cudaCheck(cudaStreamCreate(&streams[i]));
}
// TODO: HW4: use streams to overlap memory copies with kernel computation
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
}