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

Upgrade ColPack.jl #19

Closed
wants to merge 2 commits into from
Closed
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
8 changes: 4 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[compat]
ColPack_jll = "0.3.0"
LinearAlgebra = "1"
Random = "1"
SparseArrays = "1"
ColPack_jll = "0.4.0"
LinearAlgebra = "1.6"
Random = "1.6"
SparseArrays = "1.6"
julia = "1.6"
2 changes: 1 addition & 1 deletion docs/src/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ julia> adjJ = ColPack.matrix2adjmatrix(J; partition_by_rows=false)
julia> coloring = ColPackColoring(adjJ, d1_coloring(), natural_ordering());

julia> colors = get_colors(coloring)
5-element Vector{Int64}:
5-element Vector{Int32}:
1
2
1
Expand Down
8 changes: 8 additions & 0 deletions examples/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[deps]
ColPack = "ffa27691-3a59-46ab-a8d4-551f45b8d401"
ColPack_jll = "f218ff0c-cb54-5151-80c4-c0f62c730ce6"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MatrixMarket = "4d4711f2-db25-561a-b6b3-d35e7d4047d3"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
SparseDiffTools = "47a9eef4-7e08-11e9-0b38-333d64bd3804"
SuiteSparseMatrixCollection = "ac199af8-68bc-55b8-82c4-7abd6f96ed98"
64 changes: 64 additions & 0 deletions examples/coloring.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using ColPack
using SuiteSparseMatrixCollection
using MatrixMarket
using SparseArrays
using SparseDiffTools
using LinearAlgebra

ssmc = ssmc_db()
matrices = ssmc_matrices(ssmc, "Bodendiek", "CurlCurl_0")
folders = fetch_ssmc(matrices, format="MM")
paths = joinpath.(folders, matrices.name .* ".mtx")
A = MatrixMarket.mmread(paths[1])

orderings = Vector{ColoringOrder}()
push!(orderings, natural_ordering())
push!(orderings, largest_first_ordering())
push!(orderings, dynamic_largest_first_ordering())
push!(orderings, distance_two_largest_first_ordering())
push!(orderings, smallest_last_ordering())
push!(orderings, distance_two_smallest_last_ordering())
push!(orderings, incidence_degree_ordering())
push!(orderings, distance_two_incidence_degree_ordering())
push!(orderings, random_ordering())

@time sparsediff_coloring = SparseDiffTools.matrix_colors(A)
ncolors_sdt = maximum(sparsediff_coloring)

method = ColPack.d1_coloring()
verbose = false
@time colpack_coloring = ColPackColoring(paths[1], method, random_ordering(); verbose)

for ordering in orderings
colpack_coloring = ColPackColoring(paths[1], method, ordering; verbose)
end

colors_colpack = get_colors(colpack_coloring)
ncolors_colpack = maximum(colors_colpack)

method = row_partial_d2_coloring()
@time colpack_partial_coloring1 = ColPackPartialColoring(paths[1], method, natural_ordering(); verbose)
colors_row = get_colors(colpack_partial_coloring1)
maximum(colors_row)

method = column_partial_d2_coloring()
@time colpack_partial_coloring2 = ColPackPartialColoring(paths[1], method, natural_ordering(); verbose)
colors_column = get_colors(colpack_partial_coloring2)
maximum(colors_column)

method = row_partial_d2_coloring()
@time colpack_partial_coloring1 = ColPackPartialColoring(A, method, natural_ordering(); verbose)
colors_row2 = get_colors(colpack_partial_coloring1)
maximum(colors_row2)

method = column_partial_d2_coloring()
@time colpack_partial_coloring2 = ColPackPartialColoring(A, method, natural_ordering(); verbose)
colors_column2 = get_colors(colpack_partial_coloring2)
maximum(colors_column2)

method = implicit_star_bicoloring()
# method = explicit_star_bicoloring()
# method = explicit_modified_star_bicoloring()
# method = implicit_greedy_star_bicoloring()
@time colpack_bicoloring = ColPackBiColoring(paths[1], method, random_ordering(); verbose)
colors1, colors2 = get_colors(colpack_bicoloring)
9 changes: 7 additions & 2 deletions src/ColPack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@ using SparseArrays

# Definitions

include("libcolpack.jl")
include("method.jl")
include("order.jl")
include("utils.jl")
include("colpackcoloring.jl")
include("colpack_coloring.jl")
include("colpack_partial_coloring.jl")
include("colpack_bicoloring.jl")

# Exports

export ColoringMethod
export d1_coloring, d2_coloring, acyclic_coloring, star_coloring
export row_partial_d2_coloring, column_partial_d2_coloring
export implicit_star_bicoloring, explicit_star_bicoloring, explicit_modified_star_bicoloring, implicit_greedy_star_bicoloring

export ColoringOrder
export natural_ordering, largest_first_ordering, dynamic_largest_first_ordering, distance_two_largest_first_ordering
Expand All @@ -25,6 +30,6 @@ export random_ordering

export matrix2adjmatrix

export ColPackColoring, get_colors
export ColPackColoring, ColPackPartialColoring, ColPackBiColoring, get_colors

end #module
73 changes: 73 additions & 0 deletions src/colpack_bicoloring.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""
ColPackBiColoring

Struct holding the parameters of a bicoloring as well as its results (which can be queried with [`get_colors`](@ref)).

# Fields

The fields of this struct are not part of the public API, they are only useful to interface with the C++ library [ColPack](https://github.com/CSCsw/ColPack).

# Constructors

ColPackBiColoring(
filename::AbstractString,
method::ColoringMethod,
order::ColoringOrder;
verbose::Bool=false
)

ColPackBiColoring(
M::SparseMatrixCSC,
method::ColoringMethod,
order::ColoringOrder;
verbose::Bool=false
)

Perform the coloring of a matrix that is either given directly or read from a file.

The users needs to specify a bicoloring `method` and an `order` on the vertices.

# See also

- [`ColoringMethod`](@ref)
- [`ColoringOrder`](@ref)
"""
mutable struct ColPackBiColoring
refColPack::Base.RefValue{Ptr{Cvoid}}
coloring1::Vector{Cint}
coloring2::Vector{Cint}
method::ColoringMethod
order::ColoringOrder
end

Base.unsafe_convert(::Type{Ptr{Cvoid}}, coloring::ColPackBiColoring) = coloring.refColPack[]

function ColPackBiColoring(
filename::AbstractString,
method::ColoringMethod,
order::ColoringOrder;
verbose::Bool=false,
)
refColPack = Ref{Ptr{Cvoid}}(C_NULL)
reflen1 = Ref{Cint}(0)
reflen2 = Ref{Cint}(0)
ret = build_bicoloring_from_file(refColPack, reflen1, reflen2, filename, method.method, order.order, verbose)
(ret == 0) && error("ColPack bicoloring failed.")
coloring1 = zeros(Cint, reflen1[])
coloring2 = zeros(Cint, reflen2[])
g = ColPackBiColoring(refColPack, coloring1, coloring2, method, order)
finalizer(free_bicoloring, g)
return g
end

"""
get_colors(coloring::ColPackBiColoring)

Retrieve the colors from a [`ColPackBiColoring`](@ref) as vectors of integers.
"""
function get_colors(coloring::ColPackBiColoring)
get_bicoloring(coloring.refColPack[], coloring.coloring1, coloring.coloring2)
coloring.coloring1 .+= Cint(1)
coloring.coloring2 .+= Cint(1)
return coloring.coloring1, coloring.coloring2
end
65 changes: 19 additions & 46 deletions src/colpackcoloring.jl → src/colpack_coloring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,46 +37,33 @@ mutable struct ColPackColoring
coloring::Vector{Cint}
method::ColoringMethod
order::ColoringOrder
csr::Union{Vector{Ptr{Cuint}},Nothing}
end

function free_coloring(g::ColPackColoring)
@ccall libcolpack.free_coloring(g.refColPack::Ptr{Cvoid})::Cvoid
return nothing
end
Base.unsafe_convert(::Type{Ptr{Cvoid}}, coloring::ColPackColoring) = coloring.refColPack[]

function ColPackColoring(
filename::AbstractString,
method::ColoringMethod,
order::ColoringOrder;
verbose::Bool=false,
)
reflen = Vector{Cint}([Cint(0)])
refColPack = Ref{Ptr{Cvoid}}(C_NULL)
ret = @ccall libcolpack.build_coloring(
refColPack::Ptr{Cvoid},
reflen::Ptr{Cint},
filename::Cstring,
method.method::Cstring,
order.order::Cstring,
verbose::Cint,
)::Cint
if ret == 0
error("ColPack coloring failed.")
end

g = ColPackColoring(refColPack, zeros(Int, reflen[]), method, order, nothing)
reflen = Ref{Cint}(0)
ret = build_coloring_from_file(refColPack, reflen, filename, method.method, order.order, verbose)
(ret == 0) && error("ColPack coloring failed.")
coloring = zeros(Cint, reflen[])
g = ColPackColoring(refColPack, coloring, method, order)
finalizer(free_coloring, g)
return g
end

function ColPackColoring(
M::SparseMatrixCSC{VT,IT},
M::SparseMatrixCSC,
method::ColoringMethod,
order::ColoringOrder;
verbose::Bool=false,
) where {VT,IT}
@assert issymmetric(M)
verbose::Bool=false
)
# We expect M to be symmetric.
csr = Vector{Ref{Cuint}}()
csr_mem = Vector{Vector{Cuint}}()
for i in 1:(length(M.colptr) - 1)
Expand All @@ -92,36 +79,22 @@ function ColPackColoring(
nrows = size(M, 2)
reflen = Ref{Cint}(0)
refColPack = Ref{Ptr{Cvoid}}(C_NULL)
ret = @ccall libcolpack.build_coloring_from_csr(
refColPack::Ptr{Cvoid},
reflen::Ptr{Cint},
csr::Ref{Ptr{Cuint}},
nrows::Cint,
method.method::Cstring,
order.order::Cstring,
verbose::Cint,
)::Cint
if ret == 0
error("ColPack coloring failed.")
end
ret = build_coloring_from_adolc(refColPack, reflen, csr, nrows, method.method, order.order, verbose)
(ret == 0) && error("ColPack coloring failed.")

g = ColPackColoring(refColPack, zeros(Int, reflen[]), method, order, csr)
coloring = zeros(Cint, reflen[])
g = ColPackColoring(refColPack, coloring, method, order)
finalizer(free_coloring, g)
return g
end

"""
get_colors(coloring::ColPackColoring; verbose=false)
get_colors(coloring::ColPackColoring)

Retrieve the colors from a [`ColPackColoring`](@ref) as a vector of integers.
"""
function get_colors(coloring::ColPackColoring; verbose=false)
@ccall libcolpack.get_colors(
coloring.refColPack[]::Ptr{Cvoid},
coloring.coloring::Ptr{Cdouble}, # TODO: should this be Cint?
coloring.method.method::Cstring,
verbose::Cint,
)::Cvoid
# Julia colorings should be base 1
return coloring.coloring .+ 1
function get_colors(coloring::ColPackColoring)
get_coloring(coloring.refColPack[], coloring.coloring)
coloring.coloring .+= Cint(1)
return coloring.coloring
end
Loading
Loading