This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathsgemm.cpp
480 lines (407 loc) · 14.3 KB
/
sgemm.cpp
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
// sgemm.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <cuda.h>
#include <cublas_v2.h>
CUcontext hContext = 0;
cublasHandle_t hCublas = 0;
float assemblySgemm(const char* kernel, CUarray_format format, size_t size, CUdeviceptr devC, CUdeviceptr devA, CUdeviceptr devB, int N, CUevent hStart, CUevent hStop, int repeat = 1, int printVars = 0);
float cublasSgemm(const char* kernel, CUdeviceptr devC, CUdeviceptr devA, CUdeviceptr devB, int N, CUevent hStart, CUevent hStop, int repeat);
void gflops(const char* ident, int N, float ms, int repeat);
void test(float* C, float* T, int N, size_t size);
#define REPEAT_BLOCK 2000
#define CUDA_CHECK( fn ) do { \
CUresult status = (fn); \
if ( CUDA_SUCCESS != status ) { \
const char* errstr; \
cuGetErrorString(status, &errstr); \
printf("CUDA Driver Failure (line %d of file %s):\n\t%s returned 0x%x (%s)\n", __LINE__, __FILE__, #fn, status, errstr); \
if (hCublas) cublasDestroy(hCublas); \
if (hContext) cuCtxDestroy(hContext); \
exit(EXIT_FAILURE); \
} \
} while (0)
#define CUBLAS_CHECK( fn ) do { \
cublasStatus_t status = (fn); \
if ( CUBLAS_STATUS_SUCCESS != status ) { \
printf("Cublas Failure (line %d of file %s):\n\t%s returned %d\n", __LINE__, __FILE__, #fn, status); \
if (hCublas) cublasDestroy(hCublas); \
if (hContext) cuCtxDestroy(hContext); \
exit(EXIT_FAILURE); \
} \
} while (0)
int main(int argc, char* argv[])
{
char deviceName[32];
int count, ordinal, major, minor;
CUdevice hDevice;
CUevent hStart, hStop;
CUdeviceptr devA, devB, devC, devT, otherDevA, otherDevB;
// Initialize the Driver API and find a device
CUDA_CHECK( cuInit(0) );
CUDA_CHECK( cuDeviceGetCount(&count) );
for (ordinal = 0; ordinal < count; ordinal++)
{
CUDA_CHECK( cuDeviceGet(&hDevice, ordinal) );
CUDA_CHECK( cuDeviceGetAttribute (&major, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR, hDevice) );
CUDA_CHECK( cuDeviceGetAttribute (&minor, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR, hDevice) );
CUDA_CHECK( cuDeviceGetName(deviceName, sizeof(deviceName), hDevice) );
if (major >= 5 && minor >= 2)
{
//printf("Using: Id:%d %s (%d.%d)\n\n", ordinal, deviceName, major, minor);
break;
}
}
if (ordinal == count)
{
printf("No compute 5.0 device found, exiting.\n");
exit(EXIT_FAILURE);
}
// First command line arg is the size of N divided by 128
int thread128 = 64;
if (argc > 1)
thread128 = atoi(argv[1]);
if (thread128 > 64 || thread128 < 1)
thread128 = 64;
// Second command line arg is the repeat count for benchmarking
int repeat = 1;
if (argc > 2)
repeat = atoi(argv[2]);
if (repeat > 10000 || repeat < 1)
repeat = 1;
// Third command line arg is the normalized float size
CUarray_format format = CU_AD_FORMAT_FLOAT;
if (argc > 3)
format = (CUarray_format)atoi(argv[3]);
if (format != CU_AD_FORMAT_FLOAT && format != CU_AD_FORMAT_UNSIGNED_INT16 && format != CU_AD_FORMAT_UNSIGNED_INT8)
format = CU_AD_FORMAT_FLOAT;
// Forth command line arg is for printf debugging
int printVars = 0;
if (argc > 4)
printVars = atoi(argv[4]);
if (printVars > 100 || printVars < 1)
printVars = 0;
int N = thread128 * 128;
float alpha = 1, beta = 0, ms = 1;
size_t sizeOther = N * N;
size_t sizeFloat = sizeOther * 4;
float* A = (float*)malloc(sizeFloat);
float* B = (float*)malloc(sizeFloat);
float* C = (float*)malloc(sizeFloat);
float* T = (float*)malloc(sizeFloat);
float *otherA, *otherB;
//int counter = 0;
//srand((unsigned int)time(0));
for(int i = 0; i < N * N; i++) //
{
//A[i] = (float)rand() / (float)RAND_MAX;
//B[i] = (float)rand() / (float)RAND_MAX;
A[i] = B[i] = 1.0f; // * (i & 3) + 1.0f;
//A[i] = 1.0f;
//B[i * N + counter++] = 1.0f; // identity matrix
}
if (format == CU_AD_FORMAT_FLOAT)
{
sizeOther *= 4;
otherA = A;
otherB = B;
}
else if (format == CU_AD_FORMAT_UNSIGNED_INT16)
{
sizeOther *= 2;
unsigned short* othera = (unsigned short*)malloc(sizeOther);
unsigned short* otherb = (unsigned short*)malloc(sizeOther);
for(int i = 0; i < N * N; i++)
othera[i] = otherb[i] = 65535;
otherA = reinterpret_cast<float*>(othera);
otherB = reinterpret_cast<float*>(otherb);
}
else // (format == CU_AD_FORMAT_UNSIGNED_INT8)
{
otherA = (float*)malloc(sizeOther);
otherB = (float*)malloc(sizeOther);
memset(otherA, 255, sizeOther);
memset(otherB, 255, sizeOther);
}
CUDA_CHECK( cuCtxCreate(&hContext, 0, hDevice) );
//CUBLAS_CHECK( cublasCreate(&hCublas) );
CUDA_CHECK( cuEventCreate(&hStart, CU_EVENT_BLOCKING_SYNC) ); // CU_EVENT_DEFAULT
CUDA_CHECK( cuEventCreate(&hStop, CU_EVENT_BLOCKING_SYNC) );
CUDA_CHECK( cuMemAlloc(&devA, sizeFloat) );
CUDA_CHECK( cuMemAlloc(&devB, sizeFloat) );
CUDA_CHECK( cuMemAlloc(&devC, sizeFloat) );
CUDA_CHECK( cuMemAlloc(&devT, sizeFloat) );
CUDA_CHECK( cuMemcpyHtoD(devA, A, sizeFloat) );
CUDA_CHECK( cuMemcpyHtoD(devB, B, sizeFloat) );
CUDA_CHECK( cuMemsetD8(devC, 0, sizeFloat) );
CUDA_CHECK( cuMemsetD8(devT, 0, sizeFloat) );
if (format == CU_AD_FORMAT_FLOAT)
{
otherDevA = devA;
otherDevB = devB;
}
else
{
CUDA_CHECK( cuMemAlloc(&otherDevA, sizeOther) );
CUDA_CHECK( cuMemAlloc(&otherDevB, sizeOther) );
CUDA_CHECK( cuMemcpyHtoD(otherDevA, otherA, sizeOther) );
CUDA_CHECK( cuMemcpyHtoD(otherDevB, otherB, sizeOther) );
}
// Warm up the clock (unless under nsight)
//if (!getenv("NSIGHT_LAUNCHED")) // NSIGHT_CUDA_ANALYSIS NSIGHT_CUDA_DEBUGGER
// for (int i = 0; i < 3; i++)
// CUBLAS_CHECK( cublasSgemm(hCublas, CUBLAS_OP_N, CUBLAS_OP_T, N, N, N, &alpha, reinterpret_cast<float*>(devA), N, reinterpret_cast<float*>(devB), N, &beta, reinterpret_cast<float*>(devT), N) );
// Launch our kernel
ms = assemblySgemm("sgemm_kernel_64", format, sizeOther, devC, otherDevA, otherDevB, N, hStart, hStop, repeat, printVars);
gflops("Max64 ", N, ms, repeat);
ms = assemblySgemm("sgemm_kernel_128", format, sizeOther, devC, otherDevA, otherDevB, N, hStart, hStop, repeat, printVars);
gflops("Max128", N, ms, repeat);
//ms = cublasSgemm("maxwell_sgemm_128x64_nt", devT, devA, devB, N, hStart, hStop, repeat);
//gflops("Cub64 ", N, ms, repeat);
//ms = cublasSgemm("maxwell_sgemm_128x128_nt", devT, devA, devB, N, hStart, hStop, repeat);
//gflops("Cub128", N, ms, repeat);
// Run cublas again for the same repeat count for comparison
//CUDA_CHECK( cuEventRecord(hStart, NULL) );
//for (int i = 0; i < repeat; i++)
// CUBLAS_CHECK( cublasSgemm(hCublas, CUBLAS_OP_N, CUBLAS_OP_T, N, N, N, &alpha, reinterpret_cast<float*>(devA), N, reinterpret_cast<float*>(devB), N, &beta, reinterpret_cast<float*>(devT), N) );
//CUDA_CHECK( cuEventRecord(hStop, NULL) );
//CUDA_CHECK( cuEventSynchronize(hStop) );
//CUDA_CHECK( cuEventElapsedTime(&ms, hStart, hStop) );
//gflops("Cublas", N, ms, repeat);
// Get back our results from each kernel
CUDA_CHECK( cuMemcpyDtoH(C, devC, sizeFloat) );
CUDA_CHECK( cuMemcpyDtoH(T, devT, sizeFloat) );
// Cleanup and shutdown of cuda
CUDA_CHECK( cuMemFree(devA) );
CUDA_CHECK( cuMemFree(devB) );
CUDA_CHECK( cuMemFree(devC) );
CUDA_CHECK( cuMemFree(devT) );
if (format != CU_AD_FORMAT_FLOAT)
{
CUDA_CHECK( cuMemFree(otherDevA) );
CUDA_CHECK( cuMemFree(otherDevB) );
}
CUDA_CHECK( cuEventDestroy(hStart) );
CUDA_CHECK( cuEventDestroy(hStop) );
//CUBLAS_CHECK( cublasDestroy(hCublas) );
//hCublas = 0;
CUDA_CHECK( cuCtxDestroy(hContext) );
hContext = 0;
// compare C and T for accuracy
test(C, T, N, sizeFloat);
// And free up host memory
free(A); free(B); free(C); free(T);
if (format != CU_AD_FORMAT_FLOAT)
{
free(otherA);
free(otherB);
}
return 0;
}
// Our kernel wrapper function
float assemblySgemm(const char* kernel, CUarray_format format, size_t size, CUdeviceptr devC, CUdeviceptr devA, CUdeviceptr devB, int N, CUevent hStart, CUevent hStop, int repeat, int printVars)
{
// Configure our x and y grid dimensions (assume nice square matrixes).
// Each block gets 128 tracks from A and 128 tracks from B.
// Each of the 256 threads calculates 64 elements of that 128x128 sub matrix of C.
// See Figure 2 here to get the gist of things (we use a different mapping to maximize LDS.128 usage):
// http://icl.cs.utk.edu/projectsfiles/magma/pubs/fermi_gemm.pdf
int threads, width;
if (strcmp(kernel, "sgemm_kernel_64") == 0)
{
threads = 64;
width = 64;
}
else
{
threads = 256;
width = 128;
}
int gridDimXY = N / width + (N % width != 0);
int blocks = gridDimXY * gridDimXY;
// Setup out debug printf output buffer
CUdeviceptr devD = NULL;
int* D = NULL;
int sizeD = 0;
if (printVars)
{
sizeD = blocks * threads * printVars * sizeof(int);
D = (int*)malloc(sizeD);
CUDA_CHECK( cuMemAlloc(&devD, sizeD) );
CUDA_CHECK( cuMemsetD8(devD, 0, sizeD) );
}
// Load the cubin
CUmodule hModule;
CUDA_CHECK( cuModuleLoad(&hModule, "sgemm.cubin") );
// Load the textures
CUtexref texA, texB;
CUDA_CHECK( cuModuleGetTexRef(&texA, hModule, "texA") );
CUDA_CHECK( cuModuleGetTexRef(&texB, hModule, "texB") );
// Configure the textures
CUDA_CHECK( cuTexRefSetFormat(texA, format, 4) );
CUDA_CHECK( cuTexRefSetFormat(texB, format, 4) );
CUDA_CHECK( cuTexRefSetAddress(NULL, texA, devA, size) );
CUDA_CHECK( cuTexRefSetAddress(NULL, texB, devB, size) );
// Load the kernel function
CUfunction hKernel;
CUDA_CHECK( cuModuleGetFunction(&hKernel, hModule, kernel) );
// Setup the params
float alpha = 1.0f;
void* params[] = { &devC, &N, &N, &N, &N, &N, &N, &alpha, &devD };
float totalTime = 0;
// Launch the kernel repeat times.. but break it up into pieces so as not to lock things up.
while (repeat > 0)
{
float ms;
int r = repeat > REPEAT_BLOCK ? REPEAT_BLOCK : repeat;
CUDA_CHECK( cuEventRecord( hStart, NULL ) );
for (int i = 0; i < r; i++)
CUDA_CHECK( cuLaunchKernel(hKernel, gridDimXY, gridDimXY, 1, threads, 1, 1, 0, 0, params, 0) );
CUDA_CHECK( cuEventRecord( hStop, NULL ) );
CUDA_CHECK( cuEventSynchronize( hStop ) );
CUDA_CHECK( cuEventElapsedTime( &ms, hStart, hStop ) );
totalTime += ms;
repeat -= r;
}
CUDA_CHECK( cuModuleUnload(hModule) );
// And here we print out the debug info if requested:
if (printVars)
{
CUDA_CHECK( cuMemcpyDtoH(D, devD, sizeD) );
CUDA_CHECK( cuMemFree(devD) );
int *iD = D;
float *fD = reinterpret_cast<float*>(D);
unsigned int *uD = reinterpret_cast<unsigned int*>(D);
for (int by = 0; by < gridDimXY; by++)
{
for (int bx = 0; bx < gridDimXY; bx++)
{
unsigned int clock = 0xffffffff, sm = 0;
for (int tid = 0; tid < threads; tid++)
{
//printf("by: %3d, bx: %3d, tid:%3d, rA:%5d, rB:%5d, wr:%5d, rd:%5d, cx:%5d, cy:%5d, ci:%5d, c:%.2f\n",
//printf("by: %3d, bx: %3d, tid:%3d, t0:%5d, end:%5d, k:%5d, tid2:%5d, tid15:%5d, ldx:%5d, t2:%5d, t4:%5d\n",
// by, bx, tid, iD[0], iD[1], iD[2], iD[3], iD[4], iD[5], iD[6], iD[7]
//);
if (uD[1] < clock) clock = uD[1];
sm = uD[0];
iD += printVars;
fD += printVars;
uD += printVars;
}
printf("%02d %08u %d %d\n", sm, clock, by, bx);
}
}
free(D);
}
return totalTime;
}
typedef struct dPointer
{
CUdeviceptr lo;
CUdeviceptr hi;
} dPointer;
float cublasSgemm(const char* kernel, CUdeviceptr devC, CUdeviceptr devA, CUdeviceptr devB, int N, CUevent hStart, CUevent hStop, int repeat)
{
int threads, gridX, gridY;
if (strcmp(kernel, "maxwell_sgemm_128x64_nt") == 0)
{
threads = 128;
gridX = N / 128 + (N % 128 != 0);
gridY = N / 64 + (N % 64 != 0);
}
else
{
threads = 256;
gridX = gridY = N / 128 + (N % 128 != 0);
}
int blocks = gridX * gridY;
// Load the cubin
// See cublas_sgemm.ptx for info on how to build this.
CUmodule hModule;
CUDA_CHECK( cuModuleLoad(&hModule, "cublas_sgemm.cubin") );
// Load the kernel function
CUfunction hKernel;
CUDA_CHECK( cuModuleGetFunction(&hKernel, hModule, kernel) );
// Setup the params
// I should probably be working in 64 bits...
dPointer dA = { devA, 0 };
dPointer dB = { devB, 0 };
dPointer dC = { devC, 0 };
int flag = 0;
float alpha = 1.0;
float beta = 0.0;
void* params[] = { &dA, &dB, &dC, &N, &N, &N, &N, &dA, &dA, &alpha, &beta, &flag };
float totalTime = 0;
// Launch the kernel repeat times.. but break it up into pieces so as not to lock things up.
while (repeat > 0)
{
float ms;
int r = repeat > REPEAT_BLOCK ? REPEAT_BLOCK : repeat;
CUDA_CHECK( cuEventRecord( hStart, NULL ) );
for (int i = 0; i < r; i++)
CUDA_CHECK( cuLaunchKernel(hKernel, gridX, gridY, 1, threads, 1, 1, 0, 0, params, 0) );
CUDA_CHECK( cuEventRecord( hStop, NULL ) );
CUDA_CHECK( cuEventSynchronize( hStop ) );
CUDA_CHECK( cuEventElapsedTime( &ms, hStart, hStop ) );
totalTime += ms;
repeat -= r;
}
CUDA_CHECK( cuModuleUnload(hModule) );
return totalTime;
}
void gflops(const char* ident, int N, float ms, int repeat)
{
// Standard sgemm flops formula
ms /= repeat;
printf("%s GFLOPS: %.2f (size: %d, iterations: %d)\n", ident, ((double)N * N * N * 2.0 + N * N) / (ms * 1000000.0), N, repeat);
}
void test(float* C, float* T, int N, size_t size)
{
// Compare our implementation with the cublas result
int errors = memcmp(C, T, size);
if (errors)
{
if (N <= 512) // This gets too big and slow for large N
{
errors = 0;
FILE* file;
if (fopen_s(&file, "data.txt", "w") == 0)
{
for (int y = 0; y < N; ++y)
{
for (int x = 0; x < N; ++x)
{
float c = C[x*N + y];
float t = T[x*N + y];
if (c != t)
{
errors++;
fprintf(file, "%.8f!%.8f\t", c , t);
//fprintf(file, "%.0f!", c);
//fprintf(file, "!");
}
else
{
//fprintf(file, "%.0f=%.0f\t", c , t);
//fprintf(file, "%.0f=", c);
fprintf(file, "=");
}
}
fprintf(file, "\n");
}
fclose(file);
printf("%d errors\n", errors);
}
else
{ printf("Cannot open data.txt for writing\n"); }
}
else
{ printf("%d errors\n", errors); }
}
else
{ printf("%d errors\n", errors); }
}