FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
umfpack.cpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
7
8#ifdef FEAT_HAVE_UMFPACK
9#include <kernel/solver/umfpack.hpp>
10FEAT_DISABLE_WARNINGS
11#include <umfpack.h>
12FEAT_RESTORE_WARNINGS
13
14namespace FEAT
15{
16 namespace Solver
17 {
51 template<typename Idx_, int sidx_ = sizeof(Idx_), bool eq_ = (sizeof(int) == sizeof(SuiteSparse_long))>
52 struct UmfpackWrapper;
53
54 // specialization for Idx_ = 32-bit integer
55 template<typename Idx_, bool eq_>
56 struct UmfpackWrapper<Idx_, sizeof(int), eq_>
57 {
58 static void init_defaults(double* control)
59 {
60 ::umfpack_di_defaults(control);
61 }
62
63 static int init_symbolic(Idx_ nrows, Idx_ ncols, const Idx_* row_ptr, const Idx_* col_idx,
64 const double* data, void** symb, const double* ctrl, double* info)
65 {
66 static_assert(sizeof(Idx_) == sizeof(int), "invalid index size");
67 return int(::umfpack_di_symbolic(static_cast<int>(nrows), static_cast<int>(ncols),
68 reinterpret_cast<const int*>(row_ptr), reinterpret_cast<const int*>(col_idx),
69 data, symb, ctrl, info));
70 }
71
72 static int init_numeric(const Idx_* row_ptr, const Idx_* col_idx,
73 const double* data, void* symb, void** nume, const double* ctrl, double* info)
74 {
75 static_assert(sizeof(Idx_) == sizeof(int), "invalid index size");
76 return int(::umfpack_di_numeric(
77 reinterpret_cast<const int*>(row_ptr), reinterpret_cast<const int*>(col_idx),
78 data, symb, nume, ctrl, info));
79 }
80
81 static void free_symbolic(void** symb)
82 {
83 ::umfpack_di_free_symbolic(symb);
84 }
85
86 static void free_numeric(void** nume)
87 {
88 ::umfpack_di_free_numeric(nume);
89 }
90
91 static int solve(int sys, const Idx_* row_ptr, const Idx_* col_idx, const double* data,
92 double* x, const double* b, void* nume, const double* control, double* info)
93 {
94 static_assert(sizeof(Idx_) == sizeof(int), "invalid index size");
95 return int(::umfpack_di_solve(static_cast<int>(sys),
96 reinterpret_cast<const int*>(row_ptr), reinterpret_cast<const int*>(col_idx),
97 data, x, b, nume, control, info));
98 }
99 };
100
102 template<typename Idx_>
103 struct UmfpackWrapper<Idx_, sizeof(SuiteSparse_long), false>
104 {
105 static void init_defaults(double* control)
106 {
107 ::umfpack_dl_defaults(control);
108 }
109
110 static int init_symbolic(Idx_ nrows, Idx_ ncols, const Idx_* row_ptr, const Idx_* col_idx,
111 const double* data, void** symb, const double* ctrl, double* info)
112 {
113 static_assert(sizeof(Idx_) == sizeof(SuiteSparse_long), "invalid index size");
114 return int(::umfpack_dl_symbolic(static_cast<SuiteSparse_long>(nrows), static_cast<SuiteSparse_long>(ncols),
115 reinterpret_cast<const SuiteSparse_long*>(row_ptr), reinterpret_cast<const SuiteSparse_long*>(col_idx),
116 data, symb, ctrl, info));
117 }
118
119 static int init_numeric(const Idx_* row_ptr, const Idx_* col_idx,
120 const double* data, void* symb, void** nume, const double* ctrl, double* info)
121 {
122 static_assert(sizeof(Idx_) == sizeof(SuiteSparse_long), "invalid index size");
123 return int(::umfpack_dl_numeric(
124 reinterpret_cast<const SuiteSparse_long*>(row_ptr), reinterpret_cast<const SuiteSparse_long*>(col_idx),
125 data, symb, nume, ctrl, info));
126 }
127
128 static void free_symbolic(void** symb)
129 {
130 ::umfpack_dl_free_symbolic(symb);
131 }
132
133 static void free_numeric(void** nume)
134 {
135 ::umfpack_dl_free_numeric(nume);
136 }
137
138 static int solve(int sys, const Idx_* row_ptr, const Idx_* col_idx, const double* data,
139 double* x, const double* b, void* nume, const double* control, double* info)
140 {
141 static_assert(sizeof(Idx_) == sizeof(SuiteSparse_long), "invalid index size");
142 return int(::umfpack_dl_solve(sys, reinterpret_cast<const SuiteSparse_long*>(row_ptr),
143 reinterpret_cast<const SuiteSparse_long*>(col_idx), data, x, b, nume, control, info));
144 }
145 };
146
147 /* ***************************************************************************************** */
148 /* ***************************************************************************************** */
149 /* ***************************************************************************************** */
150
151 Umfpack::Umfpack(const Umfpack::MatrixType& system_matrix) :
152 _system_matrix(system_matrix),
153 _umf_control(new double[UMFPACK_CONTROL]),
154 _umf_symbolic(nullptr),
155 _umf_numeric(nullptr),
156 _sym_peak_size(0),
157 _sym_mem_size(0),
158 _num_mem_size(0),
159 _umf_peak_size(0)
160 {
161 // initialize default umfpack control values
162 UmfpackWrapper<Index>::init_defaults(_umf_control);
163 }
164
166 Umfpack::~Umfpack()
167 {
168 done();
169 if(_umf_control != nullptr)
170 delete [] _umf_control;
171 }
172
173 void Umfpack::init_symbolic()
174 {
175 BaseClass::init_symbolic();
176
177 // ensure that we don't have a system matrix assigned
178 if(_umf_symbolic != nullptr)
179 throw SolverException("UMFPACK: already have symbolic factorization");
180
181 // umfpack info array
182 double info[UMFPACK_INFO];
183
184 Memory::TypedView<Index> row_ptr_view = _system_matrix.row_ptr_view_r();
185 Memory::TypedView<Index> col_idx_view = _system_matrix.col_idx_view_r();
186
187 // try to perform symbolic factorization
188 int status = UmfpackWrapper<Index>::init_symbolic(
189 _system_matrix.num_rows(),
190 _system_matrix.num_cols(),
191 row_ptr_view.get_r(),
192 col_idx_view.get_r(),
193 nullptr,
194 &_umf_symbolic,
195 _umf_control,
196 info
197 );
198
199 // check status code
200 switch(status)
201 {
202 case UMFPACK_OK:
203 break;
204 case UMFPACK_ERROR_out_of_memory:
205 throw SolverException("UMFPACK: out of memory");
206 case UMFPACK_ERROR_invalid_matrix:
207 throw InvalidMatrixStructureException("UMFPACK: invalid matrix structure");
208 case UMFPACK_ERROR_internal_error:
209 throw SolverException("UMFPACK: internal error");
210 default:
211 throw SolverException("UMFPACK: unknown error");
212 }
213
214 // gather statistics
215 _sym_peak_size = std::size_t(info[UMFPACK_SIZE_OF_UNIT] * info[UMFPACK_SYMBOLIC_PEAK_MEMORY]);
216 _sym_mem_size = std::size_t(info[UMFPACK_SIZE_OF_UNIT] * info[UMFPACK_SYMBOLIC_SIZE]);
217 }
218
219 void Umfpack::done_symbolic()
220 {
221 if(_umf_symbolic == nullptr)
222 return;
223
224 UmfpackWrapper<Index>::free_symbolic(&_umf_symbolic);
225
226 _umf_symbolic = nullptr;
227
228 BaseClass::done_symbolic();
229 }
230
231 void Umfpack::init_numeric()
232 {
233 BaseClass::init_numeric();
234
235 if(_umf_symbolic == nullptr)
236 throw SolverException("UFMPACK: symbolic factorization missing");
237
238 // umfpack info array
239 double info[UMFPACK_INFO];
240
241 Memory::TypedView<Index> row_ptr_view = _system_matrix.row_ptr_view_r();
242 Memory::TypedView<Index> col_idx_view = _system_matrix.col_idx_view_r();
243 Memory::TypedView<double> mat_val_view = _system_matrix.val_view_r();
244
245 // try to perform symbolic factorization
246 int status = UmfpackWrapper<Index>::init_numeric(
247 row_ptr_view.get_r(),
248 col_idx_view.get_r(),
249 mat_val_view.get_r(),
250 _umf_symbolic,
251 &_umf_numeric,
252 _umf_control,
253 info
254 );
255
256 // check status code
257 switch(status)
258 {
259 case UMFPACK_OK:
260 break;
261 case UMFPACK_ERROR_out_of_memory:
262 throw SolverException("UMFPACK: out of memory");
263 case UMFPACK_ERROR_invalid_matrix:
264 throw InvalidMatrixStructureException("UMFPACK: invalid matrix structure");
265 case UMFPACK_WARNING_singular_matrix:
266 throw SingularMatrixException("UMFPACK: singular matrix");
267 case UMFPACK_ERROR_different_pattern:
268 throw SolverException("UMFPACK: different pattern");
269 case UMFPACK_ERROR_internal_error:
270 throw SolverException("UMFPACK: internal error");
271 default:
272 throw SolverException("UMFPACK: unknown error");
273 }
274
275 // gather statistics
276 _umf_peak_size = std::size_t(info[UMFPACK_SIZE_OF_UNIT] * info[UMFPACK_PEAK_MEMORY]);
277 _num_mem_size = std::size_t(info[UMFPACK_SIZE_OF_UNIT] * info[UMFPACK_NUMERIC_SIZE]);
278 }
279
280 void Umfpack::done_numeric()
281 {
282 if(_umf_numeric == nullptr)
283 return;
284
285 UmfpackWrapper<Index>::free_numeric(&_umf_numeric);
286
287 _umf_numeric = nullptr;
288 BaseClass::done_numeric();
289 }
290
291 Status Umfpack::apply(VectorType& x, const VectorType& b)
292 {
293 // umfpack info array
294 double info[UMFPACK_INFO];
295
296 Memory::TypedView<Index> row_ptr_view = _system_matrix.row_ptr_view_r();
297 Memory::TypedView<Index> col_idx_view = _system_matrix.col_idx_view_r();
298 Memory::TypedView<double> mat_val_view = _system_matrix.val_view_r();
299 Memory::TypedView<double> x_view(x.elements_view_w());
300 Memory::TypedView<double> b_view(b.elements_view_r());
301
302 // solve
303 int status = UmfpackWrapper<Index>::solve(
304 UMFPACK_At,
305 row_ptr_view.get_r(),
306 col_idx_view.get_r(),
307 mat_val_view.get_r(),
308 x_view.get_w(),
309 b_view.get_r(),
310 _umf_numeric,
311 _umf_control,
312 info);
313
314 // check status code
315 return (status == UMFPACK_OK) ? Status::success : Status::aborted;
316 }
317
318 /* ***************************************************************************************** */
319 /* ***************************************************************************************** */
320 /* ***************************************************************************************** */
321
322 UmfpackMean::UmfpackMean(const MatrixType& system_matrix, const VectorType& weight_vector) :
323 BaseClass(),
324 _system_matrix(system_matrix),
325 _weight_vector(weight_vector),
326 _umfpack(_solver_matrix)
327 {
328 }
329
330 void UmfpackMean::init_symbolic()
331 {
332 BaseClass::init_symbolic();
333
334 // get the number of rows/columns
335 const Index n = _weight_vector.size();
336 if((n != _system_matrix.num_rows()) || (n != _system_matrix.num_cols()))
337 throw InvalidMatrixStructureException("system matrix/weight vector dimension mismatch");
338
339 // get the number of non-zeroes
340 const Index nnze = _system_matrix.num_nzes();
341
342 // allocate our solver matrix
343 _solver_matrix = MatrixType(n+1, n+1, nnze + 2*n);
344
345 // get our input matrix arrays
346 const Memory::TypedView<Index> irow_ptr = _system_matrix.row_ptr_view_r();
347 const Memory::TypedView<Index> icol_idx = _system_matrix.col_idx_view_r();
348
349 // get our output matrix arrays
350 Memory::TypedView<Index> orow_ptr = _solver_matrix.row_ptr_view_w();
351 Memory::TypedView<Index> ocol_idx = _solver_matrix.col_idx_view_w();
352
353 // assemble the solver matrix structure
354 orow_ptr[0] = Index(0);
355 for(Index i(0); i < n; ++i)
356 {
357 Index op = orow_ptr[i];
358 // copy input matrix row pattern
359 for(Index ip(irow_ptr[i]); ip < irow_ptr[i+1]; ++ip, ++op)
360 {
361 ocol_idx[op] = icol_idx[ip];
362 }
363 // append a single entry in the last column
364 ocol_idx[op] = n;
365 // set next row pointer
366 orow_ptr[i+1] = ++op;
367 }
368
369 // append an (almost) dense row (length n of n+1)
370 Index op(orow_ptr[n]);
371 for(Index j(0); j < n; ++j, ++op)
372 {
373 ocol_idx[op] = j;
374 }
375 // set last row pointer
376 orow_ptr[n+1] = op;
377
378 // release views
379 ocol_idx.release();
380 orow_ptr.release();
381 icol_idx.release();
382 irow_ptr.release();
383
384 // Okay, solver matrix structure assembly complete
385
386 // create temporary vectors
387 _vec_x = _solver_matrix.create_vector_r();
388 _vec_b = _solver_matrix.create_vector_r();
389
390 // initialize umfpack
391 _umfpack.init_symbolic();
392 }
393
394 void UmfpackMean::done_symbolic()
395 {
396 _umfpack.done_symbolic();
397 _vec_b.clear();
398 _vec_x.clear();
399 _solver_matrix.clear();
400
401 BaseClass::done_symbolic();
402 }
403
404 void UmfpackMean::init_numeric()
405 {
406 BaseClass::init_numeric();
407
408 const Index n = _system_matrix.num_rows();
409
410 // get input matrix arrays
411 const Memory::TypedView<Index> irow_ptr = _system_matrix.row_ptr_view_r();
412 const Memory::TypedView<double> idata = _system_matrix.val_view_r();
413
414 // get input vector array
415 const Memory::TypedView<double> weight_view(_weight_vector.elements_view_r());
416
417 // get output matrix arrays
418 const Memory::TypedView<Index> orow_ptr = _solver_matrix.row_ptr_view_r();
419 Memory::TypedView<double> odata = _solver_matrix.val_view_w();
420
421 // loop over all input matrix rows
422 for(Index i(0); i < n; ++i)
423 {
424 Index op(orow_ptr[i]);
425 // copy input matrix row data
426 for(Index ip(irow_ptr[i]); ip < irow_ptr[i+1]; ++ip, ++op)
427 {
428 odata[op] = idata[ip];
429 }
430 // copy weight vector entry as last column entry
431 odata[op] = weight_view[i];
432 }
433
434 // copy weight vector into last row
435 Index op(orow_ptr[n]);
436 for(Index j(0); j < n; ++j, ++op)
437 {
438 odata[op] = weight_view[j];
439 }
440
441 // okay, solver matrix data assembly completed
442 odata.release();
443
444 // initialize umfpack
445 _umfpack.init_numeric();
446 }
447
448 void UmfpackMean::done_numeric()
449 {
450 _umfpack.done_numeric();
451
452 BaseClass::done_numeric();
453 }
454
455 Status UmfpackMean::apply(VectorType& vec_sol, const VectorType& vec_rhs)
456 {
457 const Index n = vec_sol.size();
458
459 // copy RHS vector
460 {
461 const Memory::TypedView<double> vr = vec_rhs.elements_view_r();
462 Memory::TypedView<double> vb = _vec_b.elements_view_w();
463 for(Index i(0); i < n; ++i)
464 {
465 vb[i] = vr[i];
466 }
467 vb[n] = 0.0;
468 }
469
470 // solve system
471 Status status = _umfpack.apply(_vec_x, _vec_b);
472
473 // copy sol vector
474 {
475 const Memory::TypedView<double> vx(_vec_x.elements_view_r());
476 Memory::TypedView<double> vs(vec_sol.elements_view_w());
477 for(Index i(0); i < n; ++i)
478 {
479 vs[i] = vx[i];
480 }
481 }
482
483 // okay
484 return status;
485 }
486
487 } // namespace Solver
488} // namespace FEAT
489#else
490// insert dummy function to suppress linker warnings
491void dummy_function_umfpack() {}
492#endif // FEAT_HAVE_UMFPACK
FEAT Kernel base header.
LAFEM::SparseMatrixCSR< double, Index > MatrixType
compatible matrix type
Definition: umfpack.hpp:45
Umfpack(const MatrixType &system_matrix)
Constructor.
Status
Solver status return codes enumeration.
Definition: base.hpp:47
Status solve(SolverBase< Vector_ > &solver, Vector_ &vec_sol, const Vector_ &vec_rhs, const Matrix_ &matrix, const Filter_ &filter)
Solve linear system with initial solution guess.
Definition: base.hpp:347
FEAT namespace.
Definition: adjactor.hpp:12
std::uint64_t Index
Index data type.