-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold_colamd.c
2596 lines (2165 loc) · 78.1 KB
/
old_colamd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*! \file
Copyright (c) 2003, The Regents of the University of California, through
Lawrence Berkeley National Laboratory (subject to receipt of any required
approvals from U.S. Dept. of Energy)
All rights reserved.
The source code is distributed under BSD license, see the file License.txt
at the top-level directory.
*/
/*! @file
* \brief An approximate minimum degree column ordering algorithm
*/
/* ========================================================================== */
/* === colamd - a sparse matrix column ordering algorithm =================== */
/* ========================================================================== */
/*
colamd: An approximate minimum degree column ordering algorithm.
Purpose:
Colamd computes a permutation Q such that the Cholesky factorization of
(AQ)'(AQ) has less fill-in and requires fewer floating point operations
than A'A. This also provides a good ordering for sparse partial
pivoting methods, P(AQ) = LU, where Q is computed prior to numerical
factorization, and P is computed during numerical factorization via
conventional partial pivoting with row interchanges. Colamd is the
column ordering method used in SuperLU, part of the ScaLAPACK library.
It is also available as user-contributed software for Matlab 5.2,
available from MathWorks, Inc. (http://www.mathworks.com). This
routine can be used in place of COLMMD in Matlab. By default, the \
and / operators in Matlab perform a column ordering (using COLMMD)
prior to LU factorization using sparse partial pivoting, in the
built-in Matlab LU(A) routine.
Authors:
The authors of the code itself are Stefan I. Larimore and Timothy A.
Davis ([email protected]), University of Florida. The algorithm was
developed in collaboration with John Gilbert, Xerox PARC, and Esmond
Ng, Oak Ridge National Laboratory.
Date:
August 3, 1998. Version 1.0.
Acknowledgements:
This work was supported by the National Science Foundation, under
grants DMS-9504974 and DMS-9803599.
Notice:
Copyright (c) 1998 by the University of Florida. All Rights Reserved.
THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY
EXPRESSED OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
Permission is hereby granted to use or copy this program for any
purpose, provided the above notices are retained on all copies.
User documentation of any code that uses this code must cite the
Authors, the Copyright, and "Used by permission." If this code is
accessible from within Matlab, then typing "help colamd" or "colamd"
(with no arguments) must cite the Authors. Permission to modify the
code and to distribute modified code is granted, provided the above
notices are retained, and a notice that the code was modified is
included with the above copyright notice. You must also retain the
Availability information below, of the original version.
This software is provided free of charge.
Availability:
This file is located at
http://www.cise.ufl.edu/~davis/colamd/colamd.c
The colamd.h file is required, located in the same directory.
The colamdmex.c file provides a Matlab interface for colamd.
The symamdmex.c file provides a Matlab interface for symamd, which is
a symmetric ordering based on this code, colamd.c. All codes are
purely ANSI C compliant (they use no Unix-specific routines, include
files, etc.).
*/
/* ========================================================================== */
/* === Description of user-callable routines ================================ */
/* ========================================================================== */
/*
Each user-callable routine (declared as PUBLIC) is briefly described below.
Refer to the comments preceding each routine for more details.
----------------------------------------------------------------------------
colamd_recommended:
----------------------------------------------------------------------------
Usage:
Alen = colamd_recommended (nnz, n_row, n_col) ;
Purpose:
Returns recommended value of Alen for use by colamd. Returns -1
if any input argument is negative.
Arguments:
int nnz ; Number of nonzeros in the matrix A. This must
be the same value as p [n_col] in the call to
colamd - otherwise you will get a wrong value
of the recommended memory to use.
int n_row ; Number of rows in the matrix A.
int n_col ; Number of columns in the matrix A.
----------------------------------------------------------------------------
colamd_set_defaults:
----------------------------------------------------------------------------
Usage:
colamd_set_defaults (knobs) ;
Purpose:
Sets the default parameters.
Arguments:
double knobs [COLAMD_KNOBS] ; Output only.
Rows with more than (knobs [COLAMD_DENSE_ROW] * n_col) entries
are removed prior to ordering. Columns with more than
(knobs [COLAMD_DENSE_COL] * n_row) entries are removed
prior to ordering, and placed last in the output column
ordering. Default values of these two knobs are both 0.5.
Currently, only knobs [0] and knobs [1] are used, but future
versions may use more knobs. If so, they will be properly set
to their defaults by the future version of colamd_set_defaults,
so that the code that calls colamd will not need to change,
assuming that you either use colamd_set_defaults, or pass a
(double *) NULL pointer as the knobs array to colamd.
----------------------------------------------------------------------------
colamd:
----------------------------------------------------------------------------
Usage:
colamd (n_row, n_col, Alen, A, p, knobs) ;
Purpose:
Computes a column ordering (Q) of A such that P(AQ)=LU or
(AQ)'AQ=LL' have less fill-in and require fewer floating point
operations than factorizing the unpermuted matrix A or A'A,
respectively.
Arguments:
int n_row ;
Number of rows in the matrix A.
Restriction: n_row >= 0.
Colamd returns FALSE if n_row is negative.
int n_col ;
Number of columns in the matrix A.
Restriction: n_col >= 0.
Colamd returns FALSE if n_col is negative.
int Alen ;
Restriction (see note):
Alen >= 2*nnz + 6*(n_col+1) + 4*(n_row+1) + n_col + COLAMD_STATS
Colamd returns FALSE if these conditions are not met.
Note: this restriction makes an modest assumption regarding
the size of the two typedef'd structures, below. We do,
however, guarantee that
Alen >= colamd_recommended (nnz, n_row, n_col)
will be sufficient.
int A [Alen] ; Input argument, stats on output.
A is an integer array of size Alen. Alen must be at least as
large as the bare minimum value given above, but this is very
low, and can result in excessive run time. For best
performance, we recommend that Alen be greater than or equal to
colamd_recommended (nnz, n_row, n_col), which adds
nnz/5 to the bare minimum value given above.
On input, the row indices of the entries in column c of the
matrix are held in A [(p [c]) ... (p [c+1]-1)]. The row indices
in a given column c need not be in ascending order, and
duplicate row indices may be be present. However, colamd will
work a little faster if both of these conditions are met
(Colamd puts the matrix into this format, if it finds that the
the conditions are not met).
The matrix is 0-based. That is, rows are in the range 0 to
n_row-1, and columns are in the range 0 to n_col-1. Colamd
returns FALSE if any row index is out of range.
The contents of A are modified during ordering, and are thus
undefined on output with the exception of a few statistics
about the ordering (A [0..COLAMD_STATS-1]):
A [0]: number of dense or empty rows ignored.
A [1]: number of dense or empty columns ignored (and ordered
last in the output permutation p)
A [2]: number of garbage collections performed.
A [3]: 0, if all row indices in each column were in sorted
order, and no duplicates were present.
1, otherwise (in which case colamd had to do more work)
Note that a row can become "empty" if it contains only
"dense" and/or "empty" columns, and similarly a column can
become "empty" if it only contains "dense" and/or "empty" rows.
Future versions may return more statistics in A, but the usage
of these 4 entries in A will remain unchanged.
int p [n_col+1] ; Both input and output argument.
p is an integer array of size n_col+1. On input, it holds the
"pointers" for the column form of the matrix A. Column c of
the matrix A is held in A [(p [c]) ... (p [c+1]-1)]. The first
entry, p [0], must be zero, and p [c] <= p [c+1] must hold
for all c in the range 0 to n_col-1. The value p [n_col] is
thus the total number of entries in the pattern of the matrix A.
Colamd returns FALSE if these conditions are not met.
On output, if colamd returns TRUE, the array p holds the column
permutation (Q, for P(AQ)=LU or (AQ)'(AQ)=LL'), where p [0] is
the first column index in the new ordering, and p [n_col-1] is
the last. That is, p [k] = j means that column j of A is the
kth pivot column, in AQ, where k is in the range 0 to n_col-1
(p [0] = j means that column j of A is the first column in AQ).
If colamd returns FALSE, then no permutation is returned, and
p is undefined on output.
double knobs [COLAMD_KNOBS] ; Input only.
See colamd_set_defaults for a description. If the knobs array
is not present (that is, if a (double *) NULL pointer is passed
in its place), then the default values of the parameters are
used instead.
*/
/* ========================================================================== */
/* === Include files ======================================================== */
/* ========================================================================== */
/* limits.h: the largest positive integer (INT_MAX) */
#include <limits.h>
/* colamd.h: knob array size, stats output size, and global prototypes */
#include "colamd.h"
/* ========================================================================== */
/* === Scaffolding code definitions ======================================== */
/* ========================================================================== */
/* Ensure that debugging is turned off: */
#ifndef NDEBUG
#define NDEBUG
#endif
/* assert.h: the assert macro (no debugging if NDEBUG is defined) */
#include <assert.h>
/*
Our "scaffolding code" philosophy: In our opinion, well-written library
code should keep its "debugging" code, and just normally have it turned off
by the compiler so as not to interfere with performance. This serves
several purposes:
(1) assertions act as comments to the reader, telling you what the code
expects at that point. All assertions will always be true (unless
there really is a bug, of course).
(2) leaving in the scaffolding code assists anyone who would like to modify
the code, or understand the algorithm (by reading the debugging output,
one can get a glimpse into what the code is doing).
(3) (gasp!) for actually finding bugs. This code has been heavily tested
and "should" be fully functional and bug-free ... but you never know...
To enable debugging, comment out the "#define NDEBUG" above. The code will
become outrageously slow when debugging is enabled. To control the level of
debugging output, set an environment variable D to 0 (little), 1 (some),
2, 3, or 4 (lots).
*/
/* ========================================================================== */
/* === Row and Column structures ============================================ */
/* ========================================================================== */
typedef struct ColInfo_struct
{
int start ; /* index for A of first row in this column, or DEAD */
/* if column is dead */
int length ; /* number of rows in this column */
union
{
int thickness ; /* number of original columns represented by this */
/* col, if the column is alive */
int parent ; /* parent in parent tree super-column structure, if */
/* the column is dead */
} shared1 ;
union
{
int score ; /* the score used to maintain heap, if col is alive */
int order ; /* pivot ordering of this column, if col is dead */
} shared2 ;
union
{
int headhash ; /* head of a hash bucket, if col is at the head of */
/* a degree list */
int hash ; /* hash value, if col is not in a degree list */
int prev ; /* previous column in degree list, if col is in a */
/* degree list (but not at the head of a degree list) */
} shared3 ;
union
{
int degree_next ; /* next column, if col is in a degree list */
int hash_next ; /* next column, if col is in a hash list */
} shared4 ;
} ColInfo ;
typedef struct RowInfo_struct
{
int start ; /* index for A of first col in this row */
int length ; /* number of principal columns in this row */
union
{
int degree ; /* number of principal & non-principal columns in row */
int p ; /* used as a row pointer in init_rows_cols () */
} shared1 ;
union
{
int mark ; /* for computing set differences and marking dead rows*/
int first_column ;/* first column in row (used in garbage collection) */
} shared2 ;
} RowInfo ;
/* ========================================================================== */
/* === Definitions ========================================================== */
/* ========================================================================== */
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define ONES_COMPLEMENT(r) (-(r)-1)
#define TRUE (1)
#define FALSE (0)
#define EMPTY (-1)
/* Row and column status */
#define ALIVE (0)
#define DEAD (-1)
/* Column status */
#define DEAD_PRINCIPAL (-1)
#define DEAD_NON_PRINCIPAL (-2)
/* Macros for row and column status update and checking. */
#define ROW_IS_DEAD(r) ROW_IS_MARKED_DEAD (Row[r].shared2.mark)
#define ROW_IS_MARKED_DEAD(row_mark) (row_mark < ALIVE)
#define ROW_IS_ALIVE(r) (Row [r].shared2.mark >= ALIVE)
#define COL_IS_DEAD(c) (Col [c].start < ALIVE)
#define COL_IS_ALIVE(c) (Col [c].start >= ALIVE)
#define COL_IS_DEAD_PRINCIPAL(c) (Col [c].start == DEAD_PRINCIPAL)
#define KILL_ROW(r) { Row [r].shared2.mark = DEAD ; }
#define KILL_PRINCIPAL_COL(c) { Col [c].start = DEAD_PRINCIPAL ; }
#define KILL_NON_PRINCIPAL_COL(c) { Col [c].start = DEAD_NON_PRINCIPAL ; }
/* Routines are either PUBLIC (user-callable) or PRIVATE (not user-callable) */
#define PUBLIC
#define PRIVATE static
/* ========================================================================== */
/* === Prototypes of PRIVATE routines ======================================= */
/* ========================================================================== */
PRIVATE int init_rows_cols
(
int n_row,
int n_col,
RowInfo Row [],
ColInfo Col [],
int A [],
int p []
) ;
PRIVATE void init_scoring
(
int n_row,
int n_col,
RowInfo Row [],
ColInfo Col [],
int A [],
int head [],
double knobs [COLAMD_KNOBS],
int *p_n_row2,
int *p_n_col2,
int *p_max_deg
) ;
PRIVATE int find_ordering
(
int n_row,
int n_col,
int Alen,
RowInfo Row [],
ColInfo Col [],
int A [],
int head [],
int n_col2,
int max_deg,
int pfree
) ;
PRIVATE void order_children
(
int n_col,
ColInfo Col [],
int p []
) ;
PRIVATE void detect_super_cols
(
#ifndef NDEBUG
int n_col,
RowInfo Row [],
#endif
ColInfo Col [],
int A [],
int head [],
int row_start,
int row_length
) ;
PRIVATE int garbage_collection
(
int n_row,
int n_col,
RowInfo Row [],
ColInfo Col [],
int A [],
int *pfree
) ;
PRIVATE int clear_mark
(
int n_row,
RowInfo Row []
) ;
/* ========================================================================== */
/* === Debugging definitions ================================================ */
/* ========================================================================== */
#ifndef NDEBUG
/* === With debugging ======================================================= */
/* stdlib.h: for getenv and atoi, to get debugging level from environment */
#include <stdlib.h>
/* stdio.h: for printf (no printing if debugging is turned off) */
#include <stdio.h>
PRIVATE void debug_deg_lists
(
int n_row,
int n_col,
RowInfo Row [],
ColInfo Col [],
int head [],
int min_score,
int should,
int max_deg
) ;
PRIVATE void debug_mark
(
int n_row,
RowInfo Row [],
int tag_mark,
int max_mark
) ;
PRIVATE void debug_matrix
(
int n_row,
int n_col,
RowInfo Row [],
ColInfo Col [],
int A []
) ;
PRIVATE void debug_structures
(
int n_row,
int n_col,
RowInfo Row [],
ColInfo Col [],
int A [],
int n_col2
) ;
/* the following is the *ONLY* global variable in this file, and is only */
/* present when debugging */
PRIVATE int debug_colamd ; /* debug print level */
#define DEBUG0(params) { (void) printf params ; }
#define DEBUG1(params) { if (debug_colamd >= 1) (void) printf params ; }
#define DEBUG2(params) { if (debug_colamd >= 2) (void) printf params ; }
#define DEBUG3(params) { if (debug_colamd >= 3) (void) printf params ; }
#define DEBUG4(params) { if (debug_colamd >= 4) (void) printf params ; }
#else
/* === No debugging ========================================================= */
#define DEBUG0(params) ;
#define DEBUG1(params) ;
#define DEBUG2(params) ;
#define DEBUG3(params) ;
#define DEBUG4(params) ;
#endif
/* ========================================================================== */
/* ========================================================================== */
/* === USER-CALLABLE ROUTINES: ============================================== */
/* ========================================================================== */
/* ========================================================================== */
/* === colamd_recommended =================================================== */
/* ========================================================================== */
/*
The colamd_recommended routine returns the suggested size for Alen. This
value has been determined to provide good balance between the number of
garbage collections and the memory requirements for colamd.
*/
PUBLIC int colamd_recommended /* returns recommended value of Alen. */
(
/* === Parameters ======================================================= */
int nnz, /* number of nonzeros in A */
int n_row, /* number of rows in A */
int n_col /* number of columns in A */
)
{
/* === Local variables ================================================== */
int minimum ; /* bare minimum requirements */
int recommended ; /* recommended value of Alen */
if (nnz < 0 || n_row < 0 || n_col < 0)
{
/* return -1 if any input argument is corrupted */
DEBUG0 (("colamd_recommended error!")) ;
DEBUG0 ((" nnz: %d, n_row: %d, n_col: %d\n", nnz, n_row, n_col)) ;
return (-1) ;
}
minimum =
2 * (nnz) /* for A */
+ (((n_col) + 1) * sizeof (ColInfo) / sizeof (int)) /* for Col */
+ (((n_row) + 1) * sizeof (RowInfo) / sizeof (int)) /* for Row */
+ n_col /* minimum elbow room to guarrantee success */
+ COLAMD_STATS ; /* for output statistics */
/* recommended is equal to the minimum plus enough memory to keep the */
/* number garbage collections low */
recommended = minimum + nnz/5 ;
return (recommended) ;
}
/* ========================================================================== */
/* === colamd_set_defaults ================================================== */
/* ========================================================================== */
/*
The colamd_set_defaults routine sets the default values of the user-
controllable parameters for colamd:
knobs [0] rows with knobs[0]*n_col entries or more are removed
prior to ordering.
knobs [1] columns with knobs[1]*n_row entries or more are removed
prior to ordering, and placed last in the column
permutation.
knobs [2..19] unused, but future versions might use this
*/
PUBLIC void colamd_set_defaults
(
/* === Parameters ======================================================= */
double knobs [COLAMD_KNOBS] /* knob array */
)
{
/* === Local variables ================================================== */
int i ;
if (!knobs)
{
return ; /* no knobs to initialize */
}
for (i = 0 ; i < COLAMD_KNOBS ; i++)
{
knobs [i] = 0 ;
}
knobs [COLAMD_DENSE_ROW] = 0.5 ; /* ignore rows over 50% dense */
knobs [COLAMD_DENSE_COL] = 0.5 ; /* ignore columns over 50% dense */
}
/* ========================================================================== */
/* === colamd =============================================================== */
/* ========================================================================== */
/*
The colamd routine computes a column ordering Q of a sparse matrix
A such that the LU factorization P(AQ) = LU remains sparse, where P is
selected via partial pivoting. The routine can also be viewed as
providing a permutation Q such that the Cholesky factorization
(AQ)'(AQ) = LL' remains sparse.
On input, the nonzero patterns of the columns of A are stored in the
array A, in order 0 to n_col-1. A is held in 0-based form (rows in the
range 0 to n_row-1 and columns in the range 0 to n_col-1). Row indices
for column c are located in A [(p [c]) ... (p [c+1]-1)], where p [0] = 0,
and thus p [n_col] is the number of entries in A. The matrix is
destroyed on output. The row indices within each column do not have to
be sorted (from small to large row indices), and duplicate row indices
may be present. However, colamd will work a little faster if columns are
sorted and no duplicates are present. Matlab 5.2 always passes the matrix
with sorted columns, and no duplicates.
The integer array A is of size Alen. Alen must be at least of size
(where nnz is the number of entries in A):
nnz for the input column form of A
+ nnz for a row form of A that colamd generates
+ 6*(n_col+1) for a ColInfo Col [0..n_col] array
(this assumes sizeof (ColInfo) is 6 int's).
+ 4*(n_row+1) for a RowInfo Row [0..n_row] array
(this assumes sizeof (RowInfo) is 4 int's).
+ elbow_room must be at least n_col. We recommend at least
nnz/5 in addition to that. If sufficient,
changes in the elbow room affect the ordering
time only, not the ordering itself.
+ COLAMD_STATS for the output statistics
Colamd returns FALSE is memory is insufficient, or TRUE otherwise.
On input, the caller must specify:
n_row the number of rows of A
n_col the number of columns of A
Alen the size of the array A
A [0 ... nnz-1] the row indices, where nnz = p [n_col]
A [nnz ... Alen-1] (need not be initialized by the user)
p [0 ... n_col] the column pointers, p [0] = 0, and p [n_col]
is the number of entries in A. Column c of A
is stored in A [p [c] ... p [c+1]-1].
knobs [0 ... 19] a set of parameters that control the behavior
of colamd. If knobs is a NULL pointer the
defaults are used. The user-callable
colamd_set_defaults routine sets the default
parameters. See that routine for a description
of the user-controllable parameters.
If the return value of Colamd is TRUE, then on output:
p [0 ... n_col-1] the column permutation. p [0] is the first
column index, and p [n_col-1] is the last.
That is, p [k] = j means that column j of A
is the kth column of AQ.
A is undefined on output (the matrix pattern is
destroyed), except for the following statistics:
A [0] the number of dense (or empty) rows ignored
A [1] the number of dense (or empty) columms. These
are ordered last, in their natural order.
A [2] the number of garbage collections performed.
If this is excessive, then you would have
gotten your results faster if Alen was larger.
A [3] 0, if all row indices in each column were in
sorted order and no duplicates were present.
1, if there were unsorted or duplicate row
indices in the input. You would have gotten
your results faster if A [3] was returned as 0.
If the return value of Colamd is FALSE, then A and p are undefined on
output.
*/
PUBLIC int colamd /* returns TRUE if successful */
(
/* === Parameters ======================================================= */
int n_row, /* number of rows in A */
int n_col, /* number of columns in A */
int Alen, /* length of A */
int A [], /* row indices of A */
int p [], /* pointers to columns in A */
double knobs [COLAMD_KNOBS] /* parameters (uses defaults if NULL) */
)
{
/* === Local variables ================================================== */
int i ; /* loop index */
int nnz ; /* nonzeros in A */
int Row_size ; /* size of Row [], in integers */
int Col_size ; /* size of Col [], in integers */
int elbow_room ; /* remaining free space */
RowInfo *Row ; /* pointer into A of Row [0..n_row] array */
ColInfo *Col ; /* pointer into A of Col [0..n_col] array */
int n_col2 ; /* number of non-dense, non-empty columns */
int n_row2 ; /* number of non-dense, non-empty rows */
int ngarbage ; /* number of garbage collections performed */
int max_deg ; /* maximum row degree */
double default_knobs [COLAMD_KNOBS] ; /* default knobs knobs array */
int init_result ; /* return code from initialization */
#ifndef NDEBUG
debug_colamd = 0 ; /* no debug printing */
/* get "D" environment variable, which gives the debug printing level */
if (getenv ("D")) debug_colamd = atoi (getenv ("D")) ;
DEBUG0 (("debug version, D = %d (THIS WILL BE SLOOOOW!)\n", debug_colamd)) ;
#endif
/* === Check the input arguments ======================================== */
if (n_row < 0 || n_col < 0 || !A || !p)
{
/* n_row and n_col must be non-negative, A and p must be present */
DEBUG0 (("colamd error! %d %d %d\n", n_row, n_col, Alen)) ;
return (FALSE) ;
}
nnz = p [n_col] ;
if (nnz < 0 || p [0] != 0)
{
/* nnz must be non-negative, and p [0] must be zero */
DEBUG0 (("colamd error! %d %d\n", nnz, p [0])) ;
return (FALSE) ;
}
/* === If no knobs, set default parameters ============================== */
if (!knobs)
{
knobs = default_knobs ;
colamd_set_defaults (knobs) ;
}
/* === Allocate the Row and Col arrays from array A ===================== */
Col_size = (n_col + 1) * sizeof (ColInfo) / sizeof (int) ;
Row_size = (n_row + 1) * sizeof (RowInfo) / sizeof (int) ;
elbow_room = Alen - (2*nnz + Col_size + Row_size) ;
if (elbow_room < n_col + COLAMD_STATS)
{
/* not enough space in array A to perform the ordering */
DEBUG0 (("colamd error! elbow_room %d, %d\n", elbow_room,n_col)) ;
return (FALSE) ;
}
Alen = 2*nnz + elbow_room ;
Col = (ColInfo *) &A [Alen] ;
Row = (RowInfo *) &A [Alen + Col_size] ;
/* === Construct the row and column data structures ===================== */
init_result = init_rows_cols (n_row, n_col, Row, Col, A, p) ;
if (init_result == -1)
{
/* input matrix is invalid */
DEBUG0 (("colamd error! matrix invalid\n")) ;
return (FALSE) ;
}
/* === Initialize scores, kill dense rows/columns ======================= */
init_scoring (n_row, n_col, Row, Col, A, p, knobs,
&n_row2, &n_col2, &max_deg) ;
/* === Order the supercolumns =========================================== */
ngarbage = find_ordering (n_row, n_col, Alen, Row, Col, A, p,
n_col2, max_deg, 2*nnz) ;
/* === Order the non-principal columns ================================== */
order_children (n_col, Col, p) ;
/* === Return statistics in A =========================================== */
for (i = 0 ; i < COLAMD_STATS ; i++)
{
A [i] = 0 ;
}
A [COLAMD_DENSE_ROW] = n_row - n_row2 ;
A [COLAMD_DENSE_COL] = n_col - n_col2 ;
A [COLAMD_DEFRAG_COUNT] = ngarbage ;
A [COLAMD_JUMBLED_COLS] = init_result ;
return (TRUE) ;
}
/* ========================================================================== */
/* === NON-USER-CALLABLE ROUTINES: ========================================== */
/* ========================================================================== */
/* There are no user-callable routines beyond this point in the file */
/* ========================================================================== */
/* === init_rows_cols ======================================================= */
/* ========================================================================== */
/*
Takes the column form of the matrix in A and creates the row form of the
matrix. Also, row and column attributes are stored in the Col and Row
structs. If the columns are un-sorted or contain duplicate row indices,
this routine will also sort and remove duplicate row indices from the
column form of the matrix. Returns -1 on error, 1 if columns jumbled,
or 0 if columns not jumbled. Not user-callable.
*/
PRIVATE int init_rows_cols /* returns status code */
(
/* === Parameters ======================================================= */
int n_row, /* number of rows of A */
int n_col, /* number of columns of A */
RowInfo Row [], /* of size n_row+1 */
ColInfo Col [], /* of size n_col+1 */
int A [], /* row indices of A, of size Alen */
int p [] /* pointers to columns in A, of size n_col+1 */
)
{
/* === Local variables ================================================== */
int col ; /* a column index */
int row ; /* a row index */
int *cp ; /* a column pointer */
int *cp_end ; /* a pointer to the end of a column */
int *rp ; /* a row pointer */
int *rp_end ; /* a pointer to the end of a row */
int last_start ; /* start index of previous column in A */
int start ; /* start index of column in A */
int last_row ; /* previous row */
int jumbled_columns ; /* indicates if columns are jumbled */
/* === Initialize columns, and check column pointers ==================== */
last_start = 0 ;
for (col = 0 ; col < n_col ; col++)
{
start = p [col] ;
if (start < last_start)
{
/* column pointers must be non-decreasing */
DEBUG0 (("colamd error! last p %d p [col] %d\n",last_start,start));
return (-1) ;
}
Col [col].start = start ;
Col [col].length = p [col+1] - start ;
Col [col].shared1.thickness = 1 ;
Col [col].shared2.score = 0 ;
Col [col].shared3.prev = EMPTY ;
Col [col].shared4.degree_next = EMPTY ;
last_start = start ;
}
/* must check the end pointer for last column */
if (p [n_col] < last_start)
{
/* column pointers must be non-decreasing */
DEBUG0 (("colamd error! last p %d p [n_col] %d\n",p[col],last_start)) ;
return (-1) ;
}
/* p [0..n_col] no longer needed, used as "head" in subsequent routines */
/* === Scan columns, compute row degrees, and check row indices ========= */
jumbled_columns = FALSE ;
for (row = 0 ; row < n_row ; row++)
{
Row [row].length = 0 ;
Row [row].shared2.mark = -1 ;
}
for (col = 0 ; col < n_col ; col++)
{
last_row = -1 ;
cp = &A [p [col]] ;
cp_end = &A [p [col+1]] ;
while (cp < cp_end)
{
row = *cp++ ;
/* make sure row indices within range */
if (row < 0 || row >= n_row)
{
DEBUG0 (("colamd error! col %d row %d last_row %d\n",
col, row, last_row)) ;
return (-1) ;
}
else if (row <= last_row)
{
/* row indices are not sorted or repeated, thus cols */
/* are jumbled */
jumbled_columns = TRUE ;
}
/* prevent repeated row from being counted */
if (Row [row].shared2.mark != col)
{
Row [row].length++ ;
Row [row].shared2.mark = col ;
last_row = row ;
}
else
{
/* this is a repeated entry in the column, */
/* it will be removed */
Col [col].length-- ;
}
}
}
/* === Compute row pointers ============================================= */
/* row form of the matrix starts directly after the column */
/* form of matrix in A */
Row [0].start = p [n_col] ;
Row [0].shared1.p = Row [0].start ;
Row [0].shared2.mark = -1 ;
for (row = 1 ; row < n_row ; row++)
{
Row [row].start = Row [row-1].start + Row [row-1].length ;
Row [row].shared1.p = Row [row].start ;
Row [row].shared2.mark = -1 ;
}
/* === Create row form ================================================== */
if (jumbled_columns)
{
/* if cols jumbled, watch for repeated row indices */
for (col = 0 ; col < n_col ; col++)
{
cp = &A [p [col]] ;
cp_end = &A [p [col+1]] ;
while (cp < cp_end)
{
row = *cp++ ;
if (Row [row].shared2.mark != col)
{
A [(Row [row].shared1.p)++] = col ;
Row [row].shared2.mark = col ;
}
}
}
}
else
{
/* if cols not jumbled, we don't need the mark (this is faster) */
for (col = 0 ; col < n_col ; col++)
{
cp = &A [p [col]] ;
cp_end = &A [p [col+1]] ;
while (cp < cp_end)