Amesos Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
amesos_cholmod_colamd.c
Go to the documentation of this file.
1 /* ========================================================================== */
2 /* === Cholesky/cholmod_colamd ============================================== */
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 /* CHOLMOD interface to the COLAMD ordering routine (version 2.4 or later).
14  * Finds a permutation p such that the Cholesky factorization of PAA'P' is
15  * sparser than AA' using colamd. If the postorder input parameter is TRUE,
16  * the column etree is found and postordered, and the colamd ordering is then
17  * combined with its postordering. A must be unsymmetric.
18  *
19  * There can be no duplicate entries in f.
20  * f can be length 0 to n if A is m-by-n.
21  *
22  * workspace: Iwork (4*nrow+ncol), Head (nrow+1), Flag (nrow)
23  * Allocates a copy of its input matrix, which
24  * is then used as CCOLAMD's workspace.
25  *
26  * Supports any xtype (pattern, real, complex, or zomplex)
27  */
28 
29 #ifndef NCHOLESKY
30 
32 #include "amesos_colamd.h"
34 
35 #if (!defined (COLAMD_VERSION) || (COLAMD_VERSION < COLAMD_VERSION_CODE (2,5)))
36 #error "COLAMD v2.5 or later is required"
37 #endif
38 
39 /* ========================================================================== */
40 /* === cholmod_colamd ======================================================= */
41 /* ========================================================================== */
42 
43 int CHOLMOD(colamd)
44 (
45  /* ---- input ---- */
46  cholmod_sparse *A, /* matrix to order */
47  Int *fset, /* subset of 0:(A->ncol)-1 */
48  size_t fsize, /* size of fset */
49  int postorder, /* if TRUE, follow with a coletree postorder */
50  /* ---- output --- */
51  Int *Perm, /* size A->nrow, output permutation */
52  /* --------------- */
53  cholmod_common *Common
54 )
55 {
56  double knobs [COLAMD_KNOBS] ;
57  cholmod_sparse *C ;
58  Int *NewPerm, *Parent, *Post, *Work2n ;
59  Int k, nrow, ncol ;
60  size_t s, alen ;
61  int ok = TRUE ;
62 
63  /* ---------------------------------------------------------------------- */
64  /* check inputs */
65  /* ---------------------------------------------------------------------- */
66 
68  RETURN_IF_NULL (A, FALSE) ;
69  RETURN_IF_NULL (Perm, FALSE) ;
71  if (A->stype != 0)
72  {
73  ERROR (CHOLMOD_INVALID, "matrix must be unsymmetric") ;
74  return (FALSE) ;
75  }
76  Common->status = CHOLMOD_OK ;
77 
78  /* ---------------------------------------------------------------------- */
79  /* get inputs */
80  /* ---------------------------------------------------------------------- */
81 
82  nrow = A->nrow ;
83  ncol = A->ncol ;
84 
85  /* ---------------------------------------------------------------------- */
86  /* allocate workspace */
87  /* ---------------------------------------------------------------------- */
88 
89  /* Note: this is less than the space used in cholmod_analyze, so if
90  * cholmod_colamd is being called by that routine, no space will be
91  * allocated.
92  */
93 
94  /* s = 4*nrow + ncol */
95  s = CHOLMOD(mult_size_t) (nrow, 4, &ok) ;
96  s = CHOLMOD(add_size_t) (s, ncol, &ok) ;
97 
98 #ifdef LONG
99  alen = amesos_colamd_l_recommended (A->nzmax, ncol, nrow) ;
101 #else
102  alen = amesos_colamd_recommended (A->nzmax, ncol, nrow) ;
104 #endif
105 
106  if (!ok || alen == 0)
107  {
108  ERROR (CHOLMOD_TOO_LARGE, "matrix invalid or too large") ;
109  return (FALSE) ;
110  }
111 
112  CHOLMOD(allocate_work) (0, s, 0, Common) ;
113  if (Common->status < CHOLMOD_OK)
114  {
115  return (FALSE) ;
116  }
117 
118  /* ---------------------------------------------------------------------- */
119  /* allocate COLAMD workspace */
120  /* ---------------------------------------------------------------------- */
121 
122  /* colamd_printf is only available in colamd v2.4 or later */
123  amesos_colamd_printf = Common->print_function ;
124 
125  C = CHOLMOD(allocate_sparse) (ncol, nrow, alen, TRUE, TRUE, 0,
126  CHOLMOD_PATTERN, Common) ;
127 
128  /* ---------------------------------------------------------------------- */
129  /* copy (and transpose) the input matrix A into the colamd workspace */
130  /* ---------------------------------------------------------------------- */
131 
132  /* C = A (:,f)', which also packs A if needed. */
133  /* workspace: Iwork (nrow if no fset; MAX (nrow,ncol) if fset) */
134  ok = CHOLMOD(transpose_unsym) (A, 0, NULL, fset, fsize, C, Common) ;
135 
136  /* ---------------------------------------------------------------------- */
137  /* order the matrix (destroys the contents of C->i and C->p) */
138  /* ---------------------------------------------------------------------- */
139 
140  /* get parameters */
141  if (Common->current < 0 || Common->current >= CHOLMOD_MAXMETHODS)
142  {
143  /* this is the CHOLMOD default, not the COLAMD default */
144  knobs [COLAMD_DENSE_ROW] = -1 ;
145  }
146  else
147  {
148  /* get the knobs from the Common parameters */
149  knobs [COLAMD_DENSE_COL] = Common->method[Common->current].prune_dense ;
150  knobs [COLAMD_DENSE_ROW] = Common->method[Common->current].prune_dense2;
151  knobs [COLAMD_AGGRESSIVE] = Common->method[Common->current].aggressive ;
152  }
153 
154  if (ok)
155  {
156  Int *Cp ;
157  Int stats [COLAMD_STATS] ;
158  Cp = C->p ;
159 
160 #ifdef LONG
161  amesos_colamd_l (ncol, nrow, alen, C->i, Cp, knobs, stats) ;
162 #else
163  amesos_colamd (ncol, nrow, alen, C->i, Cp, knobs, stats) ;
164 #endif
165 
166  ok = stats [COLAMD_STATUS] ;
167  ok = (ok == COLAMD_OK || ok == COLAMD_OK_BUT_JUMBLED) ;
168  /* permutation returned in C->p, if the ordering succeeded */
169  for (k = 0 ; k < nrow ; k++)
170  {
171  Perm [k] = Cp [k] ;
172  }
173  }
174 
175  CHOLMOD(free_sparse) (&C, Common) ;
176 
177  /* ---------------------------------------------------------------------- */
178  /* column etree postordering */
179  /* ---------------------------------------------------------------------- */
180 
181  if (postorder)
182  {
183  /* use the last 2*n space in Iwork for Parent and Post */
184  Work2n = Common->Iwork ;
185  Work2n += 2*((size_t) nrow) + ncol ;
186  Parent = Work2n ; /* size nrow (i/i/l) */
187  Post = Work2n + nrow ; /* size nrow (i/i/l) */
188 
189  /* workspace: Iwork (2*nrow+ncol), Flag (nrow), Head (nrow+1) */
190  ok = ok && CHOLMOD(analyze_ordering) (A, CHOLMOD_COLAMD, Perm, fset,
191  fsize, Parent, Post, NULL, NULL, NULL, Common) ;
192 
193  /* combine the colamd permutation with its postordering */
194  if (ok)
195  {
196  NewPerm = Common->Iwork ; /* size nrow (i/i/l) */
197  for (k = 0 ; k < nrow ; k++)
198  {
199  NewPerm [k] = Perm [Post [k]] ;
200  }
201  for (k = 0 ; k < nrow ; k++)
202  {
203  Perm [k] = NewPerm [k] ;
204  }
205  }
206  }
207 
208  return (ok) ;
209 }
210 #endif
#define CHOLMOD_TOO_LARGE
int CHOLMOD() colamd(cholmod_sparse *A, Int *fset, size_t fsize, int postorder, Int *Perm, cholmod_common *Common)
int CHOLMOD() transpose_unsym(cholmod_sparse *A, int values, Int *Perm, Int *fset, size_t fsize, cholmod_sparse *F, cholmod_common *Common)
#define Int
size_t CHOLMOD() add_size_t(size_t a, size_t b, int *ok)
#define COLAMD_KNOBS
Definition: amesos_colamd.h:97
#define FALSE
UF_long amesos_colamd_l(UF_long n_row, UF_long n_col, UF_long Alen, UF_long A[], UF_long p[], double knobs[COLAMD_KNOBS], UF_long stats[COLAMD_STATS])
#define RETURN_IF_NULL_COMMON(result)
size_t CHOLMOD() mult_size_t(size_t a, size_t k, int *ok)
#define COLAMD_STATUS
#define CHOLMOD_PATTERN
#define CHOLMOD(name)
#define COLAMD_OK_BUT_JUMBLED
#define COLAMD_AGGRESSIVE
int CHOLMOD() free_sparse(cholmod_sparse **AHandle, cholmod_common *Common)
#define NULL
size_t amesos_colamd_l_recommended(UF_long nnz, UF_long n_row, UF_long n_col)
#define COLAMD_OK
#define COLAMD_STATS
#define CHOLMOD_INVALID
#define CHOLMOD_OK
EXTERN int(* amesos_colamd_printf)(const char *,...)
int amesos_colamd(int n_row, int n_col, int Alen, int A[], int p[], double knobs[COLAMD_KNOBS], int stats[COLAMD_STATS])
void amesos_colamd_l_set_defaults(double knobs[COLAMD_KNOBS])
size_t amesos_colamd_recommended(int nnz, int n_row, int n_col)
cholmod_sparse *CHOLMOD() allocate_sparse(size_t nrow, size_t ncol, size_t nzmax, int sorted, int packed, int stype, int xtype, cholmod_common *Common)
int CHOLMOD() allocate_work(size_t nrow, size_t iworksize, size_t xworksize, cholmod_common *Common)
#define CHOLMOD_MAXMETHODS
#define COLAMD_DENSE_ROW
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)
#define CHOLMOD_COLAMD
#define ERROR(status, msg)
#define TRUE
#define RETURN_IF_XTYPE_INVALID(A, xtype1, xtype2, result)
#define CHOLMOD_ZOMPLEX
#define COLAMD_DENSE_COL
void amesos_colamd_set_defaults(double knobs[COLAMD_KNOBS])