-
Notifications
You must be signed in to change notification settings - Fork 11
/
wmma_cublas.cu
246 lines (203 loc) · 9.33 KB
/
wmma_cublas.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
#include <benchmark/benchmark.h>
#include "gemv/args.hpp"
#include "init/init.hpp"
#include "utils/utils.hpp"
#include <mma.h>
using namespace nvcuda;
// MMA matrix tile dimensions. (16, 16, 16), (32, 8, 16), and (8, 32, 16) are
// currently supported.
// static const int M = 16;
static const int N = 16;
// static const int K = 16;
static void CUDA_WMMA_GEMV_CUBLAS(benchmark::State &state) {
const auto M_GLOBAL = state.range(0);
const auto K_GLOBAL = state.range(1);
const auto N_GLOBAL = N;
const float alpha = 1.1f;
const float beta = 1.2f;
float *a_fp32;
float *x_fp32;
half *a_fp16;
half *x_fp16;
half *b_fp16;
float *y;
PRINT_IF_ERROR(cudaMalloc((void **) &a_fp32, M_GLOBAL * K_GLOBAL * sizeof(float)));
PRINT_IF_ERROR(cudaMalloc((void **) &x_fp32, K_GLOBAL * sizeof(float)));
PRINT_IF_ERROR(cudaMalloc((void **) &a_fp16, M_GLOBAL * K_GLOBAL * sizeof(half)));
PRINT_IF_ERROR(cudaMalloc((void **) &x_fp16, K_GLOBAL * sizeof(half)));
PRINT_IF_ERROR(cudaMalloc((void **) &b_fp16, K_GLOBAL * N_GLOBAL * sizeof(half)));
PRINT_IF_ERROR(cudaMalloc((void **) &y,
M_GLOBAL * N_GLOBAL *
sizeof(float))); // the first column holds the result
curandGenerator_t gen;
PRINT_IF_ERROR(curandCreateGenerator(&gen, CURAND_RNG_PSEUDO_DEFAULT));
PRINT_IF_ERROR(curandSetPseudoRandomGeneratorSeed(gen, 1337ULL));
PRINT_IF_ERROR(curandGenerateUniform(gen, a_fp32, M_GLOBAL * N_GLOBAL));
PRINT_IF_ERROR(curandGenerateUniform(gen, x_fp32, N_GLOBAL));
PRINT_IF_ERROR(curandGenerateUniform(gen, y, M_GLOBAL * N_GLOBAL));
PRINT_IF_ERROR(curandDestroyGenerator(gen));
// curand doesn't currently support fp16 so we generate in fp32 and convert to
// fp16.
convertFp32ToFp16<<<(M_GLOBAL * K_GLOBAL + 255) / 256, 256>>>(a_fp16, a_fp32,
M_GLOBAL * K_GLOBAL);
convertFp32ToFp16<<<(N_GLOBAL + 255) / 256, 256>>>(x_fp16, x_fp32, K_GLOBAL);
// copy vector x to matrix b, column-major
PRINT_IF_ERROR(
cudaMemcpy(b_fp16, x_fp16, K_GLOBAL * sizeof(half), cudaMemcpyDeviceToDevice));
cudaEvent_t start, stop;
PRINT_IF_ERROR(cudaEventCreate(&start));
PRINT_IF_ERROR(cudaEventCreate(&stop));
cublasHandle_t cublasHandle;
PRINT_IF_ERROR(cublasCreate(&cublasHandle));
PRINT_IF_ERROR(cublasSetMathMode(cublasHandle,
CUBLAS_TENSOR_OP_MATH)); // Use tensor cores
for (auto _ : state) {
PRINT_IF_ERROR(cudaEventRecord(start));
/* C = α op ( A ) op ( B ) + β C
cublasStatus_t cublasGemmEx(cublasHandle_t handle,
cublasOperation_t transa,
cublasOperation_t transb,
int m,
int n,
int k,
const void *alpha,
const void *A,
cudaDataType_t Atype,
int lda,
const void *B,
cudaDataType_t Btype,
int ldb,
const void *beta,
void *C,
cudaDataType_t Ctype,
int ldc,
cudaDataType_t computeType,
cublasGemmAlgo_t algo)
*/
PRINT_IF_ERROR(cublasGemmEx(cublasHandle, CUBLAS_OP_N, CUBLAS_OP_N, M_GLOBAL,
N_GLOBAL, K_GLOBAL, &alpha, a_fp16, CUDA_R_16F, M_GLOBAL,
b_fp16, CUDA_R_16F, K_GLOBAL, &beta, y, CUDA_R_32F,
M_GLOBAL, CUDA_R_32F, CUBLAS_GEMM_DFALT_TENSOR_OP));
PRINT_IF_ERROR(cudaEventRecord(stop));
PRINT_IF_ERROR(cudaEventSynchronize(stop));
state.PauseTiming();
float msecTotal = 0.0f;
PRINT_IF_ERROR(cudaEventElapsedTime(&msecTotal, start, stop));
state.SetIterationTime(msecTotal / 1000);
state.ResumeTiming();
}
cudaEventDestroy(start);
cudaEventDestroy(stop);
PRINT_IF_ERROR(cudaFree(a_fp32));
PRINT_IF_ERROR(cudaFree(x_fp32));
PRINT_IF_ERROR(cudaFree(y));
PRINT_IF_ERROR(cudaFree(a_fp16));
PRINT_IF_ERROR(cudaFree(x_fp16));
PRINT_IF_ERROR(cudaFree(b_fp16));
cudaDeviceReset();
state.counters.insert({{"M", M_GLOBAL},
{"N", K_GLOBAL},
{"num_elements", M_GLOBAL * K_GLOBAL},
{"flops",
{state.iterations() * 2.0 * M_GLOBAL * K_GLOBAL,
benchmark::Counter::kAvgThreadsRate}}});
}
static void CUDA_WMMA_HGEMV_CUBLAS(benchmark::State &state) {
const auto M_GLOBAL = state.range(0);
const auto K_GLOBAL = state.range(1);
const auto N_GLOBAL = N;
const __half alpha = approx_float_to_half(1.1f);
const __half beta = approx_float_to_half(1.2f);
float *a_fp32;
float *x_fp32;
float *y_fp32;
half *a_fp16;
half *x_fp16;
half *b_fp16;
half *y_fp16;
PRINT_IF_ERROR(cudaMalloc((void **) &a_fp32, M_GLOBAL * K_GLOBAL * sizeof(float)));
PRINT_IF_ERROR(cudaMalloc((void **) &x_fp32, K_GLOBAL * sizeof(float)));
PRINT_IF_ERROR(cudaMalloc((void **) &y_fp32, M_GLOBAL * N_GLOBAL * sizeof(float)));
PRINT_IF_ERROR(cudaMalloc((void **) &a_fp16, M_GLOBAL * K_GLOBAL * sizeof(half)));
PRINT_IF_ERROR(cudaMalloc((void **) &x_fp16, K_GLOBAL * sizeof(half)));
PRINT_IF_ERROR(cudaMalloc((void **) &y_fp16, M_GLOBAL * N_GLOBAL * sizeof(half)));
PRINT_IF_ERROR(cudaMalloc((void **) &b_fp16, K_GLOBAL * N_GLOBAL * sizeof(half)));
curandGenerator_t gen;
PRINT_IF_ERROR(curandCreateGenerator(&gen, CURAND_RNG_PSEUDO_DEFAULT));
PRINT_IF_ERROR(curandSetPseudoRandomGeneratorSeed(gen, 1337ULL));
PRINT_IF_ERROR(curandGenerateUniform(gen, a_fp32, M_GLOBAL * N_GLOBAL));
PRINT_IF_ERROR(curandGenerateUniform(gen, x_fp32, N_GLOBAL));
PRINT_IF_ERROR(curandGenerateUniform(gen, y_fp32, M_GLOBAL * N_GLOBAL));
PRINT_IF_ERROR(curandDestroyGenerator(gen));
// curand doesn't currently support fp16 so we generate in fp32 and convert to
// fp16.
PRINT_IF_LAUNCH_ERROR((convertFp32ToFp16<<<(M_GLOBAL * K_GLOBAL + 255) / 256, 256>>>(
a_fp16, a_fp32, M_GLOBAL * K_GLOBAL)));
PRINT_IF_LAUNCH_ERROR(
(convertFp32ToFp16<<<(N_GLOBAL + 255) / 256, 256>>>(x_fp16, x_fp32, K_GLOBAL)));
PRINT_IF_LAUNCH_ERROR((convertFp32ToFp16<<<(M_GLOBAL * N_GLOBAL + 255) / 256, 256>>>(
y_fp16, y_fp32, M_GLOBAL * N_GLOBAL)));
// copy vector x to matrix b, column-major
PRINT_IF_ERROR(
cudaMemcpy(b_fp16, x_fp16, K_GLOBAL * sizeof(half), cudaMemcpyDeviceToDevice));
cudaEvent_t start, stop;
PRINT_IF_ERROR(cudaEventCreate(&start));
PRINT_IF_ERROR(cudaEventCreate(&stop));
cublasHandle_t cublasHandle;
PRINT_IF_ERROR(cublasCreate(&cublasHandle));
PRINT_IF_ERROR(cublasSetMathMode(cublasHandle,
CUBLAS_TENSOR_OP_MATH)); // Use tensor cores
for (auto _ : state) {
PRINT_IF_ERROR(cudaEventRecord(start));
/* C = α op ( A ) op ( B ) + β C
cublasStatus_t cublasGemmEx(cublasHandle_t handle,
cublasOperation_t transa,
cublasOperation_t transb,
int m,
int n,
int k,
const void *alpha,
const void *A,
cudaDataType_t Atype,
int lda,
const void *B,
cudaDataType_t Btype,
int ldb,
const void *beta,
void *C,
cudaDataType_t Ctype,
int ldc,
cudaDataType_t computeType,
cublasGemmAlgo_t algo)
*/
PRINT_IF_ERROR(cublasGemmEx(cublasHandle, CUBLAS_OP_N, CUBLAS_OP_N, M_GLOBAL,
N_GLOBAL, K_GLOBAL, &alpha, a_fp16, CUDA_R_16F, M_GLOBAL,
b_fp16, CUDA_R_16F, K_GLOBAL, &beta, y_fp16, CUDA_R_16F,
M_GLOBAL, CUDA_R_16F, CUBLAS_GEMM_DFALT_TENSOR_OP));
PRINT_IF_ERROR(cudaEventRecord(stop));
PRINT_IF_ERROR(cudaEventSynchronize(stop));
state.PauseTiming();
float msecTotal = 0.0f;
PRINT_IF_ERROR(cudaEventElapsedTime(&msecTotal, start, stop));
state.SetIterationTime(msecTotal / 1000);
state.ResumeTiming();
}
cudaEventDestroy(start);
cudaEventDestroy(stop);
PRINT_IF_ERROR(cudaFree(a_fp32));
PRINT_IF_ERROR(cudaFree(x_fp32));
PRINT_IF_ERROR(cudaFree(y_fp32));
PRINT_IF_ERROR(cudaFree(a_fp16));
PRINT_IF_ERROR(cudaFree(x_fp16));
PRINT_IF_ERROR(cudaFree(y_fp16));
PRINT_IF_ERROR(cudaFree(b_fp16));
cudaDeviceReset();
state.counters.insert({{"M", M_GLOBAL},
{"N", K_GLOBAL},
{"num_elements", M_GLOBAL * K_GLOBAL},
{"flops",
{state.iterations() * 2.0 * M_GLOBAL * K_GLOBAL,
benchmark::Counter::kAvgThreadsRate}}});
}
BENCHMARK(CUDA_WMMA_GEMV_CUBLAS)->ARGS()->UseManualTime();
BENCHMARK(CUDA_WMMA_HGEMV_CUBLAS)->ARGS()->UseManualTime();