Amesos Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
amesos_klu_l_factor.c
Go to the documentation of this file.
1 /* ========================================================================== */
2 /* === KLU_factor =========================================================== */
3 /* ========================================================================== */
4 
5 /* Factor the matrix, after ordering and analyzing it with KLU_analyze
6  * or KLU_analyze_given.
7  */
8 
9 /* This file should make the long int version of KLU */
10 #define DLONG 1
11 
12 #include "amesos_klu_internal.h"
13 
14 /* ========================================================================== */
15 /* === KLU_factor2 ========================================================== */
16 /* ========================================================================== */
17 
18 static void factor2
19 (
20  /* inputs, not modified */
21  Int Ap [ ], /* size n+1, column pointers */
22  Int Ai [ ], /* size nz, row indices */
23  Entry Ax [ ],
24  KLU_symbolic *Symbolic,
25 
26  /* inputs, modified on output: */
27  KLU_numeric *Numeric,
28  KLU_common *Common
29 )
30 {
31  double lsize ;
32  double *Lnz, *Rs ;
33  Int *P, *Q, *R, *Pnum, *Offp, *Offi, *Pblock, *Pinv, *Iwork,
34  *Lip, *Uip, *Llen, *Ulen ;
35  Entry *Offx, *X, s, *Udiag ;
36  Unit **LUbx ;
37  Int k1, k2, nk, k, block, oldcol, pend, oldrow, n, lnz, unz, p, newrow,
38  nblocks, poff, nzoff, lnz_block, unz_block, scale, max_lnz_block,
39  max_unz_block ;
40 
41  /* ---------------------------------------------------------------------- */
42  /* initializations */
43  /* ---------------------------------------------------------------------- */
44 
45  /* get the contents of the Symbolic object */
46  n = Symbolic->n ;
47  P = Symbolic->P ;
48  Q = Symbolic->Q ;
49  R = Symbolic->R ;
50  Lnz = Symbolic->Lnz ;
51  nblocks = Symbolic->nblocks ;
52  nzoff = Symbolic->nzoff ;
53 
54  Pnum = Numeric->Pnum ;
55  Offp = Numeric->Offp ;
56  Offi = Numeric->Offi ;
57  Offx = (Entry *) Numeric->Offx ;
58 
59  Lip = Numeric->Lip ;
60  Uip = Numeric->Uip ;
61  Llen = Numeric->Llen ;
62  Ulen = Numeric->Ulen ;
63  LUbx = (Unit **) Numeric->LUbx ;
64  Udiag = Numeric->Udiag ;
65 
66  Rs = Numeric->Rs ;
67  Pinv = Numeric->Pinv ;
68  X = (Entry *) Numeric->Xwork ; /* X is of size n */
69  Iwork = Numeric->Iwork ; /* 5*maxblock for KLU_factor */
70  /* 1*maxblock for Pblock */
71  Pblock = Iwork + 5*((size_t) Symbolic->maxblock) ;
72  Common->nrealloc = 0 ;
73  scale = Common->scale ;
74  max_lnz_block = 1 ;
75  max_unz_block = 1 ;
76 
77  /* compute the inverse of P from symbolic analysis. Will be updated to
78  * become the inverse of the numerical factorization when the factorization
79  * is done, for use in KLU_refactor */
80 #ifndef NDEBUG
81  for (k = 0 ; k < n ; k++)
82  {
83  Pinv [k] = EMPTY ;
84  }
85 #endif
86  for (k = 0 ; k < n ; k++)
87  {
88  ASSERT (P [k] >= 0 && P [k] < n) ;
89  Pinv [P [k]] = k ;
90  }
91 #ifndef NDEBUG
92  for (k = 0 ; k < n ; k++) ASSERT (Pinv [k] != EMPTY) ;
93 #endif
94 
95  lnz = 0 ;
96  unz = 0 ;
97  Common->noffdiag = 0 ;
98  Offp [0] = 0 ;
99 
100  /* ---------------------------------------------------------------------- */
101  /* optionally check input matrix and compute scale factors */
102  /* ---------------------------------------------------------------------- */
103 
104  if (scale >= 0)
105  {
106  /* use Pnum as workspace. NOTE: scale factors are not yet permuted
107  * according to the final pivot row ordering, so Rs [oldrow] is the
108  * scale factor for A (oldrow,:), for the user's matrix A. Pnum is
109  * used as workspace in KLU_scale. When the factorization is done,
110  * the scale factors are permuted according to the final pivot row
111  * permutation, so that Rs [k] is the scale factor for the kth row of
112  * A(p,q) where p and q are the final row and column permutations. */
113  KLU_scale (scale, n, Ap, Ai, (double *) Ax, Rs, Pnum, Common) ;
114  if (Common->status < KLU_OK)
115  {
116  /* matrix is invalid */
117  return ;
118  }
119  }
120 
121 #ifndef NDEBUG
122  if (scale > 0)
123  {
124  for (k = 0 ; k < n ; k++) PRINTF (("Rs [%d] %g\n", k, Rs [k])) ;
125  }
126 #endif
127 
128  /* ---------------------------------------------------------------------- */
129  /* factor each block using klu */
130  /* ---------------------------------------------------------------------- */
131 
132  for (block = 0 ; block < nblocks ; block++)
133  {
134 
135  /* ------------------------------------------------------------------ */
136  /* the block is from rows/columns k1 to k2-1 */
137  /* ------------------------------------------------------------------ */
138 
139  k1 = R [block] ;
140  k2 = R [block+1] ;
141  nk = k2 - k1 ;
142  PRINTF (("FACTOR BLOCK %d, k1 %d k2-1 %d nk %d\n", block, k1,k2-1,nk)) ;
143 
144  if (nk == 1)
145  {
146 
147  /* -------------------------------------------------------------- */
148  /* singleton case */
149  /* -------------------------------------------------------------- */
150 
151  poff = Offp [k1] ;
152  oldcol = Q [k1] ;
153  pend = Ap [oldcol+1] ;
154  CLEAR (s) ;
155 
156  if (scale <= 0)
157  {
158  /* no scaling */
159  for (p = Ap [oldcol] ; p < pend ; p++)
160  {
161  oldrow = Ai [p] ;
162  newrow = Pinv [oldrow] ;
163  if (newrow < k1)
164  {
165  Offi [poff] = oldrow ;
166  Offx [poff] = Ax [p] ;
167  poff++ ;
168  }
169  else
170  {
171  ASSERT (newrow == k1) ;
172  PRINTF (("singleton block %d", block)) ;
173  PRINT_ENTRY (Ax [p]) ;
174  s = Ax [p] ;
175  }
176  }
177  }
178  else
179  {
180  /* row scaling. NOTE: scale factors are not yet permuted
181  * according to the pivot row permutation, so Rs [oldrow] is
182  * used below. When the factorization is done, the scale
183  * factors are permuted, so that Rs [newrow] will be used in
184  * klu_solve, klu_tsolve, and klu_rgrowth */
185  for (p = Ap [oldcol] ; p < pend ; p++)
186  {
187  oldrow = Ai [p] ;
188  newrow = Pinv [oldrow] ;
189  if (newrow < k1)
190  {
191  Offi [poff] = oldrow ;
192  /* Offx [poff] = Ax [p] / Rs [oldrow] ; */
193  SCALE_DIV_ASSIGN (Offx [poff], Ax [p], Rs [oldrow]) ;
194  poff++ ;
195  }
196  else
197  {
198  ASSERT (newrow == k1) ;
199  PRINTF (("singleton block %d ", block)) ;
200  PRINT_ENTRY (Ax[p]) ;
201  SCALE_DIV_ASSIGN (s, Ax [p], Rs [oldrow]) ;
202  }
203  }
204  }
205 
206  Udiag [k1] = s ;
207 
208  if (IS_ZERO (s))
209  {
210  /* singular singleton */
211  Common->status = KLU_SINGULAR ;
212  Common->numerical_rank = k1 ;
213  Common->singular_col = oldcol ;
214  if (Common->halt_if_singular)
215  {
216  return ;
217  }
218  }
219 
220  Offp [k1+1] = poff ;
221  Pnum [k1] = P [k1] ;
222  lnz++ ;
223  unz++ ;
224 
225  }
226  else
227  {
228 
229  /* -------------------------------------------------------------- */
230  /* construct and factorize the kth block */
231  /* -------------------------------------------------------------- */
232 
233  if (Lnz [block] < 0)
234  {
235  /* COLAMD was used - no estimate of fill-in */
236  /* use 10 times the nnz in A, plus n */
237  lsize = -(Common->initmem) ;
238  }
239  else
240  {
241  lsize = Common->initmem_amd * Lnz [block] + nk ;
242  }
243 
244  /* allocates 1 arrays: LUbx [block] */
245  Numeric->LUsize [block] = KLU_kernel_factor (nk, Ap, Ai, Ax, Q,
246  lsize, &LUbx [block], Udiag + k1, Llen + k1, Ulen + k1,
247  Lip + k1, Uip + k1, Pblock, &lnz_block, &unz_block,
248  X, Iwork, k1, Pinv, Rs, Offp, Offi, Offx, Common) ;
249 
250  if (Common->status < KLU_OK ||
251  (Common->status == KLU_SINGULAR && Common->halt_if_singular))
252  {
253  /* out of memory, invalid inputs, or singular */
254  return ;
255  }
256 
257  PRINTF (("\n----------------------- L %d:\n", block)) ;
258  ASSERT (KLU_valid_LU (nk, TRUE, Lip+k1, Llen+k1, LUbx [block])) ;
259  PRINTF (("\n----------------------- U %d:\n", block)) ;
260  ASSERT (KLU_valid_LU (nk, FALSE, Uip+k1, Ulen+k1, LUbx [block])) ;
261 
262  /* -------------------------------------------------------------- */
263  /* get statistics */
264  /* -------------------------------------------------------------- */
265 
266  lnz += lnz_block ;
267  unz += unz_block ;
268  max_lnz_block = MAX (max_lnz_block, lnz_block) ;
269  max_unz_block = MAX (max_unz_block, unz_block) ;
270 
271  if (Lnz [block] == EMPTY)
272  {
273  /* revise estimate for subsequent factorization */
274  Lnz [block] = MAX (lnz_block, unz_block) ;
275  }
276 
277  /* -------------------------------------------------------------- */
278  /* combine the klu row ordering with the symbolic pre-ordering */
279  /* -------------------------------------------------------------- */
280 
281  PRINTF (("Pnum, 1-based:\n")) ;
282  for (k = 0 ; k < nk ; k++)
283  {
284  ASSERT (k + k1 < n) ;
285  ASSERT (Pblock [k] + k1 < n) ;
286  Pnum [k + k1] = P [Pblock [k] + k1] ;
287  PRINTF (("Pnum (%d + %d + 1 = %d) = %d + 1 = %d\n",
288  k, k1, k+k1+1, Pnum [k+k1], Pnum [k+k1]+1)) ;
289  }
290 
291  /* the local pivot row permutation Pblock is no longer needed */
292  }
293  }
294  ASSERT (nzoff == Offp [n]) ;
295  PRINTF (("\n------------------- Off diagonal entries:\n")) ;
296  ASSERT (KLU_valid (n, Offp, Offi, Offx)) ;
297 
298  Numeric->lnz = lnz ;
299  Numeric->unz = unz ;
300  Numeric->max_lnz_block = max_lnz_block ;
301  Numeric->max_unz_block = max_unz_block ;
302 
303  /* compute the inverse of Pnum */
304 #ifndef NDEBUG
305  for (k = 0 ; k < n ; k++)
306  {
307  Pinv [k] = EMPTY ;
308  }
309 #endif
310  for (k = 0 ; k < n ; k++)
311  {
312  ASSERT (Pnum [k] >= 0 && Pnum [k] < n) ;
313  Pinv [Pnum [k]] = k ;
314  }
315 #ifndef NDEBUG
316  for (k = 0 ; k < n ; k++) ASSERT (Pinv [k] != EMPTY) ;
317 #endif
318 
319  /* permute scale factors Rs according to pivotal row order */
320  if (scale > 0)
321  {
322  for (k = 0 ; k < n ; k++)
323  {
324  REAL (X [k]) = Rs [Pnum [k]] ;
325  }
326  for (k = 0 ; k < n ; k++)
327  {
328  Rs [k] = REAL (X [k]) ;
329  }
330  }
331 
332  PRINTF (("\n------------------- Off diagonal entries, old:\n")) ;
333  ASSERT (KLU_valid (n, Offp, Offi, Offx)) ;
334 
335  /* apply the pivot row permutations to the off-diagonal entries */
336  for (p = 0 ; p < nzoff ; p++)
337  {
338  ASSERT (Offi [p] >= 0 && Offi [p] < n) ;
339  Offi [p] = Pinv [Offi [p]] ;
340  }
341 
342  PRINTF (("\n------------------- Off diagonal entries, new:\n")) ;
343  ASSERT (KLU_valid (n, Offp, Offi, Offx)) ;
344 
345 #ifndef NDEBUG
346  {
347  PRINTF (("\n ############# KLU_BTF_FACTOR done, nblocks %d\n",nblocks));
348  Entry ss, *Udiag = Numeric->Udiag ;
349  for (block = 0 ; block < nblocks && Common->status == KLU_OK ; block++)
350  {
351  k1 = R [block] ;
352  k2 = R [block+1] ;
353  nk = k2 - k1 ;
354  PRINTF (("\n======================KLU_factor output: k1 %d k2 %d nk %d\n",k1,k2,nk)) ;
355  if (nk == 1)
356  {
357  PRINTF (("singleton ")) ;
358  /* ENTRY_PRINT (singleton [block]) ; */
359  ss = Udiag [k1] ;
360  PRINT_ENTRY (ss) ;
361  }
362  else
363  {
364  Int *Lip, *Uip, *Llen, *Ulen ;
365  Unit *LU ;
366  Lip = Numeric->Lip + k1 ;
367  Llen = Numeric->Llen + k1 ;
368  LU = (Unit *) Numeric->LUbx [block] ;
369  PRINTF (("\n---- L block %d\n", block));
370  ASSERT (KLU_valid_LU (nk, TRUE, Lip, Llen, LU)) ;
371  Uip = Numeric->Uip + k1 ;
372  Ulen = Numeric->Ulen + k1 ;
373  PRINTF (("\n---- U block %d\n", block)) ;
374  ASSERT (KLU_valid_LU (nk, FALSE, Uip, Ulen, LU)) ;
375  }
376  }
377  }
378 #endif
379 }
380 
381 
382 
383 /* ========================================================================== */
384 /* === KLU_factor =========================================================== */
385 /* ========================================================================== */
386 
387 KLU_numeric *KLU_factor /* returns NULL if error, or a valid
388  KLU_numeric object if successful */
389 (
390  /* --- inputs --- */
391  Int Ap [ ], /* size n+1, column pointers */
392  Int Ai [ ], /* size nz, row indices */
393  double Ax [ ],
394  KLU_symbolic *Symbolic,
395  /* -------------- */
396  KLU_common *Common
397 )
398 {
399  Int n, nzoff, nblocks, maxblock, k, ok = TRUE ;
400  Int *R ;
401  KLU_numeric *Numeric ;
402  size_t n1, nzoff1, s, b6, n3 ;
403 
404  if (Common == NULL)
405  {
406  return (NULL) ;
407  }
408  Common->status = KLU_OK ;
409  Common->numerical_rank = EMPTY ;
410  Common->singular_col = EMPTY ;
411 
412  /* ---------------------------------------------------------------------- */
413  /* get the contents of the Symbolic object */
414  /* ---------------------------------------------------------------------- */
415 
416  /* check for a valid Symbolic object */
417  if (Symbolic == NULL)
418  {
419  Common->status = KLU_INVALID ;
420  return (NULL) ;
421  }
422 
423  n = Symbolic->n ;
424  nzoff = Symbolic->nzoff ;
425  nblocks = Symbolic->nblocks ;
426  maxblock = Symbolic->maxblock ;
427  R = Symbolic->R ;
428  PRINTF (("KLU_factor: n %d nzoff %d nblocks %d maxblock %d\n",
429  n, nzoff, nblocks, maxblock)) ;
430 
431  /* ---------------------------------------------------------------------- */
432  /* get control parameters and make sure they are in the proper range */
433  /* ---------------------------------------------------------------------- */
434 
435  Common->initmem_amd = MAX (1.0, Common->initmem_amd) ;
436  Common->initmem = MAX (1.0, Common->initmem) ;
437  Common->tol = MIN (Common->tol, 1.0) ;
438  Common->tol = MAX (0.0, Common->tol) ;
439  Common->memgrow = MAX (1.0, Common->memgrow) ;
440 
441  /* ---------------------------------------------------------------------- */
442  /* allocate the Numeric object */
443  /* ---------------------------------------------------------------------- */
444 
445  /* this will not cause size_t overflow (already checked by KLU_symbolic) */
446  n1 = ((size_t) n) + 1 ;
447  nzoff1 = ((size_t) nzoff) + 1 ;
448 
449  Numeric = KLU_malloc (sizeof (KLU_numeric), 1, Common) ;
450  if (Common->status < KLU_OK)
451  {
452  /* out of memory */
453  Common->status = KLU_OUT_OF_MEMORY ;
454  return (NULL) ;
455  }
456  Numeric->n = n ;
457  Numeric->nblocks = nblocks ;
458  Numeric->nzoff = nzoff ;
459  Numeric->Pnum = KLU_malloc (n, sizeof (Int), Common) ;
460  Numeric->Offp = KLU_malloc (n1, sizeof (Int), Common) ;
461  Numeric->Offi = KLU_malloc (nzoff1, sizeof (Int), Common) ;
462  Numeric->Offx = KLU_malloc (nzoff1, sizeof (Entry), Common) ;
463 
464  Numeric->Lip = KLU_malloc (n, sizeof (Int), Common) ;
465  Numeric->Uip = KLU_malloc (n, sizeof (Int), Common) ;
466  Numeric->Llen = KLU_malloc (n, sizeof (Int), Common) ;
467  Numeric->Ulen = KLU_malloc (n, sizeof (Int), Common) ;
468 
469  Numeric->LUsize = KLU_malloc (nblocks, sizeof (size_t), Common) ;
470 
471  Numeric->LUbx = KLU_malloc (nblocks, sizeof (Unit *), Common) ;
472  if (Numeric->LUbx != NULL)
473  {
474  for (k = 0 ; k < nblocks ; k++)
475  {
476  Numeric->LUbx [k] = NULL ;
477  }
478  }
479 
480  Numeric->Udiag = KLU_malloc (n, sizeof (Entry), Common) ;
481 
482  if (Common->scale > 0)
483  {
484  Numeric->Rs = KLU_malloc (n, sizeof (double), Common) ;
485  }
486  else
487  {
488  /* no scaling */
489  Numeric->Rs = NULL ;
490  }
491 
492  Numeric->Pinv = KLU_malloc (n, sizeof (Int), Common) ;
493 
494  /* allocate permanent workspace for factorization and solve. Note that the
495  * solver will use an Xwork of size 4n, whereas the factorization codes use
496  * an Xwork of size n and integer space (Iwork) of size 6n. KLU_condest
497  * uses an Xwork of size 2n. Total size is:
498  *
499  * n*sizeof(Entry) + max (6*maxblock*sizeof(Int), 3*n*sizeof(Entry))
500  */
501  s = KLU_mult_size_t (n, sizeof (Entry), &ok) ;
502  n3 = KLU_mult_size_t (n, 3 * sizeof (Entry), &ok) ;
503  b6 = KLU_mult_size_t (maxblock, 6 * sizeof (Int), &ok) ;
504  Numeric->worksize = KLU_add_size_t (s, MAX (n3, b6), &ok) ;
505  Numeric->Work = KLU_malloc (Numeric->worksize, 1, Common) ;
506  Numeric->Xwork = Numeric->Work ;
507  Numeric->Iwork = (Int *) ((Entry *) Numeric->Xwork + n) ;
508  if (!ok || Common->status < KLU_OK)
509  {
510  /* out of memory or problem too large */
511  Common->status = ok ? KLU_OUT_OF_MEMORY : KLU_TOO_LARGE ;
512  KLU_free_numeric (&Numeric, Common) ;
513  return (NULL) ;
514  }
515 
516  /* ---------------------------------------------------------------------- */
517  /* factorize the blocks */
518  /* ---------------------------------------------------------------------- */
519 
520  factor2 (Ap, Ai, (Entry *) Ax, Symbolic, Numeric, Common) ;
521 
522  /* ---------------------------------------------------------------------- */
523  /* return or free the Numeric object */
524  /* ---------------------------------------------------------------------- */
525 
526  if (Common->status < KLU_OK)
527  {
528  /* out of memory or inputs invalid */
529  KLU_free_numeric (&Numeric, Common) ;
530  }
531  else if (Common->status == KLU_SINGULAR)
532  {
533  if (Common->halt_if_singular)
534  {
535  /* Matrix is singular, and the Numeric object is only partially
536  * defined because we halted early. This is the default case for
537  * a singular matrix. */
538  KLU_free_numeric (&Numeric, Common) ;
539  }
540  }
541  else if (Common->status == KLU_OK)
542  {
543  /* successful non-singular factorization */
544  Common->numerical_rank = n ;
545  Common->singular_col = n ;
546  }
547  return (Numeric) ;
548 }
#define KLU_kernel_factor
#define REAL
KLU_numeric * KLU_factor(Int Ap[], Int Ai[], double Ax[], KLU_symbolic *Symbolic, KLU_common *Common)
#define KLU_INVALID
#define PRINT_ENTRY(a)
#define EMPTY
#define KLU_symbolic
#define Int
#define FALSE
#define KLU_add_size_t
#define P(k)
#define KLU_mult_size_t
double Unit
#define MAX(a, b)
#define KLU_OUT_OF_MEMORY
#define NULL
#define KLU_free_numeric
#define CLEAR(c)
#define KLU_TOO_LARGE
#define ASSERT(expression)
#define KLU_numeric
#define KLU_valid_LU
#define KLU_malloc
#define IS_ZERO(x)
#define SCALE_DIV_ASSIGN(a, c, s)
#define KLU_scale
#define KLU_valid
#define KLU_common
#define MIN(a, b)
#define KLU_SINGULAR
#define Entry
#define PRINTF(params)
int n
#define TRUE
static void factor2(Int Ap[], Int Ai[], Entry Ax[], KLU_symbolic *Symbolic, KLU_numeric *Numeric, KLU_common *Common)
#define KLU_OK