diff --git a/Project.toml b/Project.toml index b7ab00d2..ed028999 100644 --- a/Project.toml +++ b/Project.toml @@ -4,7 +4,6 @@ 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" @@ -12,13 +11,14 @@ 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" diff --git a/src/ADNLPModels.jl b/src/ADNLPModels.jl index 18650372..875bbb47 100644 --- a/src/ADNLPModels.jl +++ b/src/ADNLPModels.jl @@ -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 diff --git a/src/ad.jl b/src/ad.jl index 94ed72a7..d6d1a807 100644 --- a/src/ad.jl +++ b/src/ad.jl @@ -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) - end - return colors -end diff --git a/src/sparse_hessian.jl b/src/sparse_hessian.jl index c28cdb1e..d7755f42 100644 --- a/src/sparse_hessian.jl +++ b/src/sparse_hessian.jl @@ -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 + colors = ADTypes.column_coloring(H, alg) ncolors = maximum(colors) d = BitVector(undef, nvar) @@ -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) diff --git a/src/sparse_jacobian.jl b/src/sparse_jacobian.jl index b89317e0..58766230 100644 --- a/src/sparse_jacobian.jl +++ b/src/sparse_jacobian.jl @@ -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)