Amesos2 - Direct Sparse Solver Interfaces  Version of the Day
klu2_ext.hpp
1 /* ========================================================================== */
2 /* === klu include file ===================================================== */
3 /* ========================================================================== */
4 // @HEADER
5 // *****************************************************************************
6 // KLU2: A Direct Linear Solver package
7 //
8 // Copyright 2011 NTESS and the KLU2 contributors.
9 // SPDX-License-Identifier: LGPL-2.1-or-later
10 // *****************************************************************************
11 // @HEADER
12 
13 
14 /* Include file for user programs that call klu_* routines */
15 
16 #ifndef _TKLU_H
17 #define _TKLU_H
18 
19 
20 #include "trilinos_amd.h"
21 #include "trilinos_colamd.h"
22 #include "trilinos_btf_decl.h"
23 
24 /* -------------------------------------------------------------------------- */
25 /* Symbolic object - contains the pre-ordering computed by klu_analyze */
26 /* -------------------------------------------------------------------------- */
27 
28 /* TODO : Entry is not needed in symbolic, numeric and common. Remove TODO */
29 template <typename Entry, typename Int> struct klu_symbolic
30 {
31  /* A (P,Q) is in upper block triangular form. The kth block goes from
32  * row/col index R [k] to R [k+1]-1. The estimated number of nonzeros
33  * in the L factor of the kth block is Lnz [k].
34  */
35 
36  /* only computed if the AMD ordering is chosen: */
37  double symmetry ; /* symmetry of largest block */
38  double est_flops ; /* est. factorization flop count */
39  double lnz, unz ; /* estimated nz in L and U, including diagonals */
40  double *Lnz ; /* size n, but only Lnz [0..nblocks-1] is used */
41 
42  /* computed for all orderings: */
43  Int
44  n, /* input matrix A is n-by-n */
45  nz, /* # entries in input matrix */
46  *P, /* size n */
47  *Q, /* size n */
48  *R, /* size n+1, but only R [0..nblocks] is used */
49  nzoff, /* nz in off-diagonal blocks */
50  nblocks, /* number of blocks */
51  maxblock, /* size of largest block */
52  ordering, /* ordering used (AMD, COLAMD, or GIVEN) */
53  do_btf ; /* whether or not BTF preordering was requested */
54 
55  /* only computed if BTF preordering requested */
56  Int structural_rank ; /* 0 to n-1 if the matrix is structurally rank
57  * deficient. -1 if not computed. n if the matrix has
58  * full structural rank */
59 
60 } ;
61 
62 /* -------------------------------------------------------------------------- */
63 /* Numeric object - contains the factors computed by klu_factor */
64 /* -------------------------------------------------------------------------- */
65 template <typename Entry, typename Int> struct klu_numeric
66 {
67  /* LU factors of each block, the pivot row permutation, and the
68  * entries in the off-diagonal blocks */
69 
70  Int n ; /* A is n-by-n */
71  Int nblocks ; /* number of diagonal blocks */
72  Int lnz ; /* actual nz in L, including diagonal */
73  Int unz ; /* actual nz in U, including diagonal */
74  Int max_lnz_block ; /* max actual nz in L in any one block, incl. diag */
75  Int max_unz_block ; /* max actual nz in U in any one block, incl. diag */
76  Int *Pnum ; /* size n. final pivot permutation */
77  Int *Pinv ; /* size n. inverse of final pivot permutation */
78 
79  /* LU factors of each block */
80  Int *Lip ; /* size n. pointers into LUbx[block] for L */
81  Int *Uip ; /* size n. pointers into LUbx[block] for U */
82  Int *Llen ; /* size n. Llen [k] = # of entries in kth column of L */
83  Int *Ulen ; /* size n. Ulen [k] = # of entries in kth column of U */
84  void **LUbx ; /* L and U indices and entries (excl. diagonal of U) */
85  size_t *LUsize ; /* size of each LUbx [block], in sizeof (Unit) */
86  void *Udiag ; /* diagonal of U */
87 
88  /* scale factors; can be NULL if no scaling */
89  double *Rs ; /* size n. Rs [i] is scale factor for row i */
90 
91  /* permanent workspace for factorization and solve */
92  size_t worksize ; /* size (in bytes) of Work */
93  void *Work ; /* workspace */
94  void *Xwork ; /* alias into Numeric->Work */
95  Int *Iwork ; /* alias into Numeric->Work */
96 
97  /* off-diagonal entries in a conventional compressed-column sparse matrix */
98  Int *Offp ; /* size n+1, column pointers */
99  Int *Offi ; /* size nzoff, row indices */
100  void *Offx ; /* size nzoff, numerical values */
101  Int nzoff ;
102 
103 } ;
104 
105 /* -------------------------------------------------------------------------- */
106 /* KLU control parameters and statistics */
107 /* -------------------------------------------------------------------------- */
108 
109 /* Common->status values */
110 #define KLU_OK 0
111 #define KLU_SINGULAR (1) /* status > 0 is a warning, not an error */
112 #define KLU_OUT_OF_MEMORY (-2)
113 #define KLU_INVALID (-3)
114 #define KLU_TOO_LARGE (-4) /* integer overflow has occured */
115 
116 template <typename Entry, typename Int> struct klu_common
117 {
118 
119  /* --------------------------------------------------------------------- */
120  /* parameters */
121  /* --------------------------------------------------------------------- */
122 
123  double tol ; /* pivot tolerance for diagonal preference */
124  double memgrow ; /* realloc memory growth size for LU factors */
125  double initmem_amd ; /* init. memory size with AMD: c*nnz(L) + n */
126  double initmem ; /* init. memory size: c*nnz(A) + n */
127  double maxwork ; /* maxwork for BTF, <= 0 if no limit */
128 
129  Int btf ; /* use BTF pre-ordering, or not */
130  Int ordering ; /* 0: AMD, 1: COLAMD, 2: user P and Q,
131  * 3: user function */
132  Int scale ; /* row scaling: -1: none (and no error check),
133  * 0: none, 1: sum, 2: max */
134 
135  /* memory management routines */
136  void *(*malloc_memory) (size_t) ; /* pointer to malloc */
137  void *(*realloc_memory) (void *, size_t) ; /* pointer to realloc */
138  void (*free_memory) (void *) ; /* pointer to free */
139  void *(*calloc_memory) (size_t, size_t) ; /* pointer to calloc */
140 
141  /* pointer to user ordering function */
142  int (*user_order) (Int, Int *, Int *, Int *, struct klu_common<Entry, Int> *) ;
143 
144  /* pointer to user data, passed unchanged as the last parameter to the
145  * user ordering function (optional, the user function need not use this
146  * information). */
147  void *user_data ;
148 
149  Int halt_if_singular ; /* how to handle a singular matrix:
150  * FALSE: keep going. Return a Numeric object with a zero U(k,k). A
151  * divide-by-zero may occur when computing L(:,k). The Numeric object
152  * can be passed to klu_solve (a divide-by-zero will occur). It can
153  * also be safely passed to klu_refactor.
154  * TRUE: stop quickly. klu_factor will free the partially-constructed
155  * Numeric object. klu_refactor will not free it, but will leave the
156  * numerical values only partially defined. This is the default. */
157 
158  /* ---------------------------------------------------------------------- */
159  /* statistics */
160  /* ---------------------------------------------------------------------- */
161 
162  Int status ; /* KLU_OK if OK, < 0 if error */
163  Int nrealloc ; /* # of reallocations of L and U */
164 
165  Int structural_rank ; /* 0 to n-1 if the matrix is structurally rank
166  * deficient (as determined by maxtrans). -1 if not computed. n if the
167  * matrix has full structural rank. This is computed by klu_analyze
168  * if a BTF preordering is requested. */
169 
170  Int numerical_rank ; /* First k for which a zero U(k,k) was found,
171  * if the matrix was singular (in the range 0 to n-1). n if the matrix
172  * has full rank. This is not a true rank-estimation. It just reports
173  * where the first zero pivot was found. -1 if not computed.
174  * Computed by klu_factor and klu_refactor. */
175 
176  Int singular_col ; /* n if the matrix is not singular. If in the
177  * range 0 to n-1, this is the column index of the original matrix A that
178  * corresponds to the column of U that contains a zero diagonal entry.
179  * -1 if not computed. Computed by klu_factor and klu_refactor. */
180 
181  Int noffdiag ; /* # of off-diagonal pivots, -1 if not computed */
182 
183  double flops ; /* actual factorization flop count, from klu_flops */
184  double rcond ; /* crude reciprocal condition est., from klu_rcond */
185  double condest ; /* accurate condition est., from klu_condest */
186  double rgrowth ; /* reciprocal pivot rgrowth, from klu_rgrowth */
187  double work ; /* actual work done in BTF, in klu_analyze */
188 
189  size_t memusage ; /* current memory usage, in bytes */
190  size_t mempeak ; /* peak memory usage, in bytes */
191 
192 } ;
193 
194 /* -------------------------------------------------------------------------- */
195 /* klu_defaults: sets default control parameters */
196 /* -------------------------------------------------------------------------- */
197 
198 template <typename Entry, typename Int>
199 Int klu_defaults
200 (
201  klu_common<Entry, Int> *Common
202 ) ;
203 
204 /* -------------------------------------------------------------------------- */
205 /* klu_analyze: orders and analyzes a matrix */
206 /* -------------------------------------------------------------------------- */
207 
208 /* Order the matrix with BTF (or not), then order each block with AMD, COLAMD,
209  * a natural ordering, or with a user-provided ordering function */
210 
211 template <typename Entry, typename Int>
212 klu_symbolic<Entry, Int> *klu_analyze
213 (
214  /* inputs, not modified */
215  Int n, /* A is n-by-n */
216  Int Ap [ ], /* size n+1, column pointers */
217  Int Ai [ ], /* size nz, row indices */
218  klu_common<Entry, Int> *Common
219 ) ;
220 
221 /* -------------------------------------------------------------------------- */
222 /* klu_analyze_given: analyzes a matrix using given P and Q */
223 /* -------------------------------------------------------------------------- */
224 
225 /* Order the matrix with BTF (or not), then use natural or given ordering
226  * P and Q on the blocks. P and Q are interpretted as identity
227  * if NULL. */
228 
229 template <typename Entry, typename Int>
230 klu_symbolic<Entry, Int> *klu_analyze_given
231 (
232  /* inputs, not modified */
233  Int n, /* A is n-by-n */
234  Int Ap [ ], /* size n+1, column pointers */
235  Int Ai [ ], /* size nz, row indices */
236  Int P [ ], /* size n, user's row permutation (may be NULL) */
237  Int Q [ ], /* size n, user's column permutation (may be NULL) */
238  klu_common<Entry, Int> *Common
239 ) ;
240 
241 /* -------------------------------------------------------------------------- */
242 /* klu_factor: factors a matrix using the klu_analyze results */
243 /* -------------------------------------------------------------------------- */
244 
245 template <typename Entry, typename Int>
246 klu_numeric<Entry, Int> *klu_factor /* returns KLU_OK if OK, < 0 if error */
247 (
248  /* inputs, not modified */
249  Int Ap [ ], /* size n+1, column pointers */
250  Int Ai [ ], /* size nz, row indices */
251  Entry Ax [ ], /* size nz, numerical values */
252  klu_symbolic<Entry, Int> *Symbolic,
253  klu_common<Entry, Int> *Common
254 ) ;
255 
256 /* -------------------------------------------------------------------------- */
257 /* klu_solve: solves Ax=b using the Symbolic and Numeric objects */
258 /* -------------------------------------------------------------------------- */
259 
260 template <typename Entry, typename Int>
261 Int klu_solve
262 (
263  /* inputs, not modified */
264  klu_symbolic<Entry, Int> *Symbolic,
265  klu_numeric<Entry, Int> *Numeric,
266  Int ldim, /* leading dimension of B */
267  Int nrhs, /* number of right-hand-sides */
268 
269  /* right-hand-side on input, overwritten with solution to Ax=b on output */
270  Entry B [ ], /* size ldim*nrhs */
271  klu_common<Entry, Int> *Common
272 ) ;
273 
274 
275 /* -------------------------------------------------------------------------- */
276 /* klu_tsolve: solves A'x=b using the Symbolic and Numeric objects */
277 /* -------------------------------------------------------------------------- */
278 
279 template <typename Entry, typename Int>
280 Int klu_tsolve
281 (
282  /* inputs, not modified */
283  klu_symbolic<Entry, Int> *Symbolic,
284  klu_numeric<Entry, Int> *Numeric,
285  Int ldim, /* leading dimension of B */
286  Int nrhs, /* number of right-hand-sides */
287 
288  /* right-hand-side on input, overwritten with solution to Ax=b on output */
289  Entry B [ ], /* size ldim*nrhs */
290  klu_common<Entry, Int> *Common
291 ) ;
292 
293 /* -------------------------------------------------------------------------- */
294 /* klu_refactor: refactorizes matrix with same ordering as klu_factor */
295 /* -------------------------------------------------------------------------- */
296 
297 template <typename Entry, typename Int>
298 Int klu_refactor /* return TRUE if successful, FALSE otherwise */
299 (
300  /* inputs, not modified */
301  Int Ap [ ], /* size n+1, column pointers */
302  Int Ai [ ], /* size nz, row indices */
303  Entry Ax [ ], /* size nz, numerical values */
304  klu_symbolic<Entry, Int> *Symbolic,
305  /* input, and numerical values modified on output */
306  klu_numeric<Entry, Int> *Numeric,
307  klu_common<Entry, Int> *Common
308 ) ;
309 
310 /* -------------------------------------------------------------------------- */
311 /* klu_free_symbolic: destroys the Symbolic object */
312 /* -------------------------------------------------------------------------- */
313 
314 template <typename Entry, typename Int>
315 Int klu_free_symbolic
316 (
317  klu_symbolic<Entry, Int> **Symbolic,
318  klu_common<Entry, Int> *Common
319 ) ;
320 
321 /* -------------------------------------------------------------------------- */
322 /* klu_free_numeric: destroys the Numeric object */
323 /* -------------------------------------------------------------------------- */
324 
325 /* Note that klu_free_numeric and klu_z_free_numeric are identical; each can
326  * free both kinds of Numeric objects (real and complex) */
327 
328 template <typename Entry, typename Int>
329 Int klu_free_numeric
330 (
331  klu_numeric<Entry, Int> **Numeric,
332  klu_common<Entry, Int> *Common
333 ) ;
334 
335 /* -------------------------------------------------------------------------- */
336 /* klu_sort: sorts the columns of the LU factorization */
337 /* -------------------------------------------------------------------------- */
338 
339 /* this is not needed except for the MATLAB interface */
340 
341 template <typename Entry, typename Int>
342 Int klu_sort
343 (
344  /* inputs, not modified */
345  klu_symbolic<Entry, Int> *Symbolic,
346  /* input/output */
347  klu_numeric<Entry, Int> *Numeric,
348  klu_common<Entry, Int> *Common
349 ) ;
350 
351 /* -------------------------------------------------------------------------- */
352 /* klu_flops: determines # of flops performed in numeric factorzation */
353 /* -------------------------------------------------------------------------- */
354 
355 template <typename Entry, typename Int>
356 Int klu_flops
357 (
358  /* inputs, not modified */
359  klu_symbolic<Entry, Int> *Symbolic,
360  klu_numeric<Entry, Int> *Numeric,
361  /* input/output */
362  klu_common<Entry, Int> *Common
363 ) ;
364 
365 /* -------------------------------------------------------------------------- */
366 /* klu_rgrowth : compute the reciprocal pivot growth */
367 /* -------------------------------------------------------------------------- */
368 
369 /* Pivot growth is computed after the input matrix is permuted, scaled, and
370  * off-diagonal entries pruned. This is because the LU factorization of each
371  * block takes as input the scaled diagonal blocks of the BTF form. The
372  * reciprocal pivot growth in column j of an LU factorization of a matrix C
373  * is the largest entry in C divided by the largest entry in U; then the overall
374  * reciprocal pivot growth is the smallest such value for all columns j. Note
375  * that the off-diagonal entries are not scaled, since they do not take part in
376  * the LU factorization of the diagonal blocks.
377  *
378  * In MATLAB notation:
379  *
380  * rgrowth = min (max (abs ((R \ A(p,q)) - F)) ./ max (abs (U))) */
381 
382 template <typename Entry, typename Int>
383 Int klu_rgrowth
384 (
385  Int Ap [ ],
386  Int Ai [ ],
387  Entry Ax [ ],
388  klu_symbolic<Entry, Int> *Symbolic,
389  klu_numeric<Entry, Int> *Numeric,
390  klu_common<Entry, Int> *Common /* Common->rgrowth = reciprocal pivot growth */
391 ) ;
392 
393 /* -------------------------------------------------------------------------- */
394 /* klu_condest */
395 /* -------------------------------------------------------------------------- */
396 
397 /* Computes a reasonably accurate estimate of the 1-norm condition number, using
398  * Hager's method, as modified by Higham and Tisseur (same method as used in
399  * MATLAB's condest */
400 
401 template <typename Entry, typename Int>
402 Int klu_condest
403 (
404  Int Ap [ ], /* size n+1, column pointers, not modified */
405  Entry Ax [ ], /* size nz = Ap[n], numerical values, not modified*/
406  klu_symbolic<Entry, Int> *Symbolic, /* symbolic analysis, not modified */
407  klu_numeric<Entry, Int> *Numeric, /* numeric factorization, not modified */
408  klu_common<Entry, Int> *Common /* result returned in Common->condest */
409 ) ;
410 
411 /* -------------------------------------------------------------------------- */
412 /* klu_rcond: compute min(abs(diag(U))) / max(abs(diag(U))) */
413 /* -------------------------------------------------------------------------- */
414 
415 template <typename Entry, typename Int>
416 Int klu_rcond
417 (
418  klu_symbolic<Entry, Int> *Symbolic, /* input, not modified */
419  klu_numeric<Entry, Int> *Numeric, /* input, not modified */
420  klu_common<Entry, Int> *Common /* result in Common->rcond */
421 ) ;
422 
423 /* -------------------------------------------------------------------------- */
424 /* klu_scale */
425 /* -------------------------------------------------------------------------- */
426 
427 template <typename Entry, typename Int>
428 Int klu_scale /* return TRUE if successful, FALSE otherwise */
429 (
430  /* inputs, not modified */
431  Int scale, /* <0: none, no error check; 0: none, 1: sum, 2: max */
432  Int n,
433  Int Ap [ ], /* size n+1, column pointers */
434  Int Ai [ ], /* size nz, row indices */
435  Entry Ax [ ],
436  /* outputs, not defined on input */
437  double Rs [ ],
438  /* workspace, not defined on input or output */
439  Int W [ ], /* size n, can be NULL */
440  klu_common<Entry, Int> *Common
441 ) ;
442 
443 /* -------------------------------------------------------------------------- */
444 /* klu_extract */
445 /* -------------------------------------------------------------------------- */
446 
447 template <typename Entry, typename Int>
448 Int klu_extract /* returns TRUE if successful, FALSE otherwise */
449 (
450  /* inputs: */
451  klu_numeric<Entry, Int> *Numeric,
452  klu_symbolic<Entry, Int> *Symbolic,
453 
454  /* outputs, either allocated on input, or ignored otherwise */
455 
456  /* L */
457  Int *Lp, /* size n+1 */
458  Int *Li, /* size Numeric->lnz */
459  Entry *Lx, /* size Numeric->lnz */
460 
461  /* U */
462  Int *Up, /* size n+1 */
463  Int *Ui, /* size Numeric->unz */
464  Entry *Ux, /* size Numeric->unz */
465 
466  /* F */
467  Int *Fp, /* size n+1 */
468  Int *Fi, /* size Numeric->nzoff */
469  Entry *Fx, /* size Numeric->nzoff */
470 
471  /* P, row permutation */
472  Int *P, /* size n */
473 
474  /* Q, column permutation */
475  Int *Q, /* size n */
476 
477  /* Rs, scale factors */
478  Entry *Rs, /* size n */
479 
480  /* R, block boundaries */
481  Int *R, /* size Symbolic->nblocks+1 (nblocks is at most n) */
482 
483  klu_common<Entry, Int> *Common
484 ) ;
485 
486 
487 /* -------------------------------------------------------------------------- */
488 /* KLU memory management routines */
489 /* -------------------------------------------------------------------------- */
490 
491 template <typename Entry, typename Int>
492 void *klu_malloc /* returns pointer to the newly malloc'd block */
493 (
494  /* ---- input ---- */
495  size_t n, /* number of items */
496  size_t size, /* size of each item */
497  /* --------------- */
498  klu_common<Entry, Int> *Common
499 ) ;
500 
501 template <typename Entry, typename Int>
502 void *klu_free /* always returns NULL */
503 (
504  /* ---- in/out --- */
505  void *p, /* block of memory to free */
506  size_t n, /* number of items */
507  size_t size, /* size of each item */
508  /* --------------- */
509  klu_common<Entry, Int> *Common
510 ) ;
511 
512 template <typename Entry, typename Int>
513 void *klu_realloc /* returns pointer to reallocated block */
514 (
515  /* ---- input ---- */
516  size_t nnew, /* requested # of items in reallocated block */
517  size_t nold, /* current size of block, in # of items */
518  size_t size, /* size of each item */
519  /* ---- in/out --- */
520  void *p, /* block of memory to realloc */
521  /* --------------- */
522  klu_common<Entry, Int> *Common
523 ) ;
524 
525 /* ========================================================================== */
526 /* === KLU version ========================================================== */
527 /* ========================================================================== */
528 
529 /* All versions of KLU include these definitions.
530  * As an example, to test if the version you are using is 1.2 or later:
531  *
532  * if (KLU_VERSION >= KLU_VERSION_CODE (1,2)) ...
533  *
534  * This also works during compile-time:
535  *
536  * #if (KLU >= KLU_VERSION_CODE (1,2))
537  * printf ("This is version 1.2 or later\n") ;
538  * #else
539  * printf ("This is an early version\n") ;
540  * #endif
541  */
542 
543 #define KLU_DATE "Mar 24, 2009"
544 #define KLU_VERSION_CODE(main,sub) ((main) * 1000 + (sub))
545 #define KLU_MAIN_VERSION 1
546 #define KLU_SUB_VERSION 1
547 #define KLU_SUBSUB_VERSION 0
548 #define KLU_VERSION KLU_VERSION_CODE(KLU_MAIN_VERSION,KLU_SUB_VERSION)
549 
550 #endif
const int size
Definition: klu2_simple.cpp:50
void scale(ArrayView< Scalar1 > vals, size_t l, size_t ld, ArrayView< Scalar2 > s)
Scales a 1-D representation of a multivector.
int Ap[]
Column offsets.
Definition: klu2_simple.cpp:52
int Ai[]
Row values.
Definition: klu2_simple.cpp:54