Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace ColPack with SparseMatrixColorings #244

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ version = "0.8.0"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
ColPack = "ffa27691-3a59-46ab-a8d4-551f45b8d401"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
NLPModels = "a4795742-8479-5a88-8948-cc11e1c8c1a6"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
SparseConnectivityTracer = "9f842d2f-2579-4b1d-911e-f412cf18a3f5"
SparseMatrixColorings = "0a514795-09f3-496d-8182-132a7b665d35"

[compat]
ADTypes = "1.2.1"
ColPack = "0.4"
SparseConnectivityTracer = "0.5"
ForwardDiff = "0.9.0, 0.10.0"
NLPModels = "0.18, 0.19, 0.20, 0.21"
Requires = "1"
ReverseDiff = "1"
SparseConnectivityTracer = "0.5"
SparseMatrixColorings = "0.3.3"
julia = "^1.6"
6 changes: 4 additions & 2 deletions src/ADNLPModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ module ADNLPModels
using LinearAlgebra, SparseArrays

# external
using ADTypes: ADTypes, AbstractSparsityDetector
using SparseConnectivityTracer, ColPack, ForwardDiff, ReverseDiff
using ADTypes: ADTypes, AbstractColoringAlgorithm, AbstractSparsityDetector
using SparseConnectivityTracer: TracerSparsityDetector
using SparseMatrixColorings: GreedyColoringAlgorithm
using ForwardDiff, ReverseDiff

# JSO
using NLPModels
Expand Down
33 changes: 0 additions & 33 deletions src/ad.jl
Original file line number Diff line number Diff line change
Expand Up @@ -499,36 +499,3 @@ function ADModelNLSBackend(
hessian_residual_backend,
)
end

abstract type ColorationAlgorithm end

struct ColPackColoration{F, C, O} <: ColorationAlgorithm
partition_choice::F
coloring::C
ordering::O
end

function ColPackColoration(;
partition_choice = (m, n) -> false, # TODO: (m, n; μ = 0.6) -> n < μ * m ? true : false,
coloring::ColPack.ColoringMethod = d1_coloring(),
ordering::ColPack.ColoringOrder = incidence_degree_ordering(),
)
return ColPackColoration{typeof(partition_choice), typeof(coloring), typeof(ordering)}(
partition_choice,
coloring,
ordering,
)
end

function sparse_matrix_colors(A, alg::ColPackColoration)
m, n = size(A)
partition_by_rows = alg.partition_choice(m, n)
if !isempty(A.nzval)
adjA = ColPack.matrix2adjmatrix(A; partition_by_rows = partition_by_rows)
CPC = ColPackColoring(adjA, alg.coloring, alg.ordering)
colors = get_colors(CPC)
else
colors = zeros(Int, n)
amontoison marked this conversation as resolved.
Show resolved Hide resolved
end
return colors
end
10 changes: 6 additions & 4 deletions src/sparse_hessian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ function SparseADHessian(
ncon,
c!;
x0::S = rand(nvar),
alg = ColPackColoration(),
alg::AbstractColoringAlgorithm = GreedyColoringAlgorithm(),
detector::AbstractSparsityDetector = TracerSparsityDetector(),
kwargs...,
) where {S}
T = eltype(S)
H = compute_hessian_sparsity(f, nvar, c!, ncon, detector = detector)

colors = sparse_matrix_colors(H, alg)
# TODO: use ADTypes.symmetric_coloring instead if you have the right decompression
gdalle marked this conversation as resolved.
Show resolved Hide resolved
colors = ADTypes.column_coloring(H, alg)
gdalle marked this conversation as resolved.
Show resolved Hide resolved
ncolors = maximum(colors)

d = BitVector(undef, nvar)
Expand Down Expand Up @@ -92,13 +93,14 @@ function SparseReverseADHessian(
ncon,
c!;
x0::AbstractVector{T} = rand(nvar),
alg = ColPackColoration(),
alg::AbstractColoringAlgorithm = GreedyColoringAlgorithm(),
detector::AbstractSparsityDetector = TracerSparsityDetector(),
kwargs...,
) where {T}
H = compute_hessian_sparsity(f, nvar, c!, ncon, detector = detector)

colors = sparse_matrix_colors(H, alg)
# TODO: use ADTypes.symmetric_coloring instead if you have the right decompression
colors = ADTypes.column_coloring(H, alg)
ncolors = maximum(colors)

d = BitVector(undef, nvar)
Expand Down
5 changes: 3 additions & 2 deletions src/sparse_jacobian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ function SparseADJacobian(
ncon,
c!;
x0::AbstractVector{T} = rand(nvar),
alg = ColPackColoration(),
alg::AbstractColoringAlgorithm = GreedyColoringAlgorithm(),
detector::AbstractSparsityDetector = TracerSparsityDetector(),
kwargs...,
) where {T}
output = similar(x0, ncon)
J = compute_jacobian_sparsity(c!, output, x0, detector = detector)

colors = sparse_matrix_colors(J, alg)
# TODO: use ADTypes.row_coloring instead if you have the right decompression and some heuristic recommends it
colors = ADTypes.column_coloring(J, alg)
ncolors = maximum(colors)

d = BitVector(undef, nvar)
Expand Down
Loading