sparse_cholesky.cpp File Reference
#include <iostream>
#include <Eigen/Sparse>
#include "BenchSparseUtil.h"
#include <Eigen/Cholesky>

Macros

#define NOGMM
 
#define NOMTL
 
#define SIZE   10
 
#define DENSITY   0.01
 
#define REPEAT   1
 
#define MINDENSITY   0.0004
 
#define NBTRIES   10
 
#define BENCH(X)
 

Typedefs

typedef SparseMatrix< Scalar, SelfAdjoint|LowerTriangular > EigenSparseSelfAdjointMatrix
 

Functions

void fillSpdMatrix (float density, int rows, int cols, EigenSparseSelfAdjointMatrix &dst)
 
template<int Backend>
void doEigen (const char *name, const EigenSparseSelfAdjointMatrix &sm1, int flags=0)
 
int main (int argc, char *argv[])
 

Macro Definition Documentation

◆ BENCH

#define BENCH (   X)
Value:
timer.reset(); \
for (int _j = 0; _j < NBTRIES; ++_j) { \
timer.start(); \
for (int _k = 0; _k < REPEAT; ++_k) { \
X \
} \
timer.stop(); \
}
#define REPEAT
Definition: sparse_cholesky.cpp:30
#define NBTRIES
Definition: sparse_cholesky.cpp:40

◆ DENSITY

#define DENSITY   0.01

◆ MINDENSITY

#define MINDENSITY   0.0004

◆ NBTRIES

#define NBTRIES   10

◆ NOGMM

#define NOGMM

◆ NOMTL

#define NOMTL

◆ REPEAT

#define REPEAT   1

◆ SIZE

#define SIZE   10

Typedef Documentation

◆ EigenSparseSelfAdjointMatrix

typedef SparseMatrix<Scalar, SelfAdjoint | LowerTriangular> EigenSparseSelfAdjointMatrix

Function Documentation

◆ doEigen()

template<int Backend>
void doEigen ( const char name,
const EigenSparseSelfAdjointMatrix sm1,
int  flags = 0 
)
71  {
72  std::cout << name << "..." << std::flush;
74  timer.start();
75  SparseLLT<EigenSparseSelfAdjointMatrix, Backend> chol(sm1, flags);
76  timer.stop();
77  std::cout << ":\t" << timer.value() << endl;
78 
79  std::cout << " nnz: " << sm1.nonZeros() << " => " << chol.matrixL().nonZeros() << "\n";
80  // std::cout << "sparse\n" << chol.matrixL() << "%\n";
81 }
Definition: BenchTimer.h:55
Index nonZeros() const
Definition: SparseCompressedBase.h:64
string name
Definition: plotDoE.py:33
double timer
Definition: oomph_metis_from_parmetis_3.1.1/struct.h:210

References plotDoE::name, and Eigen::SparseMatrix< Scalar_, Options_, StorageIndex_ >::nonZeros().

◆ fillSpdMatrix()

void fillSpdMatrix ( float  density,
int  rows,
int  cols,
EigenSparseSelfAdjointMatrix dst 
)
56  {
57  dst.startFill(rows * cols * density);
58  for (int j = 0; j < cols; j++) {
59  dst.fill(j, j) = internal::random<Scalar>(10, 20);
60  for (int i = j + 1; i < rows; i++) {
61  Scalar v = (internal::random<float>(0, 1) < density) ? internal::random<Scalar>() : 0;
62  if (v != 0) dst.fill(i, j) = v;
63  }
64  }
65  dst.endFill();
66 }
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
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:45
density
Definition: UniformPSDSelfTest.py:19
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2

References cols, UniformPSDSelfTest::density, i, j, rows, and v.

Referenced by main().

◆ main()

int main ( int argc  ,
char argv[] 
)
83  {
84  int rows = SIZE;
85  int cols = SIZE;
86  float density = DENSITY;
88 
89  VectorXf b = VectorXf::Random(cols);
90  VectorXf x = VectorXf::Random(cols);
91 
92  bool densedone = false;
93 
94  // for (float density = DENSITY; density>=MINDENSITY; density*=0.5)
95  // float density = 0.5;
96  {
98  std::cout << "Generate sparse matrix (might take a while)...\n";
100  std::cout << "DONE\n\n";
101 
102 // dense matrices
103 #ifdef DENSEMATRIX
104  if (!densedone) {
105  densedone = true;
106  std::cout << "Eigen Dense\t" << density * 100 << "%\n";
108  eiToDense(sm1, m1);
109  m1 = (m1 + m1.transpose()).eval();
110  m1.diagonal() *= 0.5;
111 
112  // BENCH(LLT<DenseMatrix> chol(m1);)
113  // std::cout << "dense:\t" << timer.value() << endl;
114 
116  timer.start();
117  LLT<DenseMatrix> chol(m1);
118  timer.stop();
119  std::cout << "dense:\t" << timer.value() << endl;
120  int count = 0;
121  for (int j = 0; j < cols; ++j)
122  for (int i = j; i < rows; ++i)
123  if (!internal::isMuchSmallerThan(internal::abs(chol.matrixL()(i, j)), 0.1)) count++;
124  std::cout << "dense: "
125  << "nnz = " << count << "\n";
126  // std::cout << "dense:\n" << m1 << "\n\n" << chol.matrixL() << endl;
127  }
128 #endif
129 
130  // eigen sparse matrices
131  doEigen<Eigen::DefaultBackend>("Eigen/Sparse", sm1, Eigen::IncompleteFactorization);
132 
133 #ifdef EIGEN_CHOLMOD_SUPPORT
134  doEigen<Eigen::Cholmod>("Eigen/Cholmod", sm1, Eigen::IncompleteFactorization);
135 #endif
136 
137 #ifdef EIGEN_TAUCS_SUPPORT
138  doEigen<Eigen::Taucs>("Eigen/Taucs", sm1, Eigen::IncompleteFactorization);
139 #endif
140 
141 #if 0
142  // TAUCS
143  {
144  taucs_ccs_matrix A = sm1.asTaucsMatrix();
145 
146  //BENCH(taucs_ccs_matrix* chol = taucs_ccs_factor_llt(&A, 0, 0);)
147 // BENCH(taucs_supernodal_factor_to_ccs(taucs_ccs_factor_llt_ll(&A));)
148 // std::cout << "taucs:\t" << timer.value() << endl;
149 
150  taucs_ccs_matrix* chol = taucs_ccs_factor_llt(&A, 0, 0);
151 
152  for (int j=0; j<cols; ++j)
153  {
154  for (int i=chol->colptr[j]; i<chol->colptr[j+1]; ++i)
155  std::cout << chol->values.d[i] << " ";
156  }
157  }
158 
159  // CHOLMOD
160 #ifdef EIGEN_CHOLMOD_SUPPORT
161  {
162  cholmod_common c;
163  cholmod_start (&c);
164  cholmod_sparse A;
165  cholmod_factor *L;
166 
167  A = sm1.asCholmodMatrix();
169 // timer.reset();
170  timer.start();
171  std::vector<int> perm(cols);
172 // std::vector<int> set(ncols);
173  for (int i=0; i<cols; ++i)
174  perm[i] = i;
175 // c.nmethods = 1;
176 // c.method[0] = 1;
177 
178  c.nmethods = 1;
179  c.method [0].ordering = CHOLMOD_NATURAL;
180  c.postorder = 0;
181  c.final_ll = 1;
182 
183  L = cholmod_analyze_p(&A, &perm[0], &perm[0], cols, &c);
184  timer.stop();
185  std::cout << "cholmod/analyze:\t" << timer.value() << endl;
186  timer.reset();
187  timer.start();
188  cholmod_factorize(&A, L, &c);
189  timer.stop();
190  std::cout << "cholmod/factorize:\t" << timer.value() << endl;
191 
192  cholmod_sparse* cholmat = cholmod_factor_to_sparse(L, &c);
193 
194  cholmod_print_factor(L, "Factors", &c);
195 
196  cholmod_print_sparse(cholmat, "Chol", &c);
197  cholmod_write_sparse(stdout, cholmat, 0, 0, &c);
198 //
199 // cholmod_print_sparse(&A, "A", &c);
200 // cholmod_write_sparse(stdout, &A, 0, 0, &c);
201 
202 
203 // for (int j=0; j<cols; ++j)
204 // {
205 // for (int i=chol->colptr[j]; i<chol->colptr[j+1]; ++i)
206 // std::cout << chol->values.s[i] << " ";
207 // }
208  }
209 #endif
210 
211 #endif
212  }
213 
214  return 0;
215 }
AnnoyingScalar abs(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:135
void eiToDense(const EigenSparseMatrix &src, DenseMatrix &dst)
Definition: BenchSparseUtil.h:54
Matrix3d m1
Definition: IOFormat.cpp:2
MatrixXd L
Definition: LLT_example.cpp:6
Scalar * b
Definition: benchVecAdd.cpp:17
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:47
Standard Cholesky decomposition (LL^T) of a matrix and associated features.
Definition: LLT.h:70
A versatible sparse matrix representation.
Definition: SparseMatrix.h:121
EIGEN_DEVICE_FUNC bool isMuchSmallerThan(const Scalar &x, const OtherScalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
Definition: MathFunctions.h:1916
int c
Definition: calibrate.py:100
stdout
Definition: fix_broken_doxygen_formulae.py:258
list x
Definition: plotDoE.py:28
#define SIZE
Definition: sparse_cholesky.cpp:22
#define DENSITY
Definition: sparse_cholesky.cpp:26
void fillSpdMatrix(float density, int rows, int cols, EigenSparseSelfAdjointMatrix &dst)
Definition: sparse_cholesky.cpp:56
internal::nested_eval< T, 1 >::type eval(const T &xpr)
Definition: sparse_permutations.cpp:47

References abs(), b, calibrate::c, cols, UniformPSDSelfTest::density, DENSITY, eiToDense(), eval(), fillSpdMatrix(), i, Eigen::internal::isMuchSmallerThan(), j, L, m1, Eigen::LLT< MatrixType_, UpLo_ >::matrixL(), rows, SIZE, fix_broken_doxygen_formulae::stdout, and plotDoE::x.