Amesos Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
amesos_cholmod_analyze.c
Go to the documentation of this file.
1 /* ========================================================================== */
2 /* === Cholesky/cholmod_analyze ============================================= */
3 /* ========================================================================== */
4 
5 /* -----------------------------------------------------------------------------
6  * CHOLMOD/Cholesky Module. Copyright (C) 2005-2006, Timothy A. Davis
7  * The CHOLMOD/Cholesky Module is licensed under Version 2.1 of the GNU
8  * Lesser General Public License. See lesser.txt for a text of the license.
9  * CHOLMOD is also available under other licenses; contact authors for details.
10  * http://www.cise.ufl.edu/research/sparse
11  * -------------------------------------------------------------------------- */
12 
13 /* Order and analyze a matrix (either simplicial or supernodal), in prepartion
14  * for numerical factorization via cholmod_factorize or via the "expert"
15  * routines cholmod_rowfac and cholmod_super_numeric.
16  *
17  * symmetric case: A or A(p,p)
18  * unsymmetric case: AA', A(p,:)*A(p,:)', A(:,f)*A(:,f)', or A(p,f)*A(p,f)'
19  *
20  * For the symmetric case, only the upper or lower triangular part of A is
21  * accessed (depending on the type of A). LL'=A (or permuted A) is analzed.
22  * For the unsymmetric case (LL'=AA' or permuted A).
23  *
24  * There can be no duplicate entries in p or f. p is of length m if A is
25  * m-by-n. f can be length 0 to n.
26  *
27  * In both cases, the columns of A need not be sorted. A can be in packed
28  * or unpacked form.
29  *
30  * Ordering options include:
31  *
32  * natural: A is not permuted to reduce fill-in
33  * given: a permutation can be provided to this routine (UserPerm)
34  * AMD: approximate minumum degree (AMD for the symmetric case,
35  * COLAMD for the AA' case).
36  * METIS: nested dissection with METIS_NodeND
37  * NESDIS: nested dissection using METIS_NodeComputeSeparator,
38  * typically followed by a constrained minimum degree
39  * (CAMD for the symmetric case, CCOLAMD for the AA' case).
40  *
41  * Multiple ordering options can be tried (up to 9 of them), and the best one
42  * is selected (the one that gives the smallest number of nonzeros in the
43  * simplicial factor L). If one method fails, cholmod_analyze keeps going, and
44  * picks the best among the methods that succeeded. This routine fails (and
45  * returns NULL) if either initial memory allocation fails, all ordering methods
46  * fail, or the supernodal analysis (if requested) fails. By default, the 9
47  * methods available are:
48  *
49  * 1) given permutation (skipped if UserPerm is NULL)
50  * 2) AMD (symmetric case) or COLAMD (unsymmetric case)
51  * 3) METIS with default parameters
52  * 4) NESDIS with default parameters (stopping the partitioning when
53  * the graph is of size nd_small = 200 or less, remove nodes with
54  * more than max (16, prune_dense * sqrt (n)) nodes where
55  * prune_dense = 10, and follow partitioning with CCOLAMD, a
56  * constrained minimum degree ordering).
57  * 5) natural
58  * 6) NESDIS, nd_small = 20000, prune_dense = 10
59  * 7) NESDIS, nd_small = 4, prune_dense = 10, no min degree
60  * 8) NESDIS, nd_small = 200, prune_dense = 0
61  * 9) COLAMD for A*A' or AMD for A
62  *
63  * By default, the first two are tried, and METIS is tried if AMD reports a high
64  * flop count and fill-in. Let fl denote the flop count for the AMD, ordering,
65  * nnz(L) the # of nonzeros in L, and nnz(tril(A)) (or A*A'). If
66  * fl/nnz(L) >= 500 and nnz(L)/nnz(tril(A)) >= 5, then METIS is attempted. The
67  * best ordering is used (UserPerm if given, AMD, and METIS if attempted). If
68  * you do not have METIS, only the first two will be tried (user permutation,
69  * if provided, and AMD/COLAMD). This default behavior is obtained when
70  * Common->nmethods is zero. In this case, methods 0, 1, and 2 in
71  * Common->method [..] are reset to User-provided, AMD, and METIS (or NESDIS
72  * if Common->default_nesdis is set to the non-default value of TRUE),
73  * respectively.
74  *
75  * You can modify these 9 methods and the number of methods tried by changing
76  * parameters in the Common argument. If you know the best ordering for your
77  * matrix, set Common->nmethods to 1 and set Common->method[0].ordering to the
78  * requested ordering method. Parameters for each method can also be modified
79  * (refer to cholmod.h for details).
80  *
81  * Note that it is possible for METIS to terminate your program if it runs out
82  * of memory. This is not the case for any CHOLMOD or minimum degree ordering
83  * routine (AMD, COLAMD, CAMD, CCOLAMD, or CSYMAMD). Since NESDIS relies on
84  * METIS, it too can terminate your program.
85  *
86  * The factor L is returned as simplicial symbolic (L->is_super FALSE) if
87  * Common->supernodal <= CHOLMOD_SIMPLICIAL (0) or as supernodal symbolic if
88  * Common->supernodal >= CHOLMOD_SUPERNODAL (2). If Common->supernodal is
89  * equal to CHOLMOD_AUTO (1), then L is simplicial if the flop count per
90  * nonzero in L is less than Common->supernodal_switch (default: 40), and
91  * is returned as a supernodal factor otherwise.
92  *
93  * In both cases, L->xtype is CHOLMOD_PATTERN.
94  * A subsequent call to cholmod_factorize will perform a
95  * simplicial or supernodal factorization, depending on the type of L.
96  *
97  * For the simplicial case, L contains the fill-reducing permutation (L->Perm)
98  * and the counts of nonzeros in each column of L (L->ColCount). For the
99  * supernodal case, L also contains the nonzero pattern of each supernode.
100  *
101  * workspace: Flag (nrow), Head (nrow+1)
102  * if symmetric: Iwork (6*nrow)
103  * if unsymmetric: Iwork (6*nrow+ncol).
104  * calls various ordering routines, which typically allocate O(nnz(A))
105  * temporary workspace ((2 to 3)*nnz(A) * sizeof (Int) is typical, but it
106  * can be much higher if A*A' must be explicitly formed for METIS). Also
107  * allocates up to 2 temporary (permuted/transpose) copies of the nonzero
108  * pattern of A, and up to 3*n*sizeof(Int) additional workspace.
109  *
110  * Supports any xtype (pattern, real, complex, or zomplex)
111  */
112 
113 #ifndef NCHOLESKY
114 
115 #include "amesos_cholmod_internal.h"
116 #include "amesos_cholmod_cholesky.h"
117 
118 #ifndef NPARTITION
120 #endif
121 
122 
123 /* ========================================================================== */
124 /* === cholmod_analyze ====================================================== */
125 /* ========================================================================== */
126 
127 /* Orders and analyzes A, AA', PAP', or PAA'P' and returns a symbolic factor
128  * that can later be passed to cholmod_factorize. */
129 
131 (
132  /* ---- input ---- */
133  cholmod_sparse *A, /* matrix to order and analyze */
134  /* --------------- */
135  cholmod_common *Common
136 )
137 {
138  return (CHOLMOD(analyze_p) (A, NULL, NULL, 0, Common)) ;
139 }
140 
141 
142 /* ========================================================================== */
143 /* === permute_matrices ===================================================== */
144 /* ========================================================================== */
145 
146 /* Permute and transpose a matrix. Allocates the A1 and A2 matrices, if needed,
147  * or returns them as NULL if not needed.
148  */
149 
150 static int amesos_permute_matrices
151 (
152  /* ---- input ---- */
153  cholmod_sparse *A, /* matrix to permute */
154  Int ordering, /* ordering method used */
155  Int *Perm, /* fill-reducing permutation */
156  Int *fset, /* subset of 0:(A->ncol)-1 */
157  size_t fsize, /* size of fset */
158  Int do_rowcolcounts,/* if TRUE, compute both S and F. If FALSE, only
159  * S is needed for the symmetric case, and only F for
160  * the unsymmetric case */
161  /* ---- output --- */
162  cholmod_sparse **A1_handle, /* see comments below for A1, A2, S, F */
163  cholmod_sparse **A2_handle,
164  cholmod_sparse **S_handle,
165  cholmod_sparse **F_handle,
166  /* --------------- */
167  cholmod_common *Common
168 )
169 {
170  cholmod_sparse *A1, *A2, *S, *F ;
171 
172  *A1_handle = NULL ;
173  *A2_handle = NULL ;
174  *S_handle = NULL ;
175  *F_handle = NULL ;
176  A1 = NULL ;
177  A2 = NULL ;
178 
179  if (ordering == CHOLMOD_NATURAL)
180  {
181 
182  /* ------------------------------------------------------------------ */
183  /* natural ordering of A */
184  /* ------------------------------------------------------------------ */
185 
186  if (A->stype < 0)
187  {
188  /* symmetric lower case: A already in lower form, so S=A' */
189  /* workspace: Iwork (nrow) */
190  A2 = CHOLMOD(ptranspose) (A, 0, NULL, NULL, 0, Common) ;
191  F = A ;
192  S = A2 ;
193  }
194  else if (A->stype > 0)
195  {
196  /* symmetric upper case: F = pattern of triu (A)', S = A */
197  /* workspace: Iwork (nrow) */
198  if (do_rowcolcounts)
199  {
200  /* F not needed for symmetric case if do_rowcolcounts FALSE */
201  A1 = CHOLMOD(ptranspose) (A, 0, NULL, fset, fsize, Common) ;
202  }
203  F = A1 ;
204  S = A ;
205  }
206  else
207  {
208  /* unsymmetric case: F = pattern of A (:,f)', S = A */
209  /* workspace: Iwork (nrow if no fset, MAX(nrow,ncol) if fset) */
210  A1 = CHOLMOD(ptranspose) (A, 0, NULL, fset, fsize, Common) ;
211  F = A1 ;
212  S = A ;
213  }
214 
215  }
216  else
217  {
218 
219  /* ------------------------------------------------------------------ */
220  /* A is permuted */
221  /* ------------------------------------------------------------------ */
222 
223  if (A->stype < 0)
224  {
225  /* symmetric lower case: S = tril (A (p,p))' and F = S' */
226  /* workspace: Iwork (2*nrow) */
227  A2 = CHOLMOD(ptranspose) (A, 0, Perm, NULL, 0, Common) ;
228  S = A2 ;
229  /* workspace: Iwork (nrow) */
230  if (do_rowcolcounts)
231  {
232  /* F not needed for symmetric case if do_rowcolcounts FALSE */
233  A1 = CHOLMOD(ptranspose) (A2, 0, NULL, NULL, 0, Common) ;
234  }
235  F = A1 ;
236  }
237  else if (A->stype > 0)
238  {
239  /* symmetric upper case: F = triu (A (p,p))' and S = F' */
240  /* workspace: Iwork (2*nrow) */
241  A1 = CHOLMOD(ptranspose) (A, 0, Perm, NULL, 0, Common) ;
242  F = A1 ;
243  /* workspace: Iwork (nrow) */
244  A2 = CHOLMOD(ptranspose) (A1, 0, NULL, NULL, 0, Common) ;
245  S = A2 ;
246  }
247  else
248  {
249  /* unsymmetric case: F = A (p,f)' and S = F' */
250  /* workspace: Iwork (nrow if no fset, MAX(nrow,ncol) if fset) */
251  A1 = CHOLMOD(ptranspose) (A, 0, Perm, fset, fsize, Common) ;
252  F = A1 ;
253  if (do_rowcolcounts)
254  {
255  /* S not needed for unsymmetric case if do_rowcolcounts FALSE */
256  /* workspace: Iwork (nrow) */
257  A2 = CHOLMOD(ptranspose) (A1, 0, NULL, NULL, 0, Common) ;
258  }
259  S = A2 ;
260  }
261  }
262 
263  /* If any cholmod_*transpose fails, one or more matrices will be NULL */
264  *A1_handle = A1 ;
265  *A2_handle = A2 ;
266  *S_handle = S ;
267  *F_handle = F ;
268  return (Common->status == CHOLMOD_OK) ;
269 }
270 
271 
272 /* ========================================================================== */
273 /* === cholmod_analyze_ordering ============================================= */
274 /* ========================================================================== */
275 
276 /* Given a matrix A and its fill-reducing permutation, compute the elimination
277  * tree, its (non-weighted) postordering, and the number of nonzeros in each
278  * column of L. Also computes the flop count, the total nonzeros in L, and
279  * the nonzeros in A (Common->fl, Common->lnz, and Common->anz).
280  *
281  * The column counts of L, flop count, and other statistics from
282  * cholmod_rowcolcounts are not computed if ColCount is NULL.
283  *
284  * workspace: Iwork (2*nrow if symmetric, 2*nrow+ncol if unsymmetric),
285  * Flag (nrow), Head (nrow+1)
286  */
287 
289 (
290  /* ---- input ---- */
291  cholmod_sparse *A, /* matrix to analyze */
292  int ordering, /* ordering method used */
293  Int *Perm, /* size n, fill-reducing permutation to analyze */
294  Int *fset, /* subset of 0:(A->ncol)-1 */
295  size_t fsize, /* size of fset */
296  /* ---- output --- */
297  Int *Parent, /* size n, elimination tree */
298  Int *Post, /* size n, postordering of elimination tree */
299  Int *ColCount, /* size n, nnz in each column of L */
300  /* ---- workspace */
301  Int *First, /* size nworkspace for cholmod_postorder */
302  Int *Level, /* size n workspace for cholmod_postorder */
303  /* --------------- */
304  cholmod_common *Common
305 )
306 {
307  cholmod_sparse *A1, *A2, *S, *F ;
308  Int n, ok, do_rowcolcounts ;
309 
310  /* check inputs */
312  RETURN_IF_NULL (A, FALSE) ;
313 
314  n = A->nrow ;
315 
316  do_rowcolcounts = (ColCount != NULL) ;
317 
318  /* permute A according to Perm and fset */
319  ok = amesos_permute_matrices (A, ordering, Perm, fset, fsize, do_rowcolcounts,
320  &A1, &A2, &S, &F, Common) ;
321 
322  /* find etree of S (symmetric upper/lower case) or F (unsym case) */
323  /* workspace: symmmetric: Iwork (nrow), unsym: Iwork (nrow+ncol) */
324  ok = ok && CHOLMOD(etree) (A->stype ? S:F, Parent, Common) ;
325 
326  /* postorder the etree (required by cholmod_rowcolcounts) */
327  /* workspace: Iwork (2*nrow) */
328  ok = ok && (CHOLMOD(postorder) (Parent, n, NULL, Post, Common) == n) ;
329 
330  /* cholmod_postorder doesn't set Common->status if it returns < n */
331  Common->status = (!ok && Common->status == CHOLMOD_OK) ?
332  CHOLMOD_INVALID : Common->status ;
333 
334  /* analyze LL'=S or SS' or S(:,f)*S(:,f)' */
335  /* workspace:
336  * if symmetric: Flag (nrow), Iwork (2*nrow)
337  * if unsymmetric: Flag (nrow), Iwork (2*nrow+ncol), Head (nrow+1)
338  */
339  if (do_rowcolcounts)
340  {
341  ok = ok && CHOLMOD(rowcolcounts) (A->stype ? F:S, fset, fsize, Parent,
342  Post, NULL, ColCount, First, Level, Common) ;
343  }
344 
345  /* free temporary matrices and return result */
346  CHOLMOD(free_sparse) (&A1, Common) ;
347  CHOLMOD(free_sparse) (&A2, Common) ;
348  return (ok) ;
349 }
350 
351 
352 /* ========================================================================== */
353 /* === Free workspace and return L ========================================== */
354 /* ========================================================================== */
355 
356 #define FREE_WORKSPACE_AND_RETURN \
357 { \
358  Common->no_workspace_reallocate = FALSE ; \
359  CHOLMOD(free) (n, sizeof (Int), Lparent, Common) ; \
360  CHOLMOD(free) (n, sizeof (Int), Perm, Common) ; \
361  CHOLMOD(free) (n, sizeof (Int), ColCount, Common) ; \
362  if (Common->status < CHOLMOD_OK) \
363  { \
364  CHOLMOD(free_factor) (&L, Common) ; \
365  } \
366  ASSERT (CHOLMOD(dump_work) (TRUE, TRUE, 0, Common)) ; \
367  return (L) ; \
368 }
369 
370 
371 /* ========================================================================== */
372 /* === cholmod_analyze_p ==================================================== */
373 /* ========================================================================== */
374 
375 /* Orders and analyzes A, AA', PAP', PAA'P', FF', or PFF'P and returns a
376  * symbolic factor that can later be passed to cholmod_factorize, where
377  * F = A(:,fset) if fset is not NULL and A->stype is zero.
378  * UserPerm is tried if non-NULL. */
379 
381 (
382  /* ---- input ---- */
383  cholmod_sparse *A, /* matrix to order and analyze */
384  Int *UserPerm, /* user-provided permutation, size A->nrow */
385  Int *fset, /* subset of 0:(A->ncol)-1 */
386  size_t fsize, /* size of fset */
387  /* --------------- */
388  cholmod_common *Common
389 )
390 {
391  double lnz_best ;
392  Int *First, *Level, *Work4n, *Cmember, *CParent, *ColCount, *Lperm, *Parent,
393  *Post, *Perm, *Lparent, *Lcolcount ;
394  cholmod_factor *L ;
395  Int k, n, ordering, method, nmethods, status, default_strategy, ncol, uncol,
396  skip_analysis, skip_best ;
397  size_t s ;
398  int ok = TRUE ;
399 
400  /* ---------------------------------------------------------------------- */
401  /* check inputs */
402  /* ---------------------------------------------------------------------- */
403 
405  RETURN_IF_NULL (A, NULL) ;
407  Common->status = CHOLMOD_OK ;
408  status = CHOLMOD_OK ;
409  Common->selected = EMPTY ;
410  Common->called_nd = FALSE ;
411 
412  /* ---------------------------------------------------------------------- */
413  /* get inputs */
414  /* ---------------------------------------------------------------------- */
415 
416  n = A->nrow ;
417  ncol = A->ncol ;
418  uncol = (A->stype == 0) ? (A->ncol) : 0 ;
419 
420  /* ---------------------------------------------------------------------- */
421  /* set the default strategy */
422  /* ---------------------------------------------------------------------- */
423 
424  lnz_best = (double) EMPTY ;
425  skip_best = FALSE ;
426  nmethods = MIN (Common->nmethods, CHOLMOD_MAXMETHODS) ;
427  nmethods = MAX (0, nmethods) ;
428  PRINT1 (("nmethods "ID"\n", nmethods)) ;
429 
430  default_strategy = (nmethods == 0) ;
431  if (default_strategy)
432  {
433  /* default strategy: try UserPerm, if given. Try AMD for A, or COLAMD
434  * to order A*A'. Try METIS for the symmetric case only if AMD reports
435  * a high degree of fill-in and flop count. Always try METIS for the
436  * unsymmetric case. METIS is not tried if the Partition Module
437  * isn't installed. If Common->default_nesdis is TRUE, then NESDIS
438  * is used as the 3rd ordering instead. */
439  Common->method [0].ordering = CHOLMOD_GIVEN ;/* skip if UserPerm NULL */
440  Common->method [1].ordering = CHOLMOD_AMD ;
441  Common->method [2].ordering =
443 #ifndef NPARTITION
444  nmethods = 3 ;
445 #else
446  nmethods = 2 ;
447 #endif
448  }
449 
450 #ifdef NSUPERNODAL
451  /* CHOLMOD Supernodal module not installed, just do simplicial analysis */
452  Common->supernodal = CHOLMOD_SIMPLICIAL ;
453 #endif
454 
455  /* ---------------------------------------------------------------------- */
456  /* allocate workspace */
457  /* ---------------------------------------------------------------------- */
458 
459  /* Note: enough space needs to be allocated here so that routines called by
460  * cholmod_analyze do not reallocate the space.
461  */
462 
463  /* s = 6*n + uncol */
464  s = CHOLMOD(mult_size_t) (n, 6, &ok) ;
465  s = CHOLMOD(add_size_t) (s, uncol, &ok) ;
466  if (!ok)
467  {
468  ERROR (CHOLMOD_TOO_LARGE, "problem too large") ;
469  return (NULL) ;
470  }
471 
472  CHOLMOD(allocate_work) (n, s, 0, Common) ;
473  if (Common->status < CHOLMOD_OK)
474  {
475  return (NULL) ; /* out of memory */
476  }
477  ASSERT (CHOLMOD(dump_work) (TRUE, TRUE, 0, Common)) ;
478 
479  /* ensure that subsequent routines, called by cholmod_analyze, do not
480  * reallocate any workspace. This is set back to FALSE in the
481  * FREE_WORKSPACE_AND_RETURN macro, which is the only way this function
482  * returns to its caller. */
483  Common->no_workspace_reallocate = TRUE ;
484 
485  /* Use the last 4*n Int's in Iwork for Parent, First, Level, and Post, since
486  * other CHOLMOD routines will use the first 2n+uncol space. The ordering
487  * routines (cholmod_amd, cholmod_colamd, cholmod_ccolamd, cholmod_metis)
488  * are an exception. They can use all 6n + ncol space, since the contents
489  * of Parent, First, Level, and Post are not needed across calls to those
490  * routines. */
491  Work4n = Common->Iwork ;
492  Work4n += 2*((size_t) n) + uncol ;
493  Parent = Work4n ;
494  First = Work4n + n ;
495  Level = Work4n + 2*((size_t) n) ;
496  Post = Work4n + 3*((size_t) n) ;
497 
498  /* note that this assignment means that cholmod_nested_dissection,
499  * cholmod_ccolamd, and cholmod_camd can use only the first 4n+uncol
500  * space in Common->Iwork */
501  Cmember = Post ;
502  CParent = Level ;
503 
504  /* ---------------------------------------------------------------------- */
505  /* allocate more workspace, and an empty simplicial symbolic factor */
506  /* ---------------------------------------------------------------------- */
507 
508  L = CHOLMOD(allocate_factor) (n, Common) ;
509  Lparent = CHOLMOD(malloc) (n, sizeof (Int), Common) ;
510  Perm = CHOLMOD(malloc) (n, sizeof (Int), Common) ;
511  ColCount = CHOLMOD(malloc) (n, sizeof (Int), Common) ;
512  if (Common->status < CHOLMOD_OK)
513  {
514  /* out of memory */
516  }
517  Lperm = L->Perm ;
518  Lcolcount = L->ColCount ;
519  Common->anz = EMPTY ;
520 
521  /* ---------------------------------------------------------------------- */
522  /* try all the requested ordering options and backup to AMD if needed */
523  /* ---------------------------------------------------------------------- */
524 
525  /* turn off error handling [ */
526  Common->try_catch = TRUE ;
527 
528  for (method = 0 ; method <= nmethods ; method++)
529  {
530 
531  /* ------------------------------------------------------------------ */
532  /* determine the method to try */
533  /* ------------------------------------------------------------------ */
534 
535  Common->fl = EMPTY ;
536  Common->lnz = EMPTY ;
537  skip_analysis = FALSE ;
538 
539  if (method == nmethods)
540  {
541  /* All methods failed: backup to AMD */
542  if (Common->selected == EMPTY && !default_strategy)
543  {
544  PRINT1 (("All methods requested failed: backup to AMD\n")) ;
545  ordering = CHOLMOD_AMD ;
546  }
547  else
548  {
549  break ;
550  }
551  }
552  else
553  {
554  ordering = Common->method [method].ordering ;
555  }
556  Common->current = method ;
557  PRINT1 (("method "ID": Try method: "ID"\n", method, ordering)) ;
558 
559  /* ------------------------------------------------------------------ */
560  /* find the fill-reducing permutation */
561  /* ------------------------------------------------------------------ */
562 
563  if (ordering == CHOLMOD_NATURAL)
564  {
565 
566  /* -------------------------------------------------------------- */
567  /* natural ordering */
568  /* -------------------------------------------------------------- */
569 
570  for (k = 0 ; k < n ; k++)
571  {
572  Perm [k] = k ;
573  }
574 
575  }
576  else if (ordering == CHOLMOD_GIVEN)
577  {
578 
579  /* -------------------------------------------------------------- */
580  /* use given ordering of A, if provided */
581  /* -------------------------------------------------------------- */
582 
583  if (UserPerm == NULL)
584  {
585  /* this is not an error condition */
586  PRINT1 (("skip, no user perm given\n")) ;
587  continue ;
588  }
589  for (k = 0 ; k < n ; k++)
590  {
591  /* UserPerm is checked in cholmod_ptranspose */
592  Perm [k] = UserPerm [k] ;
593  }
594 
595  }
596  else if (ordering == CHOLMOD_AMD)
597  {
598 
599  /* -------------------------------------------------------------- */
600  /* AMD ordering of A, A*A', or A(:,f)*A(:,f)' */
601  /* -------------------------------------------------------------- */
602 
603  CHOLMOD(amd) (A, fset, fsize, Perm, Common) ;
604  skip_analysis = TRUE ;
605 
606  }
607  else if (ordering == CHOLMOD_COLAMD)
608  {
609 
610  /* -------------------------------------------------------------- */
611  /* AMD for symmetric case, COLAMD for A*A' or A(:,f)*A(:,f)' */
612  /* -------------------------------------------------------------- */
613 
614  if (A->stype)
615  {
616  CHOLMOD(amd) (A, fset, fsize, Perm, Common) ;
617  skip_analysis = TRUE ;
618  }
619  else
620  {
621  /* Alternative:
622  CHOLMOD(ccolamd) (A, fset, fsize, NULL, Perm, Common) ;
623  */
624  /* do not postorder, it is done later, below */
625  /* workspace: Iwork (4*nrow+uncol), Flag (nrow), Head (nrow+1)*/
626  CHOLMOD(colamd) (A, fset, fsize, FALSE, Perm, Common) ;
627  }
628 
629  }
630  else if (ordering == CHOLMOD_METIS)
631  {
632 
633  /* -------------------------------------------------------------- */
634  /* use METIS_NodeND directly (via a CHOLMOD wrapper) */
635  /* -------------------------------------------------------------- */
636 
637 #ifndef NPARTITION
638  /* postorder parameter is false, because it will be later, below */
639  /* workspace: Iwork (4*nrow+uncol), Flag (nrow), Head (nrow+1) */
640  Common->called_nd = TRUE ;
641  CHOLMOD(metis) (A, fset, fsize, FALSE, Perm, Common) ;
642 #else
643  Common->status = CHOLMOD_NOT_INSTALLED ;
644 #endif
645 
646  }
647  else if (ordering == CHOLMOD_NESDIS)
648  {
649 
650  /* -------------------------------------------------------------- */
651  /* use CHOLMOD's nested dissection */
652  /* -------------------------------------------------------------- */
653 
654  /* this method is based on METIS' node bissection routine
655  * (METIS_NodeComputeSeparator). In contrast to METIS_NodeND,
656  * it calls CAMD or CCOLAMD on the whole graph, instead of MMD
657  * on just the leaves. */
658 #ifndef NPARTITION
659  /* workspace: Flag (nrow), Head (nrow+1), Iwork (2*nrow) */
660  Common->called_nd = TRUE ;
661  CHOLMOD(nested_dissection) (A, fset, fsize, Perm, CParent, Cmember,
662  Common) ;
663 #else
664  Common->status = CHOLMOD_NOT_INSTALLED ;
665 #endif
666 
667  }
668  else
669  {
670 
671  /* -------------------------------------------------------------- */
672  /* invalid ordering method */
673  /* -------------------------------------------------------------- */
674 
675  Common->status = CHOLMOD_INVALID ;
676  PRINT1 (("No such ordering: "ID"\n", ordering)) ;
677  }
678 
679  ASSERT (CHOLMOD(dump_work) (TRUE, TRUE, 0, Common)) ;
680 
681  if (Common->status < CHOLMOD_OK)
682  {
683  /* out of memory, or method failed */
684  status = MIN (status, Common->status) ;
685  Common->status = CHOLMOD_OK ;
686  continue ;
687  }
688 
689  /* ------------------------------------------------------------------ */
690  /* analyze the ordering */
691  /* ------------------------------------------------------------------ */
692 
693  if (!skip_analysis)
694  {
695  if (!CHOLMOD(analyze_ordering) (A, ordering, Perm, fset, fsize,
696  Parent, Post, ColCount, First, Level, Common))
697  {
698  /* ordering method failed; clear status and try next method */
699  status = MIN (status, Common->status) ;
700  Common->status = CHOLMOD_OK ;
701  continue ;
702  }
703  }
704 
705  ASSERT (Common->fl >= 0 && Common->lnz >= 0) ;
706  Common->method [method].fl = Common->fl ;
707  Common->method [method].lnz = Common->lnz ;
708  PRINT1 (("lnz %g fl %g\n", Common->lnz, Common->fl)) ;
709 
710  /* ------------------------------------------------------------------ */
711  /* pick the best method */
712  /* ------------------------------------------------------------------ */
713 
714  /* fl.pt. compare, but lnz can never be NaN */
715  if (Common->selected == EMPTY || Common->lnz < lnz_best)
716  {
717  Common->selected = method ;
718  PRINT1 (("this is best so far, method "ID"\n", method)) ;
719  L->ordering = ordering ;
720  lnz_best = Common->lnz ;
721  for (k = 0 ; k < n ; k++)
722  {
723  Lperm [k] = Perm [k] ;
724  }
725  /* save the results of cholmod_analyze_ordering, if it was called */
726  skip_best = skip_analysis ;
727  if (!skip_analysis)
728  {
729  /* save the column count; becomes permanent part of L */
730  for (k = 0 ; k < n ; k++)
731  {
732  Lcolcount [k] = ColCount [k] ;
733  }
734  /* Parent is needed for weighted postordering and for supernodal
735  * analysis. Does not become a permanent part of L */
736  for (k = 0 ; k < n ; k++)
737  {
738  Lparent [k] = Parent [k] ;
739  }
740  }
741  }
742 
743  /* ------------------------------------------------------------------ */
744  /* determine if METIS is to be skipped */
745  /* ------------------------------------------------------------------ */
746 
747  if (default_strategy && ordering == CHOLMOD_AMD)
748  {
749  if ((Common->fl < 500 * Common->lnz) ||
750  (Common->lnz < 5 * Common->anz))
751  {
752  /* AMD found an ordering with less than 500 flops per nonzero in
753  * L, or one with a fill-in ratio (nnz(L)/nnz(A)) of less than
754  * 5. This is pretty good, and it's unlikely that METIS will do
755  * better (this heuristic is based on tests on all symmetric
756  * positive definite matrices in the UF sparse matrix
757  * collection, and it works well across a wide range of
758  * problems). METIS can take much more time and AMD. */
759  break ;
760  }
761  }
762  }
763 
764  /* turn error printing back on ] */
765  Common->try_catch = FALSE ;
766 
767  /* ---------------------------------------------------------------------- */
768  /* return if no ordering method succeeded */
769  /* ---------------------------------------------------------------------- */
770 
771  if (Common->selected == EMPTY)
772  {
773  /* All methods failed.
774  * If two or more methods failed, they may have failed for different
775  * reasons. Both would clear Common->status and skip to the next
776  * method. Common->status needs to be restored here to the worst error
777  * obtained in any of the methods. CHOLMOD_INVALID is worse
778  * than CHOLMOD_OUT_OF_MEMORY, since the former implies something may
779  * be wrong with the user's input. CHOLMOD_OUT_OF_MEMORY is simply an
780  * indication of lack of resources. */
781  ASSERT (status < CHOLMOD_OK) ;
782  ERROR (status, "all methods failed") ;
784  }
785 
786  /* ---------------------------------------------------------------------- */
787  /* do the analysis for AMD, if skipped */
788  /* ---------------------------------------------------------------------- */
789 
790  Common->fl = Common->method [Common->selected].fl ;
791  Common->lnz = Common->method [Common->selected].lnz ;
792  ASSERT (Common->lnz >= 0) ;
793 
794  if (skip_best)
795  {
796  if (!CHOLMOD(analyze_ordering) (A, L->ordering, Lperm, fset, fsize,
797  Lparent, Post, Lcolcount, First, Level, Common))
798  {
799  /* out of memory, or method failed */
801  }
802  }
803 
804  /* ---------------------------------------------------------------------- */
805  /* postorder the etree, weighted by the column counts */
806  /* ---------------------------------------------------------------------- */
807 
808  if (Common->postorder)
809  {
810  /* combine the fill-reducing ordering with the weighted postorder */
811  /* workspace: Iwork (2*nrow) */
812  if (CHOLMOD(postorder) (Lparent, n, Lcolcount, Post, Common) == n)
813  {
814  /* use First and Level as workspace [ */
815  Int *Wi = First, *InvPost = Level ;
816  Int newchild, oldchild, newparent, oldparent ;
817 
818  for (k = 0 ; k < n ; k++)
819  {
820  Wi [k] = Lperm [Post [k]] ;
821  }
822  for (k = 0 ; k < n ; k++)
823  {
824  Lperm [k] = Wi [k] ;
825  }
826 
827  for (k = 0 ; k < n ; k++)
828  {
829  Wi [k] = Lcolcount [Post [k]] ;
830  }
831  for (k = 0 ; k < n ; k++)
832  {
833  Lcolcount [k] = Wi [k] ;
834  }
835  for (k = 0 ; k < n ; k++)
836  {
837  InvPost [Post [k]] = k ;
838  }
839 
840  /* updated Lparent needed only for supernodal case */
841  for (newchild = 0 ; newchild < n ; newchild++)
842  {
843  oldchild = Post [newchild] ;
844  oldparent = Lparent [oldchild] ;
845  newparent = (oldparent == EMPTY) ? EMPTY : InvPost [oldparent] ;
846  Wi [newchild] = newparent ;
847  }
848  for (k = 0 ; k < n ; k++)
849  {
850  Lparent [k] = Wi [k] ;
851  }
852  /* done using Iwork as workspace ] */
853 
854  /* L is now postordered, no longer in natural ordering */
855  if (L->ordering == CHOLMOD_NATURAL)
856  {
858  }
859  }
860  }
861 
862  /* ---------------------------------------------------------------------- */
863  /* supernodal analysis, if requested or if selected automatically */
864  /* ---------------------------------------------------------------------- */
865 
866 #ifndef NSUPERNODAL
867  if (Common->supernodal > CHOLMOD_AUTO
868  || (Common->supernodal == CHOLMOD_AUTO &&
869  Common->lnz > 0 &&
870  (Common->fl / Common->lnz) >= Common->supernodal_switch))
871  {
872  cholmod_sparse *S, *F, *A2, *A1 ;
873 
874  amesos_permute_matrices (A, L->ordering, Lperm, fset, fsize, TRUE,
875  &A1, &A2, &S, &F, Common) ;
876 
877  /* workspace: Flag (nrow), Head (nrow), Iwork (5*nrow) */
878  CHOLMOD(super_symbolic) (S, F, Lparent, L, Common) ;
879  PRINT1 (("status %d\n", Common->status)) ;
880 
881  CHOLMOD(free_sparse) (&A1, Common) ;
882  CHOLMOD(free_sparse) (&A2, Common) ;
883  }
884 #endif
885 
886  /* ---------------------------------------------------------------------- */
887  /* free temporary matrices and workspace, and return result L */
888  /* ---------------------------------------------------------------------- */
889 
891 }
892 #endif
#define CHOLMOD_TOO_LARGE
int CHOLMOD() colamd(cholmod_sparse *A, Int *fset, size_t fsize, int postorder, Int *Perm, cholmod_common *Common)
#define CHOLMOD_NOT_INSTALLED
#define EMPTY
#define CHOLMOD_AUTO
#define Int
size_t CHOLMOD() add_size_t(size_t a, size_t b, int *ok)
#define FREE_WORKSPACE_AND_RETURN
int CHOLMOD() rowcolcounts(cholmod_sparse *A, Int *fset, size_t fsize, Int *Parent, Int *Post, Int *RowCount, Int *ColCount, Int *First, Int *Level, cholmod_common *Common)
#define FALSE
#define PRINT1(params)
#define RETURN_IF_NULL_COMMON(result)
struct cholmod_common_struct::cholmod_method_struct method[CHOLMOD_MAXMETHODS+1]
size_t CHOLMOD() mult_size_t(size_t a, size_t k, int *ok)
int CHOLMOD() dump_work(int flag, int head, UF_long wsize, cholmod_common *Common)
#define CHOLMOD_PATTERN
#define CHOLMOD(name)
#define MAX(a, b)
#define CHOLMOD_GIVEN
#define CHOLMOD_AMD
cholmod_sparse *CHOLMOD() ptranspose(cholmod_sparse *A, int values, Int *Perm, Int *fset, size_t fsize, cholmod_common *Common)
cholmod_factor *CHOLMOD() analyze(cholmod_sparse *A, cholmod_common *Common)
int CHOLMOD() free_sparse(cholmod_sparse **AHandle, cholmod_common *Common)
#define NULL
#define ASSERT(expression)
int CHOLMOD() metis(cholmod_sparse *A, Int *fset, size_t fsize, int postorder, Int *Perm, cholmod_common *Common)
int CHOLMOD() etree(cholmod_sparse *A, Int *Parent, cholmod_common *Common)
cholmod_factor *CHOLMOD() analyze_p(cholmod_sparse *A, Int *UserPerm, Int *fset, size_t fsize, cholmod_common *Common)
#define ID
#define CHOLMOD_METIS
#define CHOLMOD_POSTORDERED
#define CHOLMOD_INVALID
#define CHOLMOD_OK
int CHOLMOD() allocate_work(size_t nrow, size_t iworksize, size_t xworksize, cholmod_common *Common)
#define CHOLMOD_MAXMETHODS
int CHOLMOD() analyze_ordering(cholmod_sparse *A, int ordering, Int *Perm, Int *fset, size_t fsize, Int *Parent, Int *Post, Int *ColCount, Int *First, Int *Level, cholmod_common *Common)
#define RETURN_IF_NULL(A, result)
static int amesos_permute_matrices(cholmod_sparse *A, Int ordering, Int *Perm, Int *fset, size_t fsize, Int do_rowcolcounts, cholmod_sparse **A1_handle, cholmod_sparse **A2_handle, cholmod_sparse **S_handle, cholmod_sparse **F_handle, cholmod_common *Common)
#define CHOLMOD_COLAMD
#define MIN(a, b)
#define CHOLMOD_SIMPLICIAL
#define CHOLMOD_NESDIS
int n
#define ERROR(status, msg)
#define TRUE
int CHOLMOD() amd(cholmod_sparse *A, Int *fset, size_t fsize, Int *Perm, cholmod_common *Common)
#define RETURN_IF_XTYPE_INVALID(A, xtype1, xtype2, result)
#define CHOLMOD_ZOMPLEX
#define CHOLMOD_NATURAL
cholmod_factor *CHOLMOD() allocate_factor(size_t n, cholmod_common *Common)