Skip to content

Commit 69c2d96

Browse files
committed
Define StructuredColoringAlgorithm
1 parent d1f0a51 commit 69c2d96

9 files changed

Lines changed: 89 additions & 46 deletions

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", "Alexis Montoison"]
4-
version = "0.4.21"
4+
version = "0.4.22"
55

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

docs/make.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ using Documenter
22
using DocumenterInterLinks
33
using SparseMatrixColorings
44

5-
links = InterLinks("ADTypes" => "https://sciml.github.io/ADTypes.jl/stable/")
5+
links = InterLinks(
6+
"ADTypes" => "https://sciml.github.io/ADTypes.jl/stable/",
7+
"BandedMatrices" => "https://julialinearalgebra.github.io/BandedMatrices.jl/stable/",
8+
"LinearAlgebra" => "https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/",
9+
)
610

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

docs/src/api.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,14 @@ SparseMatrixColorings
1717
coloring
1818
fast_coloring
1919
ColoringProblem
20+
```
21+
22+
## Coloring algorithms
23+
24+
```@docs
2025
GreedyColoringAlgorithm
2126
ConstantColoringAlgorithm
27+
StructuredColoringAlgorithm
2228
```
2329

2430
## Result analysis

ext/SparseMatrixColoringsBandedMatricesExt.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ using SparseMatrixColorings:
55
BipartiteGraph,
66
ColoringProblem,
77
ColumnColoringResult,
8-
GreedyColoringAlgorithm,
8+
StructuredColoringAlgorithm,
99
RowColoringResult,
1010
column_colors,
1111
cycle_range,
@@ -21,7 +21,7 @@ https://github.com/JuliaDiff/FiniteDiff.jl
2121
function SMC.coloring(
2222
A::BandedMatrix,
2323
::ColoringProblem{:nonsymmetric,:column},
24-
algo::GreedyColoringAlgorithm;
24+
::StructuredColoringAlgorithm;
2525
kwargs...,
2626
)
2727
width = length(bandrange(A))
@@ -33,7 +33,7 @@ end
3333
function SMC.coloring(
3434
A::BandedMatrix,
3535
::ColoringProblem{:nonsymmetric,:row},
36-
algo::GreedyColoringAlgorithm;
36+
::StructuredColoringAlgorithm;
3737
kwargs...,
3838
)
3939
width = length(bandrange(A))

ext/SparseMatrixColoringsBlockBandedMatricesExt.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ using SparseMatrixColorings:
1313
BipartiteGraph,
1414
ColoringProblem,
1515
ColumnColoringResult,
16-
GreedyColoringAlgorithm,
16+
StructuredColoringAlgorithm,
1717
RowColoringResult,
1818
column_colors,
1919
cycle_range,
@@ -75,7 +75,7 @@ end
7575
function SMC.coloring(
7676
A::Union{BlockBandedMatrix,BandedBlockBandedMatrix},
7777
::ColoringProblem{:nonsymmetric,:column},
78-
algo::GreedyColoringAlgorithm;
78+
::StructuredColoringAlgorithm;
7979
kwargs...,
8080
)
8181
color = blockbanded_coloring(A, 2)
@@ -86,7 +86,7 @@ end
8686
function SMC.coloring(
8787
A::Union{BlockBandedMatrix,BandedBlockBandedMatrix},
8888
::ColoringProblem{:nonsymmetric,:row},
89-
algo::GreedyColoringAlgorithm;
89+
::StructuredColoringAlgorithm;
9090
kwargs...,
9191
)
9292
color = blockbanded_coloring(A, 1)

src/SparseMatrixColorings.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export NaturalOrder, RandomOrder, LargestFirst
6666
export DynamicDegreeBasedOrder, SmallestLast, IncidenceDegree, DynamicLargestFirst
6767
export PerfectEliminationOrder
6868
export ColoringProblem, GreedyColoringAlgorithm, AbstractColoringResult
69-
export ConstantColoringAlgorithm
69+
export ConstantColoringAlgorithm, StructuredColoringAlgorithm
7070
export coloring, fast_coloring
7171
export column_colors, row_colors, ncolors
7272
export column_groups, row_groups

src/structured.jl

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
"""
2+
StructuredColoringAlgorithm <: ADTypes.AbstractColoringAlgorithm
3+
4+
Coloring algorithm which leverages specific matrix structures to produce optimal or near-optimal solutions.
5+
6+
The following matrix types are supported:
7+
8+
- From the standard library `LinearAlgebra`: [`Diagonal`](@extref LinearAlgebra.Diagonal), [`Bidiagonal`](@extref LinearAlgebra.Bidiagonal), [`Tridiagonal`](@extref LinearAlgebra.Tridiagonal)
9+
- From [BandedMatrices.jl](https://github.com/JuliaLinearAlgebra/BandedMatrices.jl): [`BandedMatrix`](@extref BandedMatrices.BandedMatrix)
10+
11+
!!! warning
12+
Only `:nonsymmetric` structures with `:row` or `:column` partitions (aka unidirectional Jacobian colorings) are supported by this algorithm at the moment.
13+
14+
!!! tip
15+
To request support for a new type of structured matrix, open an issue on the SparseMatrixColorings.jl GitHub repository!
16+
"""
17+
struct StructuredColoringAlgorithm <: ADTypes.AbstractColoringAlgorithm end
18+
119
#=
220
This code is partly taken from ArrayInterface.jl
321
https://github.com/JuliaArrays/ArrayInterface.jl
@@ -21,7 +39,7 @@ end
2139
function coloring(
2240
A::Diagonal,
2341
::ColoringProblem{:nonsymmetric,:column},
24-
algo::GreedyColoringAlgorithm;
42+
::StructuredColoringAlgorithm;
2543
kwargs...,
2644
)
2745
color = fill(1, size(A, 2))
@@ -32,7 +50,7 @@ end
3250
function coloring(
3351
A::Diagonal,
3452
::ColoringProblem{:nonsymmetric,:row},
35-
algo::GreedyColoringAlgorithm;
53+
::StructuredColoringAlgorithm;
3654
kwargs...,
3755
)
3856
color = fill(1, size(A, 1))
@@ -61,7 +79,7 @@ end
6179
function coloring(
6280
A::Bidiagonal,
6381
::ColoringProblem{:nonsymmetric,:column},
64-
algo::GreedyColoringAlgorithm;
82+
::StructuredColoringAlgorithm;
6583
kwargs...,
6684
)
6785
color = cycle_range(2, size(A, 2))
@@ -72,7 +90,7 @@ end
7290
function coloring(
7391
A::Bidiagonal,
7492
::ColoringProblem{:nonsymmetric,:row},
75-
algo::GreedyColoringAlgorithm;
93+
::StructuredColoringAlgorithm;
7694
kwargs...,
7795
)
7896
color = cycle_range(2, size(A, 1))
@@ -113,7 +131,7 @@ end
113131
function coloring(
114132
A::Tridiagonal,
115133
::ColoringProblem{:nonsymmetric,:column},
116-
algo::GreedyColoringAlgorithm;
134+
::StructuredColoringAlgorithm;
117135
kwargs...,
118136
)
119137
color = cycle_range(3, size(A, 2))
@@ -124,7 +142,7 @@ end
124142
function coloring(
125143
A::Tridiagonal,
126144
::ColoringProblem{:nonsymmetric,:row},
127-
algo::GreedyColoringAlgorithm;
145+
::StructuredColoringAlgorithm;
128146
kwargs...,
129147
)
130148
color = cycle_range(3, size(A, 1))

test/structured.jl

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,65 @@ using SparseMatrixColorings
66
using Test
77

88
@testset "Diagonal" begin
9-
for n in (1, 2, 10, 100)
10-
A = Diagonal(rand(n))
11-
test_structured_coloring_decompression(A)
9+
@testset for algo in [GreedyColoringAlgorithm(), StructuredColoringAlgorithm()]
10+
for n in (1, 2, 10, 100)
11+
A = Diagonal(rand(n))
12+
test_structured_coloring_decompression(A, algo)
13+
end
1214
end
1315
end;
1416

1517
@testset "Bidiagonal" begin
16-
for n in (2, 10, 100)
17-
A1 = Bidiagonal(rand(n), rand(n - 1), :U)
18-
A2 = Bidiagonal(rand(n), rand(n - 1), :L)
19-
test_structured_coloring_decompression(A1)
20-
test_structured_coloring_decompression(A2)
18+
@testset for algo in [GreedyColoringAlgorithm(), StructuredColoringAlgorithm()]
19+
for n in (2, 10, 100)
20+
A1 = Bidiagonal(rand(n), rand(n - 1), :U)
21+
A2 = Bidiagonal(rand(n), rand(n - 1), :L)
22+
test_structured_coloring_decompression(A1, algo)
23+
test_structured_coloring_decompression(A2, algo)
24+
end
2125
end
2226
end;
2327

2428
@testset "Tridiagonal" begin
25-
for n in (2, 10, 100)
26-
A = Tridiagonal(rand(n - 1), rand(n), rand(n - 1))
27-
test_structured_coloring_decompression(A)
29+
@testset for algo in [GreedyColoringAlgorithm(), StructuredColoringAlgorithm()]
30+
for n in (2, 10, 100)
31+
A = Tridiagonal(rand(n - 1), rand(n), rand(n - 1))
32+
test_structured_coloring_decompression(A, algo)
33+
end
2834
end
2935
end;
3036

3137
@testset "BandedMatrices" begin
32-
@testset for (m, n) in [(10, 20), (20, 10)], l in 0:5, u in 0:5
33-
A = brand(m, n, l, u)
34-
test_structured_coloring_decompression(A)
38+
@testset for algo in [GreedyColoringAlgorithm(), StructuredColoringAlgorithm()]
39+
@testset for (m, n) in [(10, 20), (20, 10)], l in 0:5, u in 0:5
40+
A = brand(m, n, l, u)
41+
test_structured_coloring_decompression(A, algo)
42+
end
3543
end
3644
end;
3745

3846
@testset "BlockBandedMatrices" begin
39-
for (mb, nb) in [(10, 20), (20, 10)], lb in 0:3, ub in 0:3, _ in 1:10
40-
rows = rand(1:5, mb)
41-
cols = rand(1:5, nb)
42-
A = BlockBandedMatrix{Float64}(rand(sum(rows), sum(cols)), rows, cols, (lb, ub))
43-
test_structured_coloring_decompression(A)
47+
@testset for algo in [GreedyColoringAlgorithm(), StructuredColoringAlgorithm()]
48+
for (mb, nb) in [(10, 20), (20, 10)], lb in 0:3, ub in 0:3, _ in 1:10
49+
rows = rand(1:5, mb)
50+
cols = rand(1:5, nb)
51+
A = BlockBandedMatrix{Float64}(rand(sum(rows), sum(cols)), rows, cols, (lb, ub))
52+
test_structured_coloring_decompression(A, algo)
53+
end
4454
end
4555
end;
4656

4757
@testset "BandedBlockBandedMatrices" begin
48-
for (mb, nb) in [(10, 20), (20, 10)], lb in 0:3, ub in 0:3, _ in 1:10
49-
rows = rand(5:10, mb)
50-
cols = rand(5:10, nb)
51-
λ = rand(0:5)
52-
μ = rand(0:5)
53-
A = BandedBlockBandedMatrix{Float64}(
54-
rand(sum(rows), sum(cols)), rows, cols, (lb, ub), (λ, μ)
55-
)
56-
test_structured_coloring_decompression(A)
58+
@testset for algo in [GreedyColoringAlgorithm(), StructuredColoringAlgorithm()]
59+
for (mb, nb) in [(10, 20), (20, 10)], lb in 0:3, ub in 0:3, _ in 1:10
60+
rows = rand(5:10, mb)
61+
cols = rand(5:10, nb)
62+
λ = rand(0:5)
63+
μ = rand(0:5)
64+
A = BandedBlockBandedMatrix{Float64}(
65+
rand(sum(rows), sum(cols)), rows, cols, (lb, ub), (λ, μ)
66+
)
67+
test_structured_coloring_decompression(A, algo)
68+
end
5769
end
5870
end;

test/utils.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,11 @@ function test_bicoloring_decompression(
218218
end
219219
end
220220

221-
function test_structured_coloring_decompression(A::AbstractMatrix)
221+
function test_structured_coloring_decompression(
222+
A::AbstractMatrix, algo=StructuredColoringAlgorithm()
223+
)
222224
column_problem = ColoringProblem(; structure=:nonsymmetric, partition=:column)
223225
row_problem = ColoringProblem(; structure=:nonsymmetric, partition=:row)
224-
algo = GreedyColoringAlgorithm()
225226

226227
# Column
227228
result = coloring(A, column_problem, algo)
@@ -231,7 +232,9 @@ function test_structured_coloring_decompression(A::AbstractMatrix)
231232
@test D == A
232233
@test nameof(typeof(D)) == nameof(typeof(A))
233234
@test structurally_orthogonal_columns(A, color)
234-
@test color == ArrayInterface.matrix_colors(A)
235+
if algo isa StructuredColoringAlgorithm
236+
@test color == ArrayInterface.matrix_colors(A)
237+
end
235238

236239
# Row
237240
result = coloring(A, row_problem, algo)

0 commit comments

Comments
 (0)