-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathinterface.jl
More file actions
421 lines (362 loc) · 14.1 KB
/
interface.jl
File metadata and controls
421 lines (362 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
function check_valid_problem(structure::Symbol, partition::Symbol)
valid = (
(structure == :nonsymmetric && partition in (:column, :row, :bidirectional)) ||
(structure == :symmetric && partition == :column)
)
if !valid
throw(
ArgumentError(
"The combination `($(repr(structure)), $(repr(partition)))` is not supported by `ColoringProblem`.",
),
)
end
end
function check_valid_algorithm(decompression::Symbol)
valid = decompression in (:direct, :substitution)
if !valid
throw(
ArgumentError(
"The setting `decompression=$(repr(decompression))` is not supported by `GreedyColoringAlgorithm`.",
),
)
end
end
"""
ColoringProblem{structure,partition}
Selector type for the coloring problem to solve, enabling multiple dispatch.
It is passed as an argument to the main function [`coloring`](@ref).
# Constructors
ColoringProblem{structure,partition}()
ColoringProblem(; structure=:nonsymmetric, partition=:column)
- `structure::Symbol`: either `:nonsymmetric` or `:symmetric`
- `partition::Symbol`: either `:column`, `:row` or `:bidirectional`
!!! warning
The second constructor (based on keyword arguments) is type-unstable.
# Link to automatic differentiation
Matrix coloring is often used in automatic differentiation, and here is the translation guide:
| matrix | mode | `structure` | `partition` | implemented |
| -------- | ------- | --------------- | ---------------- | ----------- |
| Jacobian | forward | `:nonsymmetric` | `:column` | yes |
| Jacobian | reverse | `:nonsymmetric` | `:row` | yes |
| Jacobian | mixed | `:nonsymmetric` | `:bidirectional` | yes |
| Hessian | - | `:symmetric` | `:column` | yes |
| Hessian | - | `:symmetric` | `:row` | no |
"""
struct ColoringProblem{structure,partition} end
function ColoringProblem(; structure::Symbol=:nonsymmetric, partition::Symbol=:column)
check_valid_problem(structure, partition)
return ColoringProblem{structure,partition}()
end
"""
GreedyColoringAlgorithm{decompression} <: ADTypes.AbstractColoringAlgorithm
Greedy coloring algorithm for sparse matrices which colors columns or rows one after the other, following a configurable order.
It is passed as an argument to the main function [`coloring`](@ref).
# Constructors
GreedyColoringAlgorithm{decompression}(order=NaturalOrder(); postprocessing=false)
GreedyColoringAlgorithm(order=NaturalOrder(); postprocessing=false, decompression=:direct)
- `order::Union{AbstractOrder,Tuple}`: the order in which the columns or rows are colored, which can impact the number of colors. Can also be a tuple of different orders to try out, from which the best order (the one with the lowest total number of colors) will be used.
- `postprocessing::Bool`: whether or not the coloring will be refined by assigning the neutral color `0` to some vertices.
- `decompression::Symbol`: either `:direct` or `:substitution`. Usually `:substitution` leads to fewer colors, at the cost of a more expensive coloring (and decompression). When `:substitution` is not applicable, it falls back on `:direct` decompression.
!!! warning
The second constructor (based on keyword arguments) is type-unstable.
# ADTypes coloring interface
`GreedyColoringAlgorithm` is a subtype of [`ADTypes.AbstractColoringAlgorithm`](@extref ADTypes.AbstractColoringAlgorithm), which means the following methods are also applicable:
- [`ADTypes.column_coloring`](@extref ADTypes.column_coloring)
- [`ADTypes.row_coloring`](@extref ADTypes.row_coloring)
- [`ADTypes.symmetric_coloring`](@extref ADTypes.symmetric_coloring)
See their respective docstrings for details.
# See also
- [`AbstractOrder`](@ref)
- [`decompress`](@ref)
"""
struct GreedyColoringAlgorithm{decompression,N,O<:NTuple{N,AbstractOrder}} <:
ADTypes.AbstractColoringAlgorithm
orders::O
postprocessing::Bool
function GreedyColoringAlgorithm{decompression}(
order_or_orders::Union{AbstractOrder,Tuple}=NaturalOrder();
postprocessing::Bool=false,
) where {decompression}
check_valid_algorithm(decompression)
if order_or_orders isa AbstractOrder
orders = (order_or_orders,)
else
orders = order_or_orders
end
return new{decompression,length(orders),typeof(orders)}(orders, postprocessing)
end
end
function GreedyColoringAlgorithm(
order_or_orders::Union{AbstractOrder,Tuple}=NaturalOrder();
postprocessing::Bool=false,
decompression::Symbol=:direct,
)
return GreedyColoringAlgorithm{decompression}(order_or_orders; postprocessing)
end
## Coloring
abstract type WithOrWithoutResult end
struct WithResult <: WithOrWithoutResult end
struct WithoutResult <: WithOrWithoutResult end
"""
coloring(
S::AbstractMatrix,
problem::ColoringProblem,
algo::GreedyColoringAlgorithm;
[decompression_eltype=Float64, symmetric_pattern=false]
)
Solve a [`ColoringProblem`](@ref) on the matrix `S` with a [`GreedyColoringAlgorithm`](@ref) and return an [`AbstractColoringResult`](@ref).
The result can be used to [`compress`](@ref) and [`decompress`](@ref) a matrix `A` with the same sparsity pattern as `S`.
If `eltype(A) == decompression_eltype`, decompression might be faster.
For a `:nonsymmetric` problem (and only then), setting `symmetric_pattern=true` indicates that the pattern of nonzeros is symmetric.
This condition is weaker than the symmetry of actual values, so it can happen for some Jacobians.
Specifying it allows faster construction of the bipartite graph.
# Example
```jldoctest
julia> using SparseMatrixColorings, SparseArrays
julia> S = sparse([
0 0 1 1 0 1
1 0 0 0 1 0
0 1 0 0 1 0
0 1 1 0 0 0
]);
julia> problem = ColoringProblem(; structure=:nonsymmetric, partition=:column);
julia> algo = GreedyColoringAlgorithm(; decompression=:direct);
julia> result = coloring(S, problem, algo);
julia> column_colors(result)
6-element Vector{Int64}:
1
1
2
1
2
3
julia> collect.(column_groups(result))
3-element Vector{Vector{Int64}}:
[1, 2, 4]
[3, 5]
[6]
```
# See also
- [`ColoringProblem`](@ref)
- [`GreedyColoringAlgorithm`](@ref)
- [`AbstractColoringResult`](@ref)
- [`compress`](@ref)
- [`decompress`](@ref)
"""
function coloring(
A::AbstractMatrix,
problem::ColoringProblem,
algo::GreedyColoringAlgorithm;
decompression_eltype::Type{R}=Float64,
symmetric_pattern::Bool=false,
) where {R}
return _coloring(WithResult(), A, problem, algo, R, symmetric_pattern)
end
"""
fast_coloring(
S::AbstractMatrix,
problem::ColoringProblem,
algo::GreedyColoringAlgorithm;
[symmetric_pattern=false]
)
Solve a [`ColoringProblem`](@ref) on the matrix `S` with a [`GreedyColoringAlgorithm`](@ref) and return
- a single color vector for `:column` and `:row` problems
- a tuple of color vectors for `:bidirectional` problems
This function is very similar to [`coloring`](@ref), but it skips the computation of an [`AbstractColoringResult`](@ref) to speed things up.
# See also
- [`coloring`](@ref)
"""
function fast_coloring(
A::AbstractMatrix,
problem::ColoringProblem,
algo::GreedyColoringAlgorithm;
symmetric_pattern::Bool=false,
)
return _coloring(WithoutResult(), A, problem, algo, Float64, symmetric_pattern)
end
function _coloring(
speed_setting::WithOrWithoutResult,
A::AbstractMatrix,
::ColoringProblem{:nonsymmetric,:column},
algo::GreedyColoringAlgorithm,
decompression_eltype::Type,
symmetric_pattern::Bool;
forced_colors::Union{AbstractVector{<:Integer},Nothing}=nothing,
)
symmetric_pattern = symmetric_pattern || A isa Union{Symmetric,Hermitian}
bg = BipartiteGraph(A; symmetric_pattern)
color_by_order = map(algo.orders) do order
vertices_in_order = vertices(bg, Val(2), order)
return partial_distance2_coloring(bg, Val(2), vertices_in_order; forced_colors)
end
color = argmin(maximum, color_by_order)
if speed_setting isa WithResult
return ColumnColoringResult(A, bg, color)
else
return color
end
end
function _coloring(
speed_setting::WithOrWithoutResult,
A::AbstractMatrix,
::ColoringProblem{:nonsymmetric,:row},
algo::GreedyColoringAlgorithm,
decompression_eltype::Type,
symmetric_pattern::Bool;
forced_colors::Union{AbstractVector{<:Integer},Nothing}=nothing,
)
symmetric_pattern = symmetric_pattern || A isa Union{Symmetric,Hermitian}
bg = BipartiteGraph(A; symmetric_pattern)
color_by_order = map(algo.orders) do order
vertices_in_order = vertices(bg, Val(1), order)
return partial_distance2_coloring(bg, Val(1), vertices_in_order; forced_colors)
end
color = argmin(maximum, color_by_order)
if speed_setting isa WithResult
return RowColoringResult(A, bg, color)
else
return color
end
end
function _coloring(
speed_setting::WithOrWithoutResult,
A::AbstractMatrix,
::ColoringProblem{:symmetric,:column},
algo::GreedyColoringAlgorithm{:direct},
decompression_eltype::Type,
symmetric_pattern::Bool;
forced_colors::Union{AbstractVector{<:Integer},Nothing}=nothing,
)
ag = AdjacencyGraph(A; augmented_graph=false)
color_and_star_set_by_order = map(algo.orders) do order
vertices_in_order = vertices(ag, order)
return star_coloring(ag, vertices_in_order, algo.postprocessing; forced_colors)
end
color, star_set = argmin(maximum ∘ first, color_and_star_set_by_order)
if speed_setting isa WithResult
return StarSetColoringResult(A, ag, color, star_set, :F)
else
return color
end
end
function _coloring(
speed_setting::WithOrWithoutResult,
A::AbstractMatrix,
::ColoringProblem{:symmetric,:column},
algo::GreedyColoringAlgorithm{:substitution},
decompression_eltype::Type{R},
symmetric_pattern::Bool,
) where {R}
ag = AdjacencyGraph(A; augmented_graph=false)
color_and_tree_set_by_order = map(algo.orders) do order
vertices_in_order = vertices(ag, order)
return acyclic_coloring(ag, vertices_in_order, algo.postprocessing)
end
color, tree_set = argmin(maximum ∘ first, color_and_tree_set_by_order)
if speed_setting isa WithResult
return TreeSetColoringResult(A, ag, color, tree_set, R, :F)
else
return color
end
end
function _coloring(
speed_setting::WithOrWithoutResult,
A::AbstractMatrix,
::ColoringProblem{:nonsymmetric,:bidirectional},
algo::GreedyColoringAlgorithm{:direct},
decompression_eltype::Type{R},
symmetric_pattern::Bool;
forced_colors::Union{AbstractVector{<:Integer},Nothing}=nothing,
) where {R}
A_and_Aᵀ, edge_to_index = bidirectional_pattern(A; symmetric_pattern)
ag = AdjacencyGraph(A_and_Aᵀ, edge_to_index, 0; augmented_graph=true)
outputs_by_order = map(algo.orders) do order
vertices_in_order = vertices(ag, order)
_color, _star_set = star_coloring(
ag, vertices_in_order, algo.postprocessing; forced_colors
)
(_row_color, _column_color, _symmetric_to_row, _symmetric_to_column) = remap_colors(
eltype(ag), _color, maximum(_color), size(A)...
)
return (
_color,
_star_set,
_row_color,
_column_color,
_symmetric_to_row,
_symmetric_to_column,
)
end
(color, star_set, row_color, column_color, symmetric_to_row, symmetric_to_column) = argmin(
t -> maximum(t[3]) + maximum(t[4]), outputs_by_order
) # can't use ncolors without computing the full result
if speed_setting isa WithResult
symmetric_result = StarSetColoringResult(A_and_Aᵀ, ag, color, star_set, :L)
return BicoloringResult(
A,
ag,
symmetric_result,
row_color,
column_color,
symmetric_to_row,
symmetric_to_column,
R,
)
else
return row_color, column_color
end
end
function _coloring(
speed_setting::WithOrWithoutResult,
A::AbstractMatrix,
::ColoringProblem{:nonsymmetric,:bidirectional},
algo::GreedyColoringAlgorithm{:substitution},
decompression_eltype::Type{R},
symmetric_pattern::Bool,
) where {R}
A_and_Aᵀ, edge_to_index = bidirectional_pattern(A; symmetric_pattern)
ag = AdjacencyGraph(A_and_Aᵀ, edge_to_index, 0; augmented_graph=true)
outputs_by_order = map(algo.orders) do order
vertices_in_order = vertices(ag, order)
_color, _tree_set = acyclic_coloring(ag, vertices_in_order, algo.postprocessing)
(_row_color, _column_color, _symmetric_to_row, _symmetric_to_column) = remap_colors(
eltype(ag), _color, maximum(_color), size(A)...
)
return (
_color,
_tree_set,
_row_color,
_column_color,
_symmetric_to_row,
_symmetric_to_column,
)
end
(color, tree_set, row_color, column_color, symmetric_to_row, symmetric_to_column) = argmin(
t -> maximum(t[3]) + maximum(t[4]), outputs_by_order
) # can't use ncolors without computing the full result
if speed_setting isa WithResult
symmetric_result = TreeSetColoringResult(A_and_Aᵀ, ag, color, tree_set, R, :L)
return BicoloringResult(
A,
ag,
symmetric_result,
row_color,
column_color,
symmetric_to_row,
symmetric_to_column,
R,
)
else
return row_color, column_color
end
end
## ADTypes interface
function ADTypes.column_coloring(A::AbstractMatrix, algo::GreedyColoringAlgorithm)
return fast_coloring(A, ColoringProblem{:nonsymmetric,:column}(), algo)
end
function ADTypes.row_coloring(A::AbstractMatrix, algo::GreedyColoringAlgorithm)
return fast_coloring(A, ColoringProblem{:nonsymmetric,:row}(), algo)
end
function ADTypes.symmetric_coloring(A::AbstractMatrix, algo::GreedyColoringAlgorithm)
return fast_coloring(A, ColoringProblem{:symmetric,:column}(), algo)
end