TR-mbed 1.0
Loading...
Searching...
No Matches
BenchSparseUtil.h
Go to the documentation of this file.
1
2#include <Eigen/Sparse>
3#include <bench/BenchTimer.h>
4#include <set>
5
6using namespace std;
7using namespace Eigen;
8using namespace Eigen;
9
10#ifndef SIZE
11#define SIZE 1024
12#endif
13
14#ifndef DENSITY
15#define DENSITY 0.01
16#endif
17
18#ifndef SCALAR
19#define SCALAR double
20#endif
21
22typedef SCALAR Scalar;
26
27void fillMatrix(float density, int rows, int cols, EigenSparseMatrix& dst)
28{
29 dst.reserve(double(rows)*cols*density);
30 for(int j = 0; j < cols; j++)
31 {
32 for(int i = 0; i < rows; i++)
33 {
35 if (v!=0)
36 dst.insert(i,j) = v;
37 }
38 }
39 dst.finalize();
40}
41
42void fillMatrix2(int nnzPerCol, int rows, int cols, EigenSparseMatrix& dst)
43{
44// std::cout << "alloc " << nnzPerCol*cols << "\n";
45 dst.reserve(nnzPerCol*cols);
46 for(int j = 0; j < cols; j++)
47 {
48 std::set<int> aux;
49 for(int i = 0; i < nnzPerCol; i++)
50 {
51 int k = internal::random<int>(0,rows-1);
52 while (aux.find(k)!=aux.end())
54 aux.insert(k);
55
57 }
58 }
59 dst.finalize();
60}
61
63{
64 dst.setZero();
65 for (int j=0; j<src.cols(); ++j)
66 for (EigenSparseMatrix::InnerIterator it(src.derived(), j); it; ++it)
67 dst(it.index(),j) = it.value();
68}
69
70#ifndef NOGMM
71#include "gmm/gmm.h"
72typedef gmm::csc_matrix<Scalar> GmmSparse;
73typedef gmm::col_matrix< gmm::wsvector<Scalar> > GmmDynSparse;
74void eiToGmm(const EigenSparseMatrix& src, GmmSparse& dst)
75{
76 GmmDynSparse tmp(src.rows(), src.cols());
77 for (int j=0; j<src.cols(); ++j)
78 for (EigenSparseMatrix::InnerIterator it(src.derived(), j); it; ++it)
79 tmp(it.index(),j) = it.value();
80 gmm::copy(tmp, dst);
81}
82#endif
83
84#ifndef NOMTL
85#include <boost/numeric/mtl/mtl.hpp>
86typedef mtl::compressed2D<Scalar, mtl::matrix::parameters<mtl::tag::col_major> > MtlSparse;
87typedef mtl::compressed2D<Scalar, mtl::matrix::parameters<mtl::tag::row_major> > MtlSparseRowMajor;
88void eiToMtl(const EigenSparseMatrix& src, MtlSparse& dst)
89{
90 mtl::matrix::inserter<MtlSparse> ins(dst);
91 for (int j=0; j<src.cols(); ++j)
92 for (EigenSparseMatrix::InnerIterator it(src.derived(), j); it; ++it)
93 ins[it.index()][j] = it.value();
94}
95#endif
96
97#ifdef CSPARSE
98extern "C" {
99#include "cs.h"
100}
101void eiToCSparse(const EigenSparseMatrix& src, cs* &dst)
102{
103 cs* aux = cs_spalloc (0, 0, 1, 1, 1);
104 for (int j=0; j<src.cols(); ++j)
105 for (EigenSparseMatrix::InnerIterator it(src.derived(), j); it; ++it)
106 if (!cs_entry(aux, it.index(), j, it.value()))
107 {
108 std::cout << "cs_entry error\n";
109 exit(2);
110 }
111 dst = cs_compress(aux);
112// cs_spfree(aux);
113}
114#endif // CSPARSE
115
116#ifndef NOUBLAS
117#include <boost/numeric/ublas/vector.hpp>
118#include <boost/numeric/ublas/matrix.hpp>
119#include <boost/numeric/ublas/io.hpp>
120#include <boost/numeric/ublas/triangular.hpp>
121#include <boost/numeric/ublas/vector_sparse.hpp>
122#include <boost/numeric/ublas/matrix_sparse.hpp>
123#include <boost/numeric/ublas/vector_of_vector.hpp>
124#include <boost/numeric/ublas/operation.hpp>
125
126typedef boost::numeric::ublas::compressed_matrix<Scalar,boost::numeric::ublas::column_major> UBlasSparse;
127
129{
130 dst.resize(src.rows(), src.cols(), false);
131 for (int j=0; j<src.cols(); ++j)
132 for (EigenSparseMatrix::InnerIterator it(src.derived(), j); it; ++it)
133 dst(it.index(),j) = it.value();
134}
135
136template <typename EigenType, typename UblasType>
137void eiToUblasVec(const EigenType& src, UblasType& dst)
138{
139 dst.resize(src.size());
140 for (int j=0; j<src.size(); ++j)
141 dst[j] = src.coeff(j);
142}
143#endif
144
145#ifdef OSKI
146extern "C" {
147#include <oski/oski.h>
148}
149#endif
Array< int, Dynamic, 1 > v
Definition Array_initializer_list_vector_cxx11.cpp:1
SCALAR Scalar
Definition BenchSparseUtil.h:22
void eiToGmm(const EigenSparseMatrix &src, GmmSparse &dst)
Definition BenchSparseUtil.h:74
void eiToUblasVec(const EigenType &src, UblasType &dst)
Definition BenchSparseUtil.h:137
SparseMatrix< Scalar > EigenSparseMatrix
Definition BenchSparseUtil.h:25
Matrix< Scalar, Dynamic, 1 > DenseVector
Definition BenchSparseUtil.h:24
mtl::compressed2D< Scalar, mtl::matrix::parameters< mtl::tag::row_major > > MtlSparseRowMajor
Definition BenchSparseUtil.h:87
void eiToDense(const EigenSparseMatrix &src, DenseMatrix &dst)
Definition BenchSparseUtil.h:62
void fillMatrix(float density, int rows, int cols, EigenSparseMatrix &dst)
Definition BenchSparseUtil.h:27
gmm::col_matrix< gmm::wsvector< Scalar > > GmmDynSparse
Definition BenchSparseUtil.h:73
mtl::compressed2D< Scalar, mtl::matrix::parameters< mtl::tag::col_major > > MtlSparse
Definition BenchSparseUtil.h:86
boost::numeric::ublas::compressed_matrix< Scalar, boost::numeric::ublas::column_major > UBlasSparse
Definition BenchSparseUtil.h:126
#define SCALAR
Definition BenchSparseUtil.h:19
void fillMatrix2(int nnzPerCol, int rows, int cols, EigenSparseMatrix &dst)
Definition BenchSparseUtil.h:42
gmm::csc_matrix< Scalar > GmmSparse
Definition BenchSparseUtil.h:72
void eiToMtl(const EigenSparseMatrix &src, MtlSparse &dst)
Definition BenchSparseUtil.h:88
Matrix< Scalar, Dynamic, Dynamic > DenseMatrix
Definition BenchSparseUtil.h:23
void eiToUblas(const EigenSparseMatrix &src, UBlasSparse &dst)
Definition BenchSparseUtil.h:128
int i
Definition BiCGSTAB_step_by_step.cpp:9
int rows
Definition Tutorial_commainit_02.cpp:1
int cols
Definition Tutorial_commainit_02.cpp:1
SCALAR Scalar
Definition bench_gemm.cpp:46
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:180
EIGEN_DEVICE_FUNC Derived & setZero(Index size)
Definition CwiseNullaryOp.h:562
Definition SparseCompressedBase.h:159
const Derived & derived() const
Definition SparseMatrixBase.h:143
A versatible sparse matrix representation.
Definition SparseMatrix.h:98
void reserve(Index reserveSize)
Definition SparseMatrix.h:264
void finalize()
Definition SparseMatrix.h:425
Index rows() const
Definition SparseMatrix.h:138
Index cols() const
Definition SparseMatrix.h:140
Scalar & insert(Index row, Index col)
Definition SparseMatrix.h:1244
Namespace containing all symbols from the Eigen library.
Definition bench_norm.cpp:85
Definition BFloat16.h:88
Definition ForwardDeclarations.h:17
std::ptrdiff_t j
Definition tut_arithmetic_redux_minmax.cpp:2