-
Notifications
You must be signed in to change notification settings - Fork 3
/
eigen-bench.h
177 lines (146 loc) · 6.3 KB
/
eigen-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
#include "taco/tensor.h"
using namespace taco;
using namespace std;
#ifdef EIGEN
#include <Eigen/Sparse>
typedef Eigen::Matrix<double,Eigen::Dynamic,1> DenseVector;
typedef Eigen::SparseMatrix<double> EigenCSC;
typedef Eigen::SparseMatrix<double, Eigen::RowMajor> EigenCSR;
typedef Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> EigenColMajor;
typedef Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic, Eigen::RowMajor> EigenRowMajor;
void EigenTotaco(const EigenCSC& src, Tensor<double>& dst)
{
for (int j=0; j<src.cols(); ++j)
for (EigenCSC::InnerIterator it(src.derived(), j); it; ++it)
dst.insert({it.index(),j}, it.value());
dst.pack();
}
void tacoToEigen(const Tensor<double>& src, EigenCSC& dst){
taco_uassert(src.getFormat()==CSC)<<"Tensor have to be in CSC format to be converted to Eigen";
std::vector< Eigen::Triplet<double> > tripletList;
tripletList.reserve(src.getStorage().getValues().getSize());
for (auto& value : iterate<double>(src)) {
tripletList.push_back({value.first.at(0),value.first.at(1),value.second});
}
dst.setFromTriplets(tripletList.begin(), tripletList.end());
}
void tacoToEigen(const Tensor<double>& src, EigenCSR& dst){
taco_uassert(src.getFormat()==CSR)<<"Tensor have to be in CSR format to be converted to Eigen";
std::vector< Eigen::Triplet<double> > tripletList;
tripletList.reserve(src.getStorage().getValues().getSize());
for (auto& value : iterate<double>(src)) {
tripletList.push_back({value.first.at(0),value.first.at(1),value.second});
}
dst.setFromTriplets(tripletList.begin(), tripletList.end());
}
void tacoToEigen(const Tensor<double>& src, EigenColMajor& dst){
for (auto& value : iterate<double>(src)) {
dst(value.first.at(0),value.first.at(1)) = value.second;
}
}
void tacoToEigen(const Tensor<double>& src, EigenRowMajor& dst){
for (auto& value : iterate<double>(src)) {
dst(value.first.at(0),value.first.at(1)) = value.second;
}
}
void EigenTotaco(const DenseVector& src, Tensor<double>& dst) {
for (int j=0; j<src.rows(); ++j)
dst.insert({j}, src(j));
dst.pack();
}
void tacoToEigen(const Tensor<double>& src, DenseVector& dst) {
for (auto& value : iterate<double>(src))
dst(value.first[0]) = value.second;
}
void exprToEIGEN(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);
DenseVector xEigen(cols);
DenseVector yEigen(rows);
EigenCSR AEigen(rows,cols);
tacoToEigen(exprOperands.at("x"),xEigen);
tacoToEigen(exprOperands.at("A"),AEigen);
TACO_BENCH(yEigen.noalias() = AEigen * xEigen;,"\nEigen",repeat,timevalue,true);
Tensor<double> y_Eigen({rows}, Dense);
EigenTotaco(yEigen,y_Eigen);
validate("Eigen", y_Eigen, exprOperands.at("yRef"));
break;
}
case PLUS3: {
int rows=exprOperands.at("ARef").getDimension(0);
int cols=exprOperands.at("ARef").getDimension(1);
EigenCSC AEigen(rows,cols);
EigenCSC BEigen(rows,cols);
EigenCSC CEigen(rows,cols);
EigenCSC DEigen(rows,cols);
tacoToEigen(exprOperands.at("B"),BEigen);
tacoToEigen(exprOperands.at("C"),CEigen);
tacoToEigen(exprOperands.at("D"),DEigen);
TACO_BENCH(AEigen = BEigen + CEigen + DEigen;,"\nEigen",repeat,timevalue,true);
Tensor<double> A_Eigen({rows,cols}, CSC);
EigenTotaco(AEigen,A_Eigen);
validate("Eigen", A_Eigen, exprOperands.at("ARef"));
break;
}
case MATTRANSMUL: {
int rows=exprOperands.at("A").getDimension(0);
int cols=exprOperands.at("A").getDimension(1);
DenseVector xEigen(cols);
DenseVector zEigen(rows);
DenseVector yEigen(rows);
double alpha, beta;
EigenCSC AEigen(rows,cols);
tacoToEigen(exprOperands.at("x"),xEigen);
tacoToEigen(exprOperands.at("z"),zEigen);
tacoToEigen(exprOperands.at("A"),AEigen);
alpha = ((double*)(exprOperands.at("alpha").getStorage().getValues().getData()))[0];
beta = ((double*)(exprOperands.at("beta").getStorage().getValues().getData()))[0];
TACO_BENCH(yEigen.noalias() = alpha *AEigen.transpose() * xEigen + beta * zEigen;,"\nEigen",repeat,timevalue,true);
Tensor<double> y_Eigen({rows}, Dense);
EigenTotaco(yEigen,y_Eigen);
validate("Eigen", y_Eigen, exprOperands.at("yRef"));
break;
}
case RESIDUAL: {
int rows=exprOperands.at("A").getDimension(0);
int cols=exprOperands.at("A").getDimension(1);
DenseVector xEigen(cols);
DenseVector zEigen(rows);
DenseVector yEigen(rows);
double alpha, beta;
EigenCSR AEigen(rows,cols);
tacoToEigen(exprOperands.at("x"),xEigen);
tacoToEigen(exprOperands.at("z"),zEigen);
tacoToEigen(exprOperands.at("A"),AEigen);
alpha = ((double*)(exprOperands.at("alpha").getStorage().getValues().getData()))[0];
beta = ((double*)(exprOperands.at("beta").getStorage().getValues().getData()))[0];
TACO_BENCH(yEigen.noalias() = zEigen - AEigen * xEigen ;,"\nEigen",repeat,timevalue,true);
Tensor<double> y_Eigen({rows}, Dense);
EigenTotaco(yEigen,y_Eigen);
validate("Eigen", y_Eigen, exprOperands.at("yRef"));
break;
}
case SDDMM: {
int rows=exprOperands.at("ARef").getDimension(0);
int cols=exprOperands.at("ARef").getDimension(1);
EigenCSC AEigen(rows,cols);
EigenCSC BEigen(rows,cols);
EigenRowMajor CEigen(rows,100);
EigenColMajor DEigen(100,cols);
tacoToEigen(exprOperands.at("B"),BEigen);
tacoToEigen(exprOperands.at("C"),CEigen);
tacoToEigen(exprOperands.at("D"),DEigen);
TACO_BENCH(AEigen = BEigen.cwiseProduct(CEigen.lazyProduct(DEigen));,"\nEigen",repeat,timevalue,true);
Tensor<double> A_Eigen({rows,cols}, CSC);
EigenTotaco(AEigen,A_Eigen);
validate("Eigen", A_Eigen, exprOperands.at("ARef"));
break;
}
default:
cout << " !! Expression not implemented for Eigen" << endl;
break;
}
}
#endif