BenchSparseUtil.h
Go to the documentation of this file.
1 
2 #include <Eigen/Sparse>
3 #include <bench/BenchTimer.h>
4 #include <set>
5 
6 using namespace std;
7 using namespace Eigen;
8 using 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 
22 typedef SCALAR Scalar;
26 
27 void fillMatrix(float density, int rows, int cols, EigenSparseMatrix& dst) {
28  dst.reserve(double(rows) * cols * density);
29  for (int j = 0; j < cols; j++) {
30  for (int i = 0; i < rows; i++) {
31  Scalar v = (internal::random<float>(0, 1) < density) ? internal::random<Scalar>() : 0;
32  if (v != 0) dst.insert(i, j) = v;
33  }
34  }
35  dst.finalize();
36 }
37 
38 void fillMatrix2(int nnzPerCol, int rows, int cols, EigenSparseMatrix& dst) {
39  // std::cout << "alloc " << nnzPerCol*cols << "\n";
40  dst.reserve(nnzPerCol * cols);
41  for (int j = 0; j < cols; j++) {
42  std::set<int> aux;
43  for (int i = 0; i < nnzPerCol; i++) {
44  int k = internal::random<int>(0, rows - 1);
45  while (aux.find(k) != aux.end()) k = internal::random<int>(0, rows - 1);
46  aux.insert(k);
47 
48  dst.insert(k, j) = internal::random<Scalar>();
49  }
50  }
51  dst.finalize();
52 }
53 
54 void eiToDense(const EigenSparseMatrix& src, DenseMatrix& dst) {
55  dst.setZero();
56  for (int j = 0; j < src.cols(); ++j)
57  for (EigenSparseMatrix::InnerIterator it(src.derived(), j); it; ++it) dst(it.index(), j) = it.value();
58 }
59 
60 #ifndef NOGMM
61 #include "gmm/gmm.h"
62 typedef gmm::csc_matrix<Scalar> GmmSparse;
63 typedef gmm::col_matrix<gmm::wsvector<Scalar> > GmmDynSparse;
64 void eiToGmm(const EigenSparseMatrix& src, GmmSparse& dst) {
65  GmmDynSparse tmp(src.rows(), src.cols());
66  for (int j = 0; j < src.cols(); ++j)
67  for (EigenSparseMatrix::InnerIterator it(src.derived(), j); it; ++it) tmp(it.index(), j) = it.value();
68  gmm::copy(tmp, dst);
69 }
70 #endif
71 
72 #ifndef NOMTL
73 #include <boost/numeric/mtl/mtl.hpp>
74 typedef mtl::compressed2D<Scalar, mtl::matrix::parameters<mtl::tag::col_major> > MtlSparse;
75 typedef mtl::compressed2D<Scalar, mtl::matrix::parameters<mtl::tag::row_major> > MtlSparseRowMajor;
76 void eiToMtl(const EigenSparseMatrix& src, MtlSparse& dst) {
77  mtl::matrix::inserter<MtlSparse> ins(dst);
78  for (int j = 0; j < src.cols(); ++j)
79  for (EigenSparseMatrix::InnerIterator it(src.derived(), j); it; ++it) ins[it.index()][j] = it.value();
80 }
81 #endif
82 
83 #ifdef CSPARSE
84 extern "C" {
85 #include "cs.h"
86 }
87 void eiToCSparse(const EigenSparseMatrix& src, cs*& dst) {
88  cs* aux = cs_spalloc(0, 0, 1, 1, 1);
89  for (int j = 0; j < src.cols(); ++j)
90  for (EigenSparseMatrix::InnerIterator it(src.derived(), j); it; ++it)
91  if (!cs_entry(aux, it.index(), j, it.value())) {
92  std::cout << "cs_entry error\n";
93  exit(2);
94  }
95  dst = cs_compress(aux);
96  // cs_spfree(aux);
97 }
98 #endif // CSPARSE
99 
100 #ifndef NOUBLAS
101 #include <boost/numeric/ublas/vector.hpp>
102 #include <boost/numeric/ublas/matrix.hpp>
103 #include <boost/numeric/ublas/io.hpp>
104 #include <boost/numeric/ublas/triangular.hpp>
105 #include <boost/numeric/ublas/vector_sparse.hpp>
106 #include <boost/numeric/ublas/matrix_sparse.hpp>
107 #include <boost/numeric/ublas/vector_of_vector.hpp>
108 #include <boost/numeric/ublas/operation.hpp>
109 
110 typedef boost::numeric::ublas::compressed_matrix<Scalar, boost::numeric::ublas::column_major> UBlasSparse;
111 
112 void eiToUblas(const EigenSparseMatrix& src, UBlasSparse& dst) {
113  dst.resize(src.rows(), src.cols(), false);
114  for (int j = 0; j < src.cols(); ++j)
115  for (EigenSparseMatrix::InnerIterator it(src.derived(), j); it; ++it) dst(it.index(), j) = it.value();
116 }
117 
118 template <typename EigenType, typename UblasType>
119 void eiToUblasVec(const EigenType& src, UblasType& dst) {
120  dst.resize(src.size());
121  for (int j = 0; j < src.size(); ++j) dst[j] = src.coeff(j);
122 }
123 #endif
124 
125 #ifdef OSKI
126 extern "C" {
127 #include <oski/oski.h>
128 }
129 #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:64
void eiToUblasVec(const EigenType &src, UblasType &dst)
Definition: BenchSparseUtil.h:119
SparseMatrix< Scalar > EigenSparseMatrix
Definition: BenchSparseUtil.h:25
mtl::compressed2D< Scalar, mtl::matrix::parameters< mtl::tag::row_major > > MtlSparseRowMajor
Definition: BenchSparseUtil.h:75
void eiToDense(const EigenSparseMatrix &src, DenseMatrix &dst)
Definition: BenchSparseUtil.h:54
void fillMatrix(float density, int rows, int cols, EigenSparseMatrix &dst)
Definition: BenchSparseUtil.h:27
gmm::col_matrix< gmm::wsvector< Scalar > > GmmDynSparse
Definition: BenchSparseUtil.h:63
mtl::compressed2D< Scalar, mtl::matrix::parameters< mtl::tag::col_major > > MtlSparse
Definition: BenchSparseUtil.h:74
Matrix< Scalar, Dynamic, Dynamic > DenseMatrix
Definition: BenchSparseUtil.h:23
#define SCALAR
Definition: BenchSparseUtil.h:19
void fillMatrix2(int nnzPerCol, int rows, int cols, EigenSparseMatrix &dst)
Definition: BenchSparseUtil.h:38
gmm::csc_matrix< Scalar > GmmSparse
Definition: BenchSparseUtil.h:62
boost::numeric::ublas::compressed_matrix< Scalar, boost::numeric::ublas::column_major > UBlasSparse
Definition: BenchSparseUtil.h:110
void eiToMtl(const EigenSparseMatrix &src, MtlSparse &dst)
Definition: BenchSparseUtil.h:76
Matrix< Scalar, Dynamic, 1 > DenseVector
Definition: BenchSparseUtil.h:24
void eiToUblas(const EigenSparseMatrix &src, UBlasSparse &dst)
Definition: BenchSparseUtil.h:112
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
EIGEN_DEVICE_FUNC Derived & setZero(Index size)
Definition: CwiseNullaryOp.h:569
const Derived & derived() const
Definition: SparseMatrixBase.h:144
Index cols() const
Definition: SparseMatrix.h:161
void finalize()
Definition: SparseMatrix.h:461
Index rows() const
Definition: SparseMatrix.h:159
Base::InnerIterator InnerIterator
Definition: SparseMatrix.h:138
void reserve(Index reserveSize)
Definition: SparseMatrix.h:315
Scalar & insert(Index row, Index col)
Definition: SparseMatrix.h:1586
EIGEN_BLAS_FUNC() copy(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Definition: level1_impl.h:32
char char char int int * k
Definition: level2_impl.h:374
Eigen::Matrix< Scalar, Dynamic, Dynamic, ColMajor > tmp
Definition: level3_impl.h:365
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
density
Definition: UniformPSDSelfTest.py:19
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2