Skip to content

Commit 3d31ff0

Browse files
authored
Type-stable compress and random order in benchmarks (#198)
* Type-stable compress and random order in benchmarks * Ensure zero size * Mat
1 parent 398490d commit 3d31ff0

File tree

3 files changed

+52
-19
lines changed

3 files changed

+52
-19
lines changed

benchmark/benchmarks.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ for structure in [:nonsymmetric, :symmetric],
1919
p in [2 / n, 5 / n, 10 / n]
2020

2121
problem = ColoringProblem(; structure, partition)
22-
algo = GreedyColoringAlgorithm(; decompression, postprocessing=true)
22+
algo = GreedyColoringAlgorithm(
23+
RandomOrder(StableRNG(0), 0); decompression, postprocessing=true
24+
)
2325

2426
# use several random matrices to reduce variance
2527
nb_samples = 5

src/decompression.jl

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ function compress end
4747
function compress(A, result::AbstractColoringResult{structure,:column}) where {structure}
4848
group = column_groups(result)
4949
if isempty(group)
50-
B = similar(A, size(A, 1), 0)
50+
# ensure we get a Matrix and not a SparseMatrixCSC
51+
B_model = stack([Int[]]; dims=2) do g
52+
dropdims(sum(A[:, g]; dims=2); dims=2)
53+
end
54+
B = similar(B_model, size(A, 1), 0)
5155
else
5256
B = stack(group; dims=2) do g
5357
dropdims(sum(A[:, g]; dims=2); dims=2)
@@ -59,7 +63,11 @@ end
5963
function compress(A, result::AbstractColoringResult{structure,:row}) where {structure}
6064
group = row_groups(result)
6165
if isempty(group)
62-
B = similar(A, 0, size(A, 2))
66+
# ensure we get a Matrix and not a SparseMatrixCSC
67+
B_model = stack([Int[]]; dims=1) do g
68+
dropdims(sum(A[g, :]; dims=1); dims=1)
69+
end
70+
B = similar(B_model, 0, size(A, 2))
6371
else
6472
B = stack(group; dims=1) do g
6573
dropdims(sum(A[g, :]; dims=1); dims=1)
@@ -74,14 +82,22 @@ function compress(
7482
row_group = row_groups(result)
7583
column_group = column_groups(result)
7684
if isempty(row_group)
77-
Br = similar(A, 0, size(A, 2))
85+
# ensure we get a Matrix and not a SparseMatrixCSC
86+
Br_model = stack([Int[]]; dims=1) do g
87+
dropdims(sum(A[g, :]; dims=1); dims=1)
88+
end
89+
Br = similar(Br_model, 0, size(A, 2))
7890
else
7991
Br = stack(row_group; dims=1) do g
8092
dropdims(sum(A[g, :]; dims=1); dims=1)
8193
end
8294
end
8395
if isempty(column_group)
84-
Bc = similar(A, size(A, 1), 0)
96+
# ensure we get a Matrix and not a SparseMatrixCSC
97+
Bc_model = stack([Int[]]; dims=2) do g
98+
dropdims(sum(A[:, g]; dims=2); dims=2)
99+
end
100+
Bc = similar(Bc_model, size(A, 1), 0)
85101
else
86102
Bc = stack(column_group; dims=2) do g
87103
dropdims(sum(A[:, g]; dims=2); dims=2)

test/type_stability.jl

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,17 @@ rng = StableRNG(63)
1111

1212
@testset "Sparse coloring" begin
1313
n = 10
14-
A = sprand(rng, n, n, 5 / n)
14+
A = sparse(Symmetric(sprand(rng, n, n, 5 / n)))
1515

1616
# ADTypes
1717
@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-
)
18+
@test_opt column_coloring(A, GreedyColoringAlgorithm())
19+
@test_opt row_coloring(A, GreedyColoringAlgorithm())
20+
@test_opt symmetric_coloring(Symmetric(A), GreedyColoringAlgorithm())
21+
22+
@inferred column_coloring(A, GreedyColoringAlgorithm())
23+
@inferred row_coloring(A, GreedyColoringAlgorithm())
24+
@inferred symmetric_coloring(Symmetric(A), GreedyColoringAlgorithm())
2725
end
2826

2927
@testset "$structure - $partition - $decompression" for (
@@ -36,7 +34,12 @@ rng = StableRNG(63)
3634
(:nonsymmetric, :bidirectional, :direct),
3735
(:nonsymmetric, :bidirectional, :substitution),
3836
]
39-
@test_opt target_modules = (SparseMatrixColorings,) coloring(
37+
@test_opt coloring(
38+
A,
39+
ColoringProblem(; structure, partition),
40+
GreedyColoringAlgorithm(; decompression),
41+
)
42+
@inferred coloring(
4043
A,
4144
ColoringProblem(; structure, partition),
4245
GreedyColoringAlgorithm(; decompression),
@@ -55,7 +58,12 @@ end;
5558
(structure, partition, decompression) in
5659
[(:nonsymmetric, :column, :direct), (:nonsymmetric, :row, :direct)]
5760

58-
@test_opt target_modules = (SparseMatrixColorings,) coloring(
61+
@test_opt coloring(
62+
A,
63+
ColoringProblem(; structure, partition),
64+
GreedyColoringAlgorithm(; decompression),
65+
)
66+
@inferred coloring(
5967
A,
6068
ColoringProblem(; structure, partition),
6169
GreedyColoringAlgorithm(; decompression),
@@ -88,15 +96,21 @@ end;
8896
Br, Bc = compress(A, result)
8997
@testset "Full decompression" begin
9098
@test_opt compress(A, result)
91-
@test_opt decompress(Br, Bc, result) A0
99+
@test_opt decompress(Br, Bc, result)
92100
@test_opt decompress!(respectful_similar(A), Br, Bc, result)
101+
102+
@inferred compress(A, result)
103+
@inferred decompress(Br, Bc, result)
93104
end
94105
else
95106
B = compress(A, result)
96107
@testset "Full decompression" begin
97108
@test_opt compress(A, result)
98-
@test_opt decompress(B, result) A0
109+
@test_opt decompress(B, result)
99110
@test_opt decompress!(respectful_similar(A), B, result)
111+
112+
@inferred compress(A, result)
113+
@inferred decompress(B, result)
100114
end
101115
@testset "Single-color decompression" begin
102116
if decompression == :direct
@@ -145,5 +159,6 @@ end;
145159
)
146160
B = compress(A, result)
147161
@test_opt decompress(B, result)
162+
@inferred decompress(B, result)
148163
end
149164
end;

0 commit comments

Comments
 (0)