-
Notifications
You must be signed in to change notification settings - Fork 7
/
cublasGemmEx.cpp
155 lines (142 loc) · 5.58 KB
/
cublasGemmEx.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
/* Copyright 2020 Jeng Bai-Cheng
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "cublasGemmEx.h"
#include <cfloat>
#include <iostream>
#include "macro.h"
#include "verify.h"
std::vector<cublasGemmAlgo_t> AllCudaCoreAlgo() {
#if (CUBLAS_VER_MAJOR * 10 + CUBLAS_VER_MINOR) < 90
const cublasGemmAlgo_t CUBLAS_GEMM_DEFAULT = CUBLAS_GEMM_DFALT;
#endif
const static std::vector<cublasGemmAlgo_t> kAlgos{
CUBLAS_GEMM_DEFAULT,
CUBLAS_GEMM_ALGO0,
CUBLAS_GEMM_ALGO1,
CUBLAS_GEMM_ALGO2,
CUBLAS_GEMM_ALGO3,
CUBLAS_GEMM_ALGO4,
CUBLAS_GEMM_ALGO5,
CUBLAS_GEMM_ALGO6,
CUBLAS_GEMM_ALGO7,
#if (CUBLAS_VER_MAJOR * 10 + CUBLAS_VER_MINOR) >= 90
CUBLAS_GEMM_ALGO8,
CUBLAS_GEMM_ALGO9,
CUBLAS_GEMM_ALGO10,
CUBLAS_GEMM_ALGO11,
CUBLAS_GEMM_ALGO12,
CUBLAS_GEMM_ALGO13,
CUBLAS_GEMM_ALGO14,
CUBLAS_GEMM_ALGO15,
CUBLAS_GEMM_ALGO16,
CUBLAS_GEMM_ALGO17,
#endif
#if (CUBLAS_VER_MAJOR * 10 + CUBLAS_VER_MINOR) >= 92
CUBLAS_GEMM_ALGO18,
CUBLAS_GEMM_ALGO19,
CUBLAS_GEMM_ALGO20,
CUBLAS_GEMM_ALGO21,
CUBLAS_GEMM_ALGO22,
CUBLAS_GEMM_ALGO23
#endif
};
return kAlgos;
}
std::vector<cublasGemmAlgo_t> AllTensorCoreAlgo() {
const static std::vector<cublasGemmAlgo_t> kAlgos = {
#if (CUBLAS_VER_MAJOR * 10 + CUBLAS_VER_MINOR) >= 90
CUBLAS_GEMM_DEFAULT_TENSOR_OP,
CUBLAS_GEMM_ALGO0_TENSOR_OP,
CUBLAS_GEMM_ALGO1_TENSOR_OP,
CUBLAS_GEMM_ALGO2_TENSOR_OP,
#endif
#if (CUBLAS_VER_MAJOR * 10 + CUBLAS_VER_MINOR) >= 92
CUBLAS_GEMM_ALGO3_TENSOR_OP,
CUBLAS_GEMM_ALGO4_TENSOR_OP,
CUBLAS_GEMM_ALGO5_TENSOR_OP,
CUBLAS_GEMM_ALGO6_TENSOR_OP,
CUBLAS_GEMM_ALGO7_TENSOR_OP,
CUBLAS_GEMM_ALGO8_TENSOR_OP,
CUBLAS_GEMM_ALGO9_TENSOR_OP,
CUBLAS_GEMM_ALGO10_TENSOR_OP,
CUBLAS_GEMM_ALGO11_TENSOR_OP,
CUBLAS_GEMM_ALGO12_TENSOR_OP,
CUBLAS_GEMM_ALGO13_TENSOR_OP,
CUBLAS_GEMM_ALGO14_TENSOR_OP,
CUBLAS_GEMM_ALGO15_TENSOR_OP,
#endif
};
return kAlgos;
}
std::vector<ProfResult_t> ProfileGemm(const GemmParam_t& param,
const std::vector<cublasGemmAlgo_t>& algos, int loop, double threshold, bool debug) {
cublasHandle_t handle;
CUBLAS_CHECK(cublasCreate(&handle));
cudaEvent_t start;
cudaEvent_t end;
cublasStatus_t ret;
CUDA_CHECK(cudaEventCreate(&start));
CUDA_CHECK(cudaEventCreate(&end));
std::vector<ProfResult_t> results;
for (auto algo : algos) {
//param.algo = algo;
float time = 0.f;
bool fault = false;
CUDA_CHECK(cudaEventRecord(start));
for (int i = 0; i < loop; ++i) {
ret = cublasGemmEx(handle,
param.transa, param.transb,
param.m, param.n, param.k,
param.alpha, param.A, param.dtype.A, param.lda,
param.B, param.dtype.B, param.ldb, param.beta,
param.C, param.dtype.C, param.ldc,
param.dtype.compute_type, algo);
if (ret != CUBLAS_STATUS_SUCCESS) {
fault = true;
if (debug) {
std::cerr << "cublasGemmEx" << ", " << AlgoToString(algo) <<
", " << cublasGetErrorString(ret) << std::endl;
}
break;
}
}
CUDA_CHECK(cudaEventRecord(end));
CUDA_CHECK(cudaEventSynchronize(end));
CUDA_CHECK(cudaEventElapsedTime(&time, start, end));
if (!fault) {
auto relative_err = Verify(param.C, param.D, param.m * param.n, param.dtype.C);
if (relative_err > threshold) fault = true;
if (fault && debug) {
std::cerr << "cublasGemmEx" << ", " << AlgoToString(algo) << ", verification failed" << std::endl;
PrintMatrix(param.A, param.m, param.k, param.lda, param.dtype.A);
PrintMatrix(param.B, param.k, param.n, param.ldb, param.dtype.B);
PrintMatrix(param.C, param.m, param.n, param.ldc, param.dtype.C);
PrintMatrix(param.D, param.m, param.n, param.ldc, param.dtype.C);
}
}
CUDA_CHECK(cudaMemset(param.C, 0, param.m * param.n * DtypeToSize(param.dtype.C)));
time = fault ? FLT_MAX : (time / loop);
results.push_back(ProfResult_t{algo, time});
}
CUDA_CHECK(cudaEventDestroy(start));
CUDA_CHECK(cudaEventDestroy(end));
CUBLAS_CHECK(cublasDestroy(handle));
return results;
}