aboutsummaryrefslogtreecommitdiffhomepage
path: root/bench/spmv.cpp
blob: 959bab09b849ed74901557750484481131bbe152 (plain)
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

//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

#define SCALAR double

#include <iostream>
#include <algorithm>
#include "BenchTimer.h"
#include "BenchSparseUtil.h"

#define SPMV_BENCH(CODE) BENCH(t,tries,repeats,CODE);

// #ifdef MKL
//
// #include "mkl_types.h"
// #include "mkl_spblas.h"
//
// template<typename Lhs,typename Rhs,typename Res>
// void mkl_multiply(const Lhs& lhs, const Rhs& rhs, Res& res)
// {
//   char n = 'N';
//   float alpha = 1;
//   char matdescra[6];
//   matdescra[0] = 'G';
//   matdescra[1] = 0;
//   matdescra[2] = 0;
//   matdescra[3] = 'C';
//   mkl_scscmm(&n, lhs.rows(), rhs.cols(), lhs.cols(), &alpha, matdescra,
//              lhs._valuePtr(), lhs._innerIndexPtr(), lhs.outerIndexPtr(),
//              pntre, b, &ldb, &beta, c, &ldc);
// //   mkl_somatcopy('C', 'T', lhs.rows(), lhs.cols(), 1,
// //                 lhs._valuePtr(), lhs.rows(), DST, dst_stride);
// }
//
// #endif

int main(int argc, char *argv[])
{
  int size = 10000;
  int rows = size;
  int cols = size;
  int nnzPerCol = 40;
  int tries = 2;
  int repeats = 2;

  bool need_help = false;
  for(int i = 1; i < argc; i++)
  {
    if(argv[i][0] == 'r')
    {
      rows = atoi(argv[i]+1);
    }
    else if(argv[i][0] == 'c')
    {
      cols = atoi(argv[i]+1);
    }
    else if(argv[i][0] == 'n')
    {
      nnzPerCol = atoi(argv[i]+1);
    }
    else if(argv[i][0] == 't')
    {
      tries = atoi(argv[i]+1);
    }
    else if(argv[i][0] == 'p')
    {
      repeats = atoi(argv[i]+1);
    }
    else
    {
      need_help = true;
    }
  }
  if(need_help)
  {
    std::cout << argv[0] << " r<nb rows> c<nb columns> n<non zeros per column> t<nb tries> p<nb repeats>\n";
    return 1;
  }

  std::cout << "SpMV " << rows << " x " << cols << " with " << nnzPerCol << " non zeros per column. (" << repeats << " repeats, and " << tries << " tries)\n\n";

  EigenSparseMatrix sm(rows,cols);
  DenseVector dv(cols), res(rows);
  dv.setRandom();

  BenchTimer t;
  while (nnzPerCol>=4)
  {
    std::cout << "nnz: " << nnzPerCol << "\n";
    sm.setZero();
    fillMatrix2(nnzPerCol, rows, cols, sm);

    // dense matrices
    #ifdef DENSEMATRIX
    {
      DenseMatrix dm(rows,cols), (rows,cols);
      eiToDense(sm, dm);

      SPMV_BENCH(res = dm * sm);
      std::cout << "Dense       " << t.value()/repeats << "\t";

      SPMV_BENCH(res = dm.transpose() * sm);
      std::cout << t.value()/repeats << endl;
    }
    #endif

    // eigen sparse matrices
    {
      SPMV_BENCH(res.noalias() += sm * dv; )
      std::cout << "Eigen       " << t.value()/repeats << "\t";

      SPMV_BENCH(res.noalias() += sm.transpose() * dv; )
      std::cout << t.value()/repeats << endl;
    }

    // CSparse
    #ifdef CSPARSE
    {
      std::cout << "CSparse \n";
      cs *csm;
      eiToCSparse(sm, csm);

//       BENCH();
//       timer.stop();
//       std::cout << "   a * b:\t" << timer.value() << endl;

//       BENCH( { m3 = cs_sorted_multiply2(m1, m2); cs_spfree(m3); } );
//       std::cout << "   a * b:\t" << timer.value() << endl;
    }
    #endif

    #ifdef OSKI
    {
      oski_matrix_t om;
      oski_vecview_t ov, ores;
      oski_Init();
      om = oski_CreateMatCSC(sm._outerIndexPtr(), sm._innerIndexPtr(), sm._valuePtr(), rows, cols,
                             SHARE_INPUTMAT, 1, INDEX_ZERO_BASED);
      ov = oski_CreateVecView(dv.data(), cols, STRIDE_UNIT);
      ores = oski_CreateVecView(res.data(), rows, STRIDE_UNIT);

      SPMV_BENCH( oski_MatMult(om, OP_NORMAL, 1, ov, 0, ores) );
      std::cout << "OSKI        " << t.value()/repeats << "\t";

      SPMV_BENCH( oski_MatMult(om, OP_TRANS, 1, ov, 0, ores) );
      std::cout << t.value()/repeats << "\n";

      // tune
      t.reset();
      t.start();
      oski_SetHintMatMult(om, OP_NORMAL, 1.0, SYMBOLIC_VEC, 0.0, SYMBOLIC_VEC, ALWAYS_TUNE_AGGRESSIVELY);
      oski_TuneMat(om);
      t.stop();
      double tuning = t.value();

      SPMV_BENCH( oski_MatMult(om, OP_NORMAL, 1, ov, 0, ores) );
      std::cout << "OSKI tuned  " << t.value()/repeats << "\t";

      SPMV_BENCH( oski_MatMult(om, OP_TRANS, 1, ov, 0, ores) );
      std::cout << t.value()/repeats << "\t(" << tuning <<  ")\n";


      oski_DestroyMat(om);
      oski_DestroyVecView(ov);
      oski_DestroyVecView(ores);
      oski_Close();
    }
    #endif

    #ifndef NOUBLAS
    {
      using namespace boost::numeric;
      UblasMatrix um(rows,cols);
      eiToUblas(sm, um);

      boost::numeric::ublas::vector<Scalar> uv(cols), ures(rows);
      Map<Matrix<Scalar,Dynamic,1> >(&uv[0], cols) = dv;
      Map<Matrix<Scalar,Dynamic,1> >(&ures[0], rows) = res;

      SPMV_BENCH(ublas::axpy_prod(um, uv, ures, true));
      std::cout << "ublas       " << t.value()/repeats << "\t";

      SPMV_BENCH(ublas::axpy_prod(boost::numeric::ublas::trans(um), uv, ures, true));
      std::cout << t.value()/repeats << endl;
    }
    #endif

    // GMM++
    #ifndef NOGMM
    {
      GmmSparse gm(rows,cols);
      eiToGmm(sm, gm);

      std::vector<Scalar> gv(cols), gres(rows);
      Map<Matrix<Scalar,Dynamic,1> >(&gv[0], cols) = dv;
      Map<Matrix<Scalar,Dynamic,1> >(&gres[0], rows) = res;

      SPMV_BENCH(gmm::mult(gm, gv, gres));
      std::cout << "GMM++       " << t.value()/repeats << "\t";

      SPMV_BENCH(gmm::mult(gmm::transposed(gm), gv, gres));
      std::cout << t.value()/repeats << endl;
    }
    #endif

    // MTL4
    #ifndef NOMTL
    {
      MtlSparse mm(rows,cols);
      eiToMtl(sm, mm);
      mtl::dense_vector<Scalar> mv(cols, 1.0);
      mtl::dense_vector<Scalar> mres(rows, 1.0);

      SPMV_BENCH(mres = mm * mv);
      std::cout << "MTL4        " << t.value()/repeats << "\t";

      SPMV_BENCH(mres = trans(mm) * mv);
      std::cout << t.value()/repeats << endl;
    }
    #endif

    std::cout << "\n";

    if(nnzPerCol==1)
      break;
    nnzPerCol -= nnzPerCol/2;
  }

  return 0;
}