Skip to content

Commit 7282aae

Browse files
Use AMD's public colamd API
Replace the remaining calls to unexported AMD internals with the supported high-level colamd interface. Reconstruct the structural CSC matrix, remove the ExplicitImports exemptions, and test the public ordering path. Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Claude <[email protected]> Claude-Session: https://chatgpt.com/codex/tasks/01a03a17-ad6f-7131-82fc-d0fd57ea6512
1 parent c31c2eb commit 7282aae

4 files changed

Lines changed: 27 additions & 76 deletions

File tree

‎ext/SparseColumnPivotedQRAMDExt.jl‎

Lines changed: 13 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
module SparseColumnPivotedQRAMDExt
22

33
using 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
1612
end
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.
3214
function 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))
8542
end
8643

8744
end # module

‎test/amd_extension_tests.jl‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using AMD
2+
using Random
3+
using SparseArrays
4+
using SparseColumnPivotedQR
5+
using Test
6+
7+
@testset "AMD ordering matches AMD.colamd" begin
8+
Random.seed!(14)
9+
m, n = 50, 40
10+
A = sprand(Float64, m, n, 0.1) + sparse(1:n, 1:n, ones(n), m, n)
11+
q = scpqr_analyze(A; ordering = :amd).q
12+
@test isperm(q)
13+
@test q == AMD.colamd(A)
14+
end

‎test/qa/qa.jl‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@ run_qa(
1616
ei_kwargs = (;
1717
all_qualified_accesses_are_public = (;
1818
ignore = (
19-
# AMD.jl exposes only the allocating high-level wrappers
20-
# (`colamd`, `amd`) as public API. The extension deliberately
21-
# drives the low-level colamd bindings so it can scatter the
22-
# CSR pattern straight into colamd's workspace with no
23-
# intermediate `SparseMatrixCSC`; these have no public spelling.
24-
:SS_Int, :colamd_l, :colamd_l_recommended, :colamd_set_defaults,
2519
# The extension's whole job is to fill in the host package's
2620
# AMD hooks. `Base.moduleroot` of an extension is the extension
2721
# itself, so ExplicitImports cannot see these as same-package

‎test/scpqr_tests.jl‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -366,20 +366,6 @@ end
366366
@test nnz_amd <= 4 * nnz_nat
367367
end
368368

369-
@testset "_amd_colperm matches AMD.colamd" begin
370-
# Drives the extension's colamd hook directly on the CSR pattern and
371-
# checks it against the public `AMD.colamd` on the equivalent CSC.
372-
Random.seed!(14)
373-
m, n = 50, 40
374-
A = sprand(Float64, m, n, 0.1) + sparse(1:n, 1:n, ones(n), m, n)
375-
At = sparse(transpose(A))
376-
perm = SparseColumnPivotedQR._amd_colperm(
377-
Int.(SparseArrays.getcolptr(At)), Int.(rowvals(At)), m, n
378-
)
379-
@test isperm(perm)
380-
@test perm == AMD.colamd(A)
381-
end
382-
383369
@testset "scpqr_refactor!" begin
384370
Random.seed!(13)
385371
n = 30

0 commit comments

Comments
 (0)