1 // #define EIGEN_TAUCS_SUPPORT
2 // #define EIGEN_CHOLMOD_SUPPORT
4 #include <Eigen/Sparse>
6 // g++ -DSIZE=10000 -DDENSITY=0.001 sparse_cholesky.cpp -I.. -DDENSEMATRI -O3 -g0 -DNDEBUG -DNBTRIES=1 -I /home/gael/Coding/LinearAlgebra/taucs_full/src/ -I/home/gael/Coding/LinearAlgebra/taucs_full/build/linux/ -L/home/gael/Coding/LinearAlgebra/taucs_full/lib/linux/ -ltaucs /home/gael/Coding/LinearAlgebra/GotoBLAS/libgoto.a -lpthread -I /home/gael/Coding/LinearAlgebra/SuiteSparse/CHOLMOD/Include/ $CHOLLIB -I /home/gael/Coding/LinearAlgebra/SuiteSparse/UFconfig/ /home/gael/Coding/LinearAlgebra/SuiteSparse/CCOLAMD/Lib/libccolamd.a /home/gael/Coding/LinearAlgebra/SuiteSparse/CHOLMOD/Lib/libcholmod.a -lmetis /home/gael/Coding/LinearAlgebra/SuiteSparse/AMD/Lib/libamd.a /home/gael/Coding/LinearAlgebra/SuiteSparse/CAMD/Lib/libcamd.a /home/gael/Coding/LinearAlgebra/SuiteSparse/CCOLAMD/Lib/libccolamd.a /home/gael/Coding/LinearAlgebra/SuiteSparse/COLAMD/Lib/libcolamd.a -llapack && ./a.out
23 #include "BenchSparseUtil.h"
26 #define MINDENSITY 0.0004
35 for (int _j=0; _j<NBTRIES; ++_j) { \
37 for (int _k=0; _k<REPEAT; ++_k) { \
41 // typedef SparseMatrix<Scalar,UpperTriangular> EigenSparseTriMatrix;
42 typedef SparseMatrix
<Scalar
,SelfAdjoint
|LowerTriangular
> EigenSparseSelfAdjointMatrix
;
44 void fillSpdMatrix(float density
, int rows
, int cols
, EigenSparseSelfAdjointMatrix
& dst
)
46 dst
.startFill(rows
*cols
*density
);
47 for(int j
= 0; j
< cols
; j
++)
49 dst
.fill(j
,j
) = internal::random
<Scalar
>(10,20);
50 for(int i
= j
+1; i
< rows
; i
++)
52 Scalar v
= (internal::random
<float>(0,1) < density
) ? internal::random
<Scalar
>() : 0;
61 #include <Eigen/Cholesky>
64 void doEigen(const char* name
, const EigenSparseSelfAdjointMatrix
& sm1
, int flags
= 0)
66 std::cout
<< name
<< "..." << std::flush
;
69 SparseLLT
<EigenSparseSelfAdjointMatrix
,Backend
> chol(sm1
, flags
);
71 std::cout
<< ":\t" << timer
.value() << endl
;
73 std::cout
<< " nnz: " << sm1
.nonZeros() << " => " << chol
.matrixL().nonZeros() << "\n";
74 // std::cout << "sparse\n" << chol.matrixL() << "%\n";
77 int main(int argc
, char *argv
[])
81 float density
= DENSITY
;
84 VectorXf b
= VectorXf::Random(cols
);
85 VectorXf x
= VectorXf::Random(cols
);
87 bool densedone
= false;
89 //for (float density = DENSITY; density>=MINDENSITY; density*=0.5)
90 // float density = 0.5;
92 EigenSparseSelfAdjointMatrix
sm1(rows
, cols
);
93 std::cout
<< "Generate sparse matrix (might take a while)...\n";
94 fillSpdMatrix(density
, rows
, cols
, sm1
);
95 std::cout
<< "DONE\n\n";
102 std::cout
<< "Eigen Dense\t" << density
*100 << "%\n";
103 DenseMatrix
m1(rows
,cols
);
105 m1
= (m1
+ m1
.transpose()).eval();
106 m1
.diagonal() *= 0.5;
108 // BENCH(LLT<DenseMatrix> chol(m1);)
109 // std::cout << "dense:\t" << timer.value() << endl;
113 LLT
<DenseMatrix
> chol(m1
);
115 std::cout
<< "dense:\t" << timer
.value() << endl
;
117 for (int j
=0; j
<cols
; ++j
)
118 for (int i
=j
; i
<rows
; ++i
)
119 if (!internal::isMuchSmallerThan(internal::abs(chol
.matrixL()(i
,j
)), 0.1))
121 std::cout
<< "dense: " << "nnz = " << count
<< "\n";
122 // std::cout << "dense:\n" << m1 << "\n\n" << chol.matrixL() << endl;
126 // eigen sparse matrices
127 doEigen
<Eigen::DefaultBackend
>("Eigen/Sparse", sm1
, Eigen::IncompleteFactorization
);
129 #ifdef EIGEN_CHOLMOD_SUPPORT
130 doEigen
<Eigen::Cholmod
>("Eigen/Cholmod", sm1
, Eigen::IncompleteFactorization
);
133 #ifdef EIGEN_TAUCS_SUPPORT
134 doEigen
<Eigen::Taucs
>("Eigen/Taucs", sm1
, Eigen::IncompleteFactorization
);
140 taucs_ccs_matrix A
= sm1
.asTaucsMatrix();
142 //BENCH(taucs_ccs_matrix* chol = taucs_ccs_factor_llt(&A, 0, 0);)
143 // BENCH(taucs_supernodal_factor_to_ccs(taucs_ccs_factor_llt_ll(&A));)
144 // std::cout << "taucs:\t" << timer.value() << endl;
146 taucs_ccs_matrix
* chol
= taucs_ccs_factor_llt(&A
, 0, 0);
148 for (int j
=0; j
<cols
; ++j
)
150 for (int i
=chol
->colptr
[j
]; i
<chol
->colptr
[j
+1]; ++i
)
151 std::cout
<< chol
->values
.d
[i
] << " ";
156 #ifdef EIGEN_CHOLMOD_SUPPORT
163 A
= sm1
.asCholmodMatrix();
167 std::vector
<int> perm(cols
);
168 // std::vector<int> set(ncols);
169 for (int i
=0; i
<cols
; ++i
)
175 c
.method
[0].ordering
= CHOLMOD_NATURAL
;
179 L
= cholmod_analyze_p(&A
, &perm
[0], &perm
[0], cols
, &c
);
181 std::cout
<< "cholmod/analyze:\t" << timer
.value() << endl
;
184 cholmod_factorize(&A
, L
, &c
);
186 std::cout
<< "cholmod/factorize:\t" << timer
.value() << endl
;
188 cholmod_sparse
* cholmat
= cholmod_factor_to_sparse(L
, &c
);
190 cholmod_print_factor(L
, "Factors", &c
);
192 cholmod_print_sparse(cholmat
, "Chol", &c
);
193 cholmod_write_sparse(stdout
, cholmat
, 0, 0, &c
);
195 // cholmod_print_sparse(&A, "A", &c);
196 // cholmod_write_sparse(stdout, &A, 0, 0, &c);
199 // for (int j=0; j<cols; ++j)
201 // for (int i=chol->colptr[j]; i<chol->colptr[j+1]; ++i)
202 // std::cout << chol->values.s[i] << " ";