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
10 changes: 6 additions & 4 deletions benchmark/benchmarks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ for structure in [:nonsymmetric, :symmetric],

problem = ColoringProblem(; structure, partition)
algo = GreedyColoringAlgorithm(
RandomOrder(StableRNG(0), 0); decompression, postprocessing=true
RandomOrder(StableRNG(0), 0);
decompression,
postprocessing = true,
)

# use several random matrices to reduce variance
nb_samples = 5
As = [sparse(Symmetric(sprand(StableRNG(i), Bool, n, n, p))) for i in 1:nb_samples]
results = [coloring(A, problem, algo; decompression_eltype=Float64) for A in As]
As = [sparse(Symmetric(sprand(StableRNG(i), Bool, n, n, p))) for i = 1:nb_samples]
results = [coloring(A, problem, algo; decompression_eltype = Float64) for A in As]
Bs = [compress(Float64.(A), result) for (A, result) in zip(As, results)]

bench_col = @benchmarkable begin
Expand Down Expand Up @@ -57,7 +59,7 @@ for structure in [:nonsymmetric, :symmetric],
p in [2 / n, 5 / n, 10 / n]

nb_samples = 5
As = [sparse(Symmetric(sprand(StableRNG(i), Bool, n, n, p))) for i in 1:nb_samples]
As = [sparse(Symmetric(sprand(StableRNG(i), Bool, n, n, p))) for i = 1:nb_samples]
if structure == :symmetric
gs = [SMC.AdjacencyGraph(A) for A in As]
bench_ord = @benchmarkable begin
Expand Down
24 changes: 14 additions & 10 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,28 @@ using SparseMatrixColorings

links = InterLinks("ADTypes" => "https://sciml.github.io/ADTypes.jl/stable/")

cp(joinpath(@__DIR__, "..", "README.md"), joinpath(@__DIR__, "src", "index.md"); force=true)
cp(
joinpath(@__DIR__, "..", "README.md"),
joinpath(@__DIR__, "src", "index.md");
force = true,
)

makedocs(;
modules=[SparseMatrixColorings],
authors="Guillaume Dalle and Alexis Montoison",
sitename="SparseMatrixColorings.jl",
format=Documenter.HTML(),
pages=[
modules = [SparseMatrixColorings],
authors = "Guillaume Dalle and Alexis Montoison",
sitename = "SparseMatrixColorings.jl",
format = Documenter.HTML(),
pages = [
"Home" => "index.md",
"tutorial.md",
"api.md",
"Developer Documentation" => ["dev.md", "vis.md"],
],
plugins=[links],
plugins = [links],
)

deploydocs(;
repo="github.com/JuliaDiff/SparseMatrixColorings.jl",
push_preview=true,
devbranch="main",
repo = "github.com/JuliaDiff/SparseMatrixColorings.jl",
push_preview = true,
devbranch = "main",
)
72 changes: 52 additions & 20 deletions ext/SparseMatrixColoringsCUDAExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,31 @@ using cuSPARSE: AbstractCuSparseMatrix, CuSparseMatrixCSC, CuSparseMatrixCSR
## CSC Result

function SMC.ColumnColoringResult(
A::CuSparseMatrixCSC, bg::SMC.BipartiteGraph{T}, color::Vector{<:Integer}
A::CuSparseMatrixCSC,
bg::SMC.BipartiteGraph{T},
color::Vector{<:Integer},
) where {T<:Integer}
group = SMC.group_by_color(T, color)
compressed_indices = SMC.column_csc_indices(bg, color)
additional_info = (; compressed_indices_gpu_csc=CuVector(compressed_indices))
additional_info = (; compressed_indices_gpu_csc = CuVector(compressed_indices))
return SMC.ColumnColoringResult(
A, bg, color, group, compressed_indices, additional_info
A,
bg,
color,
group,
compressed_indices,
additional_info,
)
end

function SMC.RowColoringResult(
A::CuSparseMatrixCSC, bg::SMC.BipartiteGraph{T}, color::Vector{<:Integer}
A::CuSparseMatrixCSC,
bg::SMC.BipartiteGraph{T},
color::Vector{<:Integer},
) where {T<:Integer}
group = SMC.group_by_color(T, color)
compressed_indices = SMC.row_csc_indices(bg, color)
additional_info = (; compressed_indices_gpu_csc=CuVector(compressed_indices))
additional_info = (; compressed_indices_gpu_csc = CuVector(compressed_indices))
return SMC.RowColoringResult(A, bg, color, group, compressed_indices, additional_info)
end

Expand All @@ -35,33 +44,47 @@ function SMC.StarSetColoringResult(
) where {T<:Integer}
group = SMC.group_by_color(T, color)
compressed_indices = SMC.star_csc_indices(ag, color, star_set)
additional_info = (; compressed_indices_gpu_csc=CuVector(compressed_indices))
additional_info = (; compressed_indices_gpu_csc = CuVector(compressed_indices))
return SMC.StarSetColoringResult(
A, ag, color, group, compressed_indices, additional_info
A,
ag,
color,
group,
compressed_indices,
additional_info,
)
end

## CSR Result

function SMC.ColumnColoringResult(
A::CuSparseMatrixCSR, bg::SMC.BipartiteGraph{T}, color::Vector{<:Integer}
A::CuSparseMatrixCSR,
bg::SMC.BipartiteGraph{T},
color::Vector{<:Integer},
) where {T<:Integer}
group = SMC.group_by_color(T, color)
compressed_indices = SMC.column_csc_indices(bg, color)
compressed_indices_csr = SMC.column_csr_indices(bg, color)
additional_info = (; compressed_indices_gpu_csr=CuVector(compressed_indices_csr))
additional_info = (; compressed_indices_gpu_csr = CuVector(compressed_indices_csr))
return SMC.ColumnColoringResult(
A, bg, color, group, compressed_indices, additional_info
A,
bg,
color,
group,
compressed_indices,
additional_info,
)
end

function SMC.RowColoringResult(
A::CuSparseMatrixCSR, bg::SMC.BipartiteGraph{T}, color::Vector{<:Integer}
A::CuSparseMatrixCSR,
bg::SMC.BipartiteGraph{T},
color::Vector{<:Integer},
) where {T<:Integer}
group = SMC.group_by_color(T, color)
compressed_indices = SMC.row_csc_indices(bg, color)
compressed_indices_csr = SMC.row_csr_indices(bg, color)
additional_info = (; compressed_indices_gpu_csr=CuVector(compressed_indices_csr))
additional_info = (; compressed_indices_gpu_csr = CuVector(compressed_indices_csr))
return SMC.RowColoringResult(A, bg, color, group, compressed_indices, additional_info)
end

Expand All @@ -73,9 +96,14 @@ function SMC.StarSetColoringResult(
) where {T<:Integer}
group = SMC.group_by_color(T, color)
compressed_indices = SMC.star_csc_indices(ag, color, star_set)
additional_info = (; compressed_indices_gpu_csr=CuVector(compressed_indices))
additional_info = (; compressed_indices_gpu_csr = CuVector(compressed_indices))
return SMC.StarSetColoringResult(
A, ag, color, group, compressed_indices, additional_info
A,
ag,
color,
group,
compressed_indices,
additional_info,
)
end

Expand All @@ -84,15 +112,19 @@ end
for R in (:ColumnColoringResult, :RowColoringResult)
# loop to avoid method ambiguity
@eval function SMC.decompress!(
A::CuSparseMatrixCSC, B::CuMatrix, result::SMC.$R{<:CuSparseMatrixCSC}
A::CuSparseMatrixCSC,
B::CuMatrix,
result::SMC.$R{<:CuSparseMatrixCSC},
)
compressed_indices = result.additional_info.compressed_indices_gpu_csc
copyto!(A.nzVal, view(B, compressed_indices))
return A
end

@eval function SMC.decompress!(
A::CuSparseMatrixCSR, B::CuMatrix, result::SMC.$R{<:CuSparseMatrixCSR}
A::CuSparseMatrixCSR,
B::CuMatrix,
result::SMC.$R{<:CuSparseMatrixCSR},
)
compressed_indices = result.additional_info.compressed_indices_gpu_csr
copyto!(A.nzVal, view(B, compressed_indices))
Expand All @@ -104,12 +136,12 @@ function SMC.decompress!(
A::CuSparseMatrixCSC,
B::CuMatrix,
result::SMC.StarSetColoringResult{<:CuSparseMatrixCSC},
uplo::Symbol=:F,
uplo::Symbol = :F,
)
if uplo != :F
throw(
SMC.UnsupportedDecompressionError(
"Single-triangle decompression is not supported on GPU matrices"
"Single-triangle decompression is not supported on GPU matrices",
),
)
end
Expand All @@ -122,12 +154,12 @@ function SMC.decompress!(
A::CuSparseMatrixCSR,
B::CuMatrix,
result::SMC.StarSetColoringResult{<:CuSparseMatrixCSR},
uplo::Symbol=:F,
uplo::Symbol = :F,
)
if uplo != :F
throw(
SMC.UnsupportedDecompressionError(
"Single-triangle decompression is not supported on GPU matrices"
"Single-triangle decompression is not supported on GPU matrices",
),
)
end
Expand Down
2 changes: 1 addition & 1 deletion ext/SparseMatrixColoringsCliqueTreesExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function vertices(g::AdjacencyGraph{T}, order::PerfectEliminationOrder) where {T

# construct a perfect elimination order
# self-loops are ignored
order, _ = permutation(M; alg=order.elimination_algorithm)
order, _ = permutation(M; alg = order.elimination_algorithm)

return reverse!(order)
end
Expand Down
25 changes: 12 additions & 13 deletions ext/SparseMatrixColoringsColorsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ const DEFAULT_PAD = 0

function SparseMatrixColorings.show_colors(
res::AbstractColoringResult;
colorscheme=nothing,
background_color::Colorant=DEFAULT_BACKGROUND_COLOR, # color used for zero matrix entries and pad
border_color::Colorant=DEFAULT_BORDER_COLOR, # color used for zero matrix entries and pad
scale::Int=DEFAULT_SCALE, # scale size of matrix entries to `scale × scale` pixels
border::Int=DEFAULT_BORDER, # border around matrix entries
pad::Int=DEFAULT_PAD, # pad between matrix entries
warn::Bool=true,
colorscheme = nothing,
background_color::Colorant = DEFAULT_BACKGROUND_COLOR, # color used for zero matrix entries and pad
border_color::Colorant = DEFAULT_BORDER_COLOR, # color used for zero matrix entries and pad
scale::Int = DEFAULT_SCALE, # scale size of matrix entries to `scale × scale` pixels
border::Int = DEFAULT_BORDER, # border around matrix entries
pad::Int = DEFAULT_PAD, # pad between matrix entries
warn::Bool = true,
)
scale < 1 && throw(ArgumentError("`scale` has to be ≥ 1."))
border < 0 && throw(ArgumentError("`border` has to be ≥ 0."))
Expand All @@ -53,15 +53,14 @@ function SparseMatrixColorings.show_colors(
if warn && ncolors(res) > length(colorscheme)
@warn "`show_colors` will reuse colors since the provided `colorscheme` has $(length(colorscheme)) colors and the matrix needs $(ncolors(res)). You can turn off this warning via the keyword argument `warn = false`, or choose a larger `colorscheme` from ColorSchemes.jl."
end
colorscheme, background_color, border_color = promote_colors(
colorscheme, background_color, border_color
)
colorscheme, background_color, border_color =
promote_colors(colorscheme, background_color, border_color)
else
# Sample n distinguishable colors, excluding the background and border color
colorscheme = distinguishable_colors(
ncolors(res),
[convert(RGB, background_color), convert(RGB, border_color)];
dropseed=true,
dropseed = true,
)
end
outs = allocate_outputs(res, background_color, border_color, scale, border, pad)
Expand All @@ -86,7 +85,7 @@ function matrix_entry_area(I::CartesianIndex, scale, border, pad)
end

function matrix_entry_plus_border_area(I::CartesianIndex, scale, border, pad)
stencil = CartesianIndices((1:(scale + 2border), 1:(scale + 2border)))
stencil = CartesianIndices((1:(scale+2border), 1:(scale+2border)))
return CartesianIndex(1, 1) * pad +
(I - CartesianIndex(1, 1)) * (scale + 2border + pad) .+ stencil
end
Expand Down Expand Up @@ -272,7 +271,7 @@ function show_colors!(
A_rcolor_indices = mod1.(row_shift .+ row_colors(res), length(colorscheme))
B_ccolor_indices = mod1.(1:maximum(column_colors(res)), length(colorscheme))
B_rcolor_indices =
mod1.((row_shift + 1):(row_shift + maximum(row_colors(res))), length(colorscheme))
mod1.((row_shift+1):(row_shift+maximum(row_colors(res))), length(colorscheme))
A_ccolors = colorscheme[A_ccolor_indices]
A_rcolors = colorscheme[A_rcolor_indices]
B_ccolors = colorscheme[B_ccolor_indices]
Expand Down
6 changes: 4 additions & 2 deletions ext/SparseMatrixColoringsGPUArraysExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ SMC.matrix_versions(A::AbstractGPUSparseMatrix) = (A,)
## Compression (slow, through CPU)

function SMC.compress(
A::AbstractGPUSparseMatrix, result::SMC.AbstractColoringResult{structure,:column}
A::AbstractGPUSparseMatrix,
result::SMC.AbstractColoringResult{structure,:column},
) where {structure}
A_cpu = SparseMatrixCSC(A)
B_cpu = SMC.compress(A_cpu, result)
Expand All @@ -18,7 +19,8 @@ function SMC.compress(
end

function SMC.compress(
A::AbstractGPUSparseMatrix, result::SMC.AbstractColoringResult{structure,:row}
A::AbstractGPUSparseMatrix,
result::SMC.AbstractColoringResult{structure,:row},
) where {structure}
A_cpu = SparseMatrixCSC(A)
B_cpu = SMC.compress(A_cpu, result)
Expand Down
16 changes: 12 additions & 4 deletions ext/SparseMatrixColoringsJuMPExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ function optimal_distance2_coloring(
bg::BipartiteGraph,
::Val{side},
optimizer::O;
silent::Bool=true,
assert_solved::Bool=true,
silent::Bool = true,
assert_solved::Bool = true,
) where {side,O}
other_side = 3 - side
n = nb_vertices(bg, Val(side))
Expand Down Expand Up @@ -66,14 +66,22 @@ end
function ADTypes.column_coloring(A::AbstractMatrix, algo::OptimalColoringAlgorithm)
bg = BipartiteGraph(A)
return optimal_distance2_coloring(
bg, Val(2), algo.optimizer; algo.silent, algo.assert_solved
bg,
Val(2),
algo.optimizer;
algo.silent,
algo.assert_solved,
)
end

function ADTypes.row_coloring(A::AbstractMatrix, algo::OptimalColoringAlgorithm)
bg = BipartiteGraph(A)
return optimal_distance2_coloring(
bg, Val(1), algo.optimizer; algo.silent, algo.assert_solved
bg,
Val(1),
algo.optimizer;
algo.silent,
algo.assert_solved,
)
end

Expand Down
4 changes: 2 additions & 2 deletions src/adtypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ function coloring(
A::AbstractMatrix,
problem::ColoringProblem{structure,partition},
algo::ADTypes.AbstractColoringAlgorithm;
decompression_eltype::Type{R}=Float64,
symmetric_pattern::Bool=false,
decompression_eltype::Type{R} = Float64,
symmetric_pattern::Bool = false,
) where {structure,partition,R}
symmetric_pattern = symmetric_pattern || A isa Union{Symmetric,Hermitian}
if structure == :nonsymmetric
Expand Down
Loading
Loading