Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / ground / gcs / src / libs / eigen / bench / spmv.cpp
blob959bab09b849ed74901557750484481131bbe152
2 //g++-4.4 -DNOMTL -Wl,-rpath /usr/local/lib/oski -L /usr/local/lib/oski/ -l oski -l oski_util -l oski_util_Tid -DOSKI -I ~/Coding/LinearAlgebra/mtl4/ spmv.cpp -I .. -O2 -DNDEBUG -lrt -lm -l oski_mat_CSC_Tid -loskilt && ./a.out r200000 c200000 n100 t1 p1
4 #define SCALAR double
6 #include <iostream>
7 #include <algorithm>
8 #include "BenchTimer.h"
9 #include "BenchSparseUtil.h"
11 #define SPMV_BENCH(CODE) BENCH(t,tries,repeats,CODE);
13 // #ifdef MKL
15 // #include "mkl_types.h"
16 // #include "mkl_spblas.h"
18 // template<typename Lhs,typename Rhs,typename Res>
19 // void mkl_multiply(const Lhs& lhs, const Rhs& rhs, Res& res)
20 // {
21 // char n = 'N';
22 // float alpha = 1;
23 // char matdescra[6];
24 // matdescra[0] = 'G';
25 // matdescra[1] = 0;
26 // matdescra[2] = 0;
27 // matdescra[3] = 'C';
28 // mkl_scscmm(&n, lhs.rows(), rhs.cols(), lhs.cols(), &alpha, matdescra,
29 // lhs._valuePtr(), lhs._innerIndexPtr(), lhs.outerIndexPtr(),
30 // pntre, b, &ldb, &beta, c, &ldc);
31 // // mkl_somatcopy('C', 'T', lhs.rows(), lhs.cols(), 1,
32 // // lhs._valuePtr(), lhs.rows(), DST, dst_stride);
33 // }
35 // #endif
37 int main(int argc, char *argv[])
39 int size = 10000;
40 int rows = size;
41 int cols = size;
42 int nnzPerCol = 40;
43 int tries = 2;
44 int repeats = 2;
46 bool need_help = false;
47 for(int i = 1; i < argc; i++)
49 if(argv[i][0] == 'r')
51 rows = atoi(argv[i]+1);
53 else if(argv[i][0] == 'c')
55 cols = atoi(argv[i]+1);
57 else if(argv[i][0] == 'n')
59 nnzPerCol = atoi(argv[i]+1);
61 else if(argv[i][0] == 't')
63 tries = atoi(argv[i]+1);
65 else if(argv[i][0] == 'p')
67 repeats = atoi(argv[i]+1);
69 else
71 need_help = true;
74 if(need_help)
76 std::cout << argv[0] << " r<nb rows> c<nb columns> n<non zeros per column> t<nb tries> p<nb repeats>\n";
77 return 1;
80 std::cout << "SpMV " << rows << " x " << cols << " with " << nnzPerCol << " non zeros per column. (" << repeats << " repeats, and " << tries << " tries)\n\n";
82 EigenSparseMatrix sm(rows,cols);
83 DenseVector dv(cols), res(rows);
84 dv.setRandom();
86 BenchTimer t;
87 while (nnzPerCol>=4)
89 std::cout << "nnz: " << nnzPerCol << "\n";
90 sm.setZero();
91 fillMatrix2(nnzPerCol, rows, cols, sm);
93 // dense matrices
94 #ifdef DENSEMATRIX
96 DenseMatrix dm(rows,cols), (rows,cols);
97 eiToDense(sm, dm);
99 SPMV_BENCH(res = dm * sm);
100 std::cout << "Dense " << t.value()/repeats << "\t";
102 SPMV_BENCH(res = dm.transpose() * sm);
103 std::cout << t.value()/repeats << endl;
105 #endif
107 // eigen sparse matrices
109 SPMV_BENCH(res.noalias() += sm * dv; )
110 std::cout << "Eigen " << t.value()/repeats << "\t";
112 SPMV_BENCH(res.noalias() += sm.transpose() * dv; )
113 std::cout << t.value()/repeats << endl;
116 // CSparse
117 #ifdef CSPARSE
119 std::cout << "CSparse \n";
120 cs *csm;
121 eiToCSparse(sm, csm);
123 // BENCH();
124 // timer.stop();
125 // std::cout << " a * b:\t" << timer.value() << endl;
127 // BENCH( { m3 = cs_sorted_multiply2(m1, m2); cs_spfree(m3); } );
128 // std::cout << " a * b:\t" << timer.value() << endl;
130 #endif
132 #ifdef OSKI
134 oski_matrix_t om;
135 oski_vecview_t ov, ores;
136 oski_Init();
137 om = oski_CreateMatCSC(sm._outerIndexPtr(), sm._innerIndexPtr(), sm._valuePtr(), rows, cols,
138 SHARE_INPUTMAT, 1, INDEX_ZERO_BASED);
139 ov = oski_CreateVecView(dv.data(), cols, STRIDE_UNIT);
140 ores = oski_CreateVecView(res.data(), rows, STRIDE_UNIT);
142 SPMV_BENCH( oski_MatMult(om, OP_NORMAL, 1, ov, 0, ores) );
143 std::cout << "OSKI " << t.value()/repeats << "\t";
145 SPMV_BENCH( oski_MatMult(om, OP_TRANS, 1, ov, 0, ores) );
146 std::cout << t.value()/repeats << "\n";
148 // tune
149 t.reset();
150 t.start();
151 oski_SetHintMatMult(om, OP_NORMAL, 1.0, SYMBOLIC_VEC, 0.0, SYMBOLIC_VEC, ALWAYS_TUNE_AGGRESSIVELY);
152 oski_TuneMat(om);
153 t.stop();
154 double tuning = t.value();
156 SPMV_BENCH( oski_MatMult(om, OP_NORMAL, 1, ov, 0, ores) );
157 std::cout << "OSKI tuned " << t.value()/repeats << "\t";
159 SPMV_BENCH( oski_MatMult(om, OP_TRANS, 1, ov, 0, ores) );
160 std::cout << t.value()/repeats << "\t(" << tuning << ")\n";
163 oski_DestroyMat(om);
164 oski_DestroyVecView(ov);
165 oski_DestroyVecView(ores);
166 oski_Close();
168 #endif
170 #ifndef NOUBLAS
172 using namespace boost::numeric;
173 UblasMatrix um(rows,cols);
174 eiToUblas(sm, um);
176 boost::numeric::ublas::vector<Scalar> uv(cols), ures(rows);
177 Map<Matrix<Scalar,Dynamic,1> >(&uv[0], cols) = dv;
178 Map<Matrix<Scalar,Dynamic,1> >(&ures[0], rows) = res;
180 SPMV_BENCH(ublas::axpy_prod(um, uv, ures, true));
181 std::cout << "ublas " << t.value()/repeats << "\t";
183 SPMV_BENCH(ublas::axpy_prod(boost::numeric::ublas::trans(um), uv, ures, true));
184 std::cout << t.value()/repeats << endl;
186 #endif
188 // GMM++
189 #ifndef NOGMM
191 GmmSparse gm(rows,cols);
192 eiToGmm(sm, gm);
194 std::vector<Scalar> gv(cols), gres(rows);
195 Map<Matrix<Scalar,Dynamic,1> >(&gv[0], cols) = dv;
196 Map<Matrix<Scalar,Dynamic,1> >(&gres[0], rows) = res;
198 SPMV_BENCH(gmm::mult(gm, gv, gres));
199 std::cout << "GMM++ " << t.value()/repeats << "\t";
201 SPMV_BENCH(gmm::mult(gmm::transposed(gm), gv, gres));
202 std::cout << t.value()/repeats << endl;
204 #endif
206 // MTL4
207 #ifndef NOMTL
209 MtlSparse mm(rows,cols);
210 eiToMtl(sm, mm);
211 mtl::dense_vector<Scalar> mv(cols, 1.0);
212 mtl::dense_vector<Scalar> mres(rows, 1.0);
214 SPMV_BENCH(mres = mm * mv);
215 std::cout << "MTL4 " << t.value()/repeats << "\t";
217 SPMV_BENCH(mres = trans(mm) * mv);
218 std::cout << t.value()/repeats << endl;
220 #endif
222 std::cout << "\n";
224 if(nnzPerCol==1)
225 break;
226 nnzPerCol -= nnzPerCol/2;
229 return 0;