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