-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathblocksparsearray.jl
More file actions
273 lines (243 loc) · 8.82 KB
/
blocksparsearray.jl
File metadata and controls
273 lines (243 loc) · 8.82 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
using BlockArrays:
BlockArrays,
Block,
BlockedUnitRange,
UndefBlocksInitializer,
blockedrange,
blocklength,
undef_blocks
using DerivableInterfaces: @interface
using Dictionaries: Dictionary
using SparseArraysBase: SparseArrayDOK
"""
SparseArrayDOK{T}(undef_blocks, axes)
SparseArrayDOK{T,N}(undef_blocks, axes)
Construct the block structure of an undefined BlockSparseArray that will have
blocked axes `axes`.
Note that `undef_blocks` is defined in
[BlockArrays.jl](https://juliaarrays.github.io/BlockArrays.jl/stable/lib/public/#BlockArrays.undef_blocks)
and should be imported from that package to use it as an input to this constructor.
"""
function SparseArraysBase.SparseArrayDOK{T,N}(
::UndefBlocksInitializer, ax::Tuple{Vararg{AbstractUnitRange{<:Integer},N}}
) where {T,N}
return SparseArrayDOK{T,N}(undef, blocklength.(ax); getunstored=GetUnstoredBlock(ax))
end
function SparseArraysBase.SparseArrayDOK{T,N}(
::UndefBlocksInitializer, ax::Vararg{AbstractUnitRange{<:Integer},N}
) where {T,N}
return SparseArrayDOK{T,N}(undef_blocks, ax)
end
function SparseArraysBase.SparseArrayDOK{T,N}(
::UndefBlocksInitializer,
dims::Tuple{AbstractVector{<:Integer},Vararg{AbstractVector{<:Integer}}},
) where {T,N}
return SparseArrayDOK{T,N}(undef_blocks, blockedrange.(dims))
end
function SparseArraysBase.SparseArrayDOK{T,N}(
::UndefBlocksInitializer,
dim1::AbstractVector{<:Integer},
dim_rest::AbstractVector{<:Integer}...,
) where {T,N}
return SparseArrayDOK{T,N}(undef_blocks, (dim1, dim_rest...))
end
function SparseArraysBase.SparseArrayDOK{T}(
::UndefBlocksInitializer, ax::Tuple{Vararg{AbstractUnitRange{<:Integer},N}}
) where {T,N}
return SparseArrayDOK{T,N}(undef_blocks, ax)
end
function SparseArraysBase.SparseArrayDOK{T}(
::UndefBlocksInitializer, ax::Vararg{AbstractUnitRange{<:Integer},N}
) where {T,N}
return SparseArrayDOK{T,N}(undef_blocks, ax)
end
function SparseArraysBase.SparseArrayDOK{T}(
::UndefBlocksInitializer,
dims::Tuple{AbstractVector{<:Integer},Vararg{AbstractVector{<:Integer}}},
) where {T}
return SparseArrayDOK{T}(undef_blocks, blockedrange.(dims))
end
function SparseArraysBase.SparseArrayDOK{T}(
::UndefBlocksInitializer,
dim1::AbstractVector{<:Integer},
dim_rest::AbstractVector{<:Integer}...,
) where {T}
return SparseArrayDOK{T}(undef_blocks, (dim1, dim_rest...))
end
function _BlockSparseArray end
struct BlockSparseArray{
T,
N,
A<:AbstractArray{T,N},
Blocks<:AbstractArray{A,N},
Axes<:Tuple{Vararg{AbstractUnitRange{<:Integer},N}},
} <: AbstractBlockSparseArray{T,N}
blocks::Blocks
axes::Axes
global @inline function _BlockSparseArray(
blocks::AbstractArray{<:AbstractArray{T,N},N},
axes::Tuple{Vararg{AbstractUnitRange{<:Integer},N}},
) where {T,N}
Base.require_one_based_indexing(axes...)
Base.require_one_based_indexing(blocks)
return new{T,N,eltype(blocks),typeof(blocks),typeof(axes)}(blocks, axes)
end
end
# TODO: Can this definition be shortened?
const BlockSparseMatrix{T,A<:AbstractMatrix{T},Blocks<:AbstractMatrix{A},Axes<:Tuple{AbstractUnitRange{<:Integer},AbstractUnitRange{<:Integer}}} = BlockSparseArray{
T,2,A,Blocks,Axes
}
# TODO: Can this definition be shortened?
const BlockSparseVector{T,A<:AbstractVector{T},Blocks<:AbstractVector{A},Axes<:Tuple{AbstractUnitRange{<:Integer}}} = BlockSparseArray{
T,1,A,Blocks,Axes
}
"""
sparsemortar(blocks::AbstractArray{<:AbstractArray{T,N},N}, axes) -> ::BlockSparseArray{T,N}
Construct a block sparse array from a sparse array of arrays and specified blocked axes.
The block sizes must be commensurate with the blocks of the axes.
"""
function sparsemortar(
blocks::AbstractArray{<:AbstractArray{T,N},N},
axes::Tuple{Vararg{AbstractUnitRange{<:Integer},N}},
) where {T,N}
return _BlockSparseArray(blocks, axes)
end
function sparsemortar(
blocks::AbstractArray{<:AbstractArray{T,N},N},
axes::Vararg{AbstractUnitRange{<:Integer},N},
) where {T,N}
return sparsemortar(blocks, axes)
end
function sparsemortar(
blocks::AbstractArray{<:AbstractArray{T,N},N},
dims::Tuple{AbstractVector{<:Integer},Vararg{AbstractVector{<:Integer}}},
) where {T,N}
return sparsemortar(blocks, blockedrange.(dims))
end
function sparsemortar(
blocks::AbstractArray{<:AbstractArray{T,N},N},
dim1::AbstractVector{<:Integer},
dim_rest::AbstractVector{<:Integer}...,
) where {T,N}
return sparsemortar(blocks, (dim1, dim_rest...))
end
@doc """
BlockSparseArray{T}(undef, dims)
BlockSparseArray{T,N}(undef, dims)
BlockSparseArray{T,N,A}(undef, dims)
Construct an uninitialized N-dimensional BlockSparseArray containing elements of type T. `dims` should be a list
of block lengths in each dimension or a list of blocked ranges representing the axes.
""" BlockSparseArray
function BlockSparseArray{T,N,A}(
::UndefInitializer, axes::Tuple{Vararg{AbstractUnitRange{<:Integer},N}}
) where {T,N,A<:AbstractArray{T,N}}
return _BlockSparseArray(SparseArrayDOK{A}(undef_blocks, axes), axes)
end
function BlockSparseArray{T,N,A}(
::UndefInitializer, axes::Vararg{AbstractUnitRange{<:Integer},N}
) where {T,N,A<:AbstractArray{T,N}}
return BlockSparseArray{T,N,A}(undef, axes)
end
function BlockSparseArray{T,N,A}(
::UndefInitializer,
dims::Tuple{AbstractVector{<:Integer},Vararg{AbstractVector{<:Integer}}},
) where {T,N,A<:AbstractArray{T,N}}
return BlockSparseArray{T,N,A}(undef, blockedrange.(dims))
end
function BlockSparseArray{T,N,A}(
::UndefInitializer,
dim1::AbstractVector{<:Integer},
dim_rest::AbstractVector{<:Integer}...,
) where {T,N,A<:AbstractArray{T,N}}
return BlockSparseArray{T,N,A}(undef, (dim1, dim_rest...))
end
function BlockSparseArray{T,N}(
::UndefInitializer, axes::Tuple{Vararg{AbstractUnitRange{<:Integer},N}}
) where {T,N}
return BlockSparseArray{T,N,Array{T,N}}(undef, axes)
end
function BlockSparseArray{T,N}(
::UndefInitializer, axes::Tuple{Vararg{AbstractUnitRange{<:Integer}}}
) where {T,N}
return throw(ArgumentError("Length of axes doesn't match number of dimensions."))
end
function BlockSparseArray{T,N}(
::UndefInitializer, axes::Vararg{AbstractUnitRange{<:Integer},N}
) where {T,N}
return BlockSparseArray{T,N}(undef, axes)
end
function BlockSparseArray{T,N}(
::UndefInitializer,
dims::Tuple{AbstractVector{<:Integer},Vararg{AbstractVector{<:Integer}}},
) where {T,N}
return BlockSparseArray{T,N}(undef, blockedrange.(dims))
end
function BlockSparseArray{T,N}(
::UndefInitializer,
dim1::AbstractVector{<:Integer},
dim_rest::AbstractVector{<:Integer}...,
) where {T,N}
return BlockSparseArray{T,N}(undef, (dim1, dim_rest...))
end
function BlockSparseArray{T}(
::UndefInitializer,
dims::Tuple{AbstractVector{<:Integer},Vararg{AbstractVector{<:Integer}}},
) where {T}
return BlockSparseArray{T,length(dims)}(undef, dims)
end
function BlockSparseArray{T}(
::UndefInitializer, axes::Tuple{Vararg{AbstractUnitRange{<:Integer}}}
) where {T}
return BlockSparseArray{T,length(axes)}(undef, axes)
end
function BlockSparseArray{T}(
::UndefInitializer,
dim1::AbstractVector{<:Integer},
dim_rest::AbstractVector{<:Integer}...,
) where {T}
return BlockSparseArray{T}(undef, (dim1, dim_rest...))
end
function BlockSparseArray{T}(
::UndefInitializer, axes::Vararg{AbstractUnitRange{<:Integer}}
) where {T}
return BlockSparseArray{T}(undef, axes)
end
# Base `AbstractArray` interface
Base.axes(a::BlockSparseArray) = a.axes
# BlockArrays `AbstractBlockArray` interface.
# This is used by `blocks(::AnyAbstractBlockSparseArray)`.
@interface ::AbstractBlockSparseArrayInterface BlockArrays.blocks(a::BlockSparseArray) =
a.blocks
# TODO: Use `TypeParameterAccessors`.
function blockstype(
arraytype::Type{<:BlockSparseArray{T,N,A,Blocks}}
) where {T,N,A<:AbstractArray{T,N},Blocks<:AbstractArray{A,N}}
return Blocks
end
function blockstype(
arraytype::Type{<:BlockSparseArray{T,N,A}}
) where {T,N,A<:AbstractArray{T,N}}
return SparseArrayDOK{A,N}
end
function blockstype(arraytype::Type{<:BlockSparseArray{T,N}}) where {T,N}
return SparseArrayDOK{AbstractArray{T,N},N}
end
function blockstype(arraytype::Type{<:BlockSparseArray{T}}) where {T}
return SparseArrayDOK{AbstractArray{T}}
end
blockstype(arraytype::Type{<:BlockSparseArray}) = SparseArrayDOK{AbstractArray}
## # Base interface
## function Base.similar(
## a::AbstractBlockSparseArray, elt::Type, axes::Tuple{Vararg{BlockedUnitRange}}
## )
## # TODO: Preserve GPU data!
## return BlockSparseArray{elt}(undef, axes)
## end
# TypeParameterAccessors.jl interface
using TypeParameterAccessors: TypeParameterAccessors, Position, set_type_parameters
TypeParameterAccessors.position(::Type{BlockSparseArray}, ::typeof(eltype)) = Position(1)
TypeParameterAccessors.position(::Type{BlockSparseArray}, ::typeof(ndims)) = Position(2)
TypeParameterAccessors.position(::Type{BlockSparseArray}, ::typeof(blocktype)) = Position(3)
function TypeParameterAccessors.position(::Type{BlockSparseArray}, ::typeof(blockstype))
return Position(4)
end