| 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/fflas/fflas_sparse.h" |
| 32 |
#include "fflas-ffpack/utils/timer.h" |
| 33 |
#include "fflas-ffpack/utils/args-parser.h" |
| 34 |
|
| 35 |
#ifdef __FFLASFFPACK_USE_OPENMP |
| 36 |
typedef FFLAS::OMPTimer TTimer; |
| 37 |
#else |
| 38 |
typedef FFLAS::Timer TTimer; |
| 39 |
#endif |
| 40 |
|
| 41 |
using namespace std; |
| 42 |
using namespace FFLAS; |
| 43 |
|
| 44 |
template <typename T> T from_string(std::string const &s) { |
| 45 |
std::stringstream ss(s); |
| 46 |
T result; |
| 47 |
ss >> result; // TODO handle errors |
| 48 |
return result; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
template <class MatT, class Field, class IndexT> |
| 53 |
std::pair<double, uint64_t> test_fspmm(size_t iter, const Field &F, IndexT *row, IndexT *col, |
| 54 |
typename Field::Element_ptr dat, index_t rowdim, index_t coldim, uint64_t nnz, |
| 55 |
int blocksize, typename Field::Element_ptr x, int ldx, |
| 56 |
typename Field::Element beta, typename Field::Element_ptr y, int ldy) { |
| 57 |
MatT matrix; |
| 58 |
sparse_init(F, matrix, row, col, dat, rowdim, coldim, nnz); |
| 59 |
TTimer time; |
| 60 |
time.clear(); |
| 61 |
time.start(); |
| 62 |
for (size_t i = 0; i < iter; ++i) |
| 63 |
fspmm(F, matrix, blocksize, x, ldx, 1, y, ldy); |
| 64 |
time.stop(); |
| 65 |
sparse_delete(matrix); |
| 66 |
return make_pair(time.usertime(), matrix.nElements); |
| 67 |
} |
| 68 |
|
| 69 |
template <class T1, class T2, class T> void print_res(pair<T1, T2> &p, size_t iter, T as, int blocksize) { |
| 70 |
// cout << 2*p.second*blocksize*iter << endl; |
| 71 |
std::cout << "Time: " << p.first / double(iter) |
| 72 |
<< " Gfops: " << ((2*blocksize*p.second)/(1000000.*p.first))*(double(iter)/1000) ; |
| 73 |
FFLAS::writeCommandString(std::cout, as) << std::endl; |
| 74 |
} |
| 75 |
|
| 76 |
int main(int argc, char **argv) { |
| 77 |
|
| 78 |
using Field = Givaro::Modular<double>; |
| 79 |
using Element = typename Field::Element; |
| 80 |
|
| 81 |
size_t iter = 10; |
| 82 |
int q = 1009; |
| 83 |
int blocksize = 4; |
| 84 |
int s = 0; |
| 85 |
std::string matrixFile = ""; |
| 86 |
|
| 87 |
Argument as[] = { { 'q', "-q Q", "Set the field characteristic (-1 for random).", TYPE_INT, &q }, |
| 88 |
{ 'b', "-b Q", "Set the block size.", TYPE_INT, &blocksize }, |
| 89 |
{ 'i', "-i R", "Set number of repetitions.", TYPE_INT, &iter }, |
| 90 |
{ 's', "-s S", "Compute and print matrix statistics.", TYPE_INT, &s }, |
| 91 |
{ 'f', "-f FILE", "Set matrix file.", TYPE_STR, &matrixFile }, |
| 92 |
END_OF_ARGUMENTS }; |
| 93 |
|
| 94 |
// matrixFile = "matrix/cis.mk8-8.sms"; |
| 95 |
// matrixFile = "matrix/M06-D9.sms"; |
| 96 |
// matrixFile = "matrix/GL7d17.sms"; |
| 97 |
// matrixFile = "data/mat11.sms"; |
| 98 |
|
| 99 |
FFLAS::parseArguments(argc, argv, as); |
| 100 |
|
| 101 |
// cout << matrixFile << endl; |
| 102 |
|
| 103 |
Field F(q); |
| 104 |
|
| 105 |
index_t *row = nullptr, *col = nullptr; |
| 106 |
typename Field::Element_ptr dat; |
| 107 |
index_t rowdim, coldim; |
| 108 |
uint64_t nnz; |
| 109 |
|
| 110 |
index_t * st = nullptr ; |
| 111 |
readSmsFormat(matrixFile, F, st, col, dat, rowdim, coldim, nnz); |
| 112 |
row = fflas_new<index_t>(nnz); |
| 113 |
for (index_t j = 0 ; j < rowdim ; ++j) { |
| 114 |
for (index_t k = st[j] ; k < st[j+1] ; ++k) |
| 115 |
row[k] = j ; |
| 116 |
} |
| 117 |
|
| 118 |
if (s) { |
| 119 |
// auto stats = sparse_details::getStat(F, row, col, dat, rowdim, coldim, nnz); |
| 120 |
// std::cout << "Sparse Matrix statistics : " << std::endl; |
| 121 |
// stats.print(); |
| 122 |
std::cout << std::endl; |
| 123 |
} |
| 124 |
|
| 125 |
auto x = FFLAS::fflas_new(F, coldim, blocksize, Alignment::CACHE_LINE); |
| 126 |
auto y = FFLAS::fflas_new(F, rowdim, blocksize, Alignment::CACHE_LINE); |
| 127 |
|
| 128 |
for (size_t i = 0; i < coldim * blocksize; ++i) { |
| 129 |
x[i] = 1; |
| 130 |
} |
| 131 |
|
| 132 |
for (size_t i = 0; i < rowdim * blocksize; ++i) { |
| 133 |
y[i] = 0; |
| 134 |
} |
| 135 |
|
| 136 |
// auto coo = test_fspmm<Sparse<Field, FFLAS::SparseMatrix_t::COO>>(iter, F, row, col, dat, rowdim, coldim, nnz, |
| 137 |
// blocksize, x, blocksize, 1, y, blocksize); |
| 138 |
// cout << "COO : "; |
| 139 |
// print_res(coo, iter, as); |
| 140 |
|
| 141 |
// auto coozo = test_fspmm<Sparse<Field, FFLAS::SparseMatrix_t::COO_ZO>>(iter, F, row, col, dat, rowdim, coldim, |
| 142 |
// nnz, blocksize, x, blocksize, 1, y, blocksize); |
| 143 |
// cout << "COO_ZO : "; |
| 144 |
// print_res(coozo, iter, as); |
| 145 |
auto csr = test_fspmm<Sparse<Field, FFLAS::SparseMatrix_t::CSR>>(iter, F, row, col, dat, rowdim, coldim, nnz, |
| 146 |
blocksize, x, blocksize, 1, y, blocksize); |
| 147 |
cout << "CSR : "; |
| 148 |
print_res(csr, iter, as, blocksize); |
| 149 |
auto ell = test_fspmm<Sparse<Field, FFLAS::SparseMatrix_t::ELL>>(iter, F, row, col, dat, rowdim, coldim, nnz, |
| 150 |
blocksize, x, blocksize, 1, y, blocksize); |
| 151 |
cout << "ELL : "; |
| 152 |
print_res(ell, iter, as, blocksize); |
| 153 |
auto ellzo = test_fspmm<Sparse<Field, FFLAS::SparseMatrix_t::ELL_ZO>>(iter, F, row, col, dat, rowdim, coldim, nnz, |
| 154 |
blocksize, x, blocksize, 1, y, blocksize); |
| 155 |
cout << "ELL_ZO : "; |
| 156 |
print_res(ellzo, iter, as, blocksize); |
| 157 |
// auto csrzo = test_fspmm<Sparse<Field, FFLAS::SparseMatrix_t::CSR_ZO>>(iter, F, row, col, dat, rowdim, coldim, |
| 158 |
// nnz, blocksize, x, blocksize, 1, y, blocksize); |
| 159 |
// cout << "CSR_ZO : "; |
| 160 |
// print_res(csrzo, iter, as); |
| 161 |
// auto ell = test_fspmm<Sparse<Field, FFLAS::SparseMatrix_t::ELL>>(iter, F, row, col, dat, rowdim, coldim, nnz, |
| 162 |
// blocksize, x, blocksize, 1, y, blocksize); |
| 163 |
// cout << "ELL : "; |
| 164 |
// print_res(ell, iter, as); |
| 165 |
// auto ellzo = test_fspmm<Sparse<Field, FFLAS::SparseMatrix_t::ELL_ZO>>(iter, F, row, col, dat, rowdim, coldim, |
| 166 |
// nnz, blocksize, x, blocksize, 1, y, blocksize); |
| 167 |
// cout << "ELL_ZO : "; |
| 168 |
// print_res(ellzo, iter, as); |
| 169 |
auto hybzo = test_fspmm<Sparse<Field, FFLAS::SparseMatrix_t::HYB_ZO>>(iter, F, row, col, dat, rowdim, coldim, nnz, |
| 170 |
blocksize, x, blocksize, 1, y, blocksize); |
| 171 |
cout << "HYB_ZO : "; |
| 172 |
print_res(hybzo, iter, as, blocksize); |
| 173 |
auto csrhyb = test_fspmm<Sparse<Field, FFLAS::SparseMatrix_t::CSR_HYB>>(iter, F, row, col, dat, rowdim, coldim, nnz, |
| 174 |
blocksize, x, blocksize, 1, y, blocksize); |
| 175 |
cout << "CSR_HYB : "; |
| 176 |
print_res(csrhyb, iter, as, blocksize); |
| 177 |
// for (size_t i = 0; i < 10*blocksize; ++i) { |
| 178 |
// std::cout << y[i] << " "; |
| 179 |
// } |
| 180 |
// std::cout << std::endl; |
| 181 |
|
| 182 |
// ----------- |
| 183 |
// Standard output for benchmark - Alexis Breust 2014/11/14 |
| 184 |
|
| 185 |
// std::cout << "Time: " << coo.first / double(iter) |
| 186 |
// << " Gfops: " << (2*coo.second)/1000000000. / coo.first * double(iter); |
| 187 |
// FFLAS::writeCommandString(std::cout, as) << std::endl; |
| 188 |
|
| 189 |
// std::cout << "Time: " << csr.first / double(iter) |
| 190 |
// << " Gfops: " << (2*csr.second)/1000000000. / csr.first * double(iter); |
| 191 |
// FFLAS::writeCommandString(std::cout, as) << std::endl; |
| 192 |
fflas_delete(x); |
| 193 |
fflas_delete(y); |
| 194 |
return 0; |
| 195 |
} |
| 196 |
/* -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 197 |
// vim:sts=4:sw=4:ts=4:et:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s |