Skip to content

Commit

Permalink
Abstract types for orderings and colorings
Browse files Browse the repository at this point in the history
  • Loading branch information
michel2323 committed Feb 18, 2022
1 parent a27fcca commit cd78bd0
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 22 deletions.
25 changes: 14 additions & 11 deletions src/ColPack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ using SparseArrays

export ColPackColoring, get_colors

const d1 = "DISTANCE_ONE"
const d2 = "DISTANCE_TWO"
const acyclic = "ACYCLIC",
const star = "STAR",
abstract type AbstractColoring end
include("colorings.jl")
export AbstractColoring

abstract type AbstractOrdering end
include("orderings.jl")
export AbstractOrdering

mutable struct ColPackColoring
refColPack::Vector{Ptr{Cvoid}}
coloring::Vector{Cint}
method::AbstractString
order::AbstractString
method::AbstractColoring
order::AbstractOrdering
csr::Union{Vector{Ptr{Cuint}},Nothing}
end

Expand All @@ -29,13 +32,13 @@ function free_coloring(g::ColPackColoring)
return nothing
end

function ColPackColoring(filename::AbstractString, method::AbstractString, order::AbstractString; verbose::Bool=false)
function ColPackColoring(filename::AbstractString, method::AbstractColoring, order::AbstractOrdering; verbose::Bool=false)
reflen = Vector{Cint}([Cint(0)])
refColPack = Vector{Ptr{Cvoid}}([C_NULL])
ret = ccall(
(:build_coloring, libcolpack),
Cint, (Ptr{Cvoid}, Ptr{Cint}, Cstring, Cstring, Cstring, Cint),
refColPack, reflen, filename, method, order, Cint(verbose),
refColPack, reflen, filename, method.colpack_coloring, order.colpack_ordering, Cint(verbose),
)
if ret == 0
error("ColPack coloring failed.")
Expand All @@ -46,7 +49,7 @@ function ColPackColoring(filename::AbstractString, method::AbstractString, order
return g
end

function ColPackColoring(M::SparseMatrixCSC{VT,IT}, method::AbstractString, order::AbstractString; verbose::Bool=false) where {VT,IT}
function ColPackColoring(M::SparseMatrixCSC{VT,IT}, method::AbstractColoring, order::AbstractOrdering; verbose::Bool=false) where {VT,IT}
@assert issymmetric(M)
csr = Vector{Ref{Cuint}}()
for i in 1:(length(M.colptr) -1)
Expand All @@ -64,7 +67,7 @@ function ColPackColoring(M::SparseMatrixCSC{VT,IT}, method::AbstractString, orde
ret = ccall(
(:build_coloring_from_csr, libcolpack),
Cint, (Ptr{Cvoid}, Ptr{Cint}, Ref{Ptr{Cuint}}, Cint, Cstring, Cstring, Cint),
refColPack, reflen, csr, nrows, method, order, Cint(verbose),
refColPack, reflen, csr, nrows, method.colpack_coloring, order.colpack_ordering, Cint(verbose),
)
if ret == 0
error("ColPack coloring failed.")
Expand All @@ -79,7 +82,7 @@ function get_colors(coloring::ColPackColoring; verbose=false)
ccall(
(:get_colors, libcolpack),
Cvoid, (Ptr{Cvoid}, Ptr{Cdouble}, Cstring, Cint),
coloring.refColPack[1], coloring.coloring, coloring.method, Cint(verbose)
coloring.refColPack[1], coloring.coloring, coloring.method.colpack_coloring, Cint(verbose)
)
return coloring.coloring
end
Expand Down
22 changes: 22 additions & 0 deletions src/colorings.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
struct d1_coloring <: AbstractColoring
colpack_coloring::String
end

struct d2_coloring <: AbstractColoring
colpack_coloring::String
end

struct acyclic_coloring <: AbstractColoring
colpack_coloring::String
end

struct star_coloring <: AbstractColoring
colpack_coloring::String
end

d1_coloring() = d1_coloring("DISTANCE_ONE")
d2_coloring()= d2_coloring("DISTANCE_TWO")
acyclic_coloring() = acyclic_coloring("ACYCLIC")
star_coloring() = star_coloring("STAR")

export d1_coloring, d2_coloring, acyclic_coloring, star_coloring
42 changes: 42 additions & 0 deletions src/orderings.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
struct natural_ordering <: AbstractOrdering
colpack_ordering::String
end
natural_ordering() = natural_ordering("NATURAL")

struct largest_first_ordering <: AbstractOrdering
colpack_ordering::String
end
largest_first_ordering() = largest_first_ordering("LARGEST_FIRST")

struct dynamic_largest_first_ordering <: AbstractOrdering
colpack_ordering::String
end
dynamic_largest_first_ordering() = dynamic_largest_first_ordering("DYNAMIC_LARGEST_FIRST")

struct distance_two_largest_first_ordering <: AbstractOrdering
colpack_ordering::String
end
distance_two_largest_first_ordering() = distance_two_largest_first_ordering("DISTANCE_TWO_LARGEST_FIRST")

struct smallest_last_ordering <: AbstractOrdering
colpack_ordering::String
end
smallest_last_ordering() = smallest_last_ordering("SMALLEST_LAST")

struct distance_two_smallest_last_ordering <: AbstractOrdering
colpack_ordering::String
end
distance_two_smallest_last_ordering() = distance_two_smallest_last_ordering("DISTANCE_TWO_SMALLEST_LAST")

struct incidence_degree_ordering <: AbstractOrdering
colpack_ordering::String
end
incidence_degree_ordering() = incidence_degree_ordering("INCIDENCE_DEGREE")

struct distance_two_incidence_degree_ordering <: AbstractOrdering
colpack_ordering::String
end
distance_two_incidence_degree_ordering() = distance_two_incidence_degree_ordering("DISTANCE_TWO_INCIDENCE_DEGREE")

export natural_ordering, largest_first_ordering, dynamic_largest_first_ordering, distance_two_largest_first_ordering
export smallest_last_ordering, distance_two_smallest_last_ordering, incidence_degree_ordering, distance_two_incidence_degree_ordering
22 changes: 11 additions & 11 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ using Random
using SparseArrays
using Test

orderings = Vector{String}()
orderings = Vector{AbstractOrdering}()

push!(orderings, "NATURAL")
push!(orderings, "LARGEST_FIRST")
push!(orderings, "DYNAMIC_LARGEST_FIRST")
push!(orderings, "DISTANCE_TWO_LARGEST_FIRST")
push!(orderings, "SMALLEST_LAST")
push!(orderings, "DISTANCE_TWO_SMALLEST_LAST")
push!(orderings, "INCIDENCE_DEGREE")
push!(orderings, "DISTANCE_TWO_INCIDENCE_DEGREE")
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")

ncolors = Vector{Int}()
Expand All @@ -40,13 +40,13 @@ verbose = false
@testset "Test ColPack" begin
@testset "Test Matrix Market API" begin
for (i,ordering) in enumerate(orderings)
coloring = ColPackColoring(filename, ColPack.d1, ordering; verbose=verbose)
coloring = ColPackColoring(filename, d1_coloring(), ordering; verbose=verbose)
@test length(unique(get_colors(coloring))) == ncolors[i]
end
end
@testset "Test ADOL-C Compressed Row Storage" begin
for (i,ordering) in enumerate(orderings)
coloring = ColPackColoring(A, ColPack.d1, ordering; verbose=verbose)
coloring = ColPackColoring(A, d1_coloring(), ordering; verbose=verbose)
@test length(unique(get_colors(coloring))) == ncolors[i]
end
end
Expand Down

0 comments on commit cd78bd0

Please sign in to comment.