Skip to content

Commit 65fa107

Browse files
authored
More tests (#8)
1 parent ac70240 commit 65fa107

4 files changed

Lines changed: 107 additions & 45 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "SparseMatrixColorings"
22
uuid = "0a514795-09f3-496d-8182-132a7b665d35"
33
authors = ["Guillaume Dalle <22795598+gdalle@users.noreply.github.com>"]
4-
version = "0.3.0"
4+
version = "0.3.1"
55

66
[deps]
77
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"

src/decompression.jl

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ end
3131
colors::AbstractVector{<:Integer}
3232
) where {R<:Real}
3333
34-
Decompress the thin matrix `C` into a fat matrix `A` with the same sparsity pattern as `S`.
34+
Decompress the thin matrix `C` into the fat matrix `A` which must have the same sparsity pattern as `S`.
3535
3636
Here, `colors` is a column coloring of `S`, while `C` is a compressed representation of matrix `A` obtained by summing the columns that share the same color.
3737
"""
@@ -44,11 +44,12 @@ function decompress_columns!(
4444
colors::AbstractVector{<:Integer},
4545
) where {R<:Real}
4646
A .= zero(R)
47-
@views for j in axes(A, 2)
47+
for j in axes(A, 2)
4848
k = colors[j]
49-
rows_j = map(!iszero, S[:, j])
50-
copyto!(A[rows_j, j], C[rows_j, k])
51-
A[rows_j, j] .= C[rows_j, k]
49+
rows_j = (!iszero).(view(S, :, j))
50+
Aj = view(A, rows_j, j)
51+
Cj = view(C, rows_j, k)
52+
copyto!(Aj, Cj)
5253
end
5354
return A
5455
end
@@ -59,14 +60,18 @@ function decompress_columns!(
5960
C::AbstractMatrix{R},
6061
colors::AbstractVector{<:Integer},
6162
) where {R<:Real}
62-
# assume A and S have the same pattern
63+
if nnz(parent(A)) != nnz(parent(S))
64+
throw(DimensionMismatch("`A` and `S` must have the same sparsity pattern."))
65+
end
6366
Anz, Arv = nonzeros(A), rowvals(A)
6467
Anz .= zero(R)
65-
@views for j in axes(A, 2)
68+
for j in axes(A, 2)
6669
k = colors[j]
6770
nzrange_j = nzrange(A, j)
68-
rows_j = Arv[nzrange_j]
69-
copyto!(Anz[nzrange_j], C[rows_j, k])
71+
rows_j = view(Arv, nzrange_j)
72+
Aj = view(Anz, nzrange_j)
73+
Cj = view(C, rows_j, k)
74+
copyto!(Aj, Cj)
7075
end
7176
return A
7277
end
@@ -99,7 +104,7 @@ end
99104
colors::AbstractVector{<:Integer}
100105
) where {R<:Real}
101106
102-
Decompress the small matrix `C` into the tall matrix `A` with the same sparsity pattern as `S`.
107+
Decompress the small matrix `C` into the tall matrix `A` which must have the same sparsity pattern as `S`.
103108
104109
Here, `colors` is a row coloring of `S`, while `C` is a compressed representation of matrix `A` obtained by summing the columns that share the same color.
105110
"""
@@ -112,10 +117,12 @@ function decompress_rows!(
112117
colors::AbstractVector{<:Integer},
113118
) where {R<:Real}
114119
A .= zero(R)
115-
@views for i in axes(A, 1)
120+
for i in axes(A, 1)
116121
k = colors[i]
117-
cols_i = map(!iszero, S[i, :])
118-
copyto!(A[i, cols_i], C[k, cols_i])
122+
cols_i = (!iszero).(view(S, i, :))
123+
Ai = view(A, i, cols_i)
124+
Ci = view(C, k, cols_i)
125+
copyto!(Ai, Ci)
119126
end
120127
return A
121128
end
@@ -126,15 +133,19 @@ function decompress_rows!(
126133
C::AbstractMatrix{R},
127134
colors::AbstractVector{<:Integer},
128135
) where {R<:Real}
129-
# assume A and S have the same pattern
136+
if nnz(parent(A)) != nnz(parent(S))
137+
throw(DimensionMismatch("`A` and `S` must have the same sparsity pattern."))
138+
end
130139
PA = parent(A)
131140
PAnz, PArv = nonzeros(PA), rowvals(PA)
132141
PAnz .= zero(R)
133-
@views for i in axes(A, 1)
142+
for i in axes(A, 1)
134143
k = colors[i]
135144
nzrange_i = nzrange(PA, i)
136-
cols_i = PArv[nzrange_i]
137-
copyto!(PAnz[nzrange_i], C[k, cols_i])
145+
cols_i = view(PArv, nzrange_i)
146+
Ai = view(PAnz, nzrange_i)
147+
Ci = view(C, k, cols_i)
148+
copyto!(Ai, Ci)
138149
end
139150
return A
140151
end

test/decompression_correctness.jl

Lines changed: 76 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,91 @@ rng = StableRNG(63)
1010

1111
algo = GreedyColoringAlgorithm()
1212

13-
m, n = 10, 20
14-
15-
A0 = sprand(rng, Bool, m, n, 0.3)
16-
A1 = Matrix(A0)
17-
A0t = transpose(A0)
18-
A1t = transpose(A1)
19-
20-
S0 = map(!iszero, A0)
21-
S1 = map(!iszero, A1)
22-
S0t = transpose(S0)
23-
S1t = transpose(S1)
24-
2513
@testset "Column decompression" begin
26-
@testset "$(typeof(A))" for (A, S) in zip((A0, A1), (S0, S1))
27-
colors = column_coloring(A, algo)
14+
@testset "Small" begin
15+
A0 = [
16+
1 0 2
17+
0 3 4
18+
5 0 0
19+
]
20+
S0 = Bool[
21+
1 0 1
22+
0 1 1
23+
1 0 0
24+
]
25+
C = [
26+
1 2
27+
3 4
28+
5 0
29+
]
30+
colors = [1, 1, 2]
31+
@testset "$(typeof(A)) - $(typeof(S))" for (A, S) in [
32+
(A0, S0), #
33+
(sparse(A0), sparse(S0)),
34+
]
35+
@test decompress_columns(S, C, colors) == A
36+
end
37+
end
38+
@testset "Medium" begin
39+
m, n = 18, 20
40+
A0 = sprand(rng, Bool, m, n, 0.2)
41+
S0 = map(!iszero, A0)
42+
colors = column_coloring(A0, algo)
2843
groups = color_groups(colors)
29-
@test length(groups[1]) > 1
30-
C = stack(groups) do group
31-
dropdims(sum(A[:, group]; dims=2); dims=2)
44+
C = stack(groups; dims=2) do group
45+
dropdims(sum(A0[:, group]; dims=2); dims=2)
46+
end
47+
@test size(C) == (size(A0, 1), length(groups))
48+
@testset "$(typeof(A)) - $(typeof(S))" for (A, S) in [
49+
(A0, S0), #
50+
(Matrix(A0), Matrix(S0)),
51+
]
52+
@test decompress_columns(S, C, colors) == A
3253
end
33-
A_new = decompress_columns(S, C, colors)
34-
@test A_new == A
3554
end
3655
end
3756

3857
@testset "Row decompression" begin
39-
@testset "$(typeof(At))" for (At, St) in zip((A0t, A1t), (S0t, S1t))
40-
colors = row_coloring(At, algo)
58+
@testset "Small" begin
59+
A0 = [
60+
1 0 3
61+
0 2 0
62+
4 5 0
63+
]
64+
S0 = Bool[
65+
1 0 1
66+
0 1 0
67+
1 1 0
68+
]
69+
C = [
70+
1 2 3
71+
4 5 0
72+
]
73+
colors = [1, 1, 2]
74+
@testset "$(typeof(A)) - $(typeof(S))" for (A, S) in [
75+
(A0, S0), #
76+
(transpose(sparse(transpose(A0))), transpose(sparse(transpose(S0)))),
77+
]
78+
@test decompress_rows(S, C, colors) == A
79+
end
80+
end
81+
@testset "Medium" begin
82+
m, n = 18, 20
83+
A0 = sprand(rng, Bool, m, n, 0.2)
84+
S0 = map(!iszero, A0)
85+
A0t = transpose(A0)
86+
S0t = transpose(S0)
87+
colors = row_coloring(A0t, algo)
4188
groups = color_groups(colors)
42-
@test length(groups[1]) > 1
4389
Ct = stack(groups; dims=1) do group
44-
dropdims(sum(At[group, :]; dims=1); dims=1)
90+
dropdims(sum(A0t[group, :]; dims=1); dims=1)
91+
end
92+
@test size(Ct) == (length(groups), size(A0t, 2))
93+
@testset "$(typeof(At)) - $(typeof(St))" for (At, St) in [
94+
(A0t, S0t), #
95+
(Matrix(A0t), Matrix(S0t)),
96+
]
97+
@test decompress_rows(St, Ct, colors) == At
4598
end
46-
At_new = decompress_rows(St, Ct, colors)
47-
@test At_new == At
4899
end
49100
end

test/runtests.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ using Test
4343
include("decompression_correctness.jl")
4444
end
4545
end
46-
@testset "Performance" begin
46+
@testset verbose = true "Performance" begin
4747
if VERSION >= v"1.10"
4848
@testset "Coloring" begin
4949
include("coloring_performance.jl")
5050
end
5151
end
5252
end
53-
@testset "Comparison" begin
53+
@testset verbose = true "Comparison" begin
5454
@testset "SuiteSparse" begin
5555
include("suitesparse.jl")
5656
end

0 commit comments

Comments
 (0)