/
sparse_arrays.jl
302 lines (287 loc) · 9.06 KB
/
sparse_arrays.jl
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
import SparseArrays
const _SparseMat = SparseArrays.SparseMatrixCSC
function undef_array(
::Type{_SparseMat{Tv,Ti}},
rows::Base.OneTo,
cols::Base.OneTo,
) where {Tv,Ti}
return SparseArrays.spzeros(Tv, Ti, length(rows), length(cols))
end
function operate!(::typeof(zero), A::_SparseMat)
for i in eachindex(A.colptr)
A.colptr[i] = one(A.colptr[i])
end
empty!(A.rowval)
empty!(A.nzval)
return A
end
function promote_operation(
op::Union{typeof(+),typeof(-)},
::Type{<:SparseArrays.AbstractSparseArray{Tv,Ti,N}},
::Type{Array{T,N}},
) where {Tv,Ti,T,N}
return Array{promote_operation(op, Tv, T),N}
end
function promote_operation(
op::Union{typeof(+),typeof(-)},
::Type{Array{T,N}},
::Type{<:SparseArrays.AbstractSparseArray{Tv,Ti,N}},
) where {Tv,Ti,T,N}
return Array{promote_operation(op, Tv, T),N}
end
function _operate!(
op::Union{typeof(+),typeof(-)},
A::Matrix,
B::_SparseMat,
left_factors::Tuple,
right_factors::Tuple,
)
B_nonzeros = SparseArrays.nonzeros(B)
B_rowvals = SparseArrays.rowvals(B)
for col = 1:size(B, 2)
for k ∈ SparseArrays.nzrange(B, col)
row = B_rowvals[k]
A[row, col] = operate!!(
mul_rhs(op),
A[row, col],
left_factors...,
B_nonzeros[k],
right_factors...,
)
end
end
return A
end
similar_array_type(::Type{SparseArrays.SparseVector{Tv,Ti}}, ::Type{T}) where {T,Tv,Ti} =
SparseArrays.SparseVector{T,Ti}
similar_array_type(::Type{_SparseMat{Tv,Ti}}, ::Type{T}) where {T,Tv,Ti} = _SparseMat{T,Ti}
# `SparseArrays/src/linalg.jl` sometimes create a sparse matrix to contain the result.
# For instance with `Matrix * Adjoint{SparseMatrixCSC}` and then uses `generic_matmatmul!`
# which looks quite inefficient as it does not exploit the sparsity of the result matrix and the rhs.
# The approach used here should be more efficient as we redirect to a method that exploits the sparsity of the rhs and `copyto!` should be faster to write the result matrix.
function operate!(
::typeof(add_mul),
output::_SparseMat{T},
A::AbstractMatrix,
B::AbstractMatrix,
) where {T}
C = Matrix{T}(undef, size(output)...)
operate_to!(C, *, A, B)
copyto!(output, C)
return output
end
function operate!(
::typeof(add_mul),
ret::VecOrMat{T},
adjA::_TransposeOrAdjoint{<:Any,<:_SparseMat},
B::AbstractVecOrMat,
α::Vararg{Union{T,Scaling},N},
) where {T,N}
_dim_check(ret, adjA, B)
A = parent(adjA)
A_nonzeros = SparseArrays.nonzeros(A)
A_rowvals = SparseArrays.rowvals(A)
for k ∈ 1:size(ret, 2)
for col ∈ 1:A.n
cur = ret[col, k]
for j ∈ SparseArrays.nzrange(A, col)
A_val = _mirror_transpose_or_adjoint(A_nonzeros[j], adjA)
cur = operate!!(add_mul, cur, A_val, B[A_rowvals[j], k], α...)
end
ret[col, k] = cur
end
end
return ret
end
function operate!(
::typeof(add_mul),
ret::VecOrMat{T},
A::_SparseMat,
B::AbstractVecOrMat,
α::Vararg{Union{T,Scaling},N},
) where {T,N}
_dim_check(ret, A, B)
A_nonzeros = SparseArrays.nonzeros(A)
A_rowvals = SparseArrays.rowvals(A)
for col ∈ 1:size(A, 2)
for k ∈ 1:size(ret, 2)
αxj = *(B[col, k], α...)
for j ∈ SparseArrays.nzrange(A, col)
ret[A_rowvals[j], k] =
operate!!(add_mul, ret[A_rowvals[j], k], A_nonzeros[j], αxj)
end
end
end
return ret
end
function operate!(
::typeof(add_mul),
ret::Matrix{T},
A::AbstractMatrix,
B::_SparseMat,
α::Vararg{Union{T,Scaling},N},
) where {T,N}
_dim_check(ret, A, B)
rowval = SparseArrays.rowvals(B)
B_nonzeros = SparseArrays.nonzeros(B)
for multivec_row = 1:size(A, 1)
for col ∈ 1:size(B, 2)
cur = ret[multivec_row, col]
for k ∈ SparseArrays.nzrange(B, col)
cur =
operate!!(add_mul, cur, A[multivec_row, rowval[k]], B_nonzeros[k], α...)
end
ret[multivec_row, col] = cur
end
end
return ret
end
function operate!(
::typeof(add_mul),
ret::Matrix{T},
A::AbstractMatrix,
adjB::_TransposeOrAdjoint{<:Any,<:_SparseMat},
α::Vararg{Union{T,Scaling},N},
) where {T,N}
_dim_check(ret, A, adjB)
B = parent(adjB)
B_rowvals = SparseArrays.rowvals(B)
B_nonzeros = SparseArrays.nonzeros(B)
for B_col ∈ 1:size(B, 2), k ∈ SparseArrays.nzrange(B, B_col)
B_row = B_rowvals[k]
B_val = _mirror_transpose_or_adjoint(B_nonzeros[k], adjB)
αB_val = *(B_val, α...)
for A_row = 1:size(A, 1)
ret[A_row, B_row] =
operate!!(add_mul, ret[A_row, B_row], A[A_row, B_col], αB_val)
end
end
return ret
end
# `_SparseMat`-`_SparseMat` matrix multiplication.
# Inspired from `SparseArrays.spmatmul` which is
# Gustavsen's matrix multiplication algorithm revisited so that row indices
# are sorted.
function promote_array_mul(
::Type{<:Union{_SparseMat{S,Ti},_TransposeOrAdjoint{S,_SparseMat{S,Ti}}}},
::Type{<:Union{_SparseMat{T,Ti},_TransposeOrAdjoint{T,_SparseMat{T,Ti}}}},
) where {S,T,Ti}
return _SparseMat{promote_sum_mul(S, T),Ti}
end
function operate!(
::typeof(add_mul),
ret::_SparseMat{T},
A::_SparseMat,
B::_SparseMat,
α::Vararg{Union{T,Scaling},N},
) where {T,N}
_dim_check(ret, A, B)
rowvalA = SparseArrays.rowvals(A)
nzvalA = SparseArrays.nonzeros(A)
rowvalB = SparseArrays.rowvals(B)
nzvalB = SparseArrays.nonzeros(B)
mA, nA = size(A)
nB = size(B, 2)
nnz_ret = length(ret.rowval)
@assert length(ret.nzval) == nnz_ret
@inbounds begin
ip = 1
xb = fill(false, mA)
for i = 1:nB
if ip + mA - 1 > nnz_ret
nnz_ret += max(mA, nnz_ret >> 2)
resize!(ret.rowval, nnz_ret)
resize!(ret.nzval, nnz_ret)
end
ret.colptr[i] = ip0 = ip
k0 = ip - 1
for jp in SparseArrays.nzrange(B, i)
nzB = nzvalB[jp]
j = rowvalB[jp]
for kp in SparseArrays.nzrange(A, j)
k = rowvalA[kp]
if xb[k]
ret.nzval[k+k0] =
operate!!(add_mul, ret.nzval[k+k0], nzvalA[kp], nzB)
else
ret.nzval[k+k0] = operate(*, nzvalA[kp], nzB)
xb[k] = true
ret.rowval[ip] = k
ip += 1
end
end
end
if ip > ip0
if _prefer_sort(ip - k0, mA)
# in-place sort of indices. Effort: O(nnz*ln(nnz)).
sort!(ret.rowval, ip0, ip - 1, QuickSort, Base.Order.Forward)
for vp = ip0:ip-1
k = ret.rowval[vp]
xb[k] = false
ret.nzval[vp] = ret.nzval[k+k0]
end
else
# scan result vector (effort O(mA))
for k = 1:mA
if xb[k]
xb[k] = false
ret.rowval[ip0] = k
ret.nzval[ip0] = ret.nzval[k+k0]
ip0 += 1
end
end
end
end
end
ret.colptr[nB+1] = ip
end
# This modification of Gustavson algorithm has sorted row indices
resize!(ret.rowval, ip - 1)
resize!(ret.nzval, ip - 1)
return ret
end
# Taken from `SparseArrays.prefer_sort` added in Julia v1.1.
_prefer_sort(nz::Integer, m::Integer) = m > 6 && 3 * SparseArrays.ilog2(nz) * nz < m
function operate!(
::typeof(add_mul),
ret::_SparseMat{T},
A::_SparseMat,
B::_TransposeOrAdjoint{<:Any,<:_SparseMat},
α::Vararg{Union{T,Scaling},N},
) where {T,N}
operate!(add_mul, ret, A, copy(B), α...)
end
function operate!(
::typeof(add_mul),
ret::_SparseMat{T},
A::_TransposeOrAdjoint{<:Any,<:_SparseMat},
B::_SparseMat,
α::Vararg{Union{T,Scaling},N},
) where {T,N}
return operate!(add_mul, ret, copy(A), B, α...)
end
function operate!(
::typeof(add_mul),
ret::_SparseMat{T},
A::_TransposeOrAdjoint{<:Any,<:_SparseMat},
B::_TransposeOrAdjoint{<:Any,<:_SparseMat},
α::Vararg{Union{T,Scaling},N},
) where {T,N}
return operate!(add_mul, ret, copy(A), B, α...)
end
# This `BroadcastStyle` is used when there is a mix of sparse arrays and dense arrays.
# The result is a sparse array.
function _broadcasted_type(
::SparseArrays.HigherOrderFns.PromoteToSparse,
::Base.HasShape{1},
::Type{Eltype},
) where {Eltype}
return SparseArrays.SparseVector{Eltype,Int}
end
function _broadcasted_type(
::SparseArrays.HigherOrderFns.PromoteToSparse,
::Base.HasShape{2},
::Type{Eltype},
) where {Eltype}
return _SparseMat{Eltype,Int}
end