cholesky_solver.h 4.21 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
		N = A_in.rows() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
61
62

		// Translate sparse matrix into cholmod format
63
64
		int NNZ = A_in.nonZeros() ;
		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
		int count = 0 ;
		for(int j = 0; j < N; j++) {
			colptr[j] = count;
			for(typename Eigen::SparseMatrix<CoeffType>::InnerIterator it(A_in, j); it; ++it)
			{
				a[count] = it.value();
				rowind[count] = it.row();
				++count;
			}
		}
		colptr[N] = NNZ ;
Pierre Kraemer's avatar
Pierre Kraemer committed
82
83

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

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

92
	void solve(const Eigen::Matrix<CoeffType, Eigen::Dynamic, 1>& b_in, Eigen::Matrix<CoeffType, Eigen::Dynamic, 1>& x_out) {
Pierre Kraemer's avatar
Pierre Kraemer committed
93
    	assert(factorized_) ;
94
95
		assert(L->n == b_in.rows()) ;
		assert(L->n == x_out.rows()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

		// 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(&b, &c) ;
113
		cholmod_free_dense(&x, &c) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
    }

    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