@@ -3,6 +3,7 @@ module SparseMatrixColoringsBlockBandedMatricesExt
33if isdefined (Base, :get_extension )
44 using BlockArrays: blockaxes, blockfirsts, blocklasts, blocksize, blocklengths
55 using BlockBandedMatrices:
6+ BandedBlockBandedMatrix,
67 BlockBandedMatrix,
78 blockbandrange,
89 blockbandwidths,
@@ -22,6 +23,7 @@ if isdefined(Base, :get_extension)
2223else
2324 using .. BlockArrays: blockaxes, blockfirsts, blocklasts, blocksize, blocklengths
2425 using .. BlockBandedMatrices:
26+ BandedBlockBandedMatrix,
2527 BlockBandedMatrix,
2628 blockbandrange,
2729 blockbandwidths,
@@ -46,9 +48,14 @@ https://github.com/JuliaArrays/ArrayInterface.jl
4648https://github.com/JuliaDiff/FiniteDiff.jl
4749=#
4850
49- # # BlockBandedMatrix
51+ function subblockbandrange (A:: BandedBlockBandedMatrix )
52+ u, l = subblockbandwidths (A)
53+ return (- l): u
54+ end
5055
51- function blockbanded_coloring (A:: BlockBandedMatrix , dim:: Integer )
56+ function blockbanded_coloring (
57+ A:: Union{BlockBandedMatrix,BandedBlockBandedMatrix} , dim:: Integer
58+ )
5259 # consider blocks of columns or rows (let's call them vertices) depending on `dim`
5360 nb_blocks = blocksize (A, dim)
5461 nb_in_block = blocklengths (axes (A, dim))
@@ -61,27 +68,34 @@ function blockbanded_coloring(A::BlockBandedMatrix, dim::Integer)
6168 nb_macrocolors = length (blockbandrange (A))
6269 macrocolor = cycle_range (nb_macrocolors, nb_blocks)
6370
71+ width = if A isa BandedBlockBandedMatrix
72+ # vertices within a block are colored cleverly using bands
73+ length (subblockbandrange (A))
74+ else
75+ # vertices within a block are colored naively with distinct micro colors (~ infinite band width)
76+ minimum (size (A))
77+ end
78+
6479 # for each macroscopic color, count how many microscopic colors will be needed
65- # vertices within a block are colored naively with all distinct micro colors
66- nb_colors_in_macrocolor = [
67- maximum (nb_in_block[mc: nb_macrocolors: nb_blocks]; init= 0 ) for mc in 1 : nb_macrocolors
68- ]
80+ nb_colors_in_macrocolor = zeros (Int, nb_macrocolors)
81+ for mc in 1 : nb_macrocolors
82+ largest_nb_in_macrocolor = maximum (nb_in_block[mc: nb_macrocolors: nb_blocks]; init= 0 )
83+ nb_colors_in_macrocolor[mc] = min (width, largest_nb_in_macrocolor)
84+ end
6985 color_shift_in_macrocolor = vcat (0 , cumsum (nb_colors_in_macrocolor)[1 : (end - 1 )])
7086
7187 # assign a microscopic color to each column as a function of its macroscopic color and its position within the block
7288 for b in 1 : nb_blocks
73- mc = macrocolor[b]
74- shift = color_shift_in_macrocolor[mc]
75- for k in first_in_block[b]: last_in_block[b]
76- color[k] = shift + (k - first_in_block[b] + 1 )
77- end
89+ block_color = cycle_range (width, nb_in_block[b])
90+ shift = color_shift_in_macrocolor[macrocolor[b]]
91+ color[first_in_block[b]: last_in_block[b]] .= shift .+ block_color
7892 end
7993
8094 return color
8195end
8296
8397function SMC. coloring (
84- A:: BlockBandedMatrix ,
98+ A:: Union{ BlockBandedMatrix,BandedBlockBandedMatrix} ,
8599 :: ColoringProblem{:nonsymmetric,:column} ,
86100 algo:: GreedyColoringAlgorithm ;
87101 kwargs... ,
@@ -92,7 +106,7 @@ function SMC.coloring(
92106end
93107
94108function SMC. coloring (
95- A:: BlockBandedMatrix ,
109+ A:: Union{ BlockBandedMatrix,BandedBlockBandedMatrix} ,
96110 :: ColoringProblem{:nonsymmetric,:row} ,
97111 algo:: GreedyColoringAlgorithm ;
98112 kwargs... ,
@@ -102,6 +116,4 @@ function SMC.coloring(
102116 return RowColoringResult (A, bg, color)
103117end
104118
105-
106-
107119end
0 commit comments