Skip to content

Commit 45dcc6b

Browse files
authored
Add systematic allocation and type stability tests (#84)
* Add systematic allocation and type stability tests * Typo
1 parent 0f00909 commit 45dcc6b

5 files changed

Lines changed: 189 additions & 103 deletions

File tree

src/decompression.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,6 @@ function decompress_single_color!(
284284
) where {R<:Real}
285285
@compat (; S, group) = result
286286
check_same_pattern(A, S)
287-
view(A, :, group[c]) .= zero(R)
288287
rvS = rowvals(S)
289288
for j in group[c]
290289
for k in nzrange(S, j)
@@ -347,7 +346,6 @@ function decompress_single_color!(
347346
) where {R<:Real}
348347
@compat (; S, Sᵀ, group) = result
349348
check_same_pattern(A, S)
350-
view(A, group[c], :) .= zero(R)
351349
rvSᵀ = rowvals(Sᵀ)
352350
for i in group[c]
353351
for k in nzrange(Sᵀ, i)

test/allocations.jl

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using Chairmarks
2+
using LinearAlgebra
3+
using SparseArrays
4+
using SparseMatrixColorings
5+
using SparseMatrixColorings: partial_distance2_coloring!
6+
using StableRNGs
7+
using Test
8+
9+
rng = StableRNG(63)
10+
11+
function test_noallocs_distance2_coloring(n)
12+
bench = @be (;
13+
bg=bipartite_graph(sprand(rng, n, n, 5 / n)),
14+
color=Vector{Int}(undef, n),
15+
forbidden_colors=Vector{Int}(undef, n),
16+
) partial_distance2_coloring!(_.color, _.forbidden_colors, _.bg, Val(1), 1:n) evals = 1
17+
@test minimum(bench).allocs == 0
18+
end
19+
20+
@testset "Distance-2 coloring" begin
21+
test_noallocs_distance2_coloring(1000)
22+
end;
23+
24+
function test_noallocs_decompression(
25+
n::Integer; structure::Symbol, partition::Symbol, decompression::Symbol
26+
)
27+
A = sparse(Symmetric(sprand(rng, n, n, 5 / n)))
28+
result = coloring(
29+
A, ColoringProblem(; structure, partition), GreedyColoringAlgorithm(; decompression)
30+
)
31+
B = compress(A, result)
32+
33+
@testset "Full decompression" begin
34+
bench1_full = @be similar(A) decompress!(_, B, result) evals = 1
35+
bench2_full = @be similar(Matrix(A)) decompress!(_, B, result) evals = 1
36+
@test minimum(bench1_full).allocs == 0
37+
@test minimum(bench2_full).allocs == 0
38+
end
39+
@testset "Single-color decompression" begin
40+
if decompression == :direct
41+
b = if partition == :column
42+
B[:, 1]
43+
else
44+
B[1, :]
45+
end
46+
bench1_singlecolor = @be similar(A) decompress_single_color!(_, b, 1, result) evals =
47+
1
48+
bench2_singlecolor = @be similar(Matrix(A)) decompress_single_color!(
49+
_, b, 1, result
50+
) evals = 1
51+
@test minimum(bench1_singlecolor).allocs == 0
52+
@test minimum(bench2_singlecolor).allocs == 0
53+
end
54+
end
55+
@testset "Triangle decompression" begin
56+
if structure == :symmetric
57+
bench1_triangle = @be similar(A) decompress!(_, B, result, :U) evals = 1
58+
bench2_triangle = @be similar(Matrix(A)) decompress!(_, B, result, :U) evals = 1
59+
@test minimum(bench1_triangle).allocs == 0
60+
@test minimum(bench2_triangle).allocs == 0
61+
end
62+
end
63+
@testset "Single-color triangle decompression" begin
64+
if structure == :symmetric && decompression == :direct
65+
b = B[:, 1]
66+
bench1_singlecolor_triangle = @be similar(A) decompress_single_color!(
67+
_, b, 1, result, :U
68+
) evals = 1
69+
bench2_singlecolor_triangle = @be similar(Matrix(A)) decompress_single_color!(
70+
_, b, 1, result, :U
71+
) evals = 1
72+
@test minimum(bench1_singlecolor_triangle).allocs == 0
73+
@test minimum(bench2_singlecolor_triangle).allocs == 0
74+
end
75+
end
76+
end
77+
78+
@testset "Decompression" begin
79+
@testset "$structure - $partition - $decompression" for (
80+
structure, partition, decompression
81+
) in [
82+
(:nonsymmetric, :column, :direct),
83+
(:nonsymmetric, :row, :direct),
84+
(:symmetric, :column, :direct),
85+
(:symmetric, :column, :substitution),
86+
]
87+
test_noallocs_decompression(1000; structure, partition, decompression)
88+
end
89+
end;

test/performance.jl

Lines changed: 0 additions & 100 deletions
This file was deleted.

test/runtests.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ include("utils.jl")
5959
end
6060
@testset verbose = true "Performance" begin
6161
if VERSION >= v"1.10"
62-
include("performance.jl")
62+
@testset "Type stability" begin
63+
include("type_stability.jl")
64+
end
65+
end
66+
@testset "Allocations" begin
67+
include("allocations.jl")
6368
end
6469
end
6570
end

test/type_stability.jl

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using ADTypes: column_coloring, row_coloring, symmetric_coloring
2+
using JET
3+
using LinearAlgebra
4+
using SparseArrays
5+
using SparseMatrixColorings
6+
using SparseMatrixColorings: respectful_similar
7+
using StableRNGs
8+
using Test
9+
10+
rng = StableRNG(63)
11+
12+
@testset "Coloring" begin
13+
n = 10
14+
A = sprand(rng, n, n, 5 / n)
15+
16+
# ADTypes
17+
@testset "ADTypes" begin
18+
@test_opt target_modules = (SparseMatrixColorings,) column_coloring(
19+
A, GreedyColoringAlgorithm()
20+
)
21+
@test_opt target_modules = (SparseMatrixColorings,) row_coloring(
22+
A, GreedyColoringAlgorithm()
23+
)
24+
@test_opt target_modules = (SparseMatrixColorings,) symmetric_coloring(
25+
Symmetric(A), GreedyColoringAlgorithm()
26+
)
27+
end
28+
29+
@testset "$structure - $partition - $decompression" for (
30+
structure, partition, decompression
31+
) in [
32+
(:nonsymmetric, :column, :direct),
33+
(:nonsymmetric, :row, :direct),
34+
(:symmetric, :column, :direct),
35+
(:symmetric, :column, :substitution),
36+
]
37+
@test_opt target_modules = (SparseMatrixColorings,) coloring(
38+
A,
39+
ColoringProblem(; structure, partition),
40+
GreedyColoringAlgorithm(; decompression),
41+
)
42+
end
43+
end
44+
45+
@testset "Decompression" begin
46+
n = 10
47+
A0 = sparse(Symmetric(sprand(rng, n, n, 5 / n)))
48+
49+
@testset "$structure - $partition - $decompression" for (
50+
structure, partition, decompression
51+
) in [
52+
(:nonsymmetric, :column, :direct),
53+
(:nonsymmetric, :row, :direct),
54+
(:symmetric, :column, :direct),
55+
(:symmetric, :column, :substitution),
56+
]
57+
@testset "A::$(typeof(A))" for A in matrix_versions(A0)
58+
result = coloring(
59+
A0,
60+
ColoringProblem(; structure, partition),
61+
GreedyColoringAlgorithm(; decompression);
62+
decompression_eltype=eltype(A),
63+
)
64+
B = compress(A, result)
65+
@testset "Full decompression" begin
66+
@test_opt compress(A, result)
67+
@test_opt decompress(B, result) A0
68+
@test_opt decompress!(respectful_similar(A), B, result)
69+
end
70+
@testset "Single-color decompression" begin
71+
if decompression == :direct
72+
b = if partition == :column
73+
B[:, 1]
74+
else
75+
B[1, :]
76+
end
77+
@test_opt decompress_single_color!(respectful_similar(A), b, 1, result)
78+
end
79+
end
80+
@testset "Triangle decompression" begin
81+
if structure == :symmetric
82+
@test_opt decompress!(respectful_similar(A), B, result, :U)
83+
end
84+
end
85+
@testset "Single-color triangle decompression" begin
86+
if structure == :symmetric && decompression == :direct
87+
@test_opt decompress_single_color!(
88+
respectful_similar(A), B[:, 1], 1, result, :U
89+
)
90+
end
91+
end
92+
end
93+
end
94+
end;

0 commit comments

Comments
 (0)