| 1 |
#ifndef LINEAR_SOLVERS_INCLUDE |
| 2 |
#define LINEAR_SOLVERS_INCLUDE |
| 3 |
|
| 4 |
#ifdef USE_CHOLMOD |
| 5 |
#include <Cholmod/cholmod.h> |
| 6 |
#if defined( WIN32 ) || defined( _WIN64 ) |
| 7 |
#pragma message( "[WARNING] Need to explicitly exclude VCOMP.lib" ) |
| 8 |
#pragma comment( lib , "CHOLMOD_FULL.lib" ) |
| 9 |
#endif // WIN32 || _WIN64 |
| 10 |
#ifdef DLONG |
| 11 |
typedef long long SOLVER_LONG; |
| 12 |
#define CHOLMOD( name ) cholmod_l_ ## name |
| 13 |
#else // !DLONG |
| 14 |
typedef int SOLVER_LONG; |
| 15 |
#define CHOLMOD( name ) cholmod_ ## name |
| 16 |
#endif // DLONG |
| 17 |
#elif defined(EIGEN_USE_MKL_ALL) |
| 18 |
#pragma comment( lib , "mkl_core.lib" ) |
| 19 |
#pragma comment( lib , "mkl_intel_lp64.lib" ) |
| 20 |
#pragma comment( lib , "mkl_intel_thread.lib" ) |
| 21 |
#pragma comment( lib , "mkl_blas95_lp64.lib" ) |
| 22 |
#pragma comment( lib , "libiomp5md.lib" ) |
| 23 |
#endif // USE_CHOLMOD |
| 24 |
|
| 25 |
#include "SparseMatrixInterface.h" |
| 26 |
|
| 27 |
inline double SquareNorm( const double* values , int dim ){ double norm2 = 0 ; for( int i=0 ; i<dim ; i++ ) norm2 += values[i] * values[i] ; return norm2; } |
| 28 |
inline double SquareNorm( const float* values , int dim ){ double norm2 = 0 ; for( int i=0 ; i<dim ; i++ ) norm2 += values[i] * values[i] ; return norm2; } |
| 29 |
template< class Type > inline double SquareNorm( const Type* values , int dim ){ double norm2 = 0 ; for( int i=0 ; i<dim ; i++ ) norm2 += values[dim].squareNorm() ; return norm2 ; } |
| 30 |
|
| 31 |
inline double SquareDifference( const double* values1 , const double* values2 , int dim ){ double norm2 = 0 ; for( int i=0 ; i<dim ; i++ ) norm2 += ( values1[i] - values2[i] ) * ( values1[i] - values2[i] ) ; return norm2; } |
| 32 |
inline double SquareDifference( const float* values1 , const float* values2 , int dim ){ double norm2 = 0 ; for( int i=0 ; i<dim ; i++ ) norm2 += ( values1[i] - values2[i] ) * ( values1[i] - values2[i] ) ; return norm2; } |
| 33 |
template< class Type > inline double SquareDifference( const Type* values1 , const Type* values2 , int dim ){ double norm2 = 0 ; for( int i=0 ; i<dim ; i++ ) norm2 += ( values1[dim] - values2[dim] ).squareNorm() ; return norm2 ; } |
| 34 |
|
| 35 |
|
| 36 |
// This is the conjugate gradients solver. |
| 37 |
// The assumption is that the class SPDOperator defines a method operator()( const Real* , Real* ) which corresponds to applying a symmetric positive-definite operator. |
| 38 |
template< class Real > |
| 39 |
struct CGScratch |
| 40 |
{ |
| 41 |
Real *r , *d , *q; |
| 42 |
CGScratch( void ) : r(NULL) , d(NULL) , q(NULL) , _dim(0){ ; } |
| 43 |
CGScratch( int dim ) : r(NULL) , d(NULL) , q(NULL){ resize(dim); } |
| 44 |
~CGScratch( void ){ resize(0); } |
| 45 |
void resize( int dim ) |
| 46 |
{ |
| 47 |
if( dim!=_dim ) |
| 48 |
{ |
| 49 |
if( r ) delete[] r ; r = NULL; |
| 50 |
if( d ) delete[] d ; d = NULL; |
| 51 |
if( q ) delete[] q ; q = NULL; |
| 52 |
if( dim ) r = new Real[dim] , d = new Real[dim] , q = new Real[dim]; |
| 53 |
_dim = dim; |
| 54 |
} |
| 55 |
} |
| 56 |
protected: |
| 57 |
int _dim; |
| 58 |
}; |
| 59 |
template< class Real > |
| 60 |
struct PreconditionedCGScratch : public CGScratch< Real > |
| 61 |
{ |
| 62 |
Real *s; |
| 63 |
PreconditionedCGScratch( void ) : CGScratch< Real >() , s(NULL){ ; } |
| 64 |
PreconditionedCGScratch( int dim ) : CGScratch< Real >() { resize(dim); } |
| 65 |
~PreconditionedCGScratch( void ){ resize(0); } |
| 66 |
void resize( int dim ) |
| 67 |
{ |
| 68 |
if( dim!=CGScratch< Real >::_dim ) |
| 69 |
{ |
| 70 |
if( s ) delete[] s; s = NULL; |
| 71 |
if( dim ) s = new Real[dim]; |
| 72 |
} |
| 73 |
CGScratch< Real >::resize( dim ); |
| 74 |
} |
| 75 |
}; |
| 76 |
template< class Real > |
| 77 |
struct DiagonalPreconditioner |
| 78 |
{ |
| 79 |
Real* iDiagonal; |
| 80 |
DiagonalPreconditioner( void ) : iDiagonal(NULL) , _dim(0){ ; } |
| 81 |
~DiagonalPreconditioner( void ){ if( iDiagonal ) delete[] iDiagonal ; iDiagonal = NULL; } |
| 82 |
template< class MatrixRowIterator > |
| 83 |
void set( const SparseMatrixInterface< Real , MatrixRowIterator >& M ) |
| 84 |
{ |
| 85 |
if( _dim!=M.rows() ) |
| 86 |
{ |
| 87 |
_dim = (int)M.rows(); |
| 88 |
if( iDiagonal ) delete[] iDiagonal , iDiagonal = NULL; |
| 89 |
if( _dim>0 ) iDiagonal = new Real[_dim]; |
| 90 |
} |
| 91 |
memset( iDiagonal , 0 , sizeof(Real)*_dim ); |
| 92 |
#pragma omp parallel for |
| 93 |
for( int i=0 ; i<M.rows() ; i++ ) |
| 94 |
{ |
| 95 |
for( MatrixRowIterator iter=M.begin(i) ; iter!=M.end(i) ; iter++ ) if( iter->N==i ) iDiagonal[i] += iter->Value; |
| 96 |
iDiagonal[i] = (Real)1./iDiagonal[i]; |
| 97 |
} |
| 98 |
} |
| 99 |
void operator()( const Real* in , Real* out ) const |
| 100 |
{ |
| 101 |
#pragma omp parallel for |
| 102 |
for( int i=0 ; i<_dim ; i++ ) out[i] = in[i] * iDiagonal[i]; |
| 103 |
} |
| 104 |
protected: |
| 105 |
int _dim; |
| 106 |
}; |
| 107 |
|
| 108 |
template< class Real , class SPDOperator > |
| 109 |
int SolveCG( SPDOperator& L , int iters , int dim , const Real* b , Real* x , CGScratch< Real >* scratch=NULL , double eps=1e-8 , int threads=1 , bool verbose=false ) |
| 110 |
{ |
| 111 |
eps *= eps; |
| 112 |
Real *r , *d , *q; |
| 113 |
if( scratch ) r = scratch->r , d = scratch->d , q = scratch->q; |
| 114 |
else r = new Real[dim] , d = new Real[dim] , q = new Real[dim]; |
| 115 |
memset( r , 0 , sizeof(Real)*dim ) , memset( d , 0 , sizeof(Real)*dim ) , memset( q , 0 , sizeof(Real)*dim ); |
| 116 |
double delta_new = 0 , delta_0; |
| 117 |
|
| 118 |
L( x , r ); |
| 119 |
#pragma omp parallel for num_threads( threads ) reduction( + : delta_new ) |
| 120 |
for( int i=0 ; i<dim ; i++ ) d[i] = r[i] = b[i] - r[i] , delta_new += r[i] * r[i]; |
| 121 |
|
| 122 |
delta_0 = delta_new; |
| 123 |
if( delta_new<eps ) |
| 124 |
{ |
| 125 |
if( !scratch ) delete[] r , delete[] d , delete[] q; |
| 126 |
return 0; |
| 127 |
} |
| 128 |
|
| 129 |
int ii; |
| 130 |
for( ii=0 ; ii<iters && delta_new>eps*delta_0 ; ii++ ) |
| 131 |
{ |
| 132 |
L( d , q ); |
| 133 |
double dDotQ = 0; |
| 134 |
#pragma omp parallel for num_threads( threads ) reduction( + : dDotQ ) |
| 135 |
for( int i=0 ; i<dim ; i++ ) dDotQ += d[i] * q[i]; |
| 136 |
Real alpha = Real( delta_new / dDotQ ); |
| 137 |
|
| 138 |
double delta_old = delta_new; |
| 139 |
delta_new = 0; |
| 140 |
|
| 141 |
const int RESET_COUNT = 50; |
| 142 |
if( (ii%RESET_COUNT)==(RESET_COUNT-1) ) |
| 143 |
{ |
| 144 |
#pragma omp parallel for num_threads( threads ) |
| 145 |
for( int i=0 ; i<dim ; i++ ) x[i] += d[i] * alpha; |
| 146 |
L( x , r ); |
| 147 |
#pragma omp parallel for num_threads( threads ) reduction ( + : delta_new ) |
| 148 |
for( int i=0 ; i<dim ; i++ ) r[i] = b[i] - r[i] , delta_new += r[i] * r[i]; |
| 149 |
} |
| 150 |
else |
| 151 |
#pragma omp parallel for num_threads( threads ) reduction( + : delta_new ) |
| 152 |
for( int i=0 ; i<dim ; i++ ) r[i] -= q[i] * alpha , delta_new += r[i] * r[i] , x[i] += d[i] * alpha; |
| 153 |
|
| 154 |
Real beta = Real( delta_new / delta_old ); |
| 155 |
#pragma omp parallel for num_threads( threads ) |
| 156 |
for( int i=0 ; i<dim ; i++ ) d[i] = r[i] + d[i] * beta; |
| 157 |
} |
| 158 |
if( verbose ) |
| 159 |
{ |
| 160 |
L( x , r ); |
| 161 |
#pragma omp parallel for num_threads( threads ) |
| 162 |
for( int i=0 ; i<dim ; i++ ) r[i] -= b[i]; |
| 163 |
printf( "CG: %d %g -> %g\n" , ii , SquareNorm( b , dim ) , SquareNorm( r , dim ) ); |
| 164 |
} |
| 165 |
if( !scratch ) delete[] r , delete[] d , delete[] q; |
| 166 |
return ii; |
| 167 |
} |
| 168 |
template< class Real , class SPDOperator , class SPDPreconditioner > |
| 169 |
int SolvePreconditionedCG( SPDOperator& L , SPDPreconditioner& Pinverse , int iters , int dim , const Real* b , Real* x , PreconditionedCGScratch< Real >* scratch=NULL , double eps=1e-8 , int threads=1 , bool verbose=false ) |
| 170 |
{ |
| 171 |
eps *= eps; |
| 172 |
Real *r , *d , *q , *s; |
| 173 |
if( scratch ) r = scratch->r , d = scratch->d , q = scratch->q , s = scratch->s; |
| 174 |
else r = new Real[dim] , d = new Real[dim] , q = new Real[dim] , s = new Real[dim]; |
| 175 |
memset( r , 0 , sizeof(Real)*dim ) , memset( d , 0 , sizeof(Real)*dim ) , memset( q , 0 , sizeof(Real)*dim ) , memset( s , 0 , sizeof(Real)*dim ); |
| 176 |
double delta_new = 0 , delta_0; |
| 177 |
|
| 178 |
L( x , r ); |
| 179 |
#pragma omp parallel for num_threads( threads ) |
| 180 |
for( int i=0 ; i<dim ; i++ ) r[i] = b[i] - r[i]; |
| 181 |
Pinverse( r , d ); |
| 182 |
#pragma omp parallel for num_threads( threads ) reduction( + : delta_new ) |
| 183 |
for( int i=0 ; i<dim ; i++ ) delta_new += r[i] * d[i]; |
| 184 |
|
| 185 |
delta_0 = delta_new; |
| 186 |
if( delta_new<eps ) |
| 187 |
{ |
| 188 |
if( !scratch ) delete[] r , delete[] d , delete[] q; |
| 189 |
return 0; |
| 190 |
} |
| 191 |
int ii; |
| 192 |
for( ii=0 ; ii<iters && delta_new>eps*delta_0 ; ii++ ) |
| 193 |
{ |
| 194 |
L( d , q ); |
| 195 |
double dDotQ = 0; |
| 196 |
#pragma omp parallel for num_threads( threads ) reduction( + : dDotQ ) |
| 197 |
for( int i=0 ; i<dim ; i++ ) dDotQ += d[i] * q[i]; |
| 198 |
Real alpha = Real( delta_new / dDotQ ); |
| 199 |
|
| 200 |
const int RESET_COUNT = 50; |
| 201 |
#pragma omp parallel for num_threads( threads ) |
| 202 |
for( int i=0 ; i<dim ; i++ ) x[i] += d[i] * alpha; |
| 203 |
if( (ii%RESET_COUNT)==(RESET_COUNT-1) ) |
| 204 |
{ |
| 205 |
L( x , r ); |
| 206 |
#pragma omp parallel for num_threads( threads ) |
| 207 |
for( int i=0 ; i<dim ; i++ ) r[i] = b[i] - r[i]; |
| 208 |
} |
| 209 |
else |
| 210 |
#pragma omp parallel for num_threads( threads ) reduction( + : delta_new ) |
| 211 |
for( int i=0 ; i<dim ; i++ ) r[i] -= q[i] * alpha; |
| 212 |
Pinverse( r , s ); |
| 213 |
|
| 214 |
double delta_old = delta_new; |
| 215 |
delta_new = 0; |
| 216 |
#pragma omp parallel for num_threads( threads ) reduction( + : delta_new ) |
| 217 |
for( int i=0 ; i<dim ; i++ ) delta_new += r[i] * s[i]; |
| 218 |
|
| 219 |
Real beta = Real( delta_new / delta_old ); |
| 220 |
#pragma omp parallel for num_threads( threads ) |
| 221 |
for( int i=0 ; i<dim ; i++ ) d[i] = s[i] + d[i] * beta; |
| 222 |
} |
| 223 |
if( verbose ) |
| 224 |
{ |
| 225 |
L( x , r ); |
| 226 |
#pragma omp parallel for num_threads( threads ) |
| 227 |
for( int i=0 ; i<dim ; i++ ) r[i] -= b[i]; |
| 228 |
printf( "PCCG: %d %g -> %g\n" , ii , SquareNorm( b , dim ) , SquareNorm( r , dim ) ); |
| 229 |
} |
| 230 |
if( !scratch ) delete[] r , delete[] d , delete[] q , delete[] s; |
| 231 |
return ii; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
#ifdef USE_EIGEN |
| 236 |
#define STORE_EIGEN_MATRIX |
| 237 |
#ifdef EIGEN_USE_MKL_ALL |
| 238 |
#include <Eigen/PardisoSupport> |
| 239 |
#else // !EIGEN_USE_MKL_ALL |
| 240 |
#include <Eigen/Sparse> |
| 241 |
#endif // EIGEN_USE_MKL_ALL |
| 242 |
|
| 243 |
template< class Real , class MatrixRowIterator > |
| 244 |
struct EigenSolver |
| 245 |
{ |
| 246 |
virtual void update( const SparseMatrixInterface< Real , MatrixRowIterator >& M ) = 0; |
| 247 |
virtual void solve( ConstPointer( Real ) b , Pointer( Real ) x ) = 0; |
| 248 |
virtual size_t dimension( void ) const = 0; |
| 249 |
}; |
| 250 |
|
| 251 |
template< class Real , class MatrixRowIterator > |
| 252 |
class EigenSolverCholeskyLLt : public EigenSolver< Real , MatrixRowIterator > |
| 253 |
{ |
| 254 |
#ifdef EIGEN_USE_MKL_ALL |
| 255 |
typedef Eigen::PardisoLLT< Eigen::SparseMatrix< double > > Eigen_Solver; |
| 256 |
typedef Eigen::VectorXd Eigen_Vector; |
| 257 |
#else // !EIGEN_USE_MKL_ALL |
| 258 |
typedef Eigen::SimplicialLLT< Eigen::SparseMatrix< double > > Eigen_Solver; |
| 259 |
typedef Eigen::VectorXd Eigen_Vector; |
| 260 |
#endif // EIGEN_USE_MKL_ALL |
| 261 |
Eigen_Solver _solver; |
| 262 |
Eigen_Vector _eigenB; |
| 263 |
#ifdef STORE_EIGEN_MATRIX |
| 264 |
Eigen::SparseMatrix< double > _eigenM; |
| 265 |
#endif // STORE_EIGEN_MATRIX |
| 266 |
public: |
| 267 |
EigenSolverCholeskyLLt( const SparseMatrixInterface< Real , MatrixRowIterator >& M , bool analyzeOnly=false ) |
| 268 |
{ |
| 269 |
#ifdef STORE_EIGEN_MATRIX |
| 270 |
_eigenM.resize( int( M.rows() ) , int( M.rows() ) ); |
| 271 |
#else // !STORE_EIGEN_MATRIX |
| 272 |
Eigen::SparseMatrix< double > eigenM( int( M.rows() ) , int( M.rows() ) ); |
| 273 |
#endif // STORE_EIGEN_MATRIX |
| 274 |
std::vector< Eigen::Triplet< double > > triplets; |
| 275 |
triplets.reserve( M.entries() ); |
| 276 |
for( int i=0 ; i<M.rows() ; i++ ) for( MatrixRowIterator iter=M.begin(i) ; iter!=M.end(i) ; iter++ ) triplets.push_back( Eigen::Triplet< double >( i , iter->N , iter->Value ) ); |
| 277 |
#ifdef STORE_EIGEN_MATRIX |
| 278 |
_eigenM.setFromTriplets( triplets.begin() , triplets.end() ); |
| 279 |
_solver.analyzePattern( _eigenM ); |
| 280 |
#else // !STORE_EIGEN_MATRIX |
| 281 |
eigenM.setFromTriplets( triplets.begin() , triplets.end() ); |
| 282 |
_solver.analyzePattern( eigenM ); |
| 283 |
#endif // STORE_EIGEN_MATRIX |
| 284 |
if( !analyzeOnly ) |
| 285 |
{ |
| 286 |
#ifdef STORE_EIGEN_MATRIX |
| 287 |
_solver.factorize( _eigenM ); |
| 288 |
#else // !STORE_EIGEN_MATRIX |
| 289 |
_solver.factorize( eigenM ); |
| 290 |
#endif // STORE_EIGEN_MATRIX |
| 291 |
if( _solver.info()!=Eigen::Success ) fprintf( stderr , "[ERROR] EigenSolverCholeskyLLt::EigenSolverCholeskyLLt Failed to factorize matrix\n" ) , exit(0); |
| 292 |
} |
| 293 |
_eigenB.resize( M.rows() ); |
| 294 |
} |
| 295 |
void update( const SparseMatrixInterface< Real , MatrixRowIterator >& M ) |
| 296 |
{ |
| 297 |
#ifdef STORE_EIGEN_MATRIX |
| 298 |
#pragma omp parallel for |
| 299 |
for( int i=0 ; i<M.rows() ; i++ ) for( MatrixRowIterator iter=M.begin(i) ; iter!=M.end(i) ; iter++ ) _eigenM.coeffRef( i , iter->N ) = iter->Value; |
| 300 |
_solver.factorize( _eigenM ); |
| 301 |
#else // !STORE_EIGEN_MATRIX |
| 302 |
Eigen::SparseMatrix< double > eigenM( int( M.rows() ) , int( M.rows() ) ); |
| 303 |
std::vector< Eigen::Triplet< double > > triplets; |
| 304 |
triplets.reserve( M.entries() ); |
| 305 |
for( int i=0 ; i<M.rows() ; i++ ) for( MatrixRowIterator iter=M.begin(i) ; iter!=M.end(i) ; iter++ ) triplets.push_back( Eigen::Triplet< double >( i , iter->N , iter->Value ) ); |
| 306 |
eigenM.setFromTriplets( triplets.begin() , triplets.end() ); |
| 307 |
_solver.factorize( eigenM ); |
| 308 |
#endif // STORE_EIGEN_MATRIX |
| 309 |
switch( _solver.info() ) |
| 310 |
{ |
| 311 |
case Eigen::Success: break; |
| 312 |
case Eigen::NumericalIssue: fprintf( stderr , "[ERROR] EigenSolverCholeskyLLt::update Failed to factorize matrix (numerical issue)\n" ) , exit(0); |
| 313 |
case Eigen::NoConvergence: fprintf( stderr , "[ERROR] EigenSolverCholeskyLLt::update Failed to factorize matrix (no convergence)\n" ) , exit(0); |
| 314 |
case Eigen::InvalidInput: fprintf( stderr , "[ERROR] EigenSolverCholeskyLLt::update Failed to factorize matrix (invalid input)\n" ) , exit(0); |
| 315 |
default: fprintf( stderr , "[ERROR] EigenSolverCholeskyLLt::update Failed to factorize matrix\n" ) , exit(0); |
| 316 |
} |
| 317 |
} |
| 318 |
void solve( ConstPointer( Real ) b , Pointer( Real ) x ) |
| 319 |
{ |
| 320 |
#pragma omp parallel for |
| 321 |
for( int i=0 ; i<_eigenB.size() ; i++ ) _eigenB[i] = b[i]; |
| 322 |
Eigen_Vector eigenX = _solver.solve( _eigenB ); |
| 323 |
#pragma omp parallel for |
| 324 |
for( int i=0 ; i<eigenX.size() ; i++ ) x[i] = (Real)eigenX[i]; |
| 325 |
} |
| 326 |
size_t dimension( void ) const { return _eigenB.size(); } |
| 327 |
static void Solve( const SparseMatrixInterface< Real , MatrixRowIterator >& M , ConstPointer( Real ) b , Pointer( Real ) x ){ EigenSolverCholeskyLLt solver( M ) ; solver.solve( b , x ); } |
| 328 |
}; |
| 329 |
template< class Real , class MatrixRowIterator > |
| 330 |
class EigenSolverCholeskyLDLt : public EigenSolver< Real , MatrixRowIterator > |
| 331 |
{ |
| 332 |
#ifdef EIGEN_USE_MKL_ALL |
| 333 |
typedef Eigen::PardisoLDLT< Eigen::SparseMatrix< double > > Eigen_Solver; |
| 334 |
typedef Eigen::VectorXd Eigen_Vector; |
| 335 |
#else // !EIGEN_USE_MKL_ALL |
| 336 |
typedef Eigen::SimplicialLDLT< Eigen::SparseMatrix< double > > Eigen_Solver; |
| 337 |
typedef Eigen::VectorXd Eigen_Vector; |
| 338 |
#endif // EIGEN_USE_MKL_ALL |
| 339 |
Eigen_Solver _solver; |
| 340 |
Eigen_Vector _eigenB; |
| 341 |
public: |
| 342 |
EigenSolverCholeskyLDLt( const SparseMatrixInterface< Real , MatrixRowIterator >& M , bool analyzeOnly=false ) |
| 343 |
{ |
| 344 |
Eigen::SparseMatrix< double > eigenM( int( M.rows() ) , int( M.rows() ) ); |
| 345 |
std::vector< Eigen::Triplet<double> > triplets; |
| 346 |
triplets.reserve( M.entries() ); |
| 347 |
for( int i=0 ; i<M.rows() ; i++ ) for( MatrixRowIterator iter=M.begin(i) ; iter!=M.end(i) ; iter++ ) triplets.push_back( Eigen::Triplet< double >( i , iter->N , iter->Value ) ); |
| 348 |
eigenM.setFromTriplets( triplets.begin() , triplets.end() ); |
| 349 |
_solver.analyzePattern( eigenM ); |
| 350 |
if( !analyzeOnly ) |
| 351 |
{ |
| 352 |
_solver.factorize( eigenM ); |
| 353 |
if( _solver.info()!=Eigen::Success ) fprintf( stderr , "[ERROR] EigenSolverCholeskyLDLt::EigenSolverCholeskyLDLt Failed to factorize matrix\n" ) , exit(0); |
| 354 |
} |
| 355 |
_eigenB.resize( M.rows() ); |
| 356 |
} |
| 357 |
void update( const SparseMatrixInterface< Real , MatrixRowIterator >& M ) |
| 358 |
{ |
| 359 |
Eigen::SparseMatrix< double > eigenM( int( M.rows() ) , int( M.rows() ) ); |
| 360 |
std::vector< Eigen::Triplet<double> > triplets; |
| 361 |
triplets.reserve( M.entries() ); |
| 362 |
for( int i=0 ; i<M.rows() ; i++ ) for( MatrixRowIterator iter=M.begin(i) ; iter!=M.end(i) ; iter++ ) triplets.push_back( Eigen::Triplet< double >( i , iter->N , iter->Value ) ); |
| 363 |
eigenM.setFromTriplets( triplets.begin() , triplets.end() ); |
| 364 |
_solver.factorize( eigenM ); |
| 365 |
if( _solver.info()!=Eigen::Success ) fprintf( stderr , "[ERROR] EigenSolverCholeskyLDLt::update Failed to factorize matrix\n" ) , exit(0); |
| 366 |
} |
| 367 |
void solve( ConstPointer( Real ) b , Pointer( Real ) x ) |
| 368 |
{ |
| 369 |
#pragma omp parallel for |
| 370 |
for( int i=0 ; i<_eigenB.size() ; i++ ) _eigenB[i] = b[i]; |
| 371 |
Eigen_Vector eigenX = _solver.solve( _eigenB ); |
| 372 |
#pragma omp parallel for |
| 373 |
for( int i=0 ; i<eigenX.size() ; i++ ) x[i] = (Real)eigenX[i]; |
| 374 |
} |
| 375 |
size_t dimension( void ) const { return _eigenB.size(); } |
| 376 |
static void Solve( const SparseMatrixInterface< Real , MatrixRowIterator >& M , ConstPointer( Real ) b , Pointer( Real ) x ){ EigenSolverCholeskyLDLt solver( M ) ; solver.solve( b , x ); } |
| 377 |
}; |
| 378 |
template< class Real , class MatrixRowIterator > |
| 379 |
class EigenSolverCG : public EigenSolver< Real , MatrixRowIterator > |
| 380 |
{ |
| 381 |
#if 1 |
| 382 |
// Eigen::ConjugateGradient< Eigen::SparseMatrix< double > , Eigen::Lower , Eigen::IncompleteLUT< double > > _solver; |
| 383 |
Eigen::ConjugateGradient< Eigen::SparseMatrix< double > > _solver; |
| 384 |
#else |
| 385 |
Eigen::BiCGSTAB< Eigen::SparseMatrix< double > > _solver; |
| 386 |
#endif |
| 387 |
Eigen::VectorXd _eigenB , _eigenX; |
| 388 |
Eigen::SparseMatrix< double > _eigenM; |
| 389 |
public: |
| 390 |
EigenSolverCG( const SparseMatrixInterface< Real , MatrixRowIterator >& M , int iters=20 , double tolerance=0. ) |
| 391 |
{ |
| 392 |
_eigenM.resize( (int)M.rows() , (int)M.rows() ); |
| 393 |
std::vector< Eigen::Triplet< double > > triplets; |
| 394 |
triplets.reserve( M.entries() ); |
| 395 |
for( int i=0 ; i<M.rows() ; i++ ) for( MatrixRowIterator iter=M.begin(i) ; iter!=M.end(i) ; iter++ ) triplets.push_back( Eigen::Triplet< double >( i , iter->N , iter->Value ) ); |
| 396 |
_eigenM.setFromTriplets( triplets.begin() , triplets.end() ); |
| 397 |
_solver.compute( _eigenM ); |
| 398 |
_solver.analyzePattern( _eigenM ); |
| 399 |
if( _solver.info()!=Eigen::Success ) fprintf( stderr , "[ERROR] EigenSolverCG::EigenSolverCG Failed to factorize matrix\n" ) , exit(0); |
| 400 |
_eigenB.resize( M.rows() ) , _eigenX.resize( M.rows() ); |
| 401 |
_solver.setMaxIterations( iters ); |
| 402 |
_solver.setTolerance( tolerance ); |
| 403 |
} |
| 404 |
void update( const SparseMatrixInterface< Real , MatrixRowIterator >& M ) |
| 405 |
{ |
| 406 |
#pragma omp parallel for |
| 407 |
for( int i=0 ; i<M.rows() ; i++ ) for( MatrixRowIterator iter=M.begin(i) ; iter!=M.end(i) ; iter++ ) _eigenM.coeffRef( i , iter->N ) = iter->Value; |
| 408 |
_solver.compute( _eigenM ); |
| 409 |
_solver.analyzePattern( _eigenM ); |
| 410 |
if( _solver.info()!=Eigen::Success ) fprintf( stderr , "[ERROR] EigenSolverCG::update Failed to factorize matrix\n" ) , exit(0); |
| 411 |
} |
| 412 |
|
| 413 |
void setIters( int iters ){ _solver.setMaxIterations( iters ); } |
| 414 |
void solve( ConstPointer( Real ) b , Pointer( Real ) x ) |
| 415 |
{ |
| 416 |
#pragma omp parallel for |
| 417 |
for( int i=0 ; i<_eigenB.size() ; i++ ) _eigenB[i] = b[i] , _eigenX[i] = x[i]; |
| 418 |
_eigenX = _solver.solveWithGuess( _eigenB , _eigenX ); |
| 419 |
#pragma omp parallel for |
| 420 |
for( int i=0 ; i<_eigenX.size() ; i++ ) x[i] = _eigenX[i]; |
| 421 |
} |
| 422 |
size_t dimension( void ) const { return _eigenB.size(); } |
| 423 |
static void Solve( const SparseMatrixInterface< Real , MatrixRowIterator >& M , const Real* b , Real* x , int iters ){ EigenSolverCG solver( M , iters ) ; solver.solve( b , x ); } |
| 424 |
}; |
| 425 |
|
| 426 |
#endif // USE_EIGEN |
| 427 |
|
| 428 |
#ifdef USE_CHOLMOD |
| 429 |
class CholmodSolver |
| 430 |
{ |
| 431 |
const static bool LOWER_TRIANGULAR = true; |
| 432 |
int dim; |
| 433 |
cholmod_factor* cholmod_L; |
| 434 |
cholmod_dense* cholmod_b; |
| 435 |
cholmod_sparse* cholmod_M; |
| 436 |
std::vector< bool > flaggedValues; |
| 437 |
template< class Real , class MatrixRowIterator > void _init( const SparseMatrixInterface< Real , MatrixRowIterator >& M ); |
| 438 |
public: |
| 439 |
static cholmod_common cholmod_C; |
| 440 |
static bool cholmod_C_set; |
| 441 |
|
| 442 |
template< class Real , class MatrixRowIterator > |
| 443 |
CholmodSolver( const SparseMatrixInterface< Real , MatrixRowIterator >& M , bool analyzeOnly=false ); |
| 444 |
~CholmodSolver( void ); |
| 445 |
|
| 446 |
template< class Real > void solve( ConstPointer( Real ) b , Pointer( Real ) x ); |
| 447 |
template< class Real , class MatrixRowIterator > bool update( const SparseMatrixInterface< Real , MatrixRowIterator >& M ); |
| 448 |
int nonZeros( void ) const; |
| 449 |
|
| 450 |
}; |
| 451 |
bool CholmodSolver::cholmod_C_set = false; |
| 452 |
cholmod_common CholmodSolver::cholmod_C; |
| 453 |
|
| 454 |
template< class Real , class MatrixRowIterator > CholmodSolver::CholmodSolver( const SparseMatrixInterface< Real , MatrixRowIterator >& M , bool analyzeOnly ){ _init( M ) ; if( !analyzeOnly ) update( M ); } |
| 455 |
template< class Real , class MatrixRowIterator > |
| 456 |
void CholmodSolver::_init( const SparseMatrixInterface< Real , MatrixRowIterator >& M ) |
| 457 |
{ |
| 458 |
{ |
| 459 |
if( !cholmod_C_set ) CHOLMOD(start)( &cholmod_C ); |
| 460 |
cholmod_C_set = true; |
| 461 |
} |
| 462 |
dim = (int)M.rows(); |
| 463 |
|
| 464 |
int maxEntries; |
| 465 |
if( LOWER_TRIANGULAR ) |
| 466 |
{ |
| 467 |
maxEntries = (int)( ( M.entries()-M.rows() ) / 2 + M.rows() ); |
| 468 |
cholmod_M = CHOLMOD(allocate_sparse)( dim , dim , maxEntries , 0 , 1 , -1 , CHOLMOD_REAL , &cholmod_C ); |
| 469 |
} |
| 470 |
else |
| 471 |
{ |
| 472 |
maxEntries = (int)M.entries(); |
| 473 |
cholmod_M = CHOLMOD(allocate_sparse)( dim , dim , maxEntries , 0 , 1 , 0 , CHOLMOD_REAL , &cholmod_C ); |
| 474 |
} |
| 475 |
cholmod_M->i = malloc( sizeof( SOLVER_LONG ) * maxEntries ); |
| 476 |
cholmod_M->x = malloc( sizeof( double ) * maxEntries ); |
| 477 |
|
| 478 |
SOLVER_LONG *_p = (SOLVER_LONG*)cholmod_M->p; |
| 479 |
SOLVER_LONG *_i = (SOLVER_LONG*)cholmod_M->i; |
| 480 |
|
| 481 |
int off = 0; |
| 482 |
dim = 0; |
| 483 |
|
| 484 |
for( int i=0 ; i<M.rows() ; i++ ) |
| 485 |
{ |
| 486 |
_p[dim++] = off; |
| 487 |
for( MatrixRowIterator iter=M.begin(i) ; iter!=M.end(i) ; iter++ ) if( !LOWER_TRIANGULAR || iter->N>=i ) _i[off++] = iter->N; |
| 488 |
} |
| 489 |
_p[dim] = off; |
| 490 |
|
| 491 |
cholmod_L = CHOLMOD(analyze)( cholmod_M , &cholmod_C ); |
| 492 |
cholmod_b = CHOLMOD(allocate_dense)( dim , 1 , dim , cholmod_M->xtype , &cholmod_C ); |
| 493 |
} |
| 494 |
template< class Real , class MatrixRowIterator > |
| 495 |
bool CholmodSolver::update( const SparseMatrixInterface< Real , MatrixRowIterator >& M ) |
| 496 |
{ |
| 497 |
double *_x = (double*)cholmod_M->x; |
| 498 |
int off = 0; |
| 499 |
|
| 500 |
SOLVER_LONG *_p = (SOLVER_LONG*)cholmod_M->p; |
| 501 |
#pragma omp parallel for |
| 502 |
for( int i=0 ; i<M.rows() ; i++ ) |
| 503 |
{ |
| 504 |
int off = (int)_p[i]; |
| 505 |
for( MatrixRowIterator iter=M.begin(i) ; iter!=M.end(i) ; iter++ )if( !LOWER_TRIANGULAR || iter->N>=i ) _x[off++] = double( iter->Value ); |
| 506 |
} |
| 507 |
|
| 508 |
cholmod_C.print = 0; |
| 509 |
CHOLMOD(factorize)( cholmod_M , cholmod_L , &cholmod_C ); |
| 510 |
if( cholmod_C.status==CHOLMOD_NOT_POSDEF ) |
| 511 |
{ |
| 512 |
fprintf( stderr , "[WARNING] CholmodSolver::update: Matrix not positive-definite\n" ); |
| 513 |
return false; |
| 514 |
} |
| 515 |
else if( cholmod_C.status==CHOLMOD_OUT_OF_MEMORY ) |
| 516 |
{ |
| 517 |
fprintf( stderr , "[WARNING] CholmodSolver::update: CHOLMOD ran out of memory\n" ); |
| 518 |
return false; |
| 519 |
} |
| 520 |
else if( cholmod_C.status!=CHOLMOD_OK ) |
| 521 |
{ |
| 522 |
fprintf( stderr , "[WARNING] CholmodSolver::update: CHOLMOD status not OK: %d\n" , cholmod_C.status ); |
| 523 |
return false; |
| 524 |
} |
| 525 |
return true; |
| 526 |
} |
| 527 |
CholmodSolver::~CholmodSolver( void ) |
| 528 |
{ |
| 529 |
if( cholmod_L ) CHOLMOD(free_factor)( &cholmod_L , &cholmod_C ) , cholmod_L = NULL; |
| 530 |
if( cholmod_b ) CHOLMOD(free_dense )( &cholmod_b , &cholmod_C ) , cholmod_b = NULL; |
| 531 |
if( cholmod_M ) CHOLMOD(free_sparse)( &cholmod_M , &cholmod_C ) , cholmod_M = NULL; |
| 532 |
} |
| 533 |
|
| 534 |
template< class Real > |
| 535 |
void CholmodSolver::solve( ConstPointer( Real ) b , Pointer( Real ) x ) |
| 536 |
{ |
| 537 |
double* _b = (double*)cholmod_b->x; |
| 538 |
for( int i=0 ; i<dim ; i++ ) _b[i] = (double)b[i]; |
| 539 |
|
| 540 |
cholmod_dense* cholmod_x = CHOLMOD(solve)( CHOLMOD_A , cholmod_L , cholmod_b , &cholmod_C ); |
| 541 |
double* _x = (double*)cholmod_x->x; |
| 542 |
for( int i=0 ; i<dim ; i++ ) x[i] = (Real)_x[i]; |
| 543 |
|
| 544 |
CHOLMOD(free_dense)( &cholmod_x , &cholmod_C ); |
| 545 |
} |
| 546 |
int CholmodSolver::nonZeros( void ) const |
| 547 |
{ |
| 548 |
long long nz = 0; |
| 549 |
if( cholmod_L->xtype != CHOLMOD_PATTERN && !(cholmod_L->is_super ) ) for( int i=0 ; i<cholmod_L->n ; i++ ) nz += ((SOLVER_LONG*)cholmod_L->nz)[i]; |
| 550 |
bool examine_super = false; |
| 551 |
if( cholmod_L->xtype != CHOLMOD_PATTERN ) examine_super = true ; |
| 552 |
else examine_super = ( ((int*)cholmod_L->s)[0] != (-1)); |
| 553 |
if( examine_super ) |
| 554 |
{ |
| 555 |
/* check and print each supernode */ |
| 556 |
for (int s = 0 ; s < cholmod_L->nsuper ; s++) |
| 557 |
{ |
| 558 |
int k1 = ((int*)cholmod_L->super) [s] ; |
| 559 |
int k2 = ((int*)cholmod_L->super) [s+1] ; |
| 560 |
int psi = ((int*)cholmod_L->pi)[s] ; |
| 561 |
int psend = ((int*)cholmod_L->pi)[s+1] ; |
| 562 |
int nsrow = psend - psi ; |
| 563 |
int nscol = k2 - k1 ; |
| 564 |
nz += nscol * nsrow - (nscol*nscol - nscol)/2 ; |
| 565 |
} |
| 566 |
} |
| 567 |
return (int)nz; |
| 568 |
} |
| 569 |
#endif // USE_CHOLMOD |
| 570 |
#endif // LINEAR_SOLVERS_INCLUDE |