-
Notifications
You must be signed in to change notification settings - Fork 3
/
gmm-bench.h
134 lines (110 loc) · 4.65 KB
/
gmm-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
#include "taco/tensor.h"
using namespace taco;
using namespace std;
#ifdef GMM
#include "gmm/gmm.h"
typedef gmm::csc_matrix<double> GmmCSC;
typedef gmm::csr_matrix<double> GmmCSR;
typedef gmm::col_matrix< gmm::wsvector<double> > GmmSparse;
typedef gmm::linalg_traits<gmm::wsvector<double>>::const_iterator GmmIterator;
void GMMTotaco(const GmmSparse& src, Tensor<double>& dst) {
for (int j = 0; j < gmm::mat_ncols(src); ++j) {
typename gmm::linalg_traits<GmmSparse>::const_sub_col_type col = mat_const_col(src, j);
GmmIterator it1 = vect_const_begin(col);
GmmIterator ite1 = vect_const_end(col);
while (it1 != ite1) {
dst.insert({(int)(it1.index()),j},*it1);
++it1;
}
}
dst.pack();
}
void tacoToGMM(const Tensor<double>& src, GmmSparse& dst) {
for (auto& value : iterate<double>(src))
dst(value.first.at(0),value.first.at(1)) = value.second;
}
void GMMTotaco(const std::vector<double>& src, Tensor<double>& dst){
for (int i=0; i<dst.getDimension(0); ++i)
dst.insert({i}, src[i]);
dst.pack();
}
void tacoToGMM(const Tensor<double>& src, std::vector<double>& dst) {
for (auto& value : iterate<double>(src))
dst[value.first[0]] = value.second;
}
void exprToGMM(BenchExpr Expr, map<string,Tensor<double>> exprOperands,int repeat, taco::util::TimeResults timevalue) {
switch(Expr) {
case SpMV: {
int rows=exprOperands.at("A").getDimension(0);
int cols=exprOperands.at("A").getDimension(1);
GmmSparse Agmm_tmp(rows,cols);
tacoToGMM(exprOperands.at("A"),Agmm_tmp);
GmmCSR Agmm(rows,cols);
gmm::copy(Agmm_tmp, Agmm);
std::vector<double> xgmm(cols), ygmm(rows);
tacoToGMM(exprOperands.at("x"),xgmm);
TACO_BENCH(gmm::mult(Agmm, xgmm, ygmm);,"\nGMM",repeat,timevalue,true);
Tensor<double> y_gmm({rows}, Dense);
GMMTotaco(ygmm,y_gmm);
validate("GMM++", y_gmm, exprOperands.at("yRef"));
break;
}
case PLUS3: {
int rows=exprOperands.at("ARef").getDimension(0);
int cols=exprOperands.at("ARef").getDimension(1);
GmmSparse Agmm(rows,cols);
GmmSparse Bgmm(rows,cols);
GmmSparse Cgmm(rows,cols);
GmmSparse Dgmm(rows,cols);
tacoToGMM(exprOperands.at("B"),Bgmm);
tacoToGMM(exprOperands.at("C"),Cgmm);
tacoToGMM(exprOperands.at("D"),Dgmm);
TACO_BENCH(Agmm=Bgmm;gmm::add(Cgmm,Agmm);gmm::add(Dgmm,Agmm);,"\nGMM",repeat,timevalue,true);
Tensor<double> A_gmm({rows,cols}, CSC);
GMMTotaco(Agmm,A_gmm);
// comment out for now as Gmm++ and taco treat physical zeros differently
// validate("GMM", A_gmm, exprOperands.at("ARef"));
break;
}
case MATTRANSMUL: {
int rows=exprOperands.at("A").getDimension(0);
int cols=exprOperands.at("A").getDimension(1);
GmmSparse Agmm_tmp(rows,cols);
tacoToGMM(exprOperands.at("A"),Agmm_tmp);
GmmCSC Agmm(rows,cols);
gmm::copy(Agmm_tmp, Agmm);
std::vector<double> xgmm(cols), ygmm(rows), zgmm(rows);
tacoToGMM(exprOperands.at("x"),xgmm);
tacoToGMM(exprOperands.at("z"),zgmm);
double alpha = ((double*)(exprOperands.at("alpha").getStorage().getValues().getData()))[0];
double beta = ((double*)(exprOperands.at("beta").getStorage().getValues().getData()))[0];
TACO_BENCH(gmm::mult(gmm::transposed(Agmm), gmm::scaled(xgmm, alpha), gmm::scaled(zgmm, beta), ygmm);,"\nGMM",repeat,timevalue,true);
Tensor<double> y_gmm({rows}, Dense);
GMMTotaco(ygmm,y_gmm);
validate("GMM++", y_gmm, exprOperands.at("yRef"));
break;
}
case RESIDUAL: {
int rows=exprOperands.at("A").getDimension(0);
int cols=exprOperands.at("A").getDimension(1);
GmmSparse Agmm_tmp(rows,cols);
tacoToGMM(exprOperands.at("A"),Agmm_tmp);
GmmCSR Agmm(rows,cols);
gmm::copy(Agmm_tmp, Agmm);
std::vector<double> xgmm(cols), ygmm(rows), zgmm(rows);
tacoToGMM(exprOperands.at("x"),xgmm);
tacoToGMM(exprOperands.at("z"),zgmm);
double alpha = ((double*)(exprOperands.at("alpha").getStorage().getValues().getData()))[0];
double beta = ((double*)(exprOperands.at("beta").getStorage().getValues().getData()))[0];
TACO_BENCH(gmm::mult(Agmm, gmm::scaled(xgmm, -1.0), zgmm, ygmm);,"\nGMM",repeat,timevalue,true);
Tensor<double> y_gmm({rows}, Dense);
GMMTotaco(ygmm,y_gmm);
validate("GMM++", y_gmm, exprOperands.at("yRef"));
break;
}
default:
cout << " !! Expression not implemented for GMM" << endl;
break;
}
}
#endif