cholesky_solver.h 4.3 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
20
* Web site: http://cgogn.unistra.fr/                                           *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

/*
 * This interface to CHOLMOD is largely inspired from the one found in
 * Graphite developped by Bruno Levy, ALICE Project, LORIA, INRIA Lorraine
 */


#ifndef __CHOLESKY_SOLVER__
#define __CHOLESKY_SOLVER__

#include <cassert>
35
#include "cholmod.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
36

37
template <typename CoeffType>
Pierre Kraemer's avatar
Pierre Kraemer committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
class Solver_CHOLESKY
{
public:
    Solver_CHOLESKY() {
    	N = 0 ;
    	factorized_ = false ;
    	L = NULL ;
		// Initialize CHOLMOD library
		cholmod_start(&c) ;
    }

    ~Solver_CHOLESKY() {
		cholmod_free_factor(&L, &c) ;
		cholmod_finish(&c) ;
    }

    bool factorized() { return factorized_ ; }

56 57 58
	void factorize(const Eigen::SparseMatrix<CoeffType>& A_in) {
		assert(A_in.rows() == A_in.cols()) ;
//		assert(A_in.has_symmetric_storage()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
59

60 61
		N = A_in.rows() ;
//		int NNZ = A_in.nonZeros() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
62 63

		// Translate sparse matrix into cholmod format
64
//		cholmod_sparse* A = cholmod_allocate_sparse(N, N, NNZ, false, true, -1, CHOLMOD_REAL, &c);
Pierre Kraemer's avatar
Pierre Kraemer committed
65

66 67 68
//		int* colptr = static_cast<int*>(A->p) ;
//		int* rowind = static_cast<int*>(A->i) ;
//		double* a = static_cast<double*>(A->x) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
69 70

		// Convert local Matrix into CHOLMOD Matrix
71 72 73 74 75 76 77 78 79 80 81 82 83
//		int count = 0 ;
//		for(int j = 0; j < N; j++) {
//			const typename SparseMatrix<CoeffType>::Column& Cj = A_in.column(j) ;
//			colptr[j] = count ;
//			for(unsigned int ii = 0; ii < Cj.nb_coeffs(); ii++) {
//				a[count] = Cj.coeff(ii).a ;
//				rowind[count] = Cj.coeff(ii).index ;
//				count++ ;
//			}
//		}
//		colptr[N] = NNZ ;

		cholmod_sparse A = Eigen::viewAsCholmod(A_in);
Pierre Kraemer's avatar
Pierre Kraemer committed
84 85

		// Factorize
86 87
		L = cholmod_analyze(&A, &c) ;
		cholmod_factorize(&A, L, &c) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
88 89 90
		factorized_ = true ;

		// Cleanup
91
//		cholmod_free_sparse(&A, &c) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
92 93
    }

94
	void solve(const VECTOR& b_in, VECTOR& x_out) {
Pierre Kraemer's avatar
Pierre Kraemer committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    	assert(factorized_) ;
        assert(L->n == b_in.dimension()) ;
        assert(L->n == x_out.dimension()) ;

		// Translate right-hand side into cholmod format
		cholmod_dense* b = cholmod_allocate_dense(N, 1, N, CHOLMOD_REAL, &c) ;
		double* cb = static_cast<double*>(b->x) ;
		for(int i = 0; i < N; ++i)
			cb[i] = b_in[i] ;

		// Solve
		cholmod_dense* x = cholmod_solve(CHOLMOD_A, L, b, &c) ;

		// Get result
		double* cx = static_cast<double*>(x->x) ;
		for(int i = 0; i < N; ++i)
			x_out[i] = cx[i] ;

		// Cleanup
		cholmod_free_dense(&x, &c) ;
		cholmod_free_dense(&b, &c) ;
    }

    void reset() {
    	if(factorized_)
    	{
    		cholmod_free_factor(&L, &c) ;
			factorized_ = false ;
			L = NULL ;
    	}
    }

private:
    int N ;
    bool factorized_ ;
	cholmod_common c ;
	cholmod_factor* L ;
} ;

#endif