Debian Codegraph @

benchmarks/benchmark-fspmv.C

raw
1 /* Copyright (c) FFLAS-FFPACK
2 * Written by Bastien Vialla <[email protected]>
3 * ========LICENCE========
4 * This file is part of the library FFLAS-FFPACK.
5 *
6 * FFLAS-FFPACK is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * ========LICENCE========
20 */
21
22 #include "fflas-ffpack/fflas-ffpack-config.h"
23 #include <iostream>
24 #include <vector>
25 #include <sstream>
26 #include <cstdio>
27 #include <cstdlib>
28
29 #include "givaro/modular.h"
30 #include "givaro/modular-balanced.h"
31
32 #include "fflas-ffpack/config-blas.h"
33 #include "fflas-ffpack/fflas/fflas.h"
34 #include "fflas-ffpack/fflas/fflas_sparse.h"
35 #include "fflas-ffpack/utils/timer.h"
36 #include "fflas-ffpack/utils/args-parser.h"
37
38 #ifdef __FFLASFFPACK_USE_OPENMP
39 typedef FFLAS::OMPTimer TTimer;
40 #else
41 typedef FFLAS::Timer TTimer;
42 #endif
43
44 using namespace std;
45 using namespace FFLAS;
46
47 template <typename T> T from_string(std::string const &s) {
48 std::stringstream ss(s);
49 T result;
50 ss >> result; // TODO handle errors
51 return result;
52 }
53
54
55 template <class MatT, class Field, class IndexT>
56 std::pair<double, uint64_t> test_fspmv(size_t iter, const Field &F, IndexT *row, IndexT *col,
57 typename Field::Element_ptr dat, index_t rowdim, index_t coldim, uint64_t nnz,
58 typename Field::Element_ptr x, typename Field::Element_ptr y,
59 typename Field::Element beta) {
60 MatT matrix;
61 sparse_init(F, matrix, row, col, dat, rowdim, coldim, nnz);
62 TTimer time;
63 time.clear();
64 time.start();
65 for (size_t i = 0; i < iter; ++i)
66 fspmv(F, matrix, x, 1, y);
67 time.stop();
68 sparse_delete(matrix);
69 return make_pair(time.usertime(), matrix.nElements);
70 }
71
72 template <class T1, class T2, class T> void print_res(pair<T1, T2> &p, size_t iter, T as, int blocksize = 1) {
73 std::cout << "Time: " << p.first / double(iter)
74 << " Gfops: " << (2 * blocksize * p.second) / 1000000000. / p.first * double(iter);
75 FFLAS::writeCommandString(std::cout, as) << std::endl;
76 }
77
78 int main(int argc, char **argv) {
79
80 using Field = Givaro::Modular<int64_t,int64_t>;
81
82 size_t iter = 10;
83 Givaro::Integer q = 1009;
84 int s = 0;
85 std::string matrixFile = "";
86
87 Argument as[] = { { 'q', "-q Q", "Set the field characteristic (-1 for random).", TYPE_INTEGER, &q },
88 { 'i', "-i R", "Set number of repetitions.", TYPE_INT, &iter },
89 { 's', "-s S", "Compute and print matrix statistics.", TYPE_INT, &s },
90 { 'f', "-f FILE", "Set matrix file.", TYPE_STR, &matrixFile },
91 END_OF_ARGUMENTS };
92
93 // matrixFile = "matrix/cis.mk8-8.sms";
94 // matrixFile = "matrix/GL7d17.sms";
95 // matrixFile = "data/mat11.sms";
96
97 FFLAS::parseArguments(argc, argv, as);
98
99 // cout << matrixFile << endl;
100
101 Field F(q);
102
103 index_t *row = nullptr, *col = nullptr;
104 typename Field::Element_ptr dat;
105 index_t rowdim = 0, coldim = 0;
106 uint64_t nnz;
107
108 if ( (matrixFile.find(".sms") != std::string::npos) || (matrixFile.find(".smf") != std::string::npos)) {
109 index_t * st = nullptr ;
110 readSmsFormat(matrixFile, F, st, col, dat, rowdim, coldim, nnz);
111 row = fflas_new<index_t>(nnz);
112 for (index_t j = 0 ; j < rowdim ; ++j) {
113 for (index_t k = st[j] ; k < st[j+1] ; ++k)
114 row[k] = j ;
115 }
116 } else if (matrixFile.find(".spr") != std::string::npos) {
117 readSprFormat(matrixFile, F, row, col, dat, rowdim, coldim, nnz);
118 }
119
120 if (s) {
121 //auto stats = sparse_details::getStat(F, row, col, dat, rowdim, coldim, nnz);
122 //std::cout << "Sparse Matrix statistics : " << std::endl;
123 //stats.print();
124 //std::cout << std::endl;
125 }
126
127 auto x = FFLAS::fflas_new(F, coldim, Alignment::CACHE_LINE);
128 auto y = FFLAS::fflas_new(F, rowdim, Alignment::CACHE_LINE);
129
130 for (size_t i = 0; i < coldim; ++i) {
131 x[i] = 1;
132 }
133
134 for (size_t i = 0; i < rowdim; ++i) {
135 y[i] = 0;
136 }
137
138 auto coo =
139 test_fspmv<Sparse<Field, FFLAS::SparseMatrix_t::COO>>(iter, F, row, col, dat, rowdim, coldim, nnz, x, y, 1);
140 cout << "COO : ";
141 print_res(coo, iter, as);
142 auto coozo =
143 test_fspmv<Sparse<Field, FFLAS::SparseMatrix_t::COO_ZO>>(iter, F, row, col, dat, rowdim, coldim, nnz, x, y, 1);
144 cout << "COO_ZO : ";
145 print_res(coozo, iter, as);
146 auto csr =
147 test_fspmv<Sparse<Field, FFLAS::SparseMatrix_t::CSR>>(iter, F, row, col, dat, rowdim, coldim, nnz, x, y, 1);
148 cout << "CSR : ";
149 print_res(csr, iter, as);
150 auto csrzo =
151 test_fspmv<Sparse<Field, FFLAS::SparseMatrix_t::CSR_ZO>>(iter, F, row, col, dat, rowdim, coldim, nnz, x, y, 1);
152 cout << "CSR_ZO : ";
153 print_res(csrzo, iter, as);
154 auto ell =
155 test_fspmv<Sparse<Field, FFLAS::SparseMatrix_t::ELL>>(iter, F, row, col, dat, rowdim, coldim, nnz, x, y, 1);
156 cout << "ELL : ";
157 print_res(ell, iter, as);
158 auto ellzo =
159 test_fspmv<Sparse<Field, FFLAS::SparseMatrix_t::ELL_ZO>>(iter, F, row, col, dat, rowdim, coldim, nnz, x, y, 1);
160 cout << "ELL_ZO : ";
161 print_res(ellzo, iter, as);
162 // auto ellsimd = test_fspmv<Sparse<Field, FFLAS::SparseMatrix_t::ELL_simd>>(iter, F, row, col, dat, rowdim, coldim,
163 // nnz, x, y, 1);
164 // cout << "ELL_simd : ";
165 // print_res(ellsimd, iter, as);
166 // auto ellsimdzo = test_fspmv<Sparse<Field, FFLAS::SparseMatrix_t::ELL_simd_ZO>>(iter, F, row, col, dat, rowdim,
167 // coldim, nnz, x, y, 1);
168 // cout << "ELL_simd_ZO : ";
169 // print_res(ellsimdzo, iter, as);
170 auto csrhyb =
171 test_fspmv<Sparse<Field, FFLAS::SparseMatrix_t::CSR_HYB>>(iter, F, row, col, dat, rowdim, coldim, nnz, x, y, 1);
172 cout << "CSR_HYB : ";
173 print_res(csrhyb, iter, as);
174 auto hybzo =
175 test_fspmv<Sparse<Field, FFLAS::SparseMatrix_t::HYB_ZO>>(iter, F, row, col, dat, rowdim, coldim, nnz, x, y, 1);
176 cout << "HYB_ZO : ";
177 print_res(hybzo, iter, as);
178 // -----------
179 // Standard output for benchmark - Alexis Breust 2014/11/14
180
181 // std::cout << "Time: " << coo.first / double(iter)
182 // << " Gfops: " << (2*coo.second)/1000000000. / coo.first * double(iter);
183 // FFLAS::writeCommandString(std::cout, as) << std::endl;
184
185 // std::cout << "Time: " << csr.first / double(iter)
186 // << " Gfops: " << (2*csr.second)/1000000000. / csr.first * double(iter);
187 // FFLAS::writeCommandString(std::cout, as) << std::endl;
188 return 0;
189 }
190 /* -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
191 // vim:sts=4:sw=4:ts=4:et:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s