Skip to content

Commit c743b22

Browse files
committed
Row coloring
1 parent de4a21a commit c743b22

1 file changed

Lines changed: 33 additions & 18 deletions

File tree

ext/SparseMatrixColoringsBlockBandedMatricesExt.jl

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,43 +46,58 @@ https://github.com/JuliaArrays/ArrayInterface.jl
4646
https://github.com/JuliaDiff/FiniteDiff.jl
4747
=#
4848

49-
function SMC.coloring(
50-
A::BlockBandedMatrix,
51-
::ColoringProblem{:nonsymmetric,:column},
52-
algo::GreedyColoringAlgorithm;
53-
kwargs...,
54-
)
55-
# consider blocks of columns
56-
nb_blocks = blocksize(A, 2)
57-
nb_cols_in_block = blocklengths(axes(A, 2))
58-
first_col_in_block = blockfirsts(axes(A, 2))
59-
last_col_in_block = blocklasts(axes(A, 2))
49+
function blockbanded_coloring(A::BlockBandedMatrix, dim::Integer)
50+
# consider blocks of columns or rows (let's call them vertices) depending on `dim`
51+
nb_blocks = blocksize(A, dim)
52+
nb_in_block = blocklengths(axes(A, dim))
53+
first_in_block = blockfirsts(axes(A, dim))
54+
last_in_block = blocklasts(axes(A, dim))
55+
color = zeros(Int, size(A, dim))
6056

61-
# give a macroscopic color to each block, so that 2 blocks of columns with the same macro color do not intersect
57+
# give a macroscopic color to each block, so that 2 blocks with the same macro color are orthogonal
6258
# same idea as for BandedMatrices
6359
nb_macrocolors = length(blockbandrange(A))
6460
macrocolor = cycle_range(nb_macrocolors, nb_blocks)
6561

6662
# for each macroscopic color, count how many microscopic colors will be needed
67-
# columns within a block are colored naively with all distinct micro colors
63+
# vertices within a block are colored naively with all distinct micro colors
6864
nb_colors_in_macrocolor = [
69-
maximum(nb_cols_in_block[mc:nb_macrocolors:nb_blocks]; init=0) for
70-
mc in 1:nb_macrocolors
65+
maximum(nb_in_block[mc:nb_macrocolors:nb_blocks]; init=0) for mc in 1:nb_macrocolors
7166
]
7267
color_shift_in_macrocolor = vcat(0, cumsum(nb_colors_in_macrocolor)[1:(end - 1)])
7368

7469
# assign a microscopic color to each column as a function of its macroscopic color and its position within the block
75-
color = Vector{Int}(undef, size(A, 2))
7670
for b in 1:nb_blocks
7771
mc = macrocolor[b]
7872
shift = color_shift_in_macrocolor[mc]
79-
for j in first_col_in_block[b]:last_col_in_block[b]
80-
color[j] = shift + (j - first_col_in_block[b] + 1)
73+
for k in first_in_block[b]:last_in_block[b]
74+
color[k] = shift + (k - first_in_block[b] + 1)
8175
end
8276
end
8377

78+
return color
79+
end
80+
81+
function SMC.coloring(
82+
A::BlockBandedMatrix,
83+
::ColoringProblem{:nonsymmetric,:column},
84+
algo::GreedyColoringAlgorithm;
85+
kwargs...,
86+
)
87+
color = blockbanded_coloring(A, 2)
8488
bg = BipartiteGraph(A)
8589
return ColumnColoringResult(A, bg, color)
8690
end
8791

92+
function SMC.coloring(
93+
A::BlockBandedMatrix,
94+
::ColoringProblem{:nonsymmetric,:row},
95+
algo::GreedyColoringAlgorithm;
96+
kwargs...,
97+
)
98+
color = blockbanded_coloring(A, 1)
99+
bg = BipartiteGraph(A)
100+
return RowColoringResult(A, bg, color)
101+
end
102+
88103
end

0 commit comments

Comments
 (0)