-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathArrayInterfaceBlockBandedMatricesExt.jl
More file actions
246 lines (236 loc) · 8.48 KB
/
ArrayInterfaceBlockBandedMatricesExt.jl
File metadata and controls
246 lines (236 loc) · 8.48 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
module ArrayInterfaceBlockBandedMatricesExt
using ArrayInterface
using ArrayInterface: BandedMatrixIndex
using BlockBandedMatrices
using BlockBandedMatrices.BlockArrays
struct BlockBandedMatrixIndex <: ArrayInterface.MatrixIndex
count::Int
refinds::Array{Int,1}
refcoords::Array{Int,1}# storing col or row inds at ref points
isrow::Bool
end
Base.firstindex(i::BlockBandedMatrixIndex) = 1
Base.lastindex(i::BlockBandedMatrixIndex) = i.count
Base.length(i::BlockBandedMatrixIndex) = lastindex(i)
function BlockBandedMatrixIndex(nrowblock, ncolblock, rowsizes, colsizes, l, u)
blockrowind = BandedMatrixIndex(nrowblock, ncolblock, l, u, true)
blockcolind = BandedMatrixIndex(nrowblock, ncolblock, l, u, false)
sortedinds = sort(
[(blockrowind[i], blockcolind[i]) for i = 1:length(blockrowind)],
by=x -> x[1],
)
sort!(sortedinds, by=x -> x[2], alg=InsertionSort)# stable sort keeps the second index in order
refinds = Array{Int,1}()
refrowcoords = Array{Int,1}()
refcolcoords = Array{Int,1}()
rowheights = pushfirst!(copy(rowsizes), 1)
cumsum!(rowheights, rowheights)
blockheight = 0
blockrow = 1
blockcol = 1
currenti = 1
lastrowind = sortedinds[1][1] - 1
lastcolind = sortedinds[1][2]
for ind in sortedinds
rowind, colind = ind
if colind == lastcolind
if rowind > lastrowind
blockheight += rowsizes[rowind]
end
else
for j = blockcol:blockcol+colsizes[lastcolind]-1
push!(refinds, currenti)
push!(refrowcoords, blockrow)
push!(refcolcoords, j)
currenti += blockheight
end
blockcol += colsizes[lastcolind]
blockrow = rowheights[rowind]
blockheight = rowsizes[rowind]
end
lastcolind = colind
lastrowind = rowind
end
for j = blockcol:blockcol+colsizes[lastcolind]-1
push!(refinds, currenti)
push!(refrowcoords, blockrow)
push!(refcolcoords, j)
currenti += blockheight
end
push!(refinds, currenti)# guard
push!(refrowcoords, -1)
push!(refcolcoords, -1)
rowindobj = BlockBandedMatrixIndex(currenti - 1, refinds, refrowcoords, true)
colindobj = BlockBandedMatrixIndex(currenti - 1, refinds, refcolcoords, false)
rowindobj, colindobj
end
Base.@propagate_inbounds function Base.getindex(ind::BlockBandedMatrixIndex, i::Int)
@boundscheck 1 <= i <= ind.count || throw(BoundsError(ind, i))
p = 1
while i - ind.refinds[p] >= 0
p += 1
end
p -= 1
_i = i - ind.refinds[p]
if ind.isrow
return ind.refcoords[p] + _i
else
return ind.refcoords[p]
end
end
function ArrayInterface.findstructralnz(x::BlockBandedMatrices.BlockBandedMatrix)
l, u = BlockBandedMatrices.blockbandwidths(x)
nrowblock = BlockBandedMatrices.blocksize(x, 1)
ncolblock = BlockBandedMatrices.blocksize(x, 2)
rowsizes = BlockArrays.blocklengths(axes(x, 1))
colsizes = BlockArrays.blocklengths(axes(x, 2))
return BlockBandedMatrixIndex(
nrowblock,
ncolblock,
rowsizes,
colsizes,
l,
u,
)
end
struct BandedBlockBandedMatrixIndex <: ArrayInterface.MatrixIndex
count::Int
refinds::Array{Int,1}
refcoords::Array{Int,1}# storing col or row inds at ref points
reflocalinds::Array{BandedMatrixIndex,1}
isrow::Bool
end
Base.firstindex(i::BandedBlockBandedMatrixIndex) = 1
Base.lastindex(i::BandedBlockBandedMatrixIndex) = i.count
Base.length(i::BandedBlockBandedMatrixIndex) = lastindex(i)
Base.@propagate_inbounds function Base.getindex(ind::BandedBlockBandedMatrixIndex, i::Int)
@boundscheck 1 <= i <= ind.count || throw(BoundsError(ind, i))
p = 1
while i - ind.refinds[p] >= 0
p += 1
end
p -= 1
_i = i - ind.refinds[p] + 1
ind.reflocalinds[p][_i] + ind.refcoords[p] - 1
end
function BandedBlockBandedMatrixIndex(
nrowblock,
ncolblock,
rowsizes,
colsizes,
l,
u,
lambda,
mu,
)
blockrowind = BandedMatrixIndex(nrowblock, ncolblock, l, u, true)
blockcolind = BandedMatrixIndex(nrowblock, ncolblock, l, u, false)
sortedinds = sort(
[(blockrowind[i], blockcolind[i]) for i = 1:length(blockrowind)],
by=x -> x[1],
)
sort!(sortedinds, by=x -> x[2], alg=InsertionSort)# stable sort keeps the second index in order
rowheights = pushfirst!(copy(rowsizes), 1)
cumsum!(rowheights, rowheights)
colwidths = pushfirst!(copy(colsizes), 1)
cumsum!(colwidths, colwidths)
currenti = 1
refinds = Array{Int,1}()
refrowcoords = Array{Int,1}()
refcolcoords = Array{Int,1}()
reflocalrowinds = Array{BandedMatrixIndex,1}()
reflocalcolinds = Array{BandedMatrixIndex,1}()
for ind in sortedinds
rowind, colind = ind
localrowind =
BandedMatrixIndex(rowsizes[rowind], colsizes[colind], lambda, mu, true)
localcolind =
BandedMatrixIndex(rowsizes[rowind], colsizes[colind], lambda, mu, false)
push!(refinds, currenti)
push!(refrowcoords, rowheights[rowind])
push!(refcolcoords, colwidths[colind])
push!(reflocalrowinds, localrowind)
push!(reflocalcolinds, localcolind)
currenti += localrowind.count
end
push!(refinds, currenti)
push!(refrowcoords, -1)
push!(refcolcoords, -1)
rowindobj = BandedBlockBandedMatrixIndex(
currenti - 1,
refinds,
refrowcoords,
reflocalrowinds,
true,
)
colindobj = BandedBlockBandedMatrixIndex(
currenti - 1,
refinds,
refcolcoords,
reflocalcolinds,
false,
)
rowindobj, colindobj
end
function ArrayInterface.findstructralnz(x::BlockBandedMatrices.BandedBlockBandedMatrix)
l, u = BlockBandedMatrices.blockbandwidths(x)
lambda, mu = BlockBandedMatrices.subblockbandwidths(x)
nrowblock = BlockBandedMatrices.blocksize(x, 1)
ncolblock = BlockBandedMatrices.blocksize(x, 2)
rowsizes = BlockArrays.blocklengths(axes(x, 1))
colsizes = BlockArrays.blocklengths(axes(x, 2))
return BandedBlockBandedMatrixIndex(
nrowblock,
ncolblock,
rowsizes,
colsizes,
l,
u,
lambda,
mu,
)
end
ArrayInterface.has_sparsestruct(::Type{<:BlockBandedMatrices.BlockBandedMatrix}) = true
ArrayInterface.has_sparsestruct(::Type{<:BlockBandedMatrices.BandedBlockBandedMatrix}) = true
ArrayInterface.isstructured(::Type{<:BlockBandedMatrices.BlockBandedMatrix}) = true
ArrayInterface.isstructured(::Type{<:BlockBandedMatrices.BandedBlockBandedMatrix}) = true
ArrayInterface.fast_matrix_colors(::Type{<:BlockBandedMatrices.BlockBandedMatrix}) = true
ArrayInterface.fast_matrix_colors(::Type{<:BlockBandedMatrices.BandedBlockBandedMatrix}) = true
function ArrayInterface.matrix_colors(A::BlockBandedMatrices.BlockBandedMatrix)
l, u = BlockBandedMatrices.blockbandwidths(A)
blockwidth = l + u + 1
nblock = BlockBandedMatrices.blocksize(A, 2)
cols = BlockArrays.blocklengths(axes(A, 2))
blockcolors = ArrayInterface._cycle(1:blockwidth, nblock)
# the reserved number of colors of a block is the maximum length of columns of blocks with the same block color
ncolors = [maximum(cols[i:blockwidth:nblock]) for i = 1:blockwidth]
endinds = cumsum(ncolors)
startinds = [endinds[i] - ncolors[i] + 1 for i = 1:blockwidth]
colors = [
(startinds[blockcolors[i]]:endinds[blockcolors[i]])[1:cols[i]]
for i = 1:nblock
]
return reduce(vcat, colors)
end
function ArrayInterface.matrix_colors(A::BlockBandedMatrices.BandedBlockBandedMatrix)
l, u = BlockBandedMatrices.blockbandwidths(A)
lambda, mu = BlockBandedMatrices.subblockbandwidths(A)
blockwidth = l + u + 1
subblockwidth = lambda + mu + 1
nblock = BlockBandedMatrices.blocksize(A, 2)
cols = BlockArrays.blocklengths(axes(A, 2))
blockcolors = ArrayInterface._cycle(1:blockwidth, nblock)
# the reserved number of colors of a block is the min of subblockwidth and the largest length of columns of blocks with the same block color
ncolors = [
min(subblockwidth, maximum(cols[i:blockwidth:nblock]))
for i = 1:min(blockwidth, nblock)
]
endinds = cumsum(ncolors)
startinds = [endinds[i] - ncolors[i] + 1 for i = 1:min(blockwidth, nblock)]
colors = [
ArrayInterface._cycle(startinds[blockcolors[i]]:endinds[blockcolors[i]], cols[i])
for i = 1:nblock
]
return reduce(vcat, colors)
end
end # module