-
Notifications
You must be signed in to change notification settings - Fork 3
/
mkl-bench.h
260 lines (234 loc) · 9.37 KB
/
mkl-bench.h
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
#include "taco/tensor.h"
using namespace taco;
using namespace std;
#ifdef MKL
#include "mkl_spblas.h"
#include "mkl_blas.h"
#include "mkl.h"
void exprToMKL(BenchExpr Expr, map<string,Tensor<double>> exprOperands,int repeat, taco::util::TimeResults timevalue) {
switch(Expr) {
case SpMV: {
char matdescra[6] = "G C ";
int rows=exprOperands.at("A").getDimension(0);
int cols=exprOperands.at("A").getDimension(1);
int nnz=exprOperands.at("A").getStorage().getValues().getSize();
// convert to CSR
Tensor<double> ACSR({rows,cols}, CSR);
for (auto& value : iterate<double>(exprOperands.at("A"))) {
ACSR.insert({value.first.at(0),value.first.at(1)},value.second);
}
ACSR.pack();
double *a_CSR;
int* ia_CSR;
int* ja_CSR;
getCSRArrays(ACSR,&ia_CSR,&ja_CSR,&a_CSR);
for (int i = 0; i < rows+1; ++i) {
ia_CSR[i] = ia_CSR[i] + 1;
}
for (int i = 0; i < nnz; ++i) {
ja_CSR[i] = ja_CSR[i] + 1;
}
Tensor<double> y_mkl({rows}, Dense);
y_mkl.pack();
char transa = 'N';
TACO_BENCH(mkl_dcsrgemv(&transa, &rows, a_CSR, ia_CSR, ja_CSR,
(double*)(exprOperands.at("x").getStorage().getValues().getData()),
(double*)(y_mkl.getStorage().getValues().getData()));,
"\nMKL", repeat,timevalue,true)
// commented for now due to floating-point precision issues
// validate("MKL", y_mkl, exprOperands.at("yRef"));
break;
}
case PLUS3: {
int rows=exprOperands.at("ARef").getDimension(0);
int cols=exprOperands.at("ARef").getDimension(1);
int nnz=exprOperands.at("B").getStorage().getValues().getSize();
// convert to CSR
Tensor<double> BCSR({rows,cols}, CSR);
for (auto& value : iterate<double>(exprOperands.at("B"))) {
BCSR.insert({value.first.at(0),value.first.at(1)},value.second);
}
BCSR.pack();
double *b_CSR;
int* ib_CSR;
int* jb_CSR;
getCSRArrays(BCSR,&ib_CSR,&jb_CSR,&b_CSR);
Tensor<double> CCSR({rows,cols}, CSR);
for (auto& value : iterate<double>(exprOperands.at("C"))) {
CCSR.insert({value.first.at(0),value.first.at(1)},value.second);
}
CCSR.pack();
double *c_CSR;
int* ic_CSR;
int* jc_CSR;
getCSRArrays(CCSR,&ic_CSR,&jc_CSR,&c_CSR);
Tensor<double> DCSR({rows,cols}, CSR);
for (auto& value : iterate<double>(exprOperands.at("D"))) {
DCSR.insert({value.first.at(0),value.first.at(1)},value.second);
}
DCSR.pack();
double *d_CSR;
int* id_CSR;
int* jd_CSR;
getCSRArrays(DCSR,&id_CSR,&jd_CSR,&d_CSR);
char transa = 'N';
int ptrsize = rows + 1;
double malpha=1.0;
double mbeta=0.0;
for (int i = 0; i < ptrsize; ++i) {
ib_CSR[i] = ib_CSR[i] + 1;
ic_CSR[i] = ic_CSR[i] + 1;
id_CSR[i] = id_CSR[i] + 1;
}
for (int i = 0; i < nnz; ++i) {
jb_CSR[i] = jb_CSR[i] + 1;
jc_CSR[i] = jc_CSR[i] + 1;
jd_CSR[i] = jd_CSR[i] + 1;
}
MKL_INT request = 0;
MKL_INT sort = 0;
MKL_INT ret;
double *a_CSR;
int* ia_CSR;
int* ja_CSR;
TACO_BENCH( a_CSR = new double[4*nnz]; ia_CSR = new int[4*nnz]; ja_CSR = new int[4*nnz];
mkl_dcsradd(&transa, &request, &sort, &rows, &cols, b_CSR, jb_CSR, ib_CSR, &malpha, c_CSR, jc_CSR, ic_CSR, a_CSR, ja_CSR, ia_CSR, &nnz, &ret);
mkl_dcsradd(&transa, &request, &sort, &rows, &cols, a_CSR, ja_CSR, ia_CSR, &malpha, d_CSR, jd_CSR, id_CSR, a_CSR, ja_CSR, ia_CSR, &nnz, &ret);,
"\nMKL",repeat,timevalue,true);
for (int i = 0; i < ptrsize; ++i) {
ia_CSR[i] = ia_CSR[i] - 1;
}
for (int i = 0; i < nnz; ++i) {
ja_CSR[i] = ja_CSR[i] - 1;
}
Tensor<double> AMKL=makeCSR("AMKL",{rows,cols}, ia_CSR, ja_CSR, a_CSR);
validate("MKL", AMKL, exprOperands.at("ARef"));
break;
}
case MATTRANSMUL: {
char matdescra[6] = "G C ";
int rows=exprOperands.at("A").getDimension(0);
int cols=exprOperands.at("A").getDimension(1);
int ptrsize = exprOperands.at("A").getStorage().getIndex().getSize();
double *a_CSC;
int* ia_CSC;
int* ja_CSC;
getCSCArrays(exprOperands.at("A"),&ia_CSC,&ja_CSC,&a_CSC);
int* pointerB=new int[ptrsize-1];
int* pointerE=new int[ptrsize-1];
for (int i=0; i<ptrsize-1; i++) {
pointerB[i]=ia_CSC[i];
pointerE[i]=ia_CSC[i+1];
}
Tensor<double> y_mkl({rows}, Dense);
y_mkl.pack();
double alpha = ((double*)(exprOperands.at("alpha").getStorage().getValues().getData()))[0];
double beta = ((double*)(exprOperands.at("beta").getStorage().getValues().getData()))[0];
double* yvals=((double*)(y_mkl.getStorage().getValues().getData()));
double* zvals=((double*)(exprOperands.at("z").getStorage().getValues().getData()));
char transa = 'T';
TACO_BENCH(for (auto k=0; k<rows; k++) {yvals[k]=zvals[k];} ;
mkl_dcscmv(&transa, &rows, &cols, &alpha, matdescra, a_CSC, ja_CSC, pointerB,
pointerE, (double*)(exprOperands.at("x").getStorage().getValues().getData()),
&beta, (double*)(y_mkl.getStorage().getValues().getData()));,
"\nMKL", repeat,timevalue,true)
validate("MKL", y_mkl, exprOperands.at("yRef"));
break;
}
case RESIDUAL: {
char matdescra[6] = "G C ";
int rows=exprOperands.at("A").getDimension(0);
int cols=exprOperands.at("A").getDimension(1);
int nnz=exprOperands.at("A").getStorage().getValues().getSize();
// convert to CSR
Tensor<double> ACSR({rows,cols}, CSR);
for (auto& value : iterate<double>(exprOperands.at("A"))) {
ACSR.insert({value.first.at(0),value.first.at(1)},value.second);
}
ACSR.pack();
double *a_CSR;
int* ia_CSR;
int* ja_CSR;
getCSRArrays(ACSR,&ia_CSR,&ja_CSR,&a_CSR);
double alpha=-1.0;
double beta=1.0;
int* pointerB=new int[nnz];
int* pointerE=new int[nnz];
for (int i=0; i<nnz; i++) {
pointerB[i]=ia_CSR[i];
pointerE[i]=ia_CSR[i+1];
}
Tensor<double> y_mkl({rows}, Dense);
y_mkl.pack();
double* yvals=((double*)(y_mkl.getStorage().getValues().getData()));
double* zvals=((double*)(exprOperands.at("z").getStorage().getValues().getData()));
char transa = 'N';
TACO_BENCH(for (auto k=0; k<rows; k++) {yvals[k]=zvals[k];} ;
mkl_dcsrmv(&transa, &rows, &cols, &alpha, matdescra, a_CSR, ja_CSR, pointerB,
pointerE, (double*)(exprOperands.at("x").getStorage().getValues().getData()),
&beta, (double*)(y_mkl.getStorage().getValues().getData()));,
"\nMKL", repeat,timevalue,true)
validate("MKL", y_mkl, exprOperands.at("yRef"));
break;
}
case SparsitySpMDM: {
// use MKL to benchmark dense matrix-matrix mult
int rows=exprOperands.at("CRef").getDimension(0);
int cols=exprOperands.at("CRef").getDimension(1);
double* C_mkl = (double*)malloc(sizeof(double)*rows*cols);
double* A_mkl = (double*)exprOperands.at("A").getStorage().getValues().getData();
double* B_mkl = (double*)exprOperands.at("B").getStorage().getValues().getData();
#ifdef MKL_PRINT_DENSE
for (int i=0; i<rows; i++) {
for (int j=0; j<cols; j++) {
printf(" %g ", ((double*)(exprOperands.at("A").getStorage().getValues().getData()))[i+j*rows]);
}
printf("\n");
}
printf("\n");
for (int i=0; i<rows; i++) {
for (int j=0; j<cols; j++) {
printf(" %g ", ((double*)(exprOperands.at("B").getStorage().getValues().getData()))[i+j*rows]);
}
printf("\n");
}
printf("\n");
#endif
double alpha = 1.0;
double beta = 0.0;
// this does alpha * op(A) * op(B) + beta*C
TACO_BENCH(
cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, rows, cols,
rows, alpha, A_mkl, rows, B_mkl, cols, beta, C_mkl, rows);,
"\nMKL", repeat, timevalue, true);
Tensor<double> C_mkl_validation({rows, cols}, Format({Dense,Dense}));
for (int i=0; i<rows; i++) {
for (int j=0; j<cols; j++) {
C_mkl_validation.insert({i,j},C_mkl[i+j*rows]);
}
}
validate("MKL", C_mkl_validation, exprOperands.at("CRef"));
#ifdef MKL_PRINT_DENSE
for (int i=0; i<rows; i++) {
for (int j=0; j<cols; j++) {
printf(" %g ", C_mkl[i+j*rows]);
}
printf("\n");
}
printf("\n");
for (int i=0; i<rows; i++) {
for (int j=0; j<cols; j++) {
printf(" %g ", ((double*)(exprOperands.at("CRef").getStorage().getValues().getData()))[i+j*rows]);
}
printf("\n");
}
#endif
free(C_mkl);
break;
}
default:
cout << " !! Expression not implemented for MKL" << endl;
break;
}
}
#endif