Skip to content
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[compat]
ColPack_jll = "0.4.1"
ColPack_jll = "=0.4.3"
LinearAlgebra = "1.10"
Random = "1.10"
SparseArrays = "1.10"
Expand Down
8 changes: 8 additions & 0 deletions gen/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[deps]
ColPack_jll = "f218ff0c-cb54-5151-80c4-c0f62c730ce6"
Clang = "40e3b903-d033-50b4-a0cc-940c62c95e31"
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"

[compat]
ColPack_jll = "0.4.3"
julia = "1.10"
9 changes: 9 additions & 0 deletions gen/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Wrapping headers

This directory contains a script that can be used to automatically generate wrappers from C headers provided by ColPack.
This is done using Clang.jl.

# Usage

Either run `julia --project wrapper.jl` directly, or include it and call the function `main()`.
Be sure to activate the project environment in this folder (`julia --project`), which will install `Clang.jl` and `JuliaFormatter.jl`.
12 changes: 12 additions & 0 deletions gen/colpack.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[general]
use_julia_native_enum_type = true
print_using_CEnum = false
library_name = "libcolpack"

[codegen]
use_julia_bool = true
always_NUL_terminated_string = false
use_ccall_macro = true

[codegen.macro]
macro_mode = "disable"
32 changes: 32 additions & 0 deletions gen/wrapper.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Script to parse CUTEst headers and generate Julia wrappers.
using ColPack_jll
using Clang
using Clang.Generators
using JuliaFormatter

# Add cinterface.h in the next release of ColPack_jll.jl!
function main()
cd(@__DIR__)
include_dir = joinpath(ColPack_jll.artifact_dir, "include")
# headers = joinpath(include_dir, "cinterface.h")
headers = joinpath("..", "..", "ColPack", "src", "Utilities", "cinterface.h")

options = load_options(joinpath(@__DIR__, "colpack.toml"))
options["general"]["output_file_path"] = joinpath("..", "src", "libcolpack.jl")
options["general"]["output_ignorelist"] = []

args = get_default_args()
push!(args, "-I$include_dir")

ctx = create_context(headers, args, options)
build!(ctx)

path = options["general"]["output_file_path"]
format_file(path, YASStyle())
return nothing
end

# If we want to use the file as a script with `julia wrapper.jl`
if abspath(PROGRAM_FILE) == @__FILE__
main()
end
5 changes: 4 additions & 1 deletion src/ColPack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ A Julia interface to the C++ library [ColPack](https://github.com/CSCsw/ColPack)
- [`ColPackPartialColoring`](@ref)
- [`ColPackBiColoring`](@ref)
- [`colpack`](@ref)
- [`get_ordering`](@ref)
- [`get_colors`](@ref)
- [`ncolors`](@ref)
- [`timer_ordering`](@ref)
- [`timer_coloring`](@ref)
"""
module ColPack

Expand All @@ -32,6 +35,6 @@ include("colpack_bicoloring.jl")

# Exports

export ColPackColoring, ColPackPartialColoring, ColPackBiColoring, colpack, get_colors, ncolors
export ColPackColoring, ColPackPartialColoring, ColPackBiColoring, colpack, get_ordering, get_colors, ncolors, timer_ordering, timer_coloring

end #module
51 changes: 41 additions & 10 deletions src/colpack_bicoloring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ The fields of this struct are not part of the public API, they are only useful t
"""
mutable struct ColPackBiColoring
refColPack::Base.RefValue{Ptr{Cvoid}}
ordering::Vector{Cint}
coloring1::Vector{Cint}
coloring2::Vector{Cint}
method::String
Expand All @@ -59,9 +60,10 @@ function ColPackBiColoring(
refColPack, reflen1, reflen2, filename, method, order, verbose
)
(ret == 0) && error("ColPack bicoloring failed.")
ordering = zeros(Cint, reflen1[] + reflen2[])
coloring1 = zeros(Cint, reflen1[])
coloring2 = zeros(Cint, reflen2[])
g = ColPackBiColoring(refColPack, coloring1, coloring2, method, order)
g = ColPackBiColoring(refColPack, ordering, coloring1, coloring2, method, order)
finalizer(free_bicoloring, g)
return g
end
Expand All @@ -81,31 +83,42 @@ function ColPackBiColoring(
nrows, ncols = size(M)

# Version adolc
# Mᵀ = sparse(M')
# adolc, adolc_mem = csr_to_adolc(Mᵀ)
# ret = build_bicoloring_from_adolc(refColPack, reflen1, reflen2, adolc, nrows, ncols, method, order, verbose)
Mᵀ = sparse(M')
adolc, adolc_mem = csr_to_adolc(Mᵀ)
ret = build_bicoloring_from_adolc(refColPack, reflen1, reflen2, adolc, nrows, ncols, method, order, verbose)

# Version csc
# ColPack expects sparse CSC / CSR matrices with 0-based indexing.
rowval = Cint.(M.rowval) .- Cint(1)
colptr = Cint.(M.colptr) .- Cint(1)
ret = build_bicoloring_from_csc(refColPack, reflen1, reflen2, rowval, colptr, nrows, ncols, method, order, verbose)

# rowval = Cint.(M.rowval) .- Cint(1)
# colptr = Cint.(M.colptr) .- Cint(1)
# ret = build_bicoloring_from_csc(refColPack, reflen1, reflen2, rowval, colptr, nrows, ncols, method, order, verbose)
(ret == 0) && error("ColPack partial coloring failed.")
ordering = zeros(Cint, reflen1[] + reflen2[])
coloring1 = zeros(Cint, reflen1[])
coloring2 = zeros(Cint, reflen2[])
g = ColPackBiColoring(refColPack, coloring1, coloring2, method, order)
g = ColPackBiColoring(refColPack, ordering, coloring1, coloring2, method, order)
finalizer(free_partial_coloring, g)
return g
end

"""
get_ordering(coloring::ColPackBiColoring)

Retrieve the ordering from a [`ColPackBiColoring`](@ref) as a vector of integers.
"""
function get_ordering(coloring::ColPackBiColoring)
order_bicoloring(coloring.refColPack[], coloring.ordering)
coloring.ordering .+= Cint(1)
return coloring.ordering
end

"""
get_colors(coloring::ColPackBiColoring)

Retrieve the colors from a [`ColPackBiColoring`](@ref) as two vectors of integers, one for the rows and one for the columns respectively.
"""
function get_colors(coloring::ColPackBiColoring)
get_bicoloring(coloring.refColPack[], coloring.coloring1, coloring.coloring2)
colors_bicoloring(coloring.refColPack[], coloring.coloring1, coloring.coloring2)
#=
Zero is a neutral color in bicoloring, it may make sense to keep it.
I am not yet sure how the coloring vectors are defined.
Expand All @@ -123,3 +136,21 @@ Retrieve the number of colors from a [`ColPackBiColoring`](@ref).
function ncolors(coloring::ColPackBiColoring)
return ncolors_bicoloring(coloring.refColPack[])
end

"""
timer_ordering(coloring::ColPackBiColoring)

Retrieve the timer for ordering from a [`ColPackBiColoring`](@ref).
"""
function timer_ordering(coloring::ColPackBiColoring)
return timer_order_bicoloring(coloring.refColPack[])
end

"""
timer_coloring(coloring::ColPackBiColoring)

Retrieve the timer for coloring from a [`ColPackBiColoring`](@ref).
"""
function timer_coloring(coloring::ColPackBiColoring)
return timer_colors_bicoloring(coloring.refColPack[])
end
39 changes: 35 additions & 4 deletions src/colpack_coloring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ The fields of this struct are not part of the public API, they are only useful t
"""
mutable struct ColPackColoring
refColPack::Base.RefValue{Ptr{Cvoid}}
ordering::Vector{Cint}
coloring::Vector{Cint}
method::String
order::String
Expand All @@ -70,8 +71,9 @@ function ColPackColoring(
reflen = Ref{Cint}(0)
ret = build_coloring_from_file(refColPack, reflen, filename, method, order, verbose)
(ret == 0) && error("ColPack coloring failed.")
ordering = zeros(Cint, reflen[])
coloring = zeros(Cint, reflen[])
g = ColPackColoring(refColPack, coloring, method, order)
g = ColPackColoring(refColPack, ordering, coloring, method, order)
finalizer(free_coloring, g)
return g
end
Expand All @@ -95,20 +97,31 @@ function ColPackColoring(
refColPack = Ref{Ptr{Cvoid}}(C_NULL)
ret = build_coloring_from_adolc(refColPack, reflen, adolc, nrows, method, order, verbose)
(ret == 0) && error("ColPack coloring failed.")

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

"""
get_ordering(coloring::ColPackColoring)

Retrieve the ordering from a [`ColPackColoring`](@ref) as a vector of integers.
"""
function get_ordering(coloring::ColPackColoring)
order_coloring(coloring.refColPack[], coloring.ordering)
coloring.ordering .+= Cint(1)
return coloring.ordering
end

"""
get_colors(coloring::ColPackColoring)

Retrieve the colors from a [`ColPackColoring`](@ref) as a vector of integers.
"""
function get_colors(coloring::ColPackColoring)
get_coloring(coloring.refColPack[], coloring.coloring)
colors_coloring(coloring.refColPack[], coloring.coloring)
coloring.coloring .+= Cint(1)
return coloring.coloring
end
Expand All @@ -121,3 +134,21 @@ Retrieve the number of colors from a [`ColPackColoring`](@ref).
function ncolors(coloring::ColPackColoring)
return ncolors_coloring(coloring.refColPack[])
end

"""
timer_ordering(coloring::ColPackColoring)

Retrieve the timer for ordering from a [`ColPackColoring`](@ref).
"""
function timer_ordering(coloring::ColPackColoring)
return timer_order_coloring(coloring.refColPack[])
end

"""
timer_coloring(coloring::ColPackColoring)

Retrieve the timer for coloring from a [`ColPackColoring`](@ref).
"""
function timer_coloring(coloring::ColPackColoring)
return timer_colors_coloring(coloring.refColPack[])
end
56 changes: 46 additions & 10 deletions src/colpack_partial_coloring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ The fields of this struct are not part of the public API, they are only useful t
"""
mutable struct ColPackPartialColoring
refColPack::Base.RefValue{Ptr{Cvoid}}
ordering::Vector{Cint}
coloring::Vector{Cint}
method::String
order::String
Expand All @@ -76,8 +77,9 @@ function ColPackPartialColoring(
refColPack, reflen, filename, method, order, verbose
)
(ret == 0) && error("ColPack partial coloring failed.")
ordering = zeros(Cint, reflen[])
coloring = zeros(Cint, reflen[])
g = ColPackPartialColoring(refColPack, coloring, method, order)
g = ColPackPartialColoring(refColPack, ordering, coloring, method, order)
finalizer(free_partial_coloring, g)
return g
end
Expand All @@ -96,30 +98,46 @@ function ColPackPartialColoring(
nrows, ncols = size(M)

# Version adolc
# Mᵀ = sparse(M')
# adolc, adolc_mem = csr_to_adolc(Mᵀ)
# ret = build_partial_coloring_from_adolc(refColPack, reflen, adolc, nrows, ncols, method, order, verbose)
Mᵀ = sparse(M')
adolc, adolc_mem = csr_to_adolc(Mᵀ)
ret = build_partial_coloring_from_adolc(refColPack, reflen, adolc, nrows, ncols, method, order, verbose)

# Version csc
# ColPack expects sparse CSC / CSR matrices with 0-based indexing.
rowval = Cint.(M.rowval) .- Cint(1)
colptr = Cint.(M.colptr) .- Cint(1)
ret = build_partial_coloring_from_csc(refColPack, reflen, rowval, colptr, nrows, ncols, method, order, verbose)

# rowval = Cint.(M.rowval) .- Cint(1)
# colptr = Cint.(M.colptr) .- Cint(1)
# ret = build_partial_coloring_from_csc(refColPack, reflen, rowval, colptr, nrows, ncols, method, order, verbose)
(ret == 0) && error("ColPack partial coloring failed.")
ordering = zeros(Cint, reflen[])
coloring = zeros(Cint, reflen[])
g = ColPackPartialColoring(refColPack, coloring, method, order)
g = ColPackPartialColoring(refColPack, ordering, coloring, method, order)
finalizer(free_partial_coloring, g)
return g
end

"""
get_ordering(coloring::ColPackPartialColoring)

Retrieve the ordering from a [`ColPackPartialColoring`](@ref) as a vector of integers.
"""
function get_ordering(coloring::ColPackPartialColoring)
order_partial_coloring(coloring.refColPack[], coloring.ordering)
if coloring.method == "COLUMN_PARTIAL_DISTANCE_TWO"
offset = minimum(coloring.ordering) - 1
coloring.ordering .-= Cint(offset)
else
coloring.ordering .+= Cint(1)
end
return coloring.ordering
end

"""
get_colors(coloring::ColPackPartialColoring)

Retrieve the colors from a [`ColPackPartialColoring`](@ref) as a vector of integers.
"""
function get_colors(coloring::ColPackPartialColoring)
get_partial_coloring(coloring.refColPack[], coloring.coloring)
colors_partial_coloring(coloring.refColPack[], coloring.coloring)
coloring.coloring .+= Cint(1)
return coloring.coloring
end
Expand All @@ -132,3 +150,21 @@ Retrieve the number of colors from a [`ColPackPartialColoring`](@ref).
function ncolors(coloring::ColPackPartialColoring)
return ncolors_partial_coloring(coloring.refColPack[])
end

"""
timer_ordering(coloring::ColPackPartialColoring)

Retrieve the timer for ordering from a [`ColPackPartialColoring`](@ref).
"""
function timer_ordering(coloring::ColPackPartialColoring)
return timer_order_partial_coloring(coloring.refColPack[])
end

"""
timer_coloring(coloring::ColPackPartialColoring)

Retrieve the timer for coloring from a [`ColPackPartialColoring`](@ref).
"""
function timer_coloring(coloring::ColPackPartialColoring)
return timer_colors_partial_coloring(coloring.refColPack[])
end
Loading
Loading