Go to the documentation of this file.00001
00002
00003 #include <iostream>
00004 #include <Eigen/Sparse>
00005
00006
00007
00008 #define NOGMM
00009 #define NOMTL
00010
00011 #ifndef SIZE
00012 #define SIZE 10
00013 #endif
00014
00015 #ifndef DENSITY
00016 #define DENSITY 0.01
00017 #endif
00018
00019 #ifndef REPEAT
00020 #define REPEAT 1
00021 #endif
00022
00023 #include "BenchSparseUtil.h"
00024
00025 #ifndef MINDENSITY
00026 #define MINDENSITY 0.0004
00027 #endif
00028
00029 #ifndef NBTRIES
00030 #define NBTRIES 10
00031 #endif
00032
00033 #define BENCH(X) \
00034 timer.reset(); \
00035 for (int _j=0; _j<NBTRIES; ++_j) { \
00036 timer.start(); \
00037 for (int _k=0; _k<REPEAT; ++_k) { \
00038 X \
00039 } timer.stop(); }
00040
00041
00042 typedef SparseMatrix<Scalar,SelfAdjoint|LowerTriangular> EigenSparseSelfAdjointMatrix;
00043
00044 void fillSpdMatrix(float density, int rows, int cols, EigenSparseSelfAdjointMatrix& dst)
00045 {
00046 dst.startFill(rows*cols*density);
00047 for(int j = 0; j < cols; j++)
00048 {
00049 dst.fill(j,j) = internal::random<Scalar>(10,20);
00050 for(int i = j+1; i < rows; i++)
00051 {
00052 Scalar v = (internal::random<float>(0,1) < density) ? internal::random<Scalar>() : 0;
00053 if (v!=0)
00054 dst.fill(i,j) = v;
00055 }
00056
00057 }
00058 dst.endFill();
00059 }
00060
00061 #include <Eigen/Cholesky>
00062
00063 template<int Backend>
00064 void doEigen(const char* name, const EigenSparseSelfAdjointMatrix& sm1, int flags = 0)
00065 {
00066 std::cout << name << "..." << std::flush;
00067 BenchTimer timer;
00068 timer.start();
00069 SparseLLT<EigenSparseSelfAdjointMatrix,Backend> chol(sm1, flags);
00070 timer.stop();
00071 std::cout << ":\t" << timer.value() << endl;
00072
00073 std::cout << " nnz: " << sm1.nonZeros() << " => " << chol.matrixL().nonZeros() << "\n";
00074
00075 }
00076
00077 int main(int argc, char *argv[])
00078 {
00079 int rows = SIZE;
00080 int cols = SIZE;
00081 float density = DENSITY;
00082 BenchTimer timer;
00083
00084 VectorXf b = VectorXf::Random(cols);
00085 VectorXf x = VectorXf::Random(cols);
00086
00087 bool densedone = false;
00088
00089
00090
00091 {
00092 EigenSparseSelfAdjointMatrix sm1(rows, cols);
00093 std::cout << "Generate sparse matrix (might take a while)...\n";
00094 fillSpdMatrix(density, rows, cols, sm1);
00095 std::cout << "DONE\n\n";
00096
00097
00098 #ifdef DENSEMATRIX
00099 if (!densedone)
00100 {
00101 densedone = true;
00102 std::cout << "Eigen Dense\t" << density*100 << "%\n";
00103 DenseMatrix m1(rows,cols);
00104 eiToDense(sm1, m1);
00105 m1 = (m1 + m1.transpose()).eval();
00106 m1.diagonal() *= 0.5;
00107
00108
00109
00110
00111 BenchTimer timer;
00112 timer.start();
00113 LLT<DenseMatrix> chol(m1);
00114 timer.stop();
00115 std::cout << "dense:\t" << timer.value() << endl;
00116 int count = 0;
00117 for (int j=0; j<cols; ++j)
00118 for (int i=j; i<rows; ++i)
00119 if (!internal::isMuchSmallerThan(internal::abs(chol.matrixL()(i,j)), 0.1))
00120 count++;
00121 std::cout << "dense: " << "nnz = " << count << "\n";
00122
00123 }
00124 #endif
00125
00126
00127 doEigen<Eigen::DefaultBackend>("Eigen/Sparse", sm1, Eigen::IncompleteFactorization);
00128
00129 #ifdef EIGEN_CHOLMOD_SUPPORT
00130 doEigen<Eigen::Cholmod>("Eigen/Cholmod", sm1, Eigen::IncompleteFactorization);
00131 #endif
00132
00133 #ifdef EIGEN_TAUCS_SUPPORT
00134 doEigen<Eigen::Taucs>("Eigen/Taucs", sm1, Eigen::IncompleteFactorization);
00135 #endif
00136
00137 #if 0
00138
00139 {
00140 taucs_ccs_matrix A = sm1.asTaucsMatrix();
00141
00142
00143
00144
00145
00146 taucs_ccs_matrix* chol = taucs_ccs_factor_llt(&A, 0, 0);
00147
00148 for (int j=0; j<cols; ++j)
00149 {
00150 for (int i=chol->colptr[j]; i<chol->colptr[j+1]; ++i)
00151 std::cout << chol->values.d[i] << " ";
00152 }
00153 }
00154
00155
00156 #ifdef EIGEN_CHOLMOD_SUPPORT
00157 {
00158 cholmod_common c;
00159 cholmod_start (&c);
00160 cholmod_sparse A;
00161 cholmod_factor *L;
00162
00163 A = sm1.asCholmodMatrix();
00164 BenchTimer timer;
00165
00166 timer.start();
00167 std::vector<int> perm(cols);
00168
00169 for (int i=0; i<cols; ++i)
00170 perm[i] = i;
00171
00172
00173
00174 c.nmethods = 1;
00175 c.method [0].ordering = CHOLMOD_NATURAL;
00176 c.postorder = 0;
00177 c.final_ll = 1;
00178
00179 L = cholmod_analyze_p(&A, &perm[0], &perm[0], cols, &c);
00180 timer.stop();
00181 std::cout << "cholmod/analyze:\t" << timer.value() << endl;
00182 timer.reset();
00183 timer.start();
00184 cholmod_factorize(&A, L, &c);
00185 timer.stop();
00186 std::cout << "cholmod/factorize:\t" << timer.value() << endl;
00187
00188 cholmod_sparse* cholmat = cholmod_factor_to_sparse(L, &c);
00189
00190 cholmod_print_factor(L, "Factors", &c);
00191
00192 cholmod_print_sparse(cholmat, "Chol", &c);
00193 cholmod_write_sparse(stdout, cholmat, 0, 0, &c);
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204 }
00205 #endif
00206
00207 #endif
00208
00209
00210
00211 }
00212
00213
00214 return 0;
00215 }
00216