Debian Codegraph @

benchmarks/benchmark-pfspmv.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 <iostream>
23 #include <vector>
24 #include <sstream>
25 #include <cstdio>
26 #include <cstdlib>
27
28 #include "fflas-ffpack/config-blas.h"
29 // #include "fflas-ffpac/field/modular-double.h"
30 #include "fflas-ffpack/fflas/fflas.h"
31 #include "fflas-ffpack/utils/timer.h"
32 #include "fflas-ffpack/utils/args-parser.h"
33
34 #ifdef __FFLASFFPACK_USE_OPENMP
35 typedef FFLAS::OMPTimer TTimer;
36 #else
37 typedef FFLAS::Timer TTimer;
38 #endif
39
40 using namespace std;
41 using namespace FFLAS;
42
43 template <typename T> T from_string(std::string const &s) {
44 std::stringstream ss(s);
45 T result;
46 ss >> result; // TODO handle errors
47 return result;
48 }
49
50
51 template <class MatT, class Field, class IndexT>
52 std::pair<double, uint64_t> test_pfspmv(size_t iter, const Field &F, IndexT *row, IndexT *col,
53 typename Field::Element_ptr dat, index_t rowdim, index_t coldim, uint64_t nnz,
54 typename Field::Element_ptr x, typename Field::Element_ptr y,
55 typename Field::Element beta) {
56 MatT matrix;
57 sparse_init(F, matrix, row, col, dat, rowdim, coldim, nnz);
58 TTimer time;
59 time.clear();
60 time.start();
61 for (size_t i = 0; i < iter; ++i)
62 pfspmv(F, matrix, x, 1, y);
63 time.stop();
64 sparse_delete(matrix);
65 return make_pair(time.usertime(), matrix.nElements);
66 }
67
68 template <class T1, class T2, class T> void print_res(pair<T1, T2> &p, size_t iter, T as, int blocksize = 1) {
69 std::cout << "Time: " << p.first / double(iter)
70 << " Gfops: " << (2 * blocksize * p.second) / 1000000000. / p.first * double(iter);
71 FFLAS::writeCommandString(std::cout, as) << std::endl;
72 }
73
74 int main(int argc, char **argv) {
75
76 using Field = FFPACK::Modular<double>;
77 using Element = typename Field::Element;
78
79 size_t iter = 10;
80 int q = 1009;
81 std::string matrixFile = "";
82
83 Argument as[] = { { 'q', "-q Q", "Set the field characteristic (-1 for random).", TYPE_INT, &q },
84 { 'i', "-i R", "Set number of repetitions.", TYPE_INT, &iter },
85 { 'f', "-f FILE", "Set matrix file.", TYPE_STR, &matrixFile },
86 END_OF_ARGUMENTS };
87
88 matrixFile = "matrix/cis.mk8-8.sms";
89 // matrixFile = "matrix/GL7d17.sms";
90 // matrixFile = "data/mat11.sms";
91 // cout << matrixFile << endl;
92
93 FFLAS::parseArguments(argc, argv, as);
94
95 Field F(q);
96
97 index_t *row = nullptr, *col = nullptr;
98 typename Field::Element_ptr dat;
99 index_t rowdim, coldim;
100 uint64_t nnz;
101
102 if (matrixFile.find(".sms") != std::string::npos) {
103 readSmsFormat(matrixFile, F, row, col, dat, rowdim, coldim, nnz);
104 } else if (matrixFile.find(".spr") != std::string::npos) {
105 readSprFormat(matrixFile, F, row, col, dat, rowdim, coldim, nnz);
106 }
107
108 if (s) {
109 auto stats = sparse_details::getStat(F, row, col, dat, rowdim, coldim, nnz);
110 std::cout << "Sparse Matrix statistics : " << std::endl;
111 stats.print();
112 std::cout << std::endl;
113 }
114
115 auto x = FFLAS::fflas_new(F, coldim, Alignment::CACHE_LINE);
116 auto y = FFLAS::fflas_new(F, rowdim, Alignment::CACHE_LINE);
117
118 for (size_t i = 0; i < coldim; ++i) {
119 x[i] = 1;
120 }
121
122 for (size_t i = 0; i < rowdim; ++i) {
123 y[i] = 0;
124 }
125
126 auto csr =
127 test_pfspmv<Sparse<Field, FFLAS::SparseMatrix_t::CSR>>(iter, F, row, col, dat, rowdim, coldim, nnz, x, y, 1);
128 cout << "CSR : ";
129 print_res(csr, iter, as);
130 auto csrzo =
131 test_pfspmv<Sparse<Field, FFLAS::SparseMatrix_t::CSR_ZO>>(iter, F, row, col, dat, rowdim, coldim, nnz, x, y, 1);
132 cout << "CSR_ZO : ";
133 print_res(csrzo, iter, as);
134 auto ell =
135 test_pfspmv<Sparse<Field, FFLAS::SparseMatrix_t::ELL>>(iter, F, row, col, dat, rowdim, coldim, nnz, x, y, 1);
136 cout << "ELL : ";
137 print_res(ell, iter, as);
138 auto ellzo =
139 test_pfspmv<Sparse<Field, FFLAS::SparseMatrix_t::ELL_ZO>>(iter, F, row, col, dat, rowdim, coldim, nnz, x, y, 1);
140 cout << "ELL_ZO : ";
141 print_res(ellzo, iter, as);
142 auto ellsimd = test_pfspmv<Sparse<Field, FFLAS::SparseMatrix_t::ELL_simd>>(iter, F, row, col, dat, rowdim, coldim,
143 nnz, x, y, 1);
144 cout << "ELL_simd : ";
145 print_res(ellsimd, iter, as);
146 auto ellsimdzo = test_pfspmv<Sparse<Field, FFLAS::SparseMatrix_t::ELL_simd_ZO>>(iter, F, row, col, dat, rowdim,
147 coldim, nnz, x, y, 1);
148 cout << "ELL_simd_ZO : ";
149 print_res(ellsimdzo, iter, as);
150 // auto csrhyb = test_fspmv<Sparse<Field, FFLAS::SparseMatrix_t::CSR_HYB>>(iter, F, row, col, dat, rowdim, coldim,
151 // nnz, x, y, 1);
152 // cout << "CSR_HYB : ";
153 // print_res(csrhyb, iter, as);
154 // -----------
155 // Standard output for benchmark - Alexis Breust 2014/11/14
156
157 // std::cout << "Time: " << coo.first / double(iter)
158 // << " Gfops: " << (2*coo.second)/1000000000. / coo.first * double(iter);
159 // FFLAS::writeCommandString(std::cout, as) << std::endl;
160
161 // std::cout << "Time: " << csr.first / double(iter)
162 // << " Gfops: " << (2*csr.second)/1000000000. / csr.first * double(iter);
163 // FFLAS::writeCommandString(std::cout, as) << std::endl;
164 return 0;
165 }
166 /* -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
167 // vim:sts=4:sw=4:ts=4:et:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s