11module SparseColumnPivotedQRAMDExt
22
33using SparseColumnPivotedQR: SparseColumnPivotedQR
4- using AMD: AMD
5-
6- # Integer type of AMD.jl's `_l` (long) colamd wrappers. AMD.jl < 0.5.4 exposed
7- # it as `AMD.SS_Int`; 0.5.4 removed that constant and hardcodes `Int64` to match
8- # SuiteSparse >= 6, where `SuiteSparse_long` is `int64_t` on every platform.
9- const SS = isdefined (AMD, :SS_Int ) ? AMD. SS_Int : Int64
4+ using AMD: colamd
5+ using SparseArrays: SparseMatrixCSC
106
117# Flag the host module so `:default` ordering resolves to `:amd` and so the
128# `:amd` opt-in doesn't error. Set on extension load, never cleared.
@@ -15,73 +11,34 @@ function __init__()
1511 return nothing
1612end
1713
18- # Override the AMD column ordering hook in SparseColumnPivotedQR.
19- #
20- # `colamd_l` needs only the structural pattern (no numeric values), in 0-based
21- # CSC layout: `p` = column pointers (length n+1) and a workspace array whose
22- # leading `nnz` entries are the row indices in column-major order, padded out
23- # to `colamd_l_recommended(nnz, m, n)` (colamd uses the slack in place).
24- #
25- # We have the CSR pattern (rowptr, colval). Rather than materialize a full
26- # `SparseMatrixCSC` with a dummy `nzval` vector (which `colamd` never reads),
27- # we scatter the CSR pattern directly into colamd's workspace in CSC order.
28- # This avoids the `ones(Float64, nnz)` value buffer, the `Int.(p)` output copy,
29- # and AMD.jl's internal `p .- 1` / `workspace .- 1` temporaries.
30- #
31- # Falls back to natural ordering if AMD reports failure for any reason.
3214function SparseColumnPivotedQR. _amd_colperm (
3315 rowptr:: Vector{Int} , colval:: Vector{Int} ,
3416 m:: Int , n:: Int
3517 )
3618 nnz_total = length (colval)
37-
38- # 0-based CSC column pointers in `p` (length n+1).
39- p = Vector {SS} (undef, n + 1 )
40- @inbounds for j in 1 : (n + 1 )
41- p[j] = zero (SS)
42- end
19+ colcounts = zeros (Int, n)
4320 @inbounds for q in 1 : nnz_total
44- p [colval[q] + 1 ] += one (SS) # count per column (shifted by one)
21+ colcounts [colval[q]] += 1
4522 end
23+ colptr = Vector {Int} (undef, n + 1 )
24+ colptr[1 ] = 1
4625 @inbounds for j in 1 : n
47- p [j + 1 ] += p [j] # prefix sum -> 0-based colptr
26+ colptr [j + 1 ] = colptr [j] + colcounts[j]
4827 end
4928
50- # Workspace: leading `nnz` entries are 0-based row indices in CSC order,
51- # padded to the recommended length. colamd overwrites/uses the tail.
52- len = AMD. colamd_l_recommended (SS (nnz_total), SS (m), SS (n))
53- workspace = Vector {SS} (undef, len)
54- # Running write cursor per column (CSC starts), kept in a small scratch.
55- cursor = Vector {Int} (undef, n)
56- @inbounds for j in 1 : n
57- cursor[j] = p[j] # 0-based start offset of column j
58- end
29+ rowidx = Vector {Int} (undef, nnz_total)
30+ work = copy (colptr)
5931 @inbounds for i in 1 : m
6032 r1 = rowptr[i]; r2 = rowptr[i + 1 ] - 1
6133 for q in r1: r2
6234 j = colval[q]
63- workspace[cursor [j] + 1 ] = SS (i - 1 ) # 0-based row index
64- cursor [j] += 1
35+ rowidx[work [j]] = i
36+ work [j] += 1
6537 end
6638 end
67- @inbounds for q in (nnz_total + 1 ): len
68- workspace[q] = zero (SS)
69- end
7039
71- meta = AMD. Colamd {SS} ()
72- AMD. colamd_set_defaults (meta. knobs)
73- valid = AMD. colamd_l (SS (m), SS (n), SS (len), workspace, p, meta. knobs, meta. stats)
74- if ! Bool (valid)
75- return collect (1 : n)
76- end
77- # colamd writes the column permutation (0-based) into p[1:n]; convert to
78- # a 1-based `Vector{Int}` (SS == Int on 64-bit, but keep the explicit
79- # conversion so this stays correct when SS and Int differ).
80- perm = Vector {Int} (undef, n)
81- @inbounds for k in 1 : n
82- perm[k] = Int (p[k]) + 1
83- end
84- return perm
40+ pattern = SparseMatrixCSC (m, n, colptr, rowidx, ones (Bool, nnz_total))
41+ return Int .(colamd (pattern))
8542end
8643
8744end # module
0 commit comments