1*bf2c3715SXin Li // // This file is part of Eigen, a lightweight C++ template library
2*bf2c3715SXin Li // for linear algebra.
3*bf2c3715SXin Li //
4*bf2c3715SXin Li // Copyright (C) 2012 Desire Nuentsa Wakam <[email protected]>
5*bf2c3715SXin Li //
6*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
7*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
8*bf2c3715SXin Li // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9*bf2c3715SXin Li
10*bf2c3715SXin Li // This file is modified from the colamd/symamd library. The copyright is below
11*bf2c3715SXin Li
12*bf2c3715SXin Li // The authors of the code itself are Stefan I. Larimore and Timothy A.
13*bf2c3715SXin Li // Davis ([email protected]), University of Florida. The algorithm was
14*bf2c3715SXin Li // developed in collaboration with John Gilbert, Xerox PARC, and Esmond
15*bf2c3715SXin Li // Ng, Oak Ridge National Laboratory.
16*bf2c3715SXin Li //
17*bf2c3715SXin Li // Date:
18*bf2c3715SXin Li //
19*bf2c3715SXin Li // September 8, 2003. Version 2.3.
20*bf2c3715SXin Li //
21*bf2c3715SXin Li // Acknowledgements:
22*bf2c3715SXin Li //
23*bf2c3715SXin Li // This work was supported by the National Science Foundation, under
24*bf2c3715SXin Li // grants DMS-9504974 and DMS-9803599.
25*bf2c3715SXin Li //
26*bf2c3715SXin Li // Notice:
27*bf2c3715SXin Li //
28*bf2c3715SXin Li // Copyright (c) 1998-2003 by the University of Florida.
29*bf2c3715SXin Li // All Rights Reserved.
30*bf2c3715SXin Li //
31*bf2c3715SXin Li // THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY
32*bf2c3715SXin Li // EXPRESSED OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
33*bf2c3715SXin Li //
34*bf2c3715SXin Li // Permission is hereby granted to use, copy, modify, and/or distribute
35*bf2c3715SXin Li // this program, provided that the Copyright, this License, and the
36*bf2c3715SXin Li // Availability of the original version is retained on all copies and made
37*bf2c3715SXin Li // accessible to the end-user of any code or package that includes COLAMD
38*bf2c3715SXin Li // or any modified version of COLAMD.
39*bf2c3715SXin Li //
40*bf2c3715SXin Li // Availability:
41*bf2c3715SXin Li //
42*bf2c3715SXin Li // The colamd/symamd library is available at
43*bf2c3715SXin Li //
44*bf2c3715SXin Li // http://www.suitesparse.com
45*bf2c3715SXin Li
46*bf2c3715SXin Li
47*bf2c3715SXin Li #ifndef EIGEN_COLAMD_H
48*bf2c3715SXin Li #define EIGEN_COLAMD_H
49*bf2c3715SXin Li
50*bf2c3715SXin Li namespace internal {
51*bf2c3715SXin Li
52*bf2c3715SXin Li namespace Colamd {
53*bf2c3715SXin Li
54*bf2c3715SXin Li /* Ensure that debugging is turned off: */
55*bf2c3715SXin Li #ifndef COLAMD_NDEBUG
56*bf2c3715SXin Li #define COLAMD_NDEBUG
57*bf2c3715SXin Li #endif /* NDEBUG */
58*bf2c3715SXin Li
59*bf2c3715SXin Li
60*bf2c3715SXin Li /* ========================================================================== */
61*bf2c3715SXin Li /* === Knob and statistics definitions ====================================== */
62*bf2c3715SXin Li /* ========================================================================== */
63*bf2c3715SXin Li
64*bf2c3715SXin Li /* size of the knobs [ ] array. Only knobs [0..1] are currently used. */
65*bf2c3715SXin Li const int NKnobs = 20;
66*bf2c3715SXin Li
67*bf2c3715SXin Li /* number of output statistics. Only stats [0..6] are currently used. */
68*bf2c3715SXin Li const int NStats = 20;
69*bf2c3715SXin Li
70*bf2c3715SXin Li /* Indices into knobs and stats array. */
71*bf2c3715SXin Li enum KnobsStatsIndex {
72*bf2c3715SXin Li /* knobs [0] and stats [0]: dense row knob and output statistic. */
73*bf2c3715SXin Li DenseRow = 0,
74*bf2c3715SXin Li
75*bf2c3715SXin Li /* knobs [1] and stats [1]: dense column knob and output statistic. */
76*bf2c3715SXin Li DenseCol = 1,
77*bf2c3715SXin Li
78*bf2c3715SXin Li /* stats [2]: memory defragmentation count output statistic */
79*bf2c3715SXin Li DefragCount = 2,
80*bf2c3715SXin Li
81*bf2c3715SXin Li /* stats [3]: colamd status: zero OK, > 0 warning or notice, < 0 error */
82*bf2c3715SXin Li Status = 3,
83*bf2c3715SXin Li
84*bf2c3715SXin Li /* stats [4..6]: error info, or info on jumbled columns */
85*bf2c3715SXin Li Info1 = 4,
86*bf2c3715SXin Li Info2 = 5,
87*bf2c3715SXin Li Info3 = 6
88*bf2c3715SXin Li };
89*bf2c3715SXin Li
90*bf2c3715SXin Li /* error codes returned in stats [3]: */
91*bf2c3715SXin Li enum Status {
92*bf2c3715SXin Li Ok = 0,
93*bf2c3715SXin Li OkButJumbled = 1,
94*bf2c3715SXin Li ErrorANotPresent = -1,
95*bf2c3715SXin Li ErrorPNotPresent = -2,
96*bf2c3715SXin Li ErrorNrowNegative = -3,
97*bf2c3715SXin Li ErrorNcolNegative = -4,
98*bf2c3715SXin Li ErrorNnzNegative = -5,
99*bf2c3715SXin Li ErrorP0Nonzero = -6,
100*bf2c3715SXin Li ErrorATooSmall = -7,
101*bf2c3715SXin Li ErrorColLengthNegative = -8,
102*bf2c3715SXin Li ErrorRowIndexOutOfBounds = -9,
103*bf2c3715SXin Li ErrorOutOfMemory = -10,
104*bf2c3715SXin Li ErrorInternalError = -999
105*bf2c3715SXin Li };
106*bf2c3715SXin Li /* ========================================================================== */
107*bf2c3715SXin Li /* === Definitions ========================================================== */
108*bf2c3715SXin Li /* ========================================================================== */
109*bf2c3715SXin Li
110*bf2c3715SXin Li template <typename IndexType>
ones_complement(const IndexType r)111*bf2c3715SXin Li IndexType ones_complement(const IndexType r) {
112*bf2c3715SXin Li return (-(r)-1);
113*bf2c3715SXin Li }
114*bf2c3715SXin Li
115*bf2c3715SXin Li /* -------------------------------------------------------------------------- */
116*bf2c3715SXin Li const int Empty = -1;
117*bf2c3715SXin Li
118*bf2c3715SXin Li /* Row and column status */
119*bf2c3715SXin Li enum RowColumnStatus {
120*bf2c3715SXin Li Alive = 0,
121*bf2c3715SXin Li Dead = -1
122*bf2c3715SXin Li };
123*bf2c3715SXin Li
124*bf2c3715SXin Li /* Column status */
125*bf2c3715SXin Li enum ColumnStatus {
126*bf2c3715SXin Li DeadPrincipal = -1,
127*bf2c3715SXin Li DeadNonPrincipal = -2
128*bf2c3715SXin Li };
129*bf2c3715SXin Li
130*bf2c3715SXin Li /* ========================================================================== */
131*bf2c3715SXin Li /* === Colamd reporting mechanism =========================================== */
132*bf2c3715SXin Li /* ========================================================================== */
133*bf2c3715SXin Li
134*bf2c3715SXin Li // == Row and Column structures ==
135*bf2c3715SXin Li template <typename IndexType>
136*bf2c3715SXin Li struct ColStructure
137*bf2c3715SXin Li {
138*bf2c3715SXin Li IndexType start ; /* index for A of first row in this column, or Dead */
139*bf2c3715SXin Li /* if column is dead */
140*bf2c3715SXin Li IndexType length ; /* number of rows in this column */
141*bf2c3715SXin Li union
142*bf2c3715SXin Li {
143*bf2c3715SXin Li IndexType thickness ; /* number of original columns represented by this */
144*bf2c3715SXin Li /* col, if the column is alive */
145*bf2c3715SXin Li IndexType parent ; /* parent in parent tree super-column structure, if */
146*bf2c3715SXin Li /* the column is dead */
147*bf2c3715SXin Li } shared1 ;
148*bf2c3715SXin Li union
149*bf2c3715SXin Li {
150*bf2c3715SXin Li IndexType score ; /* the score used to maintain heap, if col is alive */
151*bf2c3715SXin Li IndexType order ; /* pivot ordering of this column, if col is dead */
152*bf2c3715SXin Li } shared2 ;
153*bf2c3715SXin Li union
154*bf2c3715SXin Li {
155*bf2c3715SXin Li IndexType headhash ; /* head of a hash bucket, if col is at the head of */
156*bf2c3715SXin Li /* a degree list */
157*bf2c3715SXin Li IndexType hash ; /* hash value, if col is not in a degree list */
158*bf2c3715SXin Li IndexType prev ; /* previous column in degree list, if col is in a */
159*bf2c3715SXin Li /* degree list (but not at the head of a degree list) */
160*bf2c3715SXin Li } shared3 ;
161*bf2c3715SXin Li union
162*bf2c3715SXin Li {
163*bf2c3715SXin Li IndexType degree_next ; /* next column, if col is in a degree list */
164*bf2c3715SXin Li IndexType hash_next ; /* next column, if col is in a hash list */
165*bf2c3715SXin Li } shared4 ;
166*bf2c3715SXin Li
is_deadColStructure167*bf2c3715SXin Li inline bool is_dead() const { return start < Alive; }
168*bf2c3715SXin Li
is_aliveColStructure169*bf2c3715SXin Li inline bool is_alive() const { return start >= Alive; }
170*bf2c3715SXin Li
is_dead_principalColStructure171*bf2c3715SXin Li inline bool is_dead_principal() const { return start == DeadPrincipal; }
172*bf2c3715SXin Li
kill_principalColStructure173*bf2c3715SXin Li inline void kill_principal() { start = DeadPrincipal; }
174*bf2c3715SXin Li
kill_non_principalColStructure175*bf2c3715SXin Li inline void kill_non_principal() { start = DeadNonPrincipal; }
176*bf2c3715SXin Li
177*bf2c3715SXin Li };
178*bf2c3715SXin Li
179*bf2c3715SXin Li template <typename IndexType>
180*bf2c3715SXin Li struct RowStructure
181*bf2c3715SXin Li {
182*bf2c3715SXin Li IndexType start ; /* index for A of first col in this row */
183*bf2c3715SXin Li IndexType length ; /* number of principal columns in this row */
184*bf2c3715SXin Li union
185*bf2c3715SXin Li {
186*bf2c3715SXin Li IndexType degree ; /* number of principal & non-principal columns in row */
187*bf2c3715SXin Li IndexType p ; /* used as a row pointer in init_rows_cols () */
188*bf2c3715SXin Li } shared1 ;
189*bf2c3715SXin Li union
190*bf2c3715SXin Li {
191*bf2c3715SXin Li IndexType mark ; /* for computing set differences and marking dead rows*/
192*bf2c3715SXin Li IndexType first_column ;/* first column in row (used in garbage collection) */
193*bf2c3715SXin Li } shared2 ;
194*bf2c3715SXin Li
is_deadRowStructure195*bf2c3715SXin Li inline bool is_dead() const { return shared2.mark < Alive; }
196*bf2c3715SXin Li
is_aliveRowStructure197*bf2c3715SXin Li inline bool is_alive() const { return shared2.mark >= Alive; }
198*bf2c3715SXin Li
killRowStructure199*bf2c3715SXin Li inline void kill() { shared2.mark = Dead; }
200*bf2c3715SXin Li
201*bf2c3715SXin Li };
202*bf2c3715SXin Li
203*bf2c3715SXin Li /* ========================================================================== */
204*bf2c3715SXin Li /* === Colamd recommended memory size ======================================= */
205*bf2c3715SXin Li /* ========================================================================== */
206*bf2c3715SXin Li
207*bf2c3715SXin Li /*
208*bf2c3715SXin Li The recommended length Alen of the array A passed to colamd is given by
209*bf2c3715SXin Li the COLAMD_RECOMMENDED (nnz, n_row, n_col) macro. It returns -1 if any
210*bf2c3715SXin Li argument is negative. 2*nnz space is required for the row and column
211*bf2c3715SXin Li indices of the matrix. colamd_c (n_col) + colamd_r (n_row) space is
212*bf2c3715SXin Li required for the Col and Row arrays, respectively, which are internal to
213*bf2c3715SXin Li colamd. An additional n_col space is the minimal amount of "elbow room",
214*bf2c3715SXin Li and nnz/5 more space is recommended for run time efficiency.
215*bf2c3715SXin Li
216*bf2c3715SXin Li This macro is not needed when using symamd.
217*bf2c3715SXin Li
218*bf2c3715SXin Li Explicit typecast to IndexType added Sept. 23, 2002, COLAMD version 2.2, to avoid
219*bf2c3715SXin Li gcc -pedantic warning messages.
220*bf2c3715SXin Li */
221*bf2c3715SXin Li template <typename IndexType>
colamd_c(IndexType n_col)222*bf2c3715SXin Li inline IndexType colamd_c(IndexType n_col)
223*bf2c3715SXin Li { return IndexType( ((n_col) + 1) * sizeof (ColStructure<IndexType>) / sizeof (IndexType) ) ; }
224*bf2c3715SXin Li
225*bf2c3715SXin Li template <typename IndexType>
colamd_r(IndexType n_row)226*bf2c3715SXin Li inline IndexType colamd_r(IndexType n_row)
227*bf2c3715SXin Li { return IndexType(((n_row) + 1) * sizeof (RowStructure<IndexType>) / sizeof (IndexType)); }
228*bf2c3715SXin Li
229*bf2c3715SXin Li // Prototypes of non-user callable routines
230*bf2c3715SXin Li template <typename IndexType>
231*bf2c3715SXin Li static IndexType init_rows_cols (IndexType n_row, IndexType n_col, RowStructure<IndexType> Row [], ColStructure<IndexType> col [], IndexType A [], IndexType p [], IndexType stats[NStats] );
232*bf2c3715SXin Li
233*bf2c3715SXin Li template <typename IndexType>
234*bf2c3715SXin Li static void init_scoring (IndexType n_row, IndexType n_col, RowStructure<IndexType> Row [], ColStructure<IndexType> Col [], IndexType A [], IndexType head [], double knobs[NKnobs], IndexType *p_n_row2, IndexType *p_n_col2, IndexType *p_max_deg);
235*bf2c3715SXin Li
236*bf2c3715SXin Li template <typename IndexType>
237*bf2c3715SXin Li static IndexType find_ordering (IndexType n_row, IndexType n_col, IndexType Alen, RowStructure<IndexType> Row [], ColStructure<IndexType> Col [], IndexType A [], IndexType head [], IndexType n_col2, IndexType max_deg, IndexType pfree);
238*bf2c3715SXin Li
239*bf2c3715SXin Li template <typename IndexType>
240*bf2c3715SXin Li static void order_children (IndexType n_col, ColStructure<IndexType> Col [], IndexType p []);
241*bf2c3715SXin Li
242*bf2c3715SXin Li template <typename IndexType>
243*bf2c3715SXin Li static void detect_super_cols (ColStructure<IndexType> Col [], IndexType A [], IndexType head [], IndexType row_start, IndexType row_length ) ;
244*bf2c3715SXin Li
245*bf2c3715SXin Li template <typename IndexType>
246*bf2c3715SXin Li static IndexType garbage_collection (IndexType n_row, IndexType n_col, RowStructure<IndexType> Row [], ColStructure<IndexType> Col [], IndexType A [], IndexType *pfree) ;
247*bf2c3715SXin Li
248*bf2c3715SXin Li template <typename IndexType>
249*bf2c3715SXin Li static inline IndexType clear_mark (IndexType n_row, RowStructure<IndexType> Row [] ) ;
250*bf2c3715SXin Li
251*bf2c3715SXin Li /* === No debugging ========================================================= */
252*bf2c3715SXin Li
253*bf2c3715SXin Li #define COLAMD_DEBUG0(params) ;
254*bf2c3715SXin Li #define COLAMD_DEBUG1(params) ;
255*bf2c3715SXin Li #define COLAMD_DEBUG2(params) ;
256*bf2c3715SXin Li #define COLAMD_DEBUG3(params) ;
257*bf2c3715SXin Li #define COLAMD_DEBUG4(params) ;
258*bf2c3715SXin Li
259*bf2c3715SXin Li #define COLAMD_ASSERT(expression) ((void) 0)
260*bf2c3715SXin Li
261*bf2c3715SXin Li
262*bf2c3715SXin Li /**
263*bf2c3715SXin Li * \brief Returns the recommended value of Alen
264*bf2c3715SXin Li *
265*bf2c3715SXin Li * Returns recommended value of Alen for use by colamd.
266*bf2c3715SXin Li * Returns -1 if any input argument is negative.
267*bf2c3715SXin Li * The use of this routine or macro is optional.
268*bf2c3715SXin Li * Note that the macro uses its arguments more than once,
269*bf2c3715SXin Li * so be careful for side effects, if you pass expressions as arguments to COLAMD_RECOMMENDED.
270*bf2c3715SXin Li *
271*bf2c3715SXin Li * \param nnz nonzeros in A
272*bf2c3715SXin Li * \param n_row number of rows in A
273*bf2c3715SXin Li * \param n_col number of columns in A
274*bf2c3715SXin Li * \return recommended value of Alen for use by colamd
275*bf2c3715SXin Li */
276*bf2c3715SXin Li template <typename IndexType>
recommended(IndexType nnz,IndexType n_row,IndexType n_col)277*bf2c3715SXin Li inline IndexType recommended ( IndexType nnz, IndexType n_row, IndexType n_col)
278*bf2c3715SXin Li {
279*bf2c3715SXin Li if ((nnz) < 0 || (n_row) < 0 || (n_col) < 0)
280*bf2c3715SXin Li return (-1);
281*bf2c3715SXin Li else
282*bf2c3715SXin Li return (2 * (nnz) + colamd_c (n_col) + colamd_r (n_row) + (n_col) + ((nnz) / 5));
283*bf2c3715SXin Li }
284*bf2c3715SXin Li
285*bf2c3715SXin Li /**
286*bf2c3715SXin Li * \brief set default parameters The use of this routine is optional.
287*bf2c3715SXin Li *
288*bf2c3715SXin Li * Colamd: rows with more than (knobs [DenseRow] * n_col)
289*bf2c3715SXin Li * entries are removed prior to ordering. Columns with more than
290*bf2c3715SXin Li * (knobs [DenseCol] * n_row) entries are removed prior to
291*bf2c3715SXin Li * ordering, and placed last in the output column ordering.
292*bf2c3715SXin Li *
293*bf2c3715SXin Li * DenseRow and DenseCol are defined as 0 and 1,
294*bf2c3715SXin Li * respectively, in colamd.h. Default values of these two knobs
295*bf2c3715SXin Li * are both 0.5. Currently, only knobs [0] and knobs [1] are
296*bf2c3715SXin Li * used, but future versions may use more knobs. If so, they will
297*bf2c3715SXin Li * be properly set to their defaults by the future version of
298*bf2c3715SXin Li * colamd_set_defaults, so that the code that calls colamd will
299*bf2c3715SXin Li * not need to change, assuming that you either use
300*bf2c3715SXin Li * colamd_set_defaults, or pass a (double *) NULL pointer as the
301*bf2c3715SXin Li * knobs array to colamd or symamd.
302*bf2c3715SXin Li *
303*bf2c3715SXin Li * \param knobs parameter settings for colamd
304*bf2c3715SXin Li */
305*bf2c3715SXin Li
set_defaults(double knobs[NKnobs])306*bf2c3715SXin Li static inline void set_defaults(double knobs[NKnobs])
307*bf2c3715SXin Li {
308*bf2c3715SXin Li /* === Local variables ================================================== */
309*bf2c3715SXin Li
310*bf2c3715SXin Li int i ;
311*bf2c3715SXin Li
312*bf2c3715SXin Li if (!knobs)
313*bf2c3715SXin Li {
314*bf2c3715SXin Li return ; /* no knobs to initialize */
315*bf2c3715SXin Li }
316*bf2c3715SXin Li for (i = 0 ; i < NKnobs ; i++)
317*bf2c3715SXin Li {
318*bf2c3715SXin Li knobs [i] = 0 ;
319*bf2c3715SXin Li }
320*bf2c3715SXin Li knobs [Colamd::DenseRow] = 0.5 ; /* ignore rows over 50% dense */
321*bf2c3715SXin Li knobs [Colamd::DenseCol] = 0.5 ; /* ignore columns over 50% dense */
322*bf2c3715SXin Li }
323*bf2c3715SXin Li
324*bf2c3715SXin Li /**
325*bf2c3715SXin Li * \brief Computes a column ordering using the column approximate minimum degree ordering
326*bf2c3715SXin Li *
327*bf2c3715SXin Li * Computes a column ordering (Q) of A such that P(AQ)=LU or
328*bf2c3715SXin Li * (AQ)'AQ=LL' have less fill-in and require fewer floating point
329*bf2c3715SXin Li * operations than factorizing the unpermuted matrix A or A'A,
330*bf2c3715SXin Li * respectively.
331*bf2c3715SXin Li *
332*bf2c3715SXin Li *
333*bf2c3715SXin Li * \param n_row number of rows in A
334*bf2c3715SXin Li * \param n_col number of columns in A
335*bf2c3715SXin Li * \param Alen, size of the array A
336*bf2c3715SXin Li * \param A row indices of the matrix, of size ALen
337*bf2c3715SXin Li * \param p column pointers of A, of size n_col+1
338*bf2c3715SXin Li * \param knobs parameter settings for colamd
339*bf2c3715SXin Li * \param stats colamd output statistics and error codes
340*bf2c3715SXin Li */
341*bf2c3715SXin Li template <typename IndexType>
compute_ordering(IndexType n_row,IndexType n_col,IndexType Alen,IndexType * A,IndexType * p,double knobs[NKnobs],IndexType stats[NStats])342*bf2c3715SXin Li static bool compute_ordering(IndexType n_row, IndexType n_col, IndexType Alen, IndexType *A, IndexType *p, double knobs[NKnobs], IndexType stats[NStats])
343*bf2c3715SXin Li {
344*bf2c3715SXin Li /* === Local variables ================================================== */
345*bf2c3715SXin Li
346*bf2c3715SXin Li IndexType i ; /* loop index */
347*bf2c3715SXin Li IndexType nnz ; /* nonzeros in A */
348*bf2c3715SXin Li IndexType Row_size ; /* size of Row [], in integers */
349*bf2c3715SXin Li IndexType Col_size ; /* size of Col [], in integers */
350*bf2c3715SXin Li IndexType need ; /* minimum required length of A */
351*bf2c3715SXin Li Colamd::RowStructure<IndexType> *Row ; /* pointer into A of Row [0..n_row] array */
352*bf2c3715SXin Li Colamd::ColStructure<IndexType> *Col ; /* pointer into A of Col [0..n_col] array */
353*bf2c3715SXin Li IndexType n_col2 ; /* number of non-dense, non-empty columns */
354*bf2c3715SXin Li IndexType n_row2 ; /* number of non-dense, non-empty rows */
355*bf2c3715SXin Li IndexType ngarbage ; /* number of garbage collections performed */
356*bf2c3715SXin Li IndexType max_deg ; /* maximum row degree */
357*bf2c3715SXin Li double default_knobs [NKnobs] ; /* default knobs array */
358*bf2c3715SXin Li
359*bf2c3715SXin Li
360*bf2c3715SXin Li /* === Check the input arguments ======================================== */
361*bf2c3715SXin Li
362*bf2c3715SXin Li if (!stats)
363*bf2c3715SXin Li {
364*bf2c3715SXin Li COLAMD_DEBUG0 (("colamd: stats not present\n")) ;
365*bf2c3715SXin Li return (false) ;
366*bf2c3715SXin Li }
367*bf2c3715SXin Li for (i = 0 ; i < NStats ; i++)
368*bf2c3715SXin Li {
369*bf2c3715SXin Li stats [i] = 0 ;
370*bf2c3715SXin Li }
371*bf2c3715SXin Li stats [Colamd::Status] = Colamd::Ok ;
372*bf2c3715SXin Li stats [Colamd::Info1] = -1 ;
373*bf2c3715SXin Li stats [Colamd::Info2] = -1 ;
374*bf2c3715SXin Li
375*bf2c3715SXin Li if (!A) /* A is not present */
376*bf2c3715SXin Li {
377*bf2c3715SXin Li stats [Colamd::Status] = Colamd::ErrorANotPresent ;
378*bf2c3715SXin Li COLAMD_DEBUG0 (("colamd: A not present\n")) ;
379*bf2c3715SXin Li return (false) ;
380*bf2c3715SXin Li }
381*bf2c3715SXin Li
382*bf2c3715SXin Li if (!p) /* p is not present */
383*bf2c3715SXin Li {
384*bf2c3715SXin Li stats [Colamd::Status] = Colamd::ErrorPNotPresent ;
385*bf2c3715SXin Li COLAMD_DEBUG0 (("colamd: p not present\n")) ;
386*bf2c3715SXin Li return (false) ;
387*bf2c3715SXin Li }
388*bf2c3715SXin Li
389*bf2c3715SXin Li if (n_row < 0) /* n_row must be >= 0 */
390*bf2c3715SXin Li {
391*bf2c3715SXin Li stats [Colamd::Status] = Colamd::ErrorNrowNegative ;
392*bf2c3715SXin Li stats [Colamd::Info1] = n_row ;
393*bf2c3715SXin Li COLAMD_DEBUG0 (("colamd: nrow negative %d\n", n_row)) ;
394*bf2c3715SXin Li return (false) ;
395*bf2c3715SXin Li }
396*bf2c3715SXin Li
397*bf2c3715SXin Li if (n_col < 0) /* n_col must be >= 0 */
398*bf2c3715SXin Li {
399*bf2c3715SXin Li stats [Colamd::Status] = Colamd::ErrorNcolNegative ;
400*bf2c3715SXin Li stats [Colamd::Info1] = n_col ;
401*bf2c3715SXin Li COLAMD_DEBUG0 (("colamd: ncol negative %d\n", n_col)) ;
402*bf2c3715SXin Li return (false) ;
403*bf2c3715SXin Li }
404*bf2c3715SXin Li
405*bf2c3715SXin Li nnz = p [n_col] ;
406*bf2c3715SXin Li if (nnz < 0) /* nnz must be >= 0 */
407*bf2c3715SXin Li {
408*bf2c3715SXin Li stats [Colamd::Status] = Colamd::ErrorNnzNegative ;
409*bf2c3715SXin Li stats [Colamd::Info1] = nnz ;
410*bf2c3715SXin Li COLAMD_DEBUG0 (("colamd: number of entries negative %d\n", nnz)) ;
411*bf2c3715SXin Li return (false) ;
412*bf2c3715SXin Li }
413*bf2c3715SXin Li
414*bf2c3715SXin Li if (p [0] != 0)
415*bf2c3715SXin Li {
416*bf2c3715SXin Li stats [Colamd::Status] = Colamd::ErrorP0Nonzero ;
417*bf2c3715SXin Li stats [Colamd::Info1] = p [0] ;
418*bf2c3715SXin Li COLAMD_DEBUG0 (("colamd: p[0] not zero %d\n", p [0])) ;
419*bf2c3715SXin Li return (false) ;
420*bf2c3715SXin Li }
421*bf2c3715SXin Li
422*bf2c3715SXin Li /* === If no knobs, set default knobs =================================== */
423*bf2c3715SXin Li
424*bf2c3715SXin Li if (!knobs)
425*bf2c3715SXin Li {
426*bf2c3715SXin Li set_defaults (default_knobs) ;
427*bf2c3715SXin Li knobs = default_knobs ;
428*bf2c3715SXin Li }
429*bf2c3715SXin Li
430*bf2c3715SXin Li /* === Allocate the Row and Col arrays from array A ===================== */
431*bf2c3715SXin Li
432*bf2c3715SXin Li Col_size = colamd_c (n_col) ;
433*bf2c3715SXin Li Row_size = colamd_r (n_row) ;
434*bf2c3715SXin Li need = 2*nnz + n_col + Col_size + Row_size ;
435*bf2c3715SXin Li
436*bf2c3715SXin Li if (need > Alen)
437*bf2c3715SXin Li {
438*bf2c3715SXin Li /* not enough space in array A to perform the ordering */
439*bf2c3715SXin Li stats [Colamd::Status] = Colamd::ErrorATooSmall ;
440*bf2c3715SXin Li stats [Colamd::Info1] = need ;
441*bf2c3715SXin Li stats [Colamd::Info2] = Alen ;
442*bf2c3715SXin Li COLAMD_DEBUG0 (("colamd: Need Alen >= %d, given only Alen = %d\n", need,Alen));
443*bf2c3715SXin Li return (false) ;
444*bf2c3715SXin Li }
445*bf2c3715SXin Li
446*bf2c3715SXin Li Alen -= Col_size + Row_size ;
447*bf2c3715SXin Li Col = (ColStructure<IndexType> *) &A [Alen] ;
448*bf2c3715SXin Li Row = (RowStructure<IndexType> *) &A [Alen + Col_size] ;
449*bf2c3715SXin Li
450*bf2c3715SXin Li /* === Construct the row and column data structures ===================== */
451*bf2c3715SXin Li
452*bf2c3715SXin Li if (!Colamd::init_rows_cols (n_row, n_col, Row, Col, A, p, stats))
453*bf2c3715SXin Li {
454*bf2c3715SXin Li /* input matrix is invalid */
455*bf2c3715SXin Li COLAMD_DEBUG0 (("colamd: Matrix invalid\n")) ;
456*bf2c3715SXin Li return (false) ;
457*bf2c3715SXin Li }
458*bf2c3715SXin Li
459*bf2c3715SXin Li /* === Initialize scores, kill dense rows/columns ======================= */
460*bf2c3715SXin Li
461*bf2c3715SXin Li Colamd::init_scoring (n_row, n_col, Row, Col, A, p, knobs,
462*bf2c3715SXin Li &n_row2, &n_col2, &max_deg) ;
463*bf2c3715SXin Li
464*bf2c3715SXin Li /* === Order the supercolumns =========================================== */
465*bf2c3715SXin Li
466*bf2c3715SXin Li ngarbage = Colamd::find_ordering (n_row, n_col, Alen, Row, Col, A, p,
467*bf2c3715SXin Li n_col2, max_deg, 2*nnz) ;
468*bf2c3715SXin Li
469*bf2c3715SXin Li /* === Order the non-principal columns ================================== */
470*bf2c3715SXin Li
471*bf2c3715SXin Li Colamd::order_children (n_col, Col, p) ;
472*bf2c3715SXin Li
473*bf2c3715SXin Li /* === Return statistics in stats ======================================= */
474*bf2c3715SXin Li
475*bf2c3715SXin Li stats [Colamd::DenseRow] = n_row - n_row2 ;
476*bf2c3715SXin Li stats [Colamd::DenseCol] = n_col - n_col2 ;
477*bf2c3715SXin Li stats [Colamd::DefragCount] = ngarbage ;
478*bf2c3715SXin Li COLAMD_DEBUG0 (("colamd: done.\n")) ;
479*bf2c3715SXin Li return (true) ;
480*bf2c3715SXin Li }
481*bf2c3715SXin Li
482*bf2c3715SXin Li /* ========================================================================== */
483*bf2c3715SXin Li /* === NON-USER-CALLABLE ROUTINES: ========================================== */
484*bf2c3715SXin Li /* ========================================================================== */
485*bf2c3715SXin Li
486*bf2c3715SXin Li /* There are no user-callable routines beyond this point in the file */
487*bf2c3715SXin Li
488*bf2c3715SXin Li /* ========================================================================== */
489*bf2c3715SXin Li /* === init_rows_cols ======================================================= */
490*bf2c3715SXin Li /* ========================================================================== */
491*bf2c3715SXin Li
492*bf2c3715SXin Li /*
493*bf2c3715SXin Li Takes the column form of the matrix in A and creates the row form of the
494*bf2c3715SXin Li matrix. Also, row and column attributes are stored in the Col and Row
495*bf2c3715SXin Li structs. If the columns are un-sorted or contain duplicate row indices,
496*bf2c3715SXin Li this routine will also sort and remove duplicate row indices from the
497*bf2c3715SXin Li column form of the matrix. Returns false if the matrix is invalid,
498*bf2c3715SXin Li true otherwise. Not user-callable.
499*bf2c3715SXin Li */
500*bf2c3715SXin Li template <typename IndexType>
init_rows_cols(IndexType n_row,IndexType n_col,RowStructure<IndexType> Row[],ColStructure<IndexType> Col[],IndexType A[],IndexType p[],IndexType stats[NStats])501*bf2c3715SXin Li static IndexType init_rows_cols /* returns true if OK, or false otherwise */
502*bf2c3715SXin Li (
503*bf2c3715SXin Li /* === Parameters ======================================================= */
504*bf2c3715SXin Li
505*bf2c3715SXin Li IndexType n_row, /* number of rows of A */
506*bf2c3715SXin Li IndexType n_col, /* number of columns of A */
507*bf2c3715SXin Li RowStructure<IndexType> Row [], /* of size n_row+1 */
508*bf2c3715SXin Li ColStructure<IndexType> Col [], /* of size n_col+1 */
509*bf2c3715SXin Li IndexType A [], /* row indices of A, of size Alen */
510*bf2c3715SXin Li IndexType p [], /* pointers to columns in A, of size n_col+1 */
511*bf2c3715SXin Li IndexType stats [NStats] /* colamd statistics */
512*bf2c3715SXin Li )
513*bf2c3715SXin Li {
514*bf2c3715SXin Li /* === Local variables ================================================== */
515*bf2c3715SXin Li
516*bf2c3715SXin Li IndexType col ; /* a column index */
517*bf2c3715SXin Li IndexType row ; /* a row index */
518*bf2c3715SXin Li IndexType *cp ; /* a column pointer */
519*bf2c3715SXin Li IndexType *cp_end ; /* a pointer to the end of a column */
520*bf2c3715SXin Li IndexType *rp ; /* a row pointer */
521*bf2c3715SXin Li IndexType *rp_end ; /* a pointer to the end of a row */
522*bf2c3715SXin Li IndexType last_row ; /* previous row */
523*bf2c3715SXin Li
524*bf2c3715SXin Li /* === Initialize columns, and check column pointers ==================== */
525*bf2c3715SXin Li
526*bf2c3715SXin Li for (col = 0 ; col < n_col ; col++)
527*bf2c3715SXin Li {
528*bf2c3715SXin Li Col [col].start = p [col] ;
529*bf2c3715SXin Li Col [col].length = p [col+1] - p [col] ;
530*bf2c3715SXin Li
531*bf2c3715SXin Li if ((Col [col].length) < 0) // extra parentheses to work-around gcc bug 10200
532*bf2c3715SXin Li {
533*bf2c3715SXin Li /* column pointers must be non-decreasing */
534*bf2c3715SXin Li stats [Colamd::Status] = Colamd::ErrorColLengthNegative ;
535*bf2c3715SXin Li stats [Colamd::Info1] = col ;
536*bf2c3715SXin Li stats [Colamd::Info2] = Col [col].length ;
537*bf2c3715SXin Li COLAMD_DEBUG0 (("colamd: col %d length %d < 0\n", col, Col [col].length)) ;
538*bf2c3715SXin Li return (false) ;
539*bf2c3715SXin Li }
540*bf2c3715SXin Li
541*bf2c3715SXin Li Col [col].shared1.thickness = 1 ;
542*bf2c3715SXin Li Col [col].shared2.score = 0 ;
543*bf2c3715SXin Li Col [col].shared3.prev = Empty ;
544*bf2c3715SXin Li Col [col].shared4.degree_next = Empty ;
545*bf2c3715SXin Li }
546*bf2c3715SXin Li
547*bf2c3715SXin Li /* p [0..n_col] no longer needed, used as "head" in subsequent routines */
548*bf2c3715SXin Li
549*bf2c3715SXin Li /* === Scan columns, compute row degrees, and check row indices ========= */
550*bf2c3715SXin Li
551*bf2c3715SXin Li stats [Info3] = 0 ; /* number of duplicate or unsorted row indices*/
552*bf2c3715SXin Li
553*bf2c3715SXin Li for (row = 0 ; row < n_row ; row++)
554*bf2c3715SXin Li {
555*bf2c3715SXin Li Row [row].length = 0 ;
556*bf2c3715SXin Li Row [row].shared2.mark = -1 ;
557*bf2c3715SXin Li }
558*bf2c3715SXin Li
559*bf2c3715SXin Li for (col = 0 ; col < n_col ; col++)
560*bf2c3715SXin Li {
561*bf2c3715SXin Li last_row = -1 ;
562*bf2c3715SXin Li
563*bf2c3715SXin Li cp = &A [p [col]] ;
564*bf2c3715SXin Li cp_end = &A [p [col+1]] ;
565*bf2c3715SXin Li
566*bf2c3715SXin Li while (cp < cp_end)
567*bf2c3715SXin Li {
568*bf2c3715SXin Li row = *cp++ ;
569*bf2c3715SXin Li
570*bf2c3715SXin Li /* make sure row indices within range */
571*bf2c3715SXin Li if (row < 0 || row >= n_row)
572*bf2c3715SXin Li {
573*bf2c3715SXin Li stats [Colamd::Status] = Colamd::ErrorRowIndexOutOfBounds ;
574*bf2c3715SXin Li stats [Colamd::Info1] = col ;
575*bf2c3715SXin Li stats [Colamd::Info2] = row ;
576*bf2c3715SXin Li stats [Colamd::Info3] = n_row ;
577*bf2c3715SXin Li COLAMD_DEBUG0 (("colamd: row %d col %d out of bounds\n", row, col)) ;
578*bf2c3715SXin Li return (false) ;
579*bf2c3715SXin Li }
580*bf2c3715SXin Li
581*bf2c3715SXin Li if (row <= last_row || Row [row].shared2.mark == col)
582*bf2c3715SXin Li {
583*bf2c3715SXin Li /* row index are unsorted or repeated (or both), thus col */
584*bf2c3715SXin Li /* is jumbled. This is a notice, not an error condition. */
585*bf2c3715SXin Li stats [Colamd::Status] = Colamd::OkButJumbled ;
586*bf2c3715SXin Li stats [Colamd::Info1] = col ;
587*bf2c3715SXin Li stats [Colamd::Info2] = row ;
588*bf2c3715SXin Li (stats [Colamd::Info3]) ++ ;
589*bf2c3715SXin Li COLAMD_DEBUG1 (("colamd: row %d col %d unsorted/duplicate\n",row,col));
590*bf2c3715SXin Li }
591*bf2c3715SXin Li
592*bf2c3715SXin Li if (Row [row].shared2.mark != col)
593*bf2c3715SXin Li {
594*bf2c3715SXin Li Row [row].length++ ;
595*bf2c3715SXin Li }
596*bf2c3715SXin Li else
597*bf2c3715SXin Li {
598*bf2c3715SXin Li /* this is a repeated entry in the column, */
599*bf2c3715SXin Li /* it will be removed */
600*bf2c3715SXin Li Col [col].length-- ;
601*bf2c3715SXin Li }
602*bf2c3715SXin Li
603*bf2c3715SXin Li /* mark the row as having been seen in this column */
604*bf2c3715SXin Li Row [row].shared2.mark = col ;
605*bf2c3715SXin Li
606*bf2c3715SXin Li last_row = row ;
607*bf2c3715SXin Li }
608*bf2c3715SXin Li }
609*bf2c3715SXin Li
610*bf2c3715SXin Li /* === Compute row pointers ============================================= */
611*bf2c3715SXin Li
612*bf2c3715SXin Li /* row form of the matrix starts directly after the column */
613*bf2c3715SXin Li /* form of matrix in A */
614*bf2c3715SXin Li Row [0].start = p [n_col] ;
615*bf2c3715SXin Li Row [0].shared1.p = Row [0].start ;
616*bf2c3715SXin Li Row [0].shared2.mark = -1 ;
617*bf2c3715SXin Li for (row = 1 ; row < n_row ; row++)
618*bf2c3715SXin Li {
619*bf2c3715SXin Li Row [row].start = Row [row-1].start + Row [row-1].length ;
620*bf2c3715SXin Li Row [row].shared1.p = Row [row].start ;
621*bf2c3715SXin Li Row [row].shared2.mark = -1 ;
622*bf2c3715SXin Li }
623*bf2c3715SXin Li
624*bf2c3715SXin Li /* === Create row form ================================================== */
625*bf2c3715SXin Li
626*bf2c3715SXin Li if (stats [Status] == OkButJumbled)
627*bf2c3715SXin Li {
628*bf2c3715SXin Li /* if cols jumbled, watch for repeated row indices */
629*bf2c3715SXin Li for (col = 0 ; col < n_col ; col++)
630*bf2c3715SXin Li {
631*bf2c3715SXin Li cp = &A [p [col]] ;
632*bf2c3715SXin Li cp_end = &A [p [col+1]] ;
633*bf2c3715SXin Li while (cp < cp_end)
634*bf2c3715SXin Li {
635*bf2c3715SXin Li row = *cp++ ;
636*bf2c3715SXin Li if (Row [row].shared2.mark != col)
637*bf2c3715SXin Li {
638*bf2c3715SXin Li A [(Row [row].shared1.p)++] = col ;
639*bf2c3715SXin Li Row [row].shared2.mark = col ;
640*bf2c3715SXin Li }
641*bf2c3715SXin Li }
642*bf2c3715SXin Li }
643*bf2c3715SXin Li }
644*bf2c3715SXin Li else
645*bf2c3715SXin Li {
646*bf2c3715SXin Li /* if cols not jumbled, we don't need the mark (this is faster) */
647*bf2c3715SXin Li for (col = 0 ; col < n_col ; col++)
648*bf2c3715SXin Li {
649*bf2c3715SXin Li cp = &A [p [col]] ;
650*bf2c3715SXin Li cp_end = &A [p [col+1]] ;
651*bf2c3715SXin Li while (cp < cp_end)
652*bf2c3715SXin Li {
653*bf2c3715SXin Li A [(Row [*cp++].shared1.p)++] = col ;
654*bf2c3715SXin Li }
655*bf2c3715SXin Li }
656*bf2c3715SXin Li }
657*bf2c3715SXin Li
658*bf2c3715SXin Li /* === Clear the row marks and set row degrees ========================== */
659*bf2c3715SXin Li
660*bf2c3715SXin Li for (row = 0 ; row < n_row ; row++)
661*bf2c3715SXin Li {
662*bf2c3715SXin Li Row [row].shared2.mark = 0 ;
663*bf2c3715SXin Li Row [row].shared1.degree = Row [row].length ;
664*bf2c3715SXin Li }
665*bf2c3715SXin Li
666*bf2c3715SXin Li /* === See if we need to re-create columns ============================== */
667*bf2c3715SXin Li
668*bf2c3715SXin Li if (stats [Status] == OkButJumbled)
669*bf2c3715SXin Li {
670*bf2c3715SXin Li COLAMD_DEBUG0 (("colamd: reconstructing column form, matrix jumbled\n")) ;
671*bf2c3715SXin Li
672*bf2c3715SXin Li
673*bf2c3715SXin Li /* === Compute col pointers ========================================= */
674*bf2c3715SXin Li
675*bf2c3715SXin Li /* col form of the matrix starts at A [0]. */
676*bf2c3715SXin Li /* Note, we may have a gap between the col form and the row */
677*bf2c3715SXin Li /* form if there were duplicate entries, if so, it will be */
678*bf2c3715SXin Li /* removed upon the first garbage collection */
679*bf2c3715SXin Li Col [0].start = 0 ;
680*bf2c3715SXin Li p [0] = Col [0].start ;
681*bf2c3715SXin Li for (col = 1 ; col < n_col ; col++)
682*bf2c3715SXin Li {
683*bf2c3715SXin Li /* note that the lengths here are for pruned columns, i.e. */
684*bf2c3715SXin Li /* no duplicate row indices will exist for these columns */
685*bf2c3715SXin Li Col [col].start = Col [col-1].start + Col [col-1].length ;
686*bf2c3715SXin Li p [col] = Col [col].start ;
687*bf2c3715SXin Li }
688*bf2c3715SXin Li
689*bf2c3715SXin Li /* === Re-create col form =========================================== */
690*bf2c3715SXin Li
691*bf2c3715SXin Li for (row = 0 ; row < n_row ; row++)
692*bf2c3715SXin Li {
693*bf2c3715SXin Li rp = &A [Row [row].start] ;
694*bf2c3715SXin Li rp_end = rp + Row [row].length ;
695*bf2c3715SXin Li while (rp < rp_end)
696*bf2c3715SXin Li {
697*bf2c3715SXin Li A [(p [*rp++])++] = row ;
698*bf2c3715SXin Li }
699*bf2c3715SXin Li }
700*bf2c3715SXin Li }
701*bf2c3715SXin Li
702*bf2c3715SXin Li /* === Done. Matrix is not (or no longer) jumbled ====================== */
703*bf2c3715SXin Li
704*bf2c3715SXin Li return (true) ;
705*bf2c3715SXin Li }
706*bf2c3715SXin Li
707*bf2c3715SXin Li
708*bf2c3715SXin Li /* ========================================================================== */
709*bf2c3715SXin Li /* === init_scoring ========================================================= */
710*bf2c3715SXin Li /* ========================================================================== */
711*bf2c3715SXin Li
712*bf2c3715SXin Li /*
713*bf2c3715SXin Li Kills dense or empty columns and rows, calculates an initial score for
714*bf2c3715SXin Li each column, and places all columns in the degree lists. Not user-callable.
715*bf2c3715SXin Li */
716*bf2c3715SXin Li template <typename IndexType>
init_scoring(IndexType n_row,IndexType n_col,RowStructure<IndexType> Row[],ColStructure<IndexType> Col[],IndexType A[],IndexType head[],double knobs[NKnobs],IndexType * p_n_row2,IndexType * p_n_col2,IndexType * p_max_deg)717*bf2c3715SXin Li static void init_scoring
718*bf2c3715SXin Li (
719*bf2c3715SXin Li /* === Parameters ======================================================= */
720*bf2c3715SXin Li
721*bf2c3715SXin Li IndexType n_row, /* number of rows of A */
722*bf2c3715SXin Li IndexType n_col, /* number of columns of A */
723*bf2c3715SXin Li RowStructure<IndexType> Row [], /* of size n_row+1 */
724*bf2c3715SXin Li ColStructure<IndexType> Col [], /* of size n_col+1 */
725*bf2c3715SXin Li IndexType A [], /* column form and row form of A */
726*bf2c3715SXin Li IndexType head [], /* of size n_col+1 */
727*bf2c3715SXin Li double knobs [NKnobs],/* parameters */
728*bf2c3715SXin Li IndexType *p_n_row2, /* number of non-dense, non-empty rows */
729*bf2c3715SXin Li IndexType *p_n_col2, /* number of non-dense, non-empty columns */
730*bf2c3715SXin Li IndexType *p_max_deg /* maximum row degree */
731*bf2c3715SXin Li )
732*bf2c3715SXin Li {
733*bf2c3715SXin Li /* === Local variables ================================================== */
734*bf2c3715SXin Li
735*bf2c3715SXin Li IndexType c ; /* a column index */
736*bf2c3715SXin Li IndexType r, row ; /* a row index */
737*bf2c3715SXin Li IndexType *cp ; /* a column pointer */
738*bf2c3715SXin Li IndexType deg ; /* degree of a row or column */
739*bf2c3715SXin Li IndexType *cp_end ; /* a pointer to the end of a column */
740*bf2c3715SXin Li IndexType *new_cp ; /* new column pointer */
741*bf2c3715SXin Li IndexType col_length ; /* length of pruned column */
742*bf2c3715SXin Li IndexType score ; /* current column score */
743*bf2c3715SXin Li IndexType n_col2 ; /* number of non-dense, non-empty columns */
744*bf2c3715SXin Li IndexType n_row2 ; /* number of non-dense, non-empty rows */
745*bf2c3715SXin Li IndexType dense_row_count ; /* remove rows with more entries than this */
746*bf2c3715SXin Li IndexType dense_col_count ; /* remove cols with more entries than this */
747*bf2c3715SXin Li IndexType min_score ; /* smallest column score */
748*bf2c3715SXin Li IndexType max_deg ; /* maximum row degree */
749*bf2c3715SXin Li IndexType next_col ; /* Used to add to degree list.*/
750*bf2c3715SXin Li
751*bf2c3715SXin Li
752*bf2c3715SXin Li /* === Extract knobs ==================================================== */
753*bf2c3715SXin Li
754*bf2c3715SXin Li dense_row_count = numext::maxi(IndexType(0), numext::mini(IndexType(knobs [Colamd::DenseRow] * n_col), n_col)) ;
755*bf2c3715SXin Li dense_col_count = numext::maxi(IndexType(0), numext::mini(IndexType(knobs [Colamd::DenseCol] * n_row), n_row)) ;
756*bf2c3715SXin Li COLAMD_DEBUG1 (("colamd: densecount: %d %d\n", dense_row_count, dense_col_count)) ;
757*bf2c3715SXin Li max_deg = 0 ;
758*bf2c3715SXin Li n_col2 = n_col ;
759*bf2c3715SXin Li n_row2 = n_row ;
760*bf2c3715SXin Li
761*bf2c3715SXin Li /* === Kill empty columns =============================================== */
762*bf2c3715SXin Li
763*bf2c3715SXin Li /* Put the empty columns at the end in their natural order, so that LU */
764*bf2c3715SXin Li /* factorization can proceed as far as possible. */
765*bf2c3715SXin Li for (c = n_col-1 ; c >= 0 ; c--)
766*bf2c3715SXin Li {
767*bf2c3715SXin Li deg = Col [c].length ;
768*bf2c3715SXin Li if (deg == 0)
769*bf2c3715SXin Li {
770*bf2c3715SXin Li /* this is a empty column, kill and order it last */
771*bf2c3715SXin Li Col [c].shared2.order = --n_col2 ;
772*bf2c3715SXin Li Col[c].kill_principal() ;
773*bf2c3715SXin Li }
774*bf2c3715SXin Li }
775*bf2c3715SXin Li COLAMD_DEBUG1 (("colamd: null columns killed: %d\n", n_col - n_col2)) ;
776*bf2c3715SXin Li
777*bf2c3715SXin Li /* === Kill dense columns =============================================== */
778*bf2c3715SXin Li
779*bf2c3715SXin Li /* Put the dense columns at the end, in their natural order */
780*bf2c3715SXin Li for (c = n_col-1 ; c >= 0 ; c--)
781*bf2c3715SXin Li {
782*bf2c3715SXin Li /* skip any dead columns */
783*bf2c3715SXin Li if (Col[c].is_dead())
784*bf2c3715SXin Li {
785*bf2c3715SXin Li continue ;
786*bf2c3715SXin Li }
787*bf2c3715SXin Li deg = Col [c].length ;
788*bf2c3715SXin Li if (deg > dense_col_count)
789*bf2c3715SXin Li {
790*bf2c3715SXin Li /* this is a dense column, kill and order it last */
791*bf2c3715SXin Li Col [c].shared2.order = --n_col2 ;
792*bf2c3715SXin Li /* decrement the row degrees */
793*bf2c3715SXin Li cp = &A [Col [c].start] ;
794*bf2c3715SXin Li cp_end = cp + Col [c].length ;
795*bf2c3715SXin Li while (cp < cp_end)
796*bf2c3715SXin Li {
797*bf2c3715SXin Li Row [*cp++].shared1.degree-- ;
798*bf2c3715SXin Li }
799*bf2c3715SXin Li Col[c].kill_principal() ;
800*bf2c3715SXin Li }
801*bf2c3715SXin Li }
802*bf2c3715SXin Li COLAMD_DEBUG1 (("colamd: Dense and null columns killed: %d\n", n_col - n_col2)) ;
803*bf2c3715SXin Li
804*bf2c3715SXin Li /* === Kill dense and empty rows ======================================== */
805*bf2c3715SXin Li
806*bf2c3715SXin Li for (r = 0 ; r < n_row ; r++)
807*bf2c3715SXin Li {
808*bf2c3715SXin Li deg = Row [r].shared1.degree ;
809*bf2c3715SXin Li COLAMD_ASSERT (deg >= 0 && deg <= n_col) ;
810*bf2c3715SXin Li if (deg > dense_row_count || deg == 0)
811*bf2c3715SXin Li {
812*bf2c3715SXin Li /* kill a dense or empty row */
813*bf2c3715SXin Li Row[r].kill() ;
814*bf2c3715SXin Li --n_row2 ;
815*bf2c3715SXin Li }
816*bf2c3715SXin Li else
817*bf2c3715SXin Li {
818*bf2c3715SXin Li /* keep track of max degree of remaining rows */
819*bf2c3715SXin Li max_deg = numext::maxi(max_deg, deg) ;
820*bf2c3715SXin Li }
821*bf2c3715SXin Li }
822*bf2c3715SXin Li COLAMD_DEBUG1 (("colamd: Dense and null rows killed: %d\n", n_row - n_row2)) ;
823*bf2c3715SXin Li
824*bf2c3715SXin Li /* === Compute initial column scores ==================================== */
825*bf2c3715SXin Li
826*bf2c3715SXin Li /* At this point the row degrees are accurate. They reflect the number */
827*bf2c3715SXin Li /* of "live" (non-dense) columns in each row. No empty rows exist. */
828*bf2c3715SXin Li /* Some "live" columns may contain only dead rows, however. These are */
829*bf2c3715SXin Li /* pruned in the code below. */
830*bf2c3715SXin Li
831*bf2c3715SXin Li /* now find the initial matlab score for each column */
832*bf2c3715SXin Li for (c = n_col-1 ; c >= 0 ; c--)
833*bf2c3715SXin Li {
834*bf2c3715SXin Li /* skip dead column */
835*bf2c3715SXin Li if (Col[c].is_dead())
836*bf2c3715SXin Li {
837*bf2c3715SXin Li continue ;
838*bf2c3715SXin Li }
839*bf2c3715SXin Li score = 0 ;
840*bf2c3715SXin Li cp = &A [Col [c].start] ;
841*bf2c3715SXin Li new_cp = cp ;
842*bf2c3715SXin Li cp_end = cp + Col [c].length ;
843*bf2c3715SXin Li while (cp < cp_end)
844*bf2c3715SXin Li {
845*bf2c3715SXin Li /* get a row */
846*bf2c3715SXin Li row = *cp++ ;
847*bf2c3715SXin Li /* skip if dead */
848*bf2c3715SXin Li if (Row[row].is_dead())
849*bf2c3715SXin Li {
850*bf2c3715SXin Li continue ;
851*bf2c3715SXin Li }
852*bf2c3715SXin Li /* compact the column */
853*bf2c3715SXin Li *new_cp++ = row ;
854*bf2c3715SXin Li /* add row's external degree */
855*bf2c3715SXin Li score += Row [row].shared1.degree - 1 ;
856*bf2c3715SXin Li /* guard against integer overflow */
857*bf2c3715SXin Li score = numext::mini(score, n_col) ;
858*bf2c3715SXin Li }
859*bf2c3715SXin Li /* determine pruned column length */
860*bf2c3715SXin Li col_length = (IndexType) (new_cp - &A [Col [c].start]) ;
861*bf2c3715SXin Li if (col_length == 0)
862*bf2c3715SXin Li {
863*bf2c3715SXin Li /* a newly-made null column (all rows in this col are "dense" */
864*bf2c3715SXin Li /* and have already been killed) */
865*bf2c3715SXin Li COLAMD_DEBUG2 (("Newly null killed: %d\n", c)) ;
866*bf2c3715SXin Li Col [c].shared2.order = --n_col2 ;
867*bf2c3715SXin Li Col[c].kill_principal() ;
868*bf2c3715SXin Li }
869*bf2c3715SXin Li else
870*bf2c3715SXin Li {
871*bf2c3715SXin Li /* set column length and set score */
872*bf2c3715SXin Li COLAMD_ASSERT (score >= 0) ;
873*bf2c3715SXin Li COLAMD_ASSERT (score <= n_col) ;
874*bf2c3715SXin Li Col [c].length = col_length ;
875*bf2c3715SXin Li Col [c].shared2.score = score ;
876*bf2c3715SXin Li }
877*bf2c3715SXin Li }
878*bf2c3715SXin Li COLAMD_DEBUG1 (("colamd: Dense, null, and newly-null columns killed: %d\n",
879*bf2c3715SXin Li n_col-n_col2)) ;
880*bf2c3715SXin Li
881*bf2c3715SXin Li /* At this point, all empty rows and columns are dead. All live columns */
882*bf2c3715SXin Li /* are "clean" (containing no dead rows) and simplicial (no supercolumns */
883*bf2c3715SXin Li /* yet). Rows may contain dead columns, but all live rows contain at */
884*bf2c3715SXin Li /* least one live column. */
885*bf2c3715SXin Li
886*bf2c3715SXin Li /* === Initialize degree lists ========================================== */
887*bf2c3715SXin Li
888*bf2c3715SXin Li
889*bf2c3715SXin Li /* clear the hash buckets */
890*bf2c3715SXin Li for (c = 0 ; c <= n_col ; c++)
891*bf2c3715SXin Li {
892*bf2c3715SXin Li head [c] = Empty ;
893*bf2c3715SXin Li }
894*bf2c3715SXin Li min_score = n_col ;
895*bf2c3715SXin Li /* place in reverse order, so low column indices are at the front */
896*bf2c3715SXin Li /* of the lists. This is to encourage natural tie-breaking */
897*bf2c3715SXin Li for (c = n_col-1 ; c >= 0 ; c--)
898*bf2c3715SXin Li {
899*bf2c3715SXin Li /* only add principal columns to degree lists */
900*bf2c3715SXin Li if (Col[c].is_alive())
901*bf2c3715SXin Li {
902*bf2c3715SXin Li COLAMD_DEBUG4 (("place %d score %d minscore %d ncol %d\n",
903*bf2c3715SXin Li c, Col [c].shared2.score, min_score, n_col)) ;
904*bf2c3715SXin Li
905*bf2c3715SXin Li /* === Add columns score to DList =============================== */
906*bf2c3715SXin Li
907*bf2c3715SXin Li score = Col [c].shared2.score ;
908*bf2c3715SXin Li
909*bf2c3715SXin Li COLAMD_ASSERT (min_score >= 0) ;
910*bf2c3715SXin Li COLAMD_ASSERT (min_score <= n_col) ;
911*bf2c3715SXin Li COLAMD_ASSERT (score >= 0) ;
912*bf2c3715SXin Li COLAMD_ASSERT (score <= n_col) ;
913*bf2c3715SXin Li COLAMD_ASSERT (head [score] >= Empty) ;
914*bf2c3715SXin Li
915*bf2c3715SXin Li /* now add this column to dList at proper score location */
916*bf2c3715SXin Li next_col = head [score] ;
917*bf2c3715SXin Li Col [c].shared3.prev = Empty ;
918*bf2c3715SXin Li Col [c].shared4.degree_next = next_col ;
919*bf2c3715SXin Li
920*bf2c3715SXin Li /* if there already was a column with the same score, set its */
921*bf2c3715SXin Li /* previous pointer to this new column */
922*bf2c3715SXin Li if (next_col != Empty)
923*bf2c3715SXin Li {
924*bf2c3715SXin Li Col [next_col].shared3.prev = c ;
925*bf2c3715SXin Li }
926*bf2c3715SXin Li head [score] = c ;
927*bf2c3715SXin Li
928*bf2c3715SXin Li /* see if this score is less than current min */
929*bf2c3715SXin Li min_score = numext::mini(min_score, score) ;
930*bf2c3715SXin Li
931*bf2c3715SXin Li
932*bf2c3715SXin Li }
933*bf2c3715SXin Li }
934*bf2c3715SXin Li
935*bf2c3715SXin Li
936*bf2c3715SXin Li /* === Return number of remaining columns, and max row degree =========== */
937*bf2c3715SXin Li
938*bf2c3715SXin Li *p_n_col2 = n_col2 ;
939*bf2c3715SXin Li *p_n_row2 = n_row2 ;
940*bf2c3715SXin Li *p_max_deg = max_deg ;
941*bf2c3715SXin Li }
942*bf2c3715SXin Li
943*bf2c3715SXin Li
944*bf2c3715SXin Li /* ========================================================================== */
945*bf2c3715SXin Li /* === find_ordering ======================================================== */
946*bf2c3715SXin Li /* ========================================================================== */
947*bf2c3715SXin Li
948*bf2c3715SXin Li /*
949*bf2c3715SXin Li Order the principal columns of the supercolumn form of the matrix
950*bf2c3715SXin Li (no supercolumns on input). Uses a minimum approximate column minimum
951*bf2c3715SXin Li degree ordering method. Not user-callable.
952*bf2c3715SXin Li */
953*bf2c3715SXin Li template <typename IndexType>
find_ordering(IndexType n_row,IndexType n_col,IndexType Alen,RowStructure<IndexType> Row[],ColStructure<IndexType> Col[],IndexType A[],IndexType head[],IndexType n_col2,IndexType max_deg,IndexType pfree)954*bf2c3715SXin Li static IndexType find_ordering /* return the number of garbage collections */
955*bf2c3715SXin Li (
956*bf2c3715SXin Li /* === Parameters ======================================================= */
957*bf2c3715SXin Li
958*bf2c3715SXin Li IndexType n_row, /* number of rows of A */
959*bf2c3715SXin Li IndexType n_col, /* number of columns of A */
960*bf2c3715SXin Li IndexType Alen, /* size of A, 2*nnz + n_col or larger */
961*bf2c3715SXin Li RowStructure<IndexType> Row [], /* of size n_row+1 */
962*bf2c3715SXin Li ColStructure<IndexType> Col [], /* of size n_col+1 */
963*bf2c3715SXin Li IndexType A [], /* column form and row form of A */
964*bf2c3715SXin Li IndexType head [], /* of size n_col+1 */
965*bf2c3715SXin Li IndexType n_col2, /* Remaining columns to order */
966*bf2c3715SXin Li IndexType max_deg, /* Maximum row degree */
967*bf2c3715SXin Li IndexType pfree /* index of first free slot (2*nnz on entry) */
968*bf2c3715SXin Li )
969*bf2c3715SXin Li {
970*bf2c3715SXin Li /* === Local variables ================================================== */
971*bf2c3715SXin Li
972*bf2c3715SXin Li IndexType k ; /* current pivot ordering step */
973*bf2c3715SXin Li IndexType pivot_col ; /* current pivot column */
974*bf2c3715SXin Li IndexType *cp ; /* a column pointer */
975*bf2c3715SXin Li IndexType *rp ; /* a row pointer */
976*bf2c3715SXin Li IndexType pivot_row ; /* current pivot row */
977*bf2c3715SXin Li IndexType *new_cp ; /* modified column pointer */
978*bf2c3715SXin Li IndexType *new_rp ; /* modified row pointer */
979*bf2c3715SXin Li IndexType pivot_row_start ; /* pointer to start of pivot row */
980*bf2c3715SXin Li IndexType pivot_row_degree ; /* number of columns in pivot row */
981*bf2c3715SXin Li IndexType pivot_row_length ; /* number of supercolumns in pivot row */
982*bf2c3715SXin Li IndexType pivot_col_score ; /* score of pivot column */
983*bf2c3715SXin Li IndexType needed_memory ; /* free space needed for pivot row */
984*bf2c3715SXin Li IndexType *cp_end ; /* pointer to the end of a column */
985*bf2c3715SXin Li IndexType *rp_end ; /* pointer to the end of a row */
986*bf2c3715SXin Li IndexType row ; /* a row index */
987*bf2c3715SXin Li IndexType col ; /* a column index */
988*bf2c3715SXin Li IndexType max_score ; /* maximum possible score */
989*bf2c3715SXin Li IndexType cur_score ; /* score of current column */
990*bf2c3715SXin Li unsigned int hash ; /* hash value for supernode detection */
991*bf2c3715SXin Li IndexType head_column ; /* head of hash bucket */
992*bf2c3715SXin Li IndexType first_col ; /* first column in hash bucket */
993*bf2c3715SXin Li IndexType tag_mark ; /* marker value for mark array */
994*bf2c3715SXin Li IndexType row_mark ; /* Row [row].shared2.mark */
995*bf2c3715SXin Li IndexType set_difference ; /* set difference size of row with pivot row */
996*bf2c3715SXin Li IndexType min_score ; /* smallest column score */
997*bf2c3715SXin Li IndexType col_thickness ; /* "thickness" (no. of columns in a supercol) */
998*bf2c3715SXin Li IndexType max_mark ; /* maximum value of tag_mark */
999*bf2c3715SXin Li IndexType pivot_col_thickness ; /* number of columns represented by pivot col */
1000*bf2c3715SXin Li IndexType prev_col ; /* Used by Dlist operations. */
1001*bf2c3715SXin Li IndexType next_col ; /* Used by Dlist operations. */
1002*bf2c3715SXin Li IndexType ngarbage ; /* number of garbage collections performed */
1003*bf2c3715SXin Li
1004*bf2c3715SXin Li
1005*bf2c3715SXin Li /* === Initialization and clear mark ==================================== */
1006*bf2c3715SXin Li
1007*bf2c3715SXin Li max_mark = INT_MAX - n_col ; /* INT_MAX defined in <limits.h> */
1008*bf2c3715SXin Li tag_mark = Colamd::clear_mark (n_row, Row) ;
1009*bf2c3715SXin Li min_score = 0 ;
1010*bf2c3715SXin Li ngarbage = 0 ;
1011*bf2c3715SXin Li COLAMD_DEBUG1 (("colamd: Ordering, n_col2=%d\n", n_col2)) ;
1012*bf2c3715SXin Li
1013*bf2c3715SXin Li /* === Order the columns ================================================ */
1014*bf2c3715SXin Li
1015*bf2c3715SXin Li for (k = 0 ; k < n_col2 ; /* 'k' is incremented below */)
1016*bf2c3715SXin Li {
1017*bf2c3715SXin Li
1018*bf2c3715SXin Li /* === Select pivot column, and order it ============================ */
1019*bf2c3715SXin Li
1020*bf2c3715SXin Li /* make sure degree list isn't empty */
1021*bf2c3715SXin Li COLAMD_ASSERT (min_score >= 0) ;
1022*bf2c3715SXin Li COLAMD_ASSERT (min_score <= n_col) ;
1023*bf2c3715SXin Li COLAMD_ASSERT (head [min_score] >= Empty) ;
1024*bf2c3715SXin Li
1025*bf2c3715SXin Li /* get pivot column from head of minimum degree list */
1026*bf2c3715SXin Li while (min_score < n_col && head [min_score] == Empty)
1027*bf2c3715SXin Li {
1028*bf2c3715SXin Li min_score++ ;
1029*bf2c3715SXin Li }
1030*bf2c3715SXin Li pivot_col = head [min_score] ;
1031*bf2c3715SXin Li COLAMD_ASSERT (pivot_col >= 0 && pivot_col <= n_col) ;
1032*bf2c3715SXin Li next_col = Col [pivot_col].shared4.degree_next ;
1033*bf2c3715SXin Li head [min_score] = next_col ;
1034*bf2c3715SXin Li if (next_col != Empty)
1035*bf2c3715SXin Li {
1036*bf2c3715SXin Li Col [next_col].shared3.prev = Empty ;
1037*bf2c3715SXin Li }
1038*bf2c3715SXin Li
1039*bf2c3715SXin Li COLAMD_ASSERT (Col[pivot_col].is_alive()) ;
1040*bf2c3715SXin Li COLAMD_DEBUG3 (("Pivot col: %d\n", pivot_col)) ;
1041*bf2c3715SXin Li
1042*bf2c3715SXin Li /* remember score for defrag check */
1043*bf2c3715SXin Li pivot_col_score = Col [pivot_col].shared2.score ;
1044*bf2c3715SXin Li
1045*bf2c3715SXin Li /* the pivot column is the kth column in the pivot order */
1046*bf2c3715SXin Li Col [pivot_col].shared2.order = k ;
1047*bf2c3715SXin Li
1048*bf2c3715SXin Li /* increment order count by column thickness */
1049*bf2c3715SXin Li pivot_col_thickness = Col [pivot_col].shared1.thickness ;
1050*bf2c3715SXin Li k += pivot_col_thickness ;
1051*bf2c3715SXin Li COLAMD_ASSERT (pivot_col_thickness > 0) ;
1052*bf2c3715SXin Li
1053*bf2c3715SXin Li /* === Garbage_collection, if necessary ============================= */
1054*bf2c3715SXin Li
1055*bf2c3715SXin Li needed_memory = numext::mini(pivot_col_score, n_col - k) ;
1056*bf2c3715SXin Li if (pfree + needed_memory >= Alen)
1057*bf2c3715SXin Li {
1058*bf2c3715SXin Li pfree = Colamd::garbage_collection (n_row, n_col, Row, Col, A, &A [pfree]) ;
1059*bf2c3715SXin Li ngarbage++ ;
1060*bf2c3715SXin Li /* after garbage collection we will have enough */
1061*bf2c3715SXin Li COLAMD_ASSERT (pfree + needed_memory < Alen) ;
1062*bf2c3715SXin Li /* garbage collection has wiped out the Row[].shared2.mark array */
1063*bf2c3715SXin Li tag_mark = Colamd::clear_mark (n_row, Row) ;
1064*bf2c3715SXin Li
1065*bf2c3715SXin Li }
1066*bf2c3715SXin Li
1067*bf2c3715SXin Li /* === Compute pivot row pattern ==================================== */
1068*bf2c3715SXin Li
1069*bf2c3715SXin Li /* get starting location for this new merged row */
1070*bf2c3715SXin Li pivot_row_start = pfree ;
1071*bf2c3715SXin Li
1072*bf2c3715SXin Li /* initialize new row counts to zero */
1073*bf2c3715SXin Li pivot_row_degree = 0 ;
1074*bf2c3715SXin Li
1075*bf2c3715SXin Li /* tag pivot column as having been visited so it isn't included */
1076*bf2c3715SXin Li /* in merged pivot row */
1077*bf2c3715SXin Li Col [pivot_col].shared1.thickness = -pivot_col_thickness ;
1078*bf2c3715SXin Li
1079*bf2c3715SXin Li /* pivot row is the union of all rows in the pivot column pattern */
1080*bf2c3715SXin Li cp = &A [Col [pivot_col].start] ;
1081*bf2c3715SXin Li cp_end = cp + Col [pivot_col].length ;
1082*bf2c3715SXin Li while (cp < cp_end)
1083*bf2c3715SXin Li {
1084*bf2c3715SXin Li /* get a row */
1085*bf2c3715SXin Li row = *cp++ ;
1086*bf2c3715SXin Li COLAMD_DEBUG4 (("Pivot col pattern %d %d\n", Row[row].is_alive(), row)) ;
1087*bf2c3715SXin Li /* skip if row is dead */
1088*bf2c3715SXin Li if (Row[row].is_dead())
1089*bf2c3715SXin Li {
1090*bf2c3715SXin Li continue ;
1091*bf2c3715SXin Li }
1092*bf2c3715SXin Li rp = &A [Row [row].start] ;
1093*bf2c3715SXin Li rp_end = rp + Row [row].length ;
1094*bf2c3715SXin Li while (rp < rp_end)
1095*bf2c3715SXin Li {
1096*bf2c3715SXin Li /* get a column */
1097*bf2c3715SXin Li col = *rp++ ;
1098*bf2c3715SXin Li /* add the column, if alive and untagged */
1099*bf2c3715SXin Li col_thickness = Col [col].shared1.thickness ;
1100*bf2c3715SXin Li if (col_thickness > 0 && Col[col].is_alive())
1101*bf2c3715SXin Li {
1102*bf2c3715SXin Li /* tag column in pivot row */
1103*bf2c3715SXin Li Col [col].shared1.thickness = -col_thickness ;
1104*bf2c3715SXin Li COLAMD_ASSERT (pfree < Alen) ;
1105*bf2c3715SXin Li /* place column in pivot row */
1106*bf2c3715SXin Li A [pfree++] = col ;
1107*bf2c3715SXin Li pivot_row_degree += col_thickness ;
1108*bf2c3715SXin Li }
1109*bf2c3715SXin Li }
1110*bf2c3715SXin Li }
1111*bf2c3715SXin Li
1112*bf2c3715SXin Li /* clear tag on pivot column */
1113*bf2c3715SXin Li Col [pivot_col].shared1.thickness = pivot_col_thickness ;
1114*bf2c3715SXin Li max_deg = numext::maxi(max_deg, pivot_row_degree) ;
1115*bf2c3715SXin Li
1116*bf2c3715SXin Li
1117*bf2c3715SXin Li /* === Kill all rows used to construct pivot row ==================== */
1118*bf2c3715SXin Li
1119*bf2c3715SXin Li /* also kill pivot row, temporarily */
1120*bf2c3715SXin Li cp = &A [Col [pivot_col].start] ;
1121*bf2c3715SXin Li cp_end = cp + Col [pivot_col].length ;
1122*bf2c3715SXin Li while (cp < cp_end)
1123*bf2c3715SXin Li {
1124*bf2c3715SXin Li /* may be killing an already dead row */
1125*bf2c3715SXin Li row = *cp++ ;
1126*bf2c3715SXin Li COLAMD_DEBUG3 (("Kill row in pivot col: %d\n", row)) ;
1127*bf2c3715SXin Li Row[row].kill() ;
1128*bf2c3715SXin Li }
1129*bf2c3715SXin Li
1130*bf2c3715SXin Li /* === Select a row index to use as the new pivot row =============== */
1131*bf2c3715SXin Li
1132*bf2c3715SXin Li pivot_row_length = pfree - pivot_row_start ;
1133*bf2c3715SXin Li if (pivot_row_length > 0)
1134*bf2c3715SXin Li {
1135*bf2c3715SXin Li /* pick the "pivot" row arbitrarily (first row in col) */
1136*bf2c3715SXin Li pivot_row = A [Col [pivot_col].start] ;
1137*bf2c3715SXin Li COLAMD_DEBUG3 (("Pivotal row is %d\n", pivot_row)) ;
1138*bf2c3715SXin Li }
1139*bf2c3715SXin Li else
1140*bf2c3715SXin Li {
1141*bf2c3715SXin Li /* there is no pivot row, since it is of zero length */
1142*bf2c3715SXin Li pivot_row = Empty ;
1143*bf2c3715SXin Li COLAMD_ASSERT (pivot_row_length == 0) ;
1144*bf2c3715SXin Li }
1145*bf2c3715SXin Li COLAMD_ASSERT (Col [pivot_col].length > 0 || pivot_row_length == 0) ;
1146*bf2c3715SXin Li
1147*bf2c3715SXin Li /* === Approximate degree computation =============================== */
1148*bf2c3715SXin Li
1149*bf2c3715SXin Li /* Here begins the computation of the approximate degree. The column */
1150*bf2c3715SXin Li /* score is the sum of the pivot row "length", plus the size of the */
1151*bf2c3715SXin Li /* set differences of each row in the column minus the pattern of the */
1152*bf2c3715SXin Li /* pivot row itself. The column ("thickness") itself is also */
1153*bf2c3715SXin Li /* excluded from the column score (we thus use an approximate */
1154*bf2c3715SXin Li /* external degree). */
1155*bf2c3715SXin Li
1156*bf2c3715SXin Li /* The time taken by the following code (compute set differences, and */
1157*bf2c3715SXin Li /* add them up) is proportional to the size of the data structure */
1158*bf2c3715SXin Li /* being scanned - that is, the sum of the sizes of each column in */
1159*bf2c3715SXin Li /* the pivot row. Thus, the amortized time to compute a column score */
1160*bf2c3715SXin Li /* is proportional to the size of that column (where size, in this */
1161*bf2c3715SXin Li /* context, is the column "length", or the number of row indices */
1162*bf2c3715SXin Li /* in that column). The number of row indices in a column is */
1163*bf2c3715SXin Li /* monotonically non-decreasing, from the length of the original */
1164*bf2c3715SXin Li /* column on input to colamd. */
1165*bf2c3715SXin Li
1166*bf2c3715SXin Li /* === Compute set differences ====================================== */
1167*bf2c3715SXin Li
1168*bf2c3715SXin Li COLAMD_DEBUG3 (("** Computing set differences phase. **\n")) ;
1169*bf2c3715SXin Li
1170*bf2c3715SXin Li /* pivot row is currently dead - it will be revived later. */
1171*bf2c3715SXin Li
1172*bf2c3715SXin Li COLAMD_DEBUG3 (("Pivot row: ")) ;
1173*bf2c3715SXin Li /* for each column in pivot row */
1174*bf2c3715SXin Li rp = &A [pivot_row_start] ;
1175*bf2c3715SXin Li rp_end = rp + pivot_row_length ;
1176*bf2c3715SXin Li while (rp < rp_end)
1177*bf2c3715SXin Li {
1178*bf2c3715SXin Li col = *rp++ ;
1179*bf2c3715SXin Li COLAMD_ASSERT (Col[col].is_alive() && col != pivot_col) ;
1180*bf2c3715SXin Li COLAMD_DEBUG3 (("Col: %d\n", col)) ;
1181*bf2c3715SXin Li
1182*bf2c3715SXin Li /* clear tags used to construct pivot row pattern */
1183*bf2c3715SXin Li col_thickness = -Col [col].shared1.thickness ;
1184*bf2c3715SXin Li COLAMD_ASSERT (col_thickness > 0) ;
1185*bf2c3715SXin Li Col [col].shared1.thickness = col_thickness ;
1186*bf2c3715SXin Li
1187*bf2c3715SXin Li /* === Remove column from degree list =========================== */
1188*bf2c3715SXin Li
1189*bf2c3715SXin Li cur_score = Col [col].shared2.score ;
1190*bf2c3715SXin Li prev_col = Col [col].shared3.prev ;
1191*bf2c3715SXin Li next_col = Col [col].shared4.degree_next ;
1192*bf2c3715SXin Li COLAMD_ASSERT (cur_score >= 0) ;
1193*bf2c3715SXin Li COLAMD_ASSERT (cur_score <= n_col) ;
1194*bf2c3715SXin Li COLAMD_ASSERT (cur_score >= Empty) ;
1195*bf2c3715SXin Li if (prev_col == Empty)
1196*bf2c3715SXin Li {
1197*bf2c3715SXin Li head [cur_score] = next_col ;
1198*bf2c3715SXin Li }
1199*bf2c3715SXin Li else
1200*bf2c3715SXin Li {
1201*bf2c3715SXin Li Col [prev_col].shared4.degree_next = next_col ;
1202*bf2c3715SXin Li }
1203*bf2c3715SXin Li if (next_col != Empty)
1204*bf2c3715SXin Li {
1205*bf2c3715SXin Li Col [next_col].shared3.prev = prev_col ;
1206*bf2c3715SXin Li }
1207*bf2c3715SXin Li
1208*bf2c3715SXin Li /* === Scan the column ========================================== */
1209*bf2c3715SXin Li
1210*bf2c3715SXin Li cp = &A [Col [col].start] ;
1211*bf2c3715SXin Li cp_end = cp + Col [col].length ;
1212*bf2c3715SXin Li while (cp < cp_end)
1213*bf2c3715SXin Li {
1214*bf2c3715SXin Li /* get a row */
1215*bf2c3715SXin Li row = *cp++ ;
1216*bf2c3715SXin Li /* skip if dead */
1217*bf2c3715SXin Li if (Row[row].is_dead())
1218*bf2c3715SXin Li {
1219*bf2c3715SXin Li continue ;
1220*bf2c3715SXin Li }
1221*bf2c3715SXin Li row_mark = Row [row].shared2.mark ;
1222*bf2c3715SXin Li COLAMD_ASSERT (row != pivot_row) ;
1223*bf2c3715SXin Li set_difference = row_mark - tag_mark ;
1224*bf2c3715SXin Li /* check if the row has been seen yet */
1225*bf2c3715SXin Li if (set_difference < 0)
1226*bf2c3715SXin Li {
1227*bf2c3715SXin Li COLAMD_ASSERT (Row [row].shared1.degree <= max_deg) ;
1228*bf2c3715SXin Li set_difference = Row [row].shared1.degree ;
1229*bf2c3715SXin Li }
1230*bf2c3715SXin Li /* subtract column thickness from this row's set difference */
1231*bf2c3715SXin Li set_difference -= col_thickness ;
1232*bf2c3715SXin Li COLAMD_ASSERT (set_difference >= 0) ;
1233*bf2c3715SXin Li /* absorb this row if the set difference becomes zero */
1234*bf2c3715SXin Li if (set_difference == 0)
1235*bf2c3715SXin Li {
1236*bf2c3715SXin Li COLAMD_DEBUG3 (("aggressive absorption. Row: %d\n", row)) ;
1237*bf2c3715SXin Li Row[row].kill() ;
1238*bf2c3715SXin Li }
1239*bf2c3715SXin Li else
1240*bf2c3715SXin Li {
1241*bf2c3715SXin Li /* save the new mark */
1242*bf2c3715SXin Li Row [row].shared2.mark = set_difference + tag_mark ;
1243*bf2c3715SXin Li }
1244*bf2c3715SXin Li }
1245*bf2c3715SXin Li }
1246*bf2c3715SXin Li
1247*bf2c3715SXin Li
1248*bf2c3715SXin Li /* === Add up set differences for each column ======================= */
1249*bf2c3715SXin Li
1250*bf2c3715SXin Li COLAMD_DEBUG3 (("** Adding set differences phase. **\n")) ;
1251*bf2c3715SXin Li
1252*bf2c3715SXin Li /* for each column in pivot row */
1253*bf2c3715SXin Li rp = &A [pivot_row_start] ;
1254*bf2c3715SXin Li rp_end = rp + pivot_row_length ;
1255*bf2c3715SXin Li while (rp < rp_end)
1256*bf2c3715SXin Li {
1257*bf2c3715SXin Li /* get a column */
1258*bf2c3715SXin Li col = *rp++ ;
1259*bf2c3715SXin Li COLAMD_ASSERT (Col[col].is_alive() && col != pivot_col) ;
1260*bf2c3715SXin Li hash = 0 ;
1261*bf2c3715SXin Li cur_score = 0 ;
1262*bf2c3715SXin Li cp = &A [Col [col].start] ;
1263*bf2c3715SXin Li /* compact the column */
1264*bf2c3715SXin Li new_cp = cp ;
1265*bf2c3715SXin Li cp_end = cp + Col [col].length ;
1266*bf2c3715SXin Li
1267*bf2c3715SXin Li COLAMD_DEBUG4 (("Adding set diffs for Col: %d.\n", col)) ;
1268*bf2c3715SXin Li
1269*bf2c3715SXin Li while (cp < cp_end)
1270*bf2c3715SXin Li {
1271*bf2c3715SXin Li /* get a row */
1272*bf2c3715SXin Li row = *cp++ ;
1273*bf2c3715SXin Li COLAMD_ASSERT(row >= 0 && row < n_row) ;
1274*bf2c3715SXin Li /* skip if dead */
1275*bf2c3715SXin Li if (Row [row].is_dead())
1276*bf2c3715SXin Li {
1277*bf2c3715SXin Li continue ;
1278*bf2c3715SXin Li }
1279*bf2c3715SXin Li row_mark = Row [row].shared2.mark ;
1280*bf2c3715SXin Li COLAMD_ASSERT (row_mark > tag_mark) ;
1281*bf2c3715SXin Li /* compact the column */
1282*bf2c3715SXin Li *new_cp++ = row ;
1283*bf2c3715SXin Li /* compute hash function */
1284*bf2c3715SXin Li hash += row ;
1285*bf2c3715SXin Li /* add set difference */
1286*bf2c3715SXin Li cur_score += row_mark - tag_mark ;
1287*bf2c3715SXin Li /* integer overflow... */
1288*bf2c3715SXin Li cur_score = numext::mini(cur_score, n_col) ;
1289*bf2c3715SXin Li }
1290*bf2c3715SXin Li
1291*bf2c3715SXin Li /* recompute the column's length */
1292*bf2c3715SXin Li Col [col].length = (IndexType) (new_cp - &A [Col [col].start]) ;
1293*bf2c3715SXin Li
1294*bf2c3715SXin Li /* === Further mass elimination ================================= */
1295*bf2c3715SXin Li
1296*bf2c3715SXin Li if (Col [col].length == 0)
1297*bf2c3715SXin Li {
1298*bf2c3715SXin Li COLAMD_DEBUG4 (("further mass elimination. Col: %d\n", col)) ;
1299*bf2c3715SXin Li /* nothing left but the pivot row in this column */
1300*bf2c3715SXin Li Col[col].kill_principal() ;
1301*bf2c3715SXin Li pivot_row_degree -= Col [col].shared1.thickness ;
1302*bf2c3715SXin Li COLAMD_ASSERT (pivot_row_degree >= 0) ;
1303*bf2c3715SXin Li /* order it */
1304*bf2c3715SXin Li Col [col].shared2.order = k ;
1305*bf2c3715SXin Li /* increment order count by column thickness */
1306*bf2c3715SXin Li k += Col [col].shared1.thickness ;
1307*bf2c3715SXin Li }
1308*bf2c3715SXin Li else
1309*bf2c3715SXin Li {
1310*bf2c3715SXin Li /* === Prepare for supercolumn detection ==================== */
1311*bf2c3715SXin Li
1312*bf2c3715SXin Li COLAMD_DEBUG4 (("Preparing supercol detection for Col: %d.\n", col)) ;
1313*bf2c3715SXin Li
1314*bf2c3715SXin Li /* save score so far */
1315*bf2c3715SXin Li Col [col].shared2.score = cur_score ;
1316*bf2c3715SXin Li
1317*bf2c3715SXin Li /* add column to hash table, for supercolumn detection */
1318*bf2c3715SXin Li hash %= n_col + 1 ;
1319*bf2c3715SXin Li
1320*bf2c3715SXin Li COLAMD_DEBUG4 ((" Hash = %d, n_col = %d.\n", hash, n_col)) ;
1321*bf2c3715SXin Li COLAMD_ASSERT (hash <= n_col) ;
1322*bf2c3715SXin Li
1323*bf2c3715SXin Li head_column = head [hash] ;
1324*bf2c3715SXin Li if (head_column > Empty)
1325*bf2c3715SXin Li {
1326*bf2c3715SXin Li /* degree list "hash" is non-empty, use prev (shared3) of */
1327*bf2c3715SXin Li /* first column in degree list as head of hash bucket */
1328*bf2c3715SXin Li first_col = Col [head_column].shared3.headhash ;
1329*bf2c3715SXin Li Col [head_column].shared3.headhash = col ;
1330*bf2c3715SXin Li }
1331*bf2c3715SXin Li else
1332*bf2c3715SXin Li {
1333*bf2c3715SXin Li /* degree list "hash" is empty, use head as hash bucket */
1334*bf2c3715SXin Li first_col = - (head_column + 2) ;
1335*bf2c3715SXin Li head [hash] = - (col + 2) ;
1336*bf2c3715SXin Li }
1337*bf2c3715SXin Li Col [col].shared4.hash_next = first_col ;
1338*bf2c3715SXin Li
1339*bf2c3715SXin Li /* save hash function in Col [col].shared3.hash */
1340*bf2c3715SXin Li Col [col].shared3.hash = (IndexType) hash ;
1341*bf2c3715SXin Li COLAMD_ASSERT (Col[col].is_alive()) ;
1342*bf2c3715SXin Li }
1343*bf2c3715SXin Li }
1344*bf2c3715SXin Li
1345*bf2c3715SXin Li /* The approximate external column degree is now computed. */
1346*bf2c3715SXin Li
1347*bf2c3715SXin Li /* === Supercolumn detection ======================================== */
1348*bf2c3715SXin Li
1349*bf2c3715SXin Li COLAMD_DEBUG3 (("** Supercolumn detection phase. **\n")) ;
1350*bf2c3715SXin Li
1351*bf2c3715SXin Li Colamd::detect_super_cols (Col, A, head, pivot_row_start, pivot_row_length) ;
1352*bf2c3715SXin Li
1353*bf2c3715SXin Li /* === Kill the pivotal column ====================================== */
1354*bf2c3715SXin Li
1355*bf2c3715SXin Li Col[pivot_col].kill_principal() ;
1356*bf2c3715SXin Li
1357*bf2c3715SXin Li /* === Clear mark =================================================== */
1358*bf2c3715SXin Li
1359*bf2c3715SXin Li tag_mark += (max_deg + 1) ;
1360*bf2c3715SXin Li if (tag_mark >= max_mark)
1361*bf2c3715SXin Li {
1362*bf2c3715SXin Li COLAMD_DEBUG2 (("clearing tag_mark\n")) ;
1363*bf2c3715SXin Li tag_mark = Colamd::clear_mark (n_row, Row) ;
1364*bf2c3715SXin Li }
1365*bf2c3715SXin Li
1366*bf2c3715SXin Li /* === Finalize the new pivot row, and column scores ================ */
1367*bf2c3715SXin Li
1368*bf2c3715SXin Li COLAMD_DEBUG3 (("** Finalize scores phase. **\n")) ;
1369*bf2c3715SXin Li
1370*bf2c3715SXin Li /* for each column in pivot row */
1371*bf2c3715SXin Li rp = &A [pivot_row_start] ;
1372*bf2c3715SXin Li /* compact the pivot row */
1373*bf2c3715SXin Li new_rp = rp ;
1374*bf2c3715SXin Li rp_end = rp + pivot_row_length ;
1375*bf2c3715SXin Li while (rp < rp_end)
1376*bf2c3715SXin Li {
1377*bf2c3715SXin Li col = *rp++ ;
1378*bf2c3715SXin Li /* skip dead columns */
1379*bf2c3715SXin Li if (Col[col].is_dead())
1380*bf2c3715SXin Li {
1381*bf2c3715SXin Li continue ;
1382*bf2c3715SXin Li }
1383*bf2c3715SXin Li *new_rp++ = col ;
1384*bf2c3715SXin Li /* add new pivot row to column */
1385*bf2c3715SXin Li A [Col [col].start + (Col [col].length++)] = pivot_row ;
1386*bf2c3715SXin Li
1387*bf2c3715SXin Li /* retrieve score so far and add on pivot row's degree. */
1388*bf2c3715SXin Li /* (we wait until here for this in case the pivot */
1389*bf2c3715SXin Li /* row's degree was reduced due to mass elimination). */
1390*bf2c3715SXin Li cur_score = Col [col].shared2.score + pivot_row_degree ;
1391*bf2c3715SXin Li
1392*bf2c3715SXin Li /* calculate the max possible score as the number of */
1393*bf2c3715SXin Li /* external columns minus the 'k' value minus the */
1394*bf2c3715SXin Li /* columns thickness */
1395*bf2c3715SXin Li max_score = n_col - k - Col [col].shared1.thickness ;
1396*bf2c3715SXin Li
1397*bf2c3715SXin Li /* make the score the external degree of the union-of-rows */
1398*bf2c3715SXin Li cur_score -= Col [col].shared1.thickness ;
1399*bf2c3715SXin Li
1400*bf2c3715SXin Li /* make sure score is less or equal than the max score */
1401*bf2c3715SXin Li cur_score = numext::mini(cur_score, max_score) ;
1402*bf2c3715SXin Li COLAMD_ASSERT (cur_score >= 0) ;
1403*bf2c3715SXin Li
1404*bf2c3715SXin Li /* store updated score */
1405*bf2c3715SXin Li Col [col].shared2.score = cur_score ;
1406*bf2c3715SXin Li
1407*bf2c3715SXin Li /* === Place column back in degree list ========================= */
1408*bf2c3715SXin Li
1409*bf2c3715SXin Li COLAMD_ASSERT (min_score >= 0) ;
1410*bf2c3715SXin Li COLAMD_ASSERT (min_score <= n_col) ;
1411*bf2c3715SXin Li COLAMD_ASSERT (cur_score >= 0) ;
1412*bf2c3715SXin Li COLAMD_ASSERT (cur_score <= n_col) ;
1413*bf2c3715SXin Li COLAMD_ASSERT (head [cur_score] >= Empty) ;
1414*bf2c3715SXin Li next_col = head [cur_score] ;
1415*bf2c3715SXin Li Col [col].shared4.degree_next = next_col ;
1416*bf2c3715SXin Li Col [col].shared3.prev = Empty ;
1417*bf2c3715SXin Li if (next_col != Empty)
1418*bf2c3715SXin Li {
1419*bf2c3715SXin Li Col [next_col].shared3.prev = col ;
1420*bf2c3715SXin Li }
1421*bf2c3715SXin Li head [cur_score] = col ;
1422*bf2c3715SXin Li
1423*bf2c3715SXin Li /* see if this score is less than current min */
1424*bf2c3715SXin Li min_score = numext::mini(min_score, cur_score) ;
1425*bf2c3715SXin Li
1426*bf2c3715SXin Li }
1427*bf2c3715SXin Li
1428*bf2c3715SXin Li /* === Resurrect the new pivot row ================================== */
1429*bf2c3715SXin Li
1430*bf2c3715SXin Li if (pivot_row_degree > 0)
1431*bf2c3715SXin Li {
1432*bf2c3715SXin Li /* update pivot row length to reflect any cols that were killed */
1433*bf2c3715SXin Li /* during super-col detection and mass elimination */
1434*bf2c3715SXin Li Row [pivot_row].start = pivot_row_start ;
1435*bf2c3715SXin Li Row [pivot_row].length = (IndexType) (new_rp - &A[pivot_row_start]) ;
1436*bf2c3715SXin Li Row [pivot_row].shared1.degree = pivot_row_degree ;
1437*bf2c3715SXin Li Row [pivot_row].shared2.mark = 0 ;
1438*bf2c3715SXin Li /* pivot row is no longer dead */
1439*bf2c3715SXin Li }
1440*bf2c3715SXin Li }
1441*bf2c3715SXin Li
1442*bf2c3715SXin Li /* === All principal columns have now been ordered ====================== */
1443*bf2c3715SXin Li
1444*bf2c3715SXin Li return (ngarbage) ;
1445*bf2c3715SXin Li }
1446*bf2c3715SXin Li
1447*bf2c3715SXin Li
1448*bf2c3715SXin Li /* ========================================================================== */
1449*bf2c3715SXin Li /* === order_children ======================================================= */
1450*bf2c3715SXin Li /* ========================================================================== */
1451*bf2c3715SXin Li
1452*bf2c3715SXin Li /*
1453*bf2c3715SXin Li The find_ordering routine has ordered all of the principal columns (the
1454*bf2c3715SXin Li representatives of the supercolumns). The non-principal columns have not
1455*bf2c3715SXin Li yet been ordered. This routine orders those columns by walking up the
1456*bf2c3715SXin Li parent tree (a column is a child of the column which absorbed it). The
1457*bf2c3715SXin Li final permutation vector is then placed in p [0 ... n_col-1], with p [0]
1458*bf2c3715SXin Li being the first column, and p [n_col-1] being the last. It doesn't look
1459*bf2c3715SXin Li like it at first glance, but be assured that this routine takes time linear
1460*bf2c3715SXin Li in the number of columns. Although not immediately obvious, the time
1461*bf2c3715SXin Li taken by this routine is O (n_col), that is, linear in the number of
1462*bf2c3715SXin Li columns. Not user-callable.
1463*bf2c3715SXin Li */
1464*bf2c3715SXin Li template <typename IndexType>
order_children(IndexType n_col,ColStructure<IndexType> Col[],IndexType p[])1465*bf2c3715SXin Li static inline void order_children
1466*bf2c3715SXin Li (
1467*bf2c3715SXin Li /* === Parameters ======================================================= */
1468*bf2c3715SXin Li
1469*bf2c3715SXin Li IndexType n_col, /* number of columns of A */
1470*bf2c3715SXin Li ColStructure<IndexType> Col [], /* of size n_col+1 */
1471*bf2c3715SXin Li IndexType p [] /* p [0 ... n_col-1] is the column permutation*/
1472*bf2c3715SXin Li )
1473*bf2c3715SXin Li {
1474*bf2c3715SXin Li /* === Local variables ================================================== */
1475*bf2c3715SXin Li
1476*bf2c3715SXin Li IndexType i ; /* loop counter for all columns */
1477*bf2c3715SXin Li IndexType c ; /* column index */
1478*bf2c3715SXin Li IndexType parent ; /* index of column's parent */
1479*bf2c3715SXin Li IndexType order ; /* column's order */
1480*bf2c3715SXin Li
1481*bf2c3715SXin Li /* === Order each non-principal column ================================== */
1482*bf2c3715SXin Li
1483*bf2c3715SXin Li for (i = 0 ; i < n_col ; i++)
1484*bf2c3715SXin Li {
1485*bf2c3715SXin Li /* find an un-ordered non-principal column */
1486*bf2c3715SXin Li COLAMD_ASSERT (col_is_dead(Col, i)) ;
1487*bf2c3715SXin Li if (!Col[i].is_dead_principal() && Col [i].shared2.order == Empty)
1488*bf2c3715SXin Li {
1489*bf2c3715SXin Li parent = i ;
1490*bf2c3715SXin Li /* once found, find its principal parent */
1491*bf2c3715SXin Li do
1492*bf2c3715SXin Li {
1493*bf2c3715SXin Li parent = Col [parent].shared1.parent ;
1494*bf2c3715SXin Li } while (!Col[parent].is_dead_principal()) ;
1495*bf2c3715SXin Li
1496*bf2c3715SXin Li /* now, order all un-ordered non-principal columns along path */
1497*bf2c3715SXin Li /* to this parent. collapse tree at the same time */
1498*bf2c3715SXin Li c = i ;
1499*bf2c3715SXin Li /* get order of parent */
1500*bf2c3715SXin Li order = Col [parent].shared2.order ;
1501*bf2c3715SXin Li
1502*bf2c3715SXin Li do
1503*bf2c3715SXin Li {
1504*bf2c3715SXin Li COLAMD_ASSERT (Col [c].shared2.order == Empty) ;
1505*bf2c3715SXin Li
1506*bf2c3715SXin Li /* order this column */
1507*bf2c3715SXin Li Col [c].shared2.order = order++ ;
1508*bf2c3715SXin Li /* collaps tree */
1509*bf2c3715SXin Li Col [c].shared1.parent = parent ;
1510*bf2c3715SXin Li
1511*bf2c3715SXin Li /* get immediate parent of this column */
1512*bf2c3715SXin Li c = Col [c].shared1.parent ;
1513*bf2c3715SXin Li
1514*bf2c3715SXin Li /* continue until we hit an ordered column. There are */
1515*bf2c3715SXin Li /* guaranteed not to be anymore unordered columns */
1516*bf2c3715SXin Li /* above an ordered column */
1517*bf2c3715SXin Li } while (Col [c].shared2.order == Empty) ;
1518*bf2c3715SXin Li
1519*bf2c3715SXin Li /* re-order the super_col parent to largest order for this group */
1520*bf2c3715SXin Li Col [parent].shared2.order = order ;
1521*bf2c3715SXin Li }
1522*bf2c3715SXin Li }
1523*bf2c3715SXin Li
1524*bf2c3715SXin Li /* === Generate the permutation ========================================= */
1525*bf2c3715SXin Li
1526*bf2c3715SXin Li for (c = 0 ; c < n_col ; c++)
1527*bf2c3715SXin Li {
1528*bf2c3715SXin Li p [Col [c].shared2.order] = c ;
1529*bf2c3715SXin Li }
1530*bf2c3715SXin Li }
1531*bf2c3715SXin Li
1532*bf2c3715SXin Li
1533*bf2c3715SXin Li /* ========================================================================== */
1534*bf2c3715SXin Li /* === detect_super_cols ==================================================== */
1535*bf2c3715SXin Li /* ========================================================================== */
1536*bf2c3715SXin Li
1537*bf2c3715SXin Li /*
1538*bf2c3715SXin Li Detects supercolumns by finding matches between columns in the hash buckets.
1539*bf2c3715SXin Li Check amongst columns in the set A [row_start ... row_start + row_length-1].
1540*bf2c3715SXin Li The columns under consideration are currently *not* in the degree lists,
1541*bf2c3715SXin Li and have already been placed in the hash buckets.
1542*bf2c3715SXin Li
1543*bf2c3715SXin Li The hash bucket for columns whose hash function is equal to h is stored
1544*bf2c3715SXin Li as follows:
1545*bf2c3715SXin Li
1546*bf2c3715SXin Li if head [h] is >= 0, then head [h] contains a degree list, so:
1547*bf2c3715SXin Li
1548*bf2c3715SXin Li head [h] is the first column in degree bucket h.
1549*bf2c3715SXin Li Col [head [h]].headhash gives the first column in hash bucket h.
1550*bf2c3715SXin Li
1551*bf2c3715SXin Li otherwise, the degree list is empty, and:
1552*bf2c3715SXin Li
1553*bf2c3715SXin Li -(head [h] + 2) is the first column in hash bucket h.
1554*bf2c3715SXin Li
1555*bf2c3715SXin Li For a column c in a hash bucket, Col [c].shared3.prev is NOT a "previous
1556*bf2c3715SXin Li column" pointer. Col [c].shared3.hash is used instead as the hash number
1557*bf2c3715SXin Li for that column. The value of Col [c].shared4.hash_next is the next column
1558*bf2c3715SXin Li in the same hash bucket.
1559*bf2c3715SXin Li
1560*bf2c3715SXin Li Assuming no, or "few" hash collisions, the time taken by this routine is
1561*bf2c3715SXin Li linear in the sum of the sizes (lengths) of each column whose score has
1562*bf2c3715SXin Li just been computed in the approximate degree computation.
1563*bf2c3715SXin Li Not user-callable.
1564*bf2c3715SXin Li */
1565*bf2c3715SXin Li template <typename IndexType>
detect_super_cols(ColStructure<IndexType> Col[],IndexType A[],IndexType head[],IndexType row_start,IndexType row_length)1566*bf2c3715SXin Li static void detect_super_cols
1567*bf2c3715SXin Li (
1568*bf2c3715SXin Li /* === Parameters ======================================================= */
1569*bf2c3715SXin Li
1570*bf2c3715SXin Li ColStructure<IndexType> Col [], /* of size n_col+1 */
1571*bf2c3715SXin Li IndexType A [], /* row indices of A */
1572*bf2c3715SXin Li IndexType head [], /* head of degree lists and hash buckets */
1573*bf2c3715SXin Li IndexType row_start, /* pointer to set of columns to check */
1574*bf2c3715SXin Li IndexType row_length /* number of columns to check */
1575*bf2c3715SXin Li )
1576*bf2c3715SXin Li {
1577*bf2c3715SXin Li /* === Local variables ================================================== */
1578*bf2c3715SXin Li
1579*bf2c3715SXin Li IndexType hash ; /* hash value for a column */
1580*bf2c3715SXin Li IndexType *rp ; /* pointer to a row */
1581*bf2c3715SXin Li IndexType c ; /* a column index */
1582*bf2c3715SXin Li IndexType super_c ; /* column index of the column to absorb into */
1583*bf2c3715SXin Li IndexType *cp1 ; /* column pointer for column super_c */
1584*bf2c3715SXin Li IndexType *cp2 ; /* column pointer for column c */
1585*bf2c3715SXin Li IndexType length ; /* length of column super_c */
1586*bf2c3715SXin Li IndexType prev_c ; /* column preceding c in hash bucket */
1587*bf2c3715SXin Li IndexType i ; /* loop counter */
1588*bf2c3715SXin Li IndexType *rp_end ; /* pointer to the end of the row */
1589*bf2c3715SXin Li IndexType col ; /* a column index in the row to check */
1590*bf2c3715SXin Li IndexType head_column ; /* first column in hash bucket or degree list */
1591*bf2c3715SXin Li IndexType first_col ; /* first column in hash bucket */
1592*bf2c3715SXin Li
1593*bf2c3715SXin Li /* === Consider each column in the row ================================== */
1594*bf2c3715SXin Li
1595*bf2c3715SXin Li rp = &A [row_start] ;
1596*bf2c3715SXin Li rp_end = rp + row_length ;
1597*bf2c3715SXin Li while (rp < rp_end)
1598*bf2c3715SXin Li {
1599*bf2c3715SXin Li col = *rp++ ;
1600*bf2c3715SXin Li if (Col[col].is_dead())
1601*bf2c3715SXin Li {
1602*bf2c3715SXin Li continue ;
1603*bf2c3715SXin Li }
1604*bf2c3715SXin Li
1605*bf2c3715SXin Li /* get hash number for this column */
1606*bf2c3715SXin Li hash = Col [col].shared3.hash ;
1607*bf2c3715SXin Li COLAMD_ASSERT (hash <= n_col) ;
1608*bf2c3715SXin Li
1609*bf2c3715SXin Li /* === Get the first column in this hash bucket ===================== */
1610*bf2c3715SXin Li
1611*bf2c3715SXin Li head_column = head [hash] ;
1612*bf2c3715SXin Li if (head_column > Empty)
1613*bf2c3715SXin Li {
1614*bf2c3715SXin Li first_col = Col [head_column].shared3.headhash ;
1615*bf2c3715SXin Li }
1616*bf2c3715SXin Li else
1617*bf2c3715SXin Li {
1618*bf2c3715SXin Li first_col = - (head_column + 2) ;
1619*bf2c3715SXin Li }
1620*bf2c3715SXin Li
1621*bf2c3715SXin Li /* === Consider each column in the hash bucket ====================== */
1622*bf2c3715SXin Li
1623*bf2c3715SXin Li for (super_c = first_col ; super_c != Empty ;
1624*bf2c3715SXin Li super_c = Col [super_c].shared4.hash_next)
1625*bf2c3715SXin Li {
1626*bf2c3715SXin Li COLAMD_ASSERT (Col [super_c].is_alive()) ;
1627*bf2c3715SXin Li COLAMD_ASSERT (Col [super_c].shared3.hash == hash) ;
1628*bf2c3715SXin Li length = Col [super_c].length ;
1629*bf2c3715SXin Li
1630*bf2c3715SXin Li /* prev_c is the column preceding column c in the hash bucket */
1631*bf2c3715SXin Li prev_c = super_c ;
1632*bf2c3715SXin Li
1633*bf2c3715SXin Li /* === Compare super_c with all columns after it ================ */
1634*bf2c3715SXin Li
1635*bf2c3715SXin Li for (c = Col [super_c].shared4.hash_next ;
1636*bf2c3715SXin Li c != Empty ; c = Col [c].shared4.hash_next)
1637*bf2c3715SXin Li {
1638*bf2c3715SXin Li COLAMD_ASSERT (c != super_c) ;
1639*bf2c3715SXin Li COLAMD_ASSERT (Col[c].is_alive()) ;
1640*bf2c3715SXin Li COLAMD_ASSERT (Col [c].shared3.hash == hash) ;
1641*bf2c3715SXin Li
1642*bf2c3715SXin Li /* not identical if lengths or scores are different */
1643*bf2c3715SXin Li if (Col [c].length != length ||
1644*bf2c3715SXin Li Col [c].shared2.score != Col [super_c].shared2.score)
1645*bf2c3715SXin Li {
1646*bf2c3715SXin Li prev_c = c ;
1647*bf2c3715SXin Li continue ;
1648*bf2c3715SXin Li }
1649*bf2c3715SXin Li
1650*bf2c3715SXin Li /* compare the two columns */
1651*bf2c3715SXin Li cp1 = &A [Col [super_c].start] ;
1652*bf2c3715SXin Li cp2 = &A [Col [c].start] ;
1653*bf2c3715SXin Li
1654*bf2c3715SXin Li for (i = 0 ; i < length ; i++)
1655*bf2c3715SXin Li {
1656*bf2c3715SXin Li /* the columns are "clean" (no dead rows) */
1657*bf2c3715SXin Li COLAMD_ASSERT ( cp1->is_alive() );
1658*bf2c3715SXin Li COLAMD_ASSERT ( cp2->is_alive() );
1659*bf2c3715SXin Li /* row indices will same order for both supercols, */
1660*bf2c3715SXin Li /* no gather scatter necessary */
1661*bf2c3715SXin Li if (*cp1++ != *cp2++)
1662*bf2c3715SXin Li {
1663*bf2c3715SXin Li break ;
1664*bf2c3715SXin Li }
1665*bf2c3715SXin Li }
1666*bf2c3715SXin Li
1667*bf2c3715SXin Li /* the two columns are different if the for-loop "broke" */
1668*bf2c3715SXin Li if (i != length)
1669*bf2c3715SXin Li {
1670*bf2c3715SXin Li prev_c = c ;
1671*bf2c3715SXin Li continue ;
1672*bf2c3715SXin Li }
1673*bf2c3715SXin Li
1674*bf2c3715SXin Li /* === Got it! two columns are identical =================== */
1675*bf2c3715SXin Li
1676*bf2c3715SXin Li COLAMD_ASSERT (Col [c].shared2.score == Col [super_c].shared2.score) ;
1677*bf2c3715SXin Li
1678*bf2c3715SXin Li Col [super_c].shared1.thickness += Col [c].shared1.thickness ;
1679*bf2c3715SXin Li Col [c].shared1.parent = super_c ;
1680*bf2c3715SXin Li Col[c].kill_non_principal() ;
1681*bf2c3715SXin Li /* order c later, in order_children() */
1682*bf2c3715SXin Li Col [c].shared2.order = Empty ;
1683*bf2c3715SXin Li /* remove c from hash bucket */
1684*bf2c3715SXin Li Col [prev_c].shared4.hash_next = Col [c].shared4.hash_next ;
1685*bf2c3715SXin Li }
1686*bf2c3715SXin Li }
1687*bf2c3715SXin Li
1688*bf2c3715SXin Li /* === Empty this hash bucket ======================================= */
1689*bf2c3715SXin Li
1690*bf2c3715SXin Li if (head_column > Empty)
1691*bf2c3715SXin Li {
1692*bf2c3715SXin Li /* corresponding degree list "hash" is not empty */
1693*bf2c3715SXin Li Col [head_column].shared3.headhash = Empty ;
1694*bf2c3715SXin Li }
1695*bf2c3715SXin Li else
1696*bf2c3715SXin Li {
1697*bf2c3715SXin Li /* corresponding degree list "hash" is empty */
1698*bf2c3715SXin Li head [hash] = Empty ;
1699*bf2c3715SXin Li }
1700*bf2c3715SXin Li }
1701*bf2c3715SXin Li }
1702*bf2c3715SXin Li
1703*bf2c3715SXin Li
1704*bf2c3715SXin Li /* ========================================================================== */
1705*bf2c3715SXin Li /* === garbage_collection =================================================== */
1706*bf2c3715SXin Li /* ========================================================================== */
1707*bf2c3715SXin Li
1708*bf2c3715SXin Li /*
1709*bf2c3715SXin Li Defragments and compacts columns and rows in the workspace A. Used when
1710*bf2c3715SXin Li all available memory has been used while performing row merging. Returns
1711*bf2c3715SXin Li the index of the first free position in A, after garbage collection. The
1712*bf2c3715SXin Li time taken by this routine is linear is the size of the array A, which is
1713*bf2c3715SXin Li itself linear in the number of nonzeros in the input matrix.
1714*bf2c3715SXin Li Not user-callable.
1715*bf2c3715SXin Li */
1716*bf2c3715SXin Li template <typename IndexType>
garbage_collection(IndexType n_row,IndexType n_col,RowStructure<IndexType> Row[],ColStructure<IndexType> Col[],IndexType A[],IndexType * pfree)1717*bf2c3715SXin Li static IndexType garbage_collection /* returns the new value of pfree */
1718*bf2c3715SXin Li (
1719*bf2c3715SXin Li /* === Parameters ======================================================= */
1720*bf2c3715SXin Li
1721*bf2c3715SXin Li IndexType n_row, /* number of rows */
1722*bf2c3715SXin Li IndexType n_col, /* number of columns */
1723*bf2c3715SXin Li RowStructure<IndexType> Row [], /* row info */
1724*bf2c3715SXin Li ColStructure<IndexType> Col [], /* column info */
1725*bf2c3715SXin Li IndexType A [], /* A [0 ... Alen-1] holds the matrix */
1726*bf2c3715SXin Li IndexType *pfree /* &A [0] ... pfree is in use */
1727*bf2c3715SXin Li )
1728*bf2c3715SXin Li {
1729*bf2c3715SXin Li /* === Local variables ================================================== */
1730*bf2c3715SXin Li
1731*bf2c3715SXin Li IndexType *psrc ; /* source pointer */
1732*bf2c3715SXin Li IndexType *pdest ; /* destination pointer */
1733*bf2c3715SXin Li IndexType j ; /* counter */
1734*bf2c3715SXin Li IndexType r ; /* a row index */
1735*bf2c3715SXin Li IndexType c ; /* a column index */
1736*bf2c3715SXin Li IndexType length ; /* length of a row or column */
1737*bf2c3715SXin Li
1738*bf2c3715SXin Li /* === Defragment the columns =========================================== */
1739*bf2c3715SXin Li
1740*bf2c3715SXin Li pdest = &A[0] ;
1741*bf2c3715SXin Li for (c = 0 ; c < n_col ; c++)
1742*bf2c3715SXin Li {
1743*bf2c3715SXin Li if (Col[c].is_alive())
1744*bf2c3715SXin Li {
1745*bf2c3715SXin Li psrc = &A [Col [c].start] ;
1746*bf2c3715SXin Li
1747*bf2c3715SXin Li /* move and compact the column */
1748*bf2c3715SXin Li COLAMD_ASSERT (pdest <= psrc) ;
1749*bf2c3715SXin Li Col [c].start = (IndexType) (pdest - &A [0]) ;
1750*bf2c3715SXin Li length = Col [c].length ;
1751*bf2c3715SXin Li for (j = 0 ; j < length ; j++)
1752*bf2c3715SXin Li {
1753*bf2c3715SXin Li r = *psrc++ ;
1754*bf2c3715SXin Li if (Row[r].is_alive())
1755*bf2c3715SXin Li {
1756*bf2c3715SXin Li *pdest++ = r ;
1757*bf2c3715SXin Li }
1758*bf2c3715SXin Li }
1759*bf2c3715SXin Li Col [c].length = (IndexType) (pdest - &A [Col [c].start]) ;
1760*bf2c3715SXin Li }
1761*bf2c3715SXin Li }
1762*bf2c3715SXin Li
1763*bf2c3715SXin Li /* === Prepare to defragment the rows =================================== */
1764*bf2c3715SXin Li
1765*bf2c3715SXin Li for (r = 0 ; r < n_row ; r++)
1766*bf2c3715SXin Li {
1767*bf2c3715SXin Li if (Row[r].is_alive())
1768*bf2c3715SXin Li {
1769*bf2c3715SXin Li if (Row [r].length == 0)
1770*bf2c3715SXin Li {
1771*bf2c3715SXin Li /* this row is of zero length. cannot compact it, so kill it */
1772*bf2c3715SXin Li COLAMD_DEBUG3 (("Defrag row kill\n")) ;
1773*bf2c3715SXin Li Row[r].kill() ;
1774*bf2c3715SXin Li }
1775*bf2c3715SXin Li else
1776*bf2c3715SXin Li {
1777*bf2c3715SXin Li /* save first column index in Row [r].shared2.first_column */
1778*bf2c3715SXin Li psrc = &A [Row [r].start] ;
1779*bf2c3715SXin Li Row [r].shared2.first_column = *psrc ;
1780*bf2c3715SXin Li COLAMD_ASSERT (Row[r].is_alive()) ;
1781*bf2c3715SXin Li /* flag the start of the row with the one's complement of row */
1782*bf2c3715SXin Li *psrc = ones_complement(r) ;
1783*bf2c3715SXin Li
1784*bf2c3715SXin Li }
1785*bf2c3715SXin Li }
1786*bf2c3715SXin Li }
1787*bf2c3715SXin Li
1788*bf2c3715SXin Li /* === Defragment the rows ============================================== */
1789*bf2c3715SXin Li
1790*bf2c3715SXin Li psrc = pdest ;
1791*bf2c3715SXin Li while (psrc < pfree)
1792*bf2c3715SXin Li {
1793*bf2c3715SXin Li /* find a negative number ... the start of a row */
1794*bf2c3715SXin Li if (*psrc++ < 0)
1795*bf2c3715SXin Li {
1796*bf2c3715SXin Li psrc-- ;
1797*bf2c3715SXin Li /* get the row index */
1798*bf2c3715SXin Li r = ones_complement(*psrc) ;
1799*bf2c3715SXin Li COLAMD_ASSERT (r >= 0 && r < n_row) ;
1800*bf2c3715SXin Li /* restore first column index */
1801*bf2c3715SXin Li *psrc = Row [r].shared2.first_column ;
1802*bf2c3715SXin Li COLAMD_ASSERT (Row[r].is_alive()) ;
1803*bf2c3715SXin Li
1804*bf2c3715SXin Li /* move and compact the row */
1805*bf2c3715SXin Li COLAMD_ASSERT (pdest <= psrc) ;
1806*bf2c3715SXin Li Row [r].start = (IndexType) (pdest - &A [0]) ;
1807*bf2c3715SXin Li length = Row [r].length ;
1808*bf2c3715SXin Li for (j = 0 ; j < length ; j++)
1809*bf2c3715SXin Li {
1810*bf2c3715SXin Li c = *psrc++ ;
1811*bf2c3715SXin Li if (Col[c].is_alive())
1812*bf2c3715SXin Li {
1813*bf2c3715SXin Li *pdest++ = c ;
1814*bf2c3715SXin Li }
1815*bf2c3715SXin Li }
1816*bf2c3715SXin Li Row [r].length = (IndexType) (pdest - &A [Row [r].start]) ;
1817*bf2c3715SXin Li
1818*bf2c3715SXin Li }
1819*bf2c3715SXin Li }
1820*bf2c3715SXin Li /* ensure we found all the rows */
1821*bf2c3715SXin Li COLAMD_ASSERT (debug_rows == 0) ;
1822*bf2c3715SXin Li
1823*bf2c3715SXin Li /* === Return the new value of pfree ==================================== */
1824*bf2c3715SXin Li
1825*bf2c3715SXin Li return ((IndexType) (pdest - &A [0])) ;
1826*bf2c3715SXin Li }
1827*bf2c3715SXin Li
1828*bf2c3715SXin Li
1829*bf2c3715SXin Li /* ========================================================================== */
1830*bf2c3715SXin Li /* === clear_mark =========================================================== */
1831*bf2c3715SXin Li /* ========================================================================== */
1832*bf2c3715SXin Li
1833*bf2c3715SXin Li /*
1834*bf2c3715SXin Li Clears the Row [].shared2.mark array, and returns the new tag_mark.
1835*bf2c3715SXin Li Return value is the new tag_mark. Not user-callable.
1836*bf2c3715SXin Li */
1837*bf2c3715SXin Li template <typename IndexType>
clear_mark(IndexType n_row,RowStructure<IndexType> Row[])1838*bf2c3715SXin Li static inline IndexType clear_mark /* return the new value for tag_mark */
1839*bf2c3715SXin Li (
1840*bf2c3715SXin Li /* === Parameters ======================================================= */
1841*bf2c3715SXin Li
1842*bf2c3715SXin Li IndexType n_row, /* number of rows in A */
1843*bf2c3715SXin Li RowStructure<IndexType> Row [] /* Row [0 ... n_row-1].shared2.mark is set to zero */
1844*bf2c3715SXin Li )
1845*bf2c3715SXin Li {
1846*bf2c3715SXin Li /* === Local variables ================================================== */
1847*bf2c3715SXin Li
1848*bf2c3715SXin Li IndexType r ;
1849*bf2c3715SXin Li
1850*bf2c3715SXin Li for (r = 0 ; r < n_row ; r++)
1851*bf2c3715SXin Li {
1852*bf2c3715SXin Li if (Row[r].is_alive())
1853*bf2c3715SXin Li {
1854*bf2c3715SXin Li Row [r].shared2.mark = 0 ;
1855*bf2c3715SXin Li }
1856*bf2c3715SXin Li }
1857*bf2c3715SXin Li return (1) ;
1858*bf2c3715SXin Li }
1859*bf2c3715SXin Li
1860*bf2c3715SXin Li } // namespace Colamd
1861*bf2c3715SXin Li
1862*bf2c3715SXin Li } // namespace internal
1863*bf2c3715SXin Li #endif
1864