Skip to content

Commit e349f50

Browse files
authored
Speed up group_by_color (#116)
* Constant number of allocations in `group_by_color` * Improvements and benchmark memory * Remove playground * Docstrings
1 parent c28490d commit e349f50

6 files changed

Lines changed: 49 additions & 24 deletions

File tree

.github/workflows/Benchmark.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
benchpkg ${{ steps.extract-package-name.outputs.package_name }} --rev="${{github.event.repository.default_branch}},${{github.event.pull_request.head.sha}}" --url=${{ github.event.repository.clone_url }} --bench-on="${{github.event.repository.default_branch}}" --output-dir=results/ --tune
3939
- name: Create markdown table from benchmarks
4040
run: |
41-
benchpkgtable ${{ steps.extract-package-name.outputs.package_name }} --rev="${{github.event.repository.default_branch}},${{github.event.pull_request.head.sha}}" --input-dir=results/ --ratio > table.md
41+
benchpkgtable ${{ steps.extract-package-name.outputs.package_name }} --mode="time,memory" --rev="${{github.event.repository.default_branch}},${{github.event.pull_request.head.sha}}" --input-dir=results/ --ratio > table.md
4242
echo '### Benchmark Results' > body.md
4343
echo '' >> body.md
4444
echo '' >> body.md

src/decompression.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ julia> A = sparse([
2626
2727
julia> result = coloring(A, ColoringProblem(), GreedyColoringAlgorithm());
2828
29-
julia> column_groups(result)
29+
julia> collect.(column_groups(result))
3030
3-element Vector{Vector{Int64}}:
3131
[1, 2, 4]
3232
[3, 5]
@@ -86,7 +86,7 @@ julia> A = sparse([
8686
8787
julia> result = coloring(A, ColoringProblem(), GreedyColoringAlgorithm());
8888
89-
julia> column_groups(result)
89+
julia> collect.(column_groups(result))
9090
3-element Vector{Vector{Int64}}:
9191
[1, 2, 4]
9292
[3, 5]
@@ -152,7 +152,7 @@ julia> A = sparse([
152152
153153
julia> result = coloring(A, ColoringProblem(), GreedyColoringAlgorithm());
154154
155-
julia> column_groups(result)
155+
julia> collect.(column_groups(result))
156156
3-element Vector{Vector{Int64}}:
157157
[1, 2, 4]
158158
[3, 5]
@@ -217,7 +217,7 @@ julia> A = sparse([
217217
218218
julia> result = coloring(A, ColoringProblem(), GreedyColoringAlgorithm());
219219
220-
julia> column_groups(result)
220+
julia> collect.(column_groups(result))
221221
3-element Vector{Vector{Int64}}:
222222
[1, 2, 4]
223223
[3, 5]

src/interface.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ julia> column_colors(result)
156156
2
157157
3
158158
159-
julia> column_groups(result)
159+
julia> collect.(column_groups(result))
160160
3-element Vector{Vector{Int64}}:
161161
[1, 2, 4]
162162
[3, 5]

src/result.jl

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,32 @@ function row_groups end
5858
"""
5959
group_by_color(color::Vector{Int})
6060
61-
Create `group::Vector{Vector{Int}}` such that `i ∈ group[c]` iff `color[i] == c`.
61+
Create a color-indexed vector `group` such that `i ∈ group[c]` iff `color[i] == c`.
6262
6363
Assumes the colors are contiguously numbered from `1` to some `cmax`.
6464
"""
6565
function group_by_color(color::AbstractVector{<:Integer})
6666
cmin, cmax = extrema(color)
67-
@assert cmin == 1
68-
group_sizes = zeros(Int, cmax)
67+
@assert cmin >= 1
68+
# Compute group sizes and offsets for a joint storage
69+
group_sizes = zeros(Int, cmax) # allocation 1, size cmax
6970
for c in color
7071
group_sizes[c] += 1
7172
end
72-
group = [Vector{Int}(undef, group_sizes[c]) for c in 1:cmax]
73-
fill!(group_sizes, 1)
73+
group_offsets = cumsum(group_sizes) # allocation 2, size cmax
74+
# Concatenate all groups inside a single vector
75+
group_flat = similar(color) # allocation 3, size n
7476
for (k, c) in enumerate(color)
75-
pos = group_sizes[c]
76-
group[c][pos] = k
77-
group_sizes[c] += 1
77+
i = group_offsets[c] - group_sizes[c] + 1
78+
group_flat[i] = k
79+
group_sizes[c] -= 1
80+
end
81+
# Create views into contiguous blocks of the group vector
82+
group = Vector{typeof(view(group_flat, 1:1))}(undef, cmax) # allocation 4, size cmax
83+
for c in 1:cmax
84+
i = 1 + (c == 1 ? 0 : group_offsets[c - 1])
85+
j = group_offsets[c]
86+
group[c] = view(group_flat, i:j)
7887
end
7988
return group
8089
end
@@ -110,7 +119,7 @@ $TYPEDFIELDS
110119
111120
- [`AbstractColoringResult`](@ref)
112121
"""
113-
struct ColumnColoringResult{M<:AbstractMatrix,G<:BipartiteGraph} <:
122+
struct ColumnColoringResult{M<:AbstractMatrix,G<:BipartiteGraph,V} <:
114123
AbstractColoringResult{:nonsymmetric,:column,:direct}
115124
"matrix that was colored"
116125
A::M
@@ -119,7 +128,7 @@ struct ColumnColoringResult{M<:AbstractMatrix,G<:BipartiteGraph} <:
119128
"one integer color for each column or row (depending on `partition`)"
120129
color::Vector{Int}
121130
"color groups for columns or rows (depending on `partition`)"
122-
group::Vector{Vector{Int}}
131+
group::V
123132
"flattened indices mapping the compressed matrix `B` to the uncompressed matrix `A` when `A isa SparseMatrixCSC`. They satisfy `nonzeros(A)[k] = vec(B)[compressed_indices[k]]`"
124133
compressed_indices::Vector{Int}
125134
end
@@ -156,12 +165,12 @@ $TYPEDFIELDS
156165
157166
- [`AbstractColoringResult`](@ref)
158167
"""
159-
struct RowColoringResult{M<:AbstractMatrix,G<:BipartiteGraph} <:
168+
struct RowColoringResult{M<:AbstractMatrix,G<:BipartiteGraph,V} <:
160169
AbstractColoringResult{:nonsymmetric,:row,:direct}
161170
A::M
162171
bg::G
163172
color::Vector{Int}
164-
group::Vector{Vector{Int}}
173+
group::V
165174
compressed_indices::Vector{Int}
166175
end
167176

@@ -197,12 +206,12 @@ $TYPEDFIELDS
197206
198207
- [`AbstractColoringResult`](@ref)
199208
"""
200-
struct StarSetColoringResult{M<:AbstractMatrix,G<:AdjacencyGraph} <:
209+
struct StarSetColoringResult{M<:AbstractMatrix,G<:AdjacencyGraph,V} <:
201210
AbstractColoringResult{:symmetric,:column,:direct}
202211
A::M
203212
ag::G
204213
color::Vector{Int}
205-
group::Vector{Vector{Int}}
214+
group::V
206215
star_set::StarSet
207216
compressed_indices::Vector{Int}
208217
end
@@ -241,12 +250,12 @@ $TYPEDFIELDS
241250
242251
- [`AbstractColoringResult`](@ref)
243252
"""
244-
struct TreeSetColoringResult{M<:AbstractMatrix,G<:AdjacencyGraph,R} <:
253+
struct TreeSetColoringResult{M<:AbstractMatrix,G<:AdjacencyGraph,V,R} <:
245254
AbstractColoringResult{:symmetric,:column,:substitution}
246255
A::M
247256
ag::G
248257
color::Vector{Int}
249-
group::Vector{Vector{Int}}
258+
group::V
250259
vertices_by_tree::Vector{Vector{Int}}
251260
reverse_bfs_orders::Vector{Vector{Tuple{Int,Int}}}
252261
buffer::Vector{R}
@@ -395,12 +404,12 @@ $TYPEDFIELDS
395404
396405
- [`AbstractColoringResult`](@ref)
397406
"""
398-
struct LinearSystemColoringResult{M<:AbstractMatrix,G<:AdjacencyGraph,R,F} <:
407+
struct LinearSystemColoringResult{M<:AbstractMatrix,G<:AdjacencyGraph,V,R,F} <:
399408
AbstractColoringResult{:symmetric,:column,:substitution}
400409
A::M
401410
ag::G
402411
color::Vector{Int}
403-
group::Vector{Vector{Int}}
412+
group::V
404413
strict_upper_nonzero_inds::Vector{Tuple{Int,Int}}
405414
strict_upper_nonzeros_A::Vector{R} # TODO: adjust type
406415
T_factorization::F # TODO: adjust type

test/result.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using SparseMatrixColorings: group_by_color
2+
using Test
3+
4+
@testset "Group by color" begin
5+
for n in 10 .^ (2, 3, 4), cmax in (1, 2, 10, 100), iteration in 1:10
6+
color = rand(1:cmax, n)
7+
group = group_by_color(color)
8+
@test length(group) == maximum(color)
9+
@test all(1:maximum(color)) do c
10+
all(color[group[c]] .== c) && issorted(group[c])
11+
end
12+
end
13+
end

test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ include("utils.jl")
4242
@testset "Constructors" begin
4343
include("constructors.jl")
4444
end
45+
@testset "Result" begin
46+
include("result.jl")
47+
end
4548
@testset "Constant coloring" begin
4649
include("constant.jl")
4750
end

0 commit comments

Comments
 (0)