CholmodSolver.cc 11.5 KB
Newer Older
Henrik Zimmer's avatar
Henrik Zimmer committed
1
2
/*===========================================================================*\
 *                                                                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
3
 *                               CoMISo                                      *
Henrik Zimmer's avatar
Henrik Zimmer committed
4
 *      Copyright (C) 2008-2009 by Computer Graphics Group, RWTH Aachen      *
Henrik Zimmer's avatar
Henrik Zimmer committed
5
 *                           www.rwth-graphics.de                            *
Henrik Zimmer's avatar
Henrik Zimmer committed
6
 *                                                                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
7
8
 *---------------------------------------------------------------------------* 
 *  This file is part of CoMISo.                                             *
Henrik Zimmer's avatar
Henrik Zimmer committed
9
 *                                                                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
10
11
12
13
 *  CoMISo is free software: you can redistribute it and/or modify           *
 *  it under the terms of the GNU General Public License as published by     *
 *  the Free Software Foundation, either version 3 of the License, or        *
 *  (at your option) any later version.                                      *
Henrik Zimmer's avatar
Henrik Zimmer committed
14
15
16
17
 *                                                                           *
 *  CoMISo 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            *
Henrik Zimmer's avatar
Henrik Zimmer committed
18
 *  GNU General Public License for more details.                             *
Henrik Zimmer's avatar
Henrik Zimmer committed
19
 *                                                                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
20
21
 *  You should have received a copy of the GNU General Public License        *
 *  along with CoMISo.  If not, see <http://www.gnu.org/licenses/>.          *
Henrik Zimmer's avatar
Henrik Zimmer committed
22
 *                                                                           *
Henrik Zimmer's avatar
Henrik Zimmer committed
23
\*===========================================================================*/ 
Henrik Zimmer's avatar
Henrik Zimmer committed
24

25
26
27
28
//== COMPILE-TIME PACKAGE REQUIREMENTS ========================================
#include <CoMISo/Config/config.hh>
#if COMISO_SUITESPARSE_AVAILABLE
//=============================================================================
Henrik Zimmer's avatar
Henrik Zimmer committed
29

30
31
32
#include "CholmodSolver.hh"


33
namespace COMISO {
34
35
36
37
38
39
40

CholmodSolver::CholmodSolver()
{
    mp_cholmodCommon = new cholmod_common;
    cholmod_start( mp_cholmodCommon );

    mp_L = 0;
David Bommes's avatar
David Bommes committed
41
42
43
44
45
46
47
48
49
50
51
52

    show_timings_ = false;

    mp_cholmodCommon->nmethods = 1;
    // use AMD ordering
    mp_cholmodCommon->method[0].ordering = CHOLMOD_AMD ;

    // use METIS ordering
    //    mp_cholmodCommon->method[0].ordering = CHOLMOD_METIS ;

    // try all methods
    // mp_cholmodCommon->nmethods = 9;
53
54
55
56
57
58
59
60
61
62
63
64
65
66
}


  //-----------------------------------------------------------------------------
  

CholmodSolver::~CholmodSolver()
{
    if( mp_L )
    {
	cholmod_free_factor( &mp_L, mp_cholmodCommon );
    }

    cholmod_finish( mp_cholmodCommon );
67
68
    delete mp_cholmodCommon;
    mp_cholmodCommon = NULL;
69
70
71
72
73
74
75
76
77
78
}
  

//-----------------------------------------------------------------------------


bool CholmodSolver::calc_system( const std::vector<int>&    _colptr, 
				 const std::vector<int>&    _rowind, 
				 const std::vector<double>& _values)
{
David Bommes's avatar
David Bommes committed
79
80
    if(show_timings_) sw_.start();

81
82
83
84
    colptr_ = _colptr;
    rowind_ = _rowind;
    values_ = _values;

Max Lyon's avatar
Max Lyon committed
85
    size_t n   = colptr_.size()-1;
86
87
88
89
90
91
92
93
94
95
96
97
98

    cholmod_sparse matA;

    matA.nrow = n;
    matA.ncol = n;
    matA.nzmax = _values.size();

    matA.p = &colptr_[0];
    matA.i = &rowind_[0];
    matA.x = &values_[0];
    matA.nz = 0;
    matA.z = 0;
    
David Bommes's avatar
David Bommes committed
99
100
    //    matA.stype = -1;
    matA.stype = 1;
101
102
103
104
105
106
    matA.itype = CHOLMOD_INT;
    matA.xtype = CHOLMOD_REAL;
    matA.dtype = CHOLMOD_DOUBLE;
    matA.sorted = 1;
    matA.packed = 1;

David Bommes's avatar
   
David Bommes committed
107

108
109
110
111
112
113
114
    // clean up
    if( mp_L )
    {
	cholmod_free_factor( &mp_L, mp_cholmodCommon );
	mp_L = 0;
    }

David Bommes's avatar
David Bommes committed
115
116
117
118
119
    if(show_timings_)
    {
      std::cerr << " Cholmod Timing cleanup: " << sw_.stop()/1000.0 << "s\n";
      sw_.start();
    }
120
121
122
123
124
125
126

    if( !(mp_L = cholmod_analyze( &matA, mp_cholmodCommon )) )
    {
	std::cout << "cholmod_analyze failed" << std::endl;
	return false;
    }

David Bommes's avatar
David Bommes committed
127
128
129
130
131
132
133
134
135
136
137
    // // show selected ordering method
    // std::cerr << "best    ordering was: " << mp_cholmodCommon->selected << std::endl;
    // std::cerr << "current ordering was: " << mp_cholmodCommon->current  << std::endl;


    if(show_timings_)
    {
      std::cerr << " Cholmod Timing analyze: " << sw_.stop()/1000.0 << "s\n";
      sw_.start();
    }

138
139
140
141
142
143
    if( !cholmod_factorize( &matA, mp_L, mp_cholmodCommon ) )
    {
	std::cout << "cholmod_factorize failed" << std::endl;
	return false;
    }

David Bommes's avatar
David Bommes committed
144
145
146
147
148
    if(show_timings_)
    {
      std::cerr << " Cholmod Timing factorize: " << sw_.stop()/1000.0 << "s\n";
      sw_.start();
    }
149
150
151
152
153

    return true;
}


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//-----------------------------------------------------------------------------


bool CholmodSolver::calc_system_prepare_pattern( const std::vector<int>&    _colptr,
                                                 const std::vector<int>&    _rowind,
                                                 const std::vector<double>& _values,
                                                 const std::vector<int>&    _colptr2,
                                                 const std::vector<int>&    _rowind2,
                                                 const std::vector<double>& _values2 )
{
    if(show_timings_) sw_.start();

    colptr_ = _colptr;
    rowind_ = _rowind;
    values_ = _values;

Max Lyon's avatar
Max Lyon committed
170
    size_t n = colptr_.size() - 1;
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271

    // setup matrix matA
    cholmod_sparse matA;

    matA.nrow = n;
    matA.ncol = n;
    matA.nzmax = _values.size();

    matA.p = &colptr_[0];
    matA.i = &rowind_[0];
    matA.x = &values_[0];
    matA.nz = 0;
    matA.z = 0;

    //    matA.stype = -1;
    matA.stype = 1;
    matA.itype = CHOLMOD_INT;
    matA.xtype = CHOLMOD_REAL;
    matA.dtype = CHOLMOD_DOUBLE;
    matA.sorted = 1;
    matA.packed = 1;

    // setup matrix matA_pattern
    cholmod_sparse matA_pattern;

    matA_pattern.nrow = n;
    matA_pattern.ncol = n;
    matA_pattern.nzmax = _values2.size();

    matA_pattern.p = (int*)(&_colptr2[0]);
    matA_pattern.i = (int*)(&_rowind2[0]);
    matA_pattern.x = (double*)(&_values2[0]);
    matA_pattern.nz = 0;
    matA_pattern.z = 0;

    //    matA_pattern.stype = -1;
    matA_pattern.stype = 1;
    matA_pattern.itype = CHOLMOD_INT;
    matA_pattern.xtype = CHOLMOD_REAL;
    matA_pattern.dtype = CHOLMOD_DOUBLE;
    matA_pattern.sorted = 1;
    matA_pattern.packed = 1;


    // clean up
    if( mp_L )
    {
        cholmod_free_factor( &mp_L, mp_cholmodCommon );
        mp_L = 0;
    }


    // compute permutation based on full pattern
    std::vector<int> perm(matA_pattern.nrow);
//    cholmod_metis(&matA_pattern, 0, 0, 0, perm.data(), mp_cholmodCommon) ;
    cholmod_amd(&matA_pattern, 0, 0, perm.data(), mp_cholmodCommon) ;

    if(show_timings_)
    {
      std::cerr << " Cholmod AMD ordering: " << sw_.stop()/1000.0 << "s\n";
      sw_.start();
    }


    if(show_timings_)
    {
      std::cerr << " Cholmod Timing cleanup: " << sw_.stop()/1000.0 << "s\n";
      sw_.start();
    }

    // fix permutation to given one
    mp_cholmodCommon->nmethods = 1;
    mp_cholmodCommon->method[0].ordering = CHOLMOD_GIVEN;

    if( !(mp_L = cholmod_analyze_p( &matA, perm.data(), 0, 0, mp_cholmodCommon )) )
    {
        std::cout << "cholmod_analyze failed" << std::endl;
        return false;
    }

    if(show_timings_)
    {
      std::cerr << " Cholmod Timing analyze: " << sw_.stop()/1000.0 << "s\n";
      sw_.start();
    }

    if( !cholmod_factorize( &matA, mp_L, mp_cholmodCommon ) )
    {
        std::cout << "cholmod_factorize failed" << std::endl;
        return false;
    }

    if(show_timings_)
    {
      std::cerr << " Cholmod Timing factorize: " << sw_.stop()/1000.0 << "s\n";
      sw_.start();
    }

    return true;
}

272
273
274
275
276
277
278
279
280
281
282
283
284
  //-----------------------------------------------------------------------------

    
bool CholmodSolver::update_system( const std::vector<int>& _colptr, 
				   const std::vector<int>& _rowind, 
				   const std::vector<double>& _values )
{
    if( !mp_L )
	return false;

    colptr_ = _colptr;
    rowind_ = _rowind;
    values_ = _values;
Max Lyon's avatar
Max Lyon committed
285
    size_t n = colptr_.size() - 1;
286
287
288
289
290
291
292
293
294
295
296
297
298

    cholmod_sparse matA;

    matA.nrow = n;
    matA.ncol = n;
    matA.nzmax = _values.size();

    matA.p = &colptr_[0];
    matA.i = &rowind_[0];
    matA.x = &values_[0];
    matA.nz = 0;
    matA.z = 0;
    
David Bommes's avatar
David Bommes committed
299
300
    //    matA.stype = -1;
    matA.stype = 1;
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
    matA.itype = CHOLMOD_INT;
    matA.xtype = CHOLMOD_REAL;
    matA.dtype = CHOLMOD_DOUBLE;
    matA.sorted = 1;
    matA.packed = 1;


    if( !cholmod_factorize( &matA, mp_L, mp_cholmodCommon ) )
    {
	std::cout << "cholmod_factorize failed" << std::endl;
	return false;
    }

    return true;
}


318
319
320
321
322
323
324
325
326
327
328
329
330
331
//-----------------------------------------------------------------------------


bool CholmodSolver::update_downdate_factor( const std::vector<int>&    _colptr,
                                            const std::vector<int>&    _rowind,
                                            const std::vector<double>& _values,
                                            const bool                 _upd)
{
    if(show_timings_) sw_.start();

    colptr_ = _colptr;
    rowind_ = _rowind;
    values_ = _values;

Max Lyon's avatar
Max Lyon committed
332
    size_t n = colptr_.size() - 1;
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384

    cholmod_sparse matA;

    matA.nrow = mp_L->n;
    matA.ncol = n;
    matA.nzmax = _values.size();

    matA.p = &colptr_[0];
    matA.i = &rowind_[0];
    matA.x = &values_[0];
    matA.nz = 0;
    matA.z = 0;

    matA.stype = 0;
    matA.itype = CHOLMOD_INT;
    matA.xtype = CHOLMOD_REAL;
    matA.dtype = CHOLMOD_DOUBLE;
    matA.sorted = 1;
    matA.packed = 1;

    // get permuted matrix
    cholmod_sparse* matAp = cholmod_submatrix ( &matA, (int*)mp_L->Perm, mp_L->n, 0, -1, true, true, mp_cholmodCommon);
    if(show_timings_)
    {
      std::cerr << " Cholmod conversion Timing: " << sw_.stop()/1000.0 << "s\n";
      sw_.start();
    }

    if( !cholmod_updown( _upd, matAp, mp_L, mp_cholmodCommon))
    {
      std::cerr << "Warning: Cholmod update/downdate failed!" << std::endl;
    }

    if(show_timings_)
    {
      std::cerr << " Cholmod update/downdate Timing: " << sw_.stop()/1000.0 << "s\n";
      sw_.start();
    }

    // clean up permuted matrix
    if(matAp)
      cholmod_free_sparse(&matAp, mp_cholmodCommon);

    if(show_timings_)
    {
      std::cerr << " Cholmod free sparse Timing: " << sw_.stop()/1000.0 << "s\n";
    }

    return true;
}


385
386
387
388
389
//-----------------------------------------------------------------------------
  

bool CholmodSolver::solve( double * _x, double * _b)
{
Max Lyon's avatar
Max Lyon committed
390
    const size_t n = mp_L->n;
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419

    cholmod_dense *x, b;

    b.nrow = n;
    b.ncol = 1;
    b.nzmax = n;
    b.d = b.nrow;
    b.x = _b;
    b.z = 0;
    b.xtype = CHOLMOD_REAL;
    b.dtype = CHOLMOD_DOUBLE;

    if( !(x = cholmod_solve( CHOLMOD_A, mp_L, &b, mp_cholmodCommon )) )
    {
	std::cout << "cholmod_solve failed" << std::endl;
	return false;
    }
    
    for( unsigned int i = 0; i < n; ++i )
	_x[i] = ((double*)x->x)[i];

    cholmod_free_dense( &x, mp_cholmodCommon );

    return true;
}


//-----------------------------------------------------------------------------

420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
int CholmodSolver::dimension()
{
  return std::max(int(0), (int)(colptr_.size()-1));
}

//-----------------------------------------------------------------------------

bool CholmodSolver::
solve ( std::vector<double>& _x0, std::vector<double>& _b)
{
  return solve( &(_x0[0]), &(_b[0]));
}

//-----------------------------------------------------------------------------

bool& CholmodSolver::
show_timings()
{
  return show_timings_;
}

441
442

}
443
444
445
446

//=============================================================================
#endif // COMISO_SUITESPARSE_AVAILABLE
//=============================================================================