mirror of
https://gitlab.com/libeigen/eigen.git
synced 2024-12-15 07:10:37 +08:00
Add a draft (not clean ) version of the COLAMD ordering implementation
This commit is contained in:
parent
773804691a
commit
b0cba2d988
File diff suppressed because it is too large
Load Diff
@ -27,6 +27,7 @@
|
||||
#define EIGEN_ORDERING_H
|
||||
|
||||
#include "Amd.h"
|
||||
#include "Eigen_Colamd.h"
|
||||
namespace Eigen {
|
||||
namespace internal {
|
||||
|
||||
@ -112,54 +113,50 @@ class NaturalOrdering
|
||||
* Get the column approximate minimum degree ordering
|
||||
* The matrix should be in column-major format
|
||||
*/
|
||||
// template<typename Scalar, typename Index>
|
||||
// class COLAMDOrdering: public OrderingBase< ColamdOrdering<Scalar, Index> >
|
||||
// {
|
||||
// public:
|
||||
// typedef OrderingBase< ColamdOrdering<Scalar, Index> > Base;
|
||||
// typedef SparseMatrix<Scalar,ColMajor,Index> MatrixType;
|
||||
//
|
||||
// public:
|
||||
// COLAMDOrdering():Base() {}
|
||||
//
|
||||
// COLAMDOrdering(const MatrixType& matrix):Base()
|
||||
// {
|
||||
// compute(matrix);
|
||||
// }
|
||||
// COLAMDOrdering(const MatrixType& mat, PermutationType& perm_c):Base()
|
||||
// {
|
||||
// compute(matrix);
|
||||
// perm_c = this.get_perm();
|
||||
// }
|
||||
// void compute(const MatrixType& mat)
|
||||
// {
|
||||
// // Test if the matrix is column major...
|
||||
//
|
||||
// int m = mat.rows();
|
||||
// int n = mat.cols();
|
||||
// int nnz = mat.nonZeros();
|
||||
// // Get the recommended value of Alen to be used by colamd
|
||||
// int Alen = colamd_recommended(nnz, m, n);
|
||||
// // Set the default parameters
|
||||
// double knobs[COLAMD_KNOBS];
|
||||
// colamd_set_defaults(knobs);
|
||||
//
|
||||
// int info;
|
||||
// VectorXi p(n), A(nnz);
|
||||
// for(int i=0; i < n; i++) p(i) = mat.outerIndexPtr()(i);
|
||||
// for(int i=0; i < nnz; i++) A(i) = mat.innerIndexPtr()(i);
|
||||
// // Call Colamd routine to compute the ordering
|
||||
// info = colamd(m, n, Alen, A,p , knobs, stats)
|
||||
// eigen_assert( (info != FALSE)&& "COLAMD failed " );
|
||||
//
|
||||
// m_P.resize(n);
|
||||
// for (int i = 0; i < n; i++) m_P(p(i)) = i;
|
||||
// m_isInitialized = true;
|
||||
// }
|
||||
// protected:
|
||||
// using Base::m_isInitialized;
|
||||
// using Base m_P;
|
||||
// };
|
||||
template<typename Index>
|
||||
class COLAMDOrdering;
|
||||
#include "Eigen_Colamd.h"
|
||||
|
||||
template<typename Index>
|
||||
class COLAMDOrdering
|
||||
{
|
||||
public:
|
||||
typedef PermutationMatrix<Dynamic, Dynamic, Index> PermutationType;
|
||||
typedef Matrix<Index, Dynamic, 1> IndexVector;
|
||||
/** Compute the permutation vector form a sparse matrix */
|
||||
|
||||
|
||||
|
||||
template <typename MatrixType>
|
||||
void operator() (const MatrixType& mat, PermutationType& perm)
|
||||
{
|
||||
int m = mat.rows();
|
||||
int n = mat.cols();
|
||||
int nnz = mat.nonZeros();
|
||||
// Get the recommended value of Alen to be used by colamd
|
||||
int Alen = eigen_colamd_recommended(nnz, m, n);
|
||||
// Set the default parameters
|
||||
double knobs [EIGEN_COLAMD_KNOBS];
|
||||
int stats [EIGEN_COLAMD_STATS];
|
||||
eigen_colamd_set_defaults(knobs);
|
||||
|
||||
int info;
|
||||
IndexVector p(n+1), A(Alen);
|
||||
for(int i=0; i <= n; i++) p(i) = mat.outerIndexPtr()[i];
|
||||
for(int i=0; i < nnz; i++) A(i) = mat.innerIndexPtr()[i];
|
||||
// Call Colamd routine to compute the ordering
|
||||
info = eigen_colamd(m, n, Alen, A.data(), p.data(), knobs, stats);
|
||||
eigen_assert( info && "COLAMD failed " );
|
||||
|
||||
perm.resize(n);
|
||||
for (int i = 0; i < n; i++) perm.indices()(p(i)) = i;
|
||||
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
|
||||
};
|
||||
|
||||
} // end namespace Eigen
|
||||
#endif
|
@ -99,8 +99,29 @@ class SparseLU
|
||||
{
|
||||
m_diagpivotthresh = thresh;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** Return the number of nonzero elements in the L factor */
|
||||
int nnzL()
|
||||
{
|
||||
if (m_factorizationIsOk)
|
||||
return m_nnzL;
|
||||
else
|
||||
{
|
||||
std::cerr<<"Numerical factorization should be done before\n";
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
/** Return the number of nonzero elements in the U factor */
|
||||
int nnzU()
|
||||
{
|
||||
if (m_factorizationIsOk)
|
||||
return m_nnzU;
|
||||
else
|
||||
{
|
||||
std::cerr<<"Numerical factorization should be done before\n";
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
/** \returns the solution X of \f$ A X = B \f$ using the current decomposition of A.
|
||||
*
|
||||
* \sa compute()
|
||||
@ -325,7 +346,8 @@ void SparseLU<MatrixType, OrderingType>::analyzePattern(const MatrixType& mat)
|
||||
ord(mat,m_perm_c);
|
||||
//FIXME Check the right semantic behind m_perm_c
|
||||
// that is, column j of mat goes to column m_perm_c(j) of mat * m_perm_c;
|
||||
|
||||
|
||||
|
||||
// Apply the permutation to the column of the input matrix
|
||||
m_mat = mat * m_perm_c.inverse(); //FIXME It should be less expensive here to permute only the structural pattern of the matrix
|
||||
|
||||
|
@ -628,7 +628,7 @@ void SuperLU<MatrixType>::factorize(const MatrixType& a)
|
||||
this->initFactorization(a);
|
||||
|
||||
//DEBUG
|
||||
m_sluOptions.ColPerm = NATURAL;
|
||||
// m_sluOptions.ColPerm = COLAMD;
|
||||
m_sluOptions.Equil = NO;
|
||||
int info = 0;
|
||||
RealScalar recip_pivot_growth, rcond;
|
||||
|
@ -6,6 +6,7 @@
|
||||
#include <iomanip>
|
||||
#include <unsupported/Eigen/SparseExtra>
|
||||
#include <Eigen/SparseLU>
|
||||
#include <bench/BenchTimer.h>
|
||||
|
||||
using namespace std;
|
||||
using namespace Eigen;
|
||||
@ -17,10 +18,12 @@ int main(int argc, char **args)
|
||||
typedef Matrix<double, Dynamic, Dynamic> DenseMatrix;
|
||||
typedef Matrix<double, Dynamic, 1> DenseRhs;
|
||||
VectorXd b, x, tmp;
|
||||
SparseLU<SparseMatrix<double, ColMajor>, AMDOrdering<int> > solver;
|
||||
// SparseLU<SparseMatrix<double, ColMajor>, AMDOrdering<int> > solver;
|
||||
SparseLU<SparseMatrix<double, ColMajor>, COLAMDOrdering<int> > solver;
|
||||
ifstream matrix_file;
|
||||
string line;
|
||||
int n;
|
||||
BenchTimer timer;
|
||||
|
||||
// Set parameters
|
||||
/* Fill the matrix with sparse matrix stored in Matrix-Market coordinate column-oriented format */
|
||||
@ -53,13 +56,26 @@ int main(int argc, char **args)
|
||||
|
||||
/* Compute the factorization */
|
||||
// solver.isSymmetric(true);
|
||||
solver.compute(A);
|
||||
|
||||
timer.start();
|
||||
// solver.compute(A);
|
||||
solver.analyzePattern(A);
|
||||
timer.stop();
|
||||
cout << "Time to analyze " << timer.value() << std::endl;
|
||||
timer.reset();
|
||||
timer.start();
|
||||
solver.factorize(A);
|
||||
timer.stop();
|
||||
cout << "Factorize Time " << timer.value() << std::endl;
|
||||
timer.reset();
|
||||
timer.start();
|
||||
solver._solve(b, x);
|
||||
timer.stop();
|
||||
cout << "solve time " << timer.value() << std::endl;
|
||||
/* Check the accuracy */
|
||||
VectorXd tmp2 = b - A*x;
|
||||
double tempNorm = tmp2.norm()/b.norm();
|
||||
cout << "Relative norm of the computed solution : " << tempNorm <<"\n";
|
||||
cout << "Number of nonzeros in the factor : " << solver.nnzL() + solver.nnzU() << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user