Skip to content

Commit 0776c2e

Browse files
authored
support general backends for BlockArray (#76)
* support general backends for BlockArray * Don't convert in mortar * tridiagonal * Use getblock to support overloading for sparse * Allow general vector blocksize backends * template out blocksizes * add another layer to _find_block to support fast overrides * Allow general BlockSizes, test for constant block sizes * Simplify show * fix simple show * Revert BlockArray(undef_blocks, ::Type) behaviour * Fix docs * more doc fixes * another doc fix * try removing space * Increase coverage * increase coverage
1 parent 8f60fd0 commit 0776c2e

17 files changed

+221
-163
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
docs/build/
55
docs/site/
66
*.jld
7-
benchmark/*.md
7+
benchmark/*.md
8+
src/.DS_Store

docs/src/man/abstractblockarrayinterface.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ With the methods above implemented the following are automatically provided:
2828
* A pretty printing `show` function that uses unicode lines to split up the blocks:
2929
```
3030
julia> A = BlockArray(rand(4, 5), [1,3], [2,3])
31-
2×2-blocked 4×5 BlockArrays.BlockArray{Float64,2,Array{Float64,2}}:
31+
2×2-blocked 4×5 BlockArray{Float64,2}:
3232
0.61179 0.965631 │ 0.696476 0.392796 0.712462
3333
--------------------┼-------------------------------
3434
0.620099 0.364706 │ 0.0311643 0.27895 0.73477

docs/src/man/blockarrays.md

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ A block array can be created with initialized blocks using the `BlockArray{T}(bl
1515
function. The block_sizes are each an `AbstractVector{Int}` which determines the size of the blocks in that dimension. We here create a `[1,2]×[3,2]` block matrix of `Float32`s:
1616
```julia
1717
julia> BlockArray{Float32}(undef, [1,2], [3,2])
18-
2×2-blocked 3×5 BlockArrays.BlockArray{Float32,2,Array{Float32,2}}:
18+
2×2-blocked 3×5 BlockArray{Float32,2}:
1919
9.39116f-26 1.4013f-45 3.34245f-219.39064f-26 1.4013f-45
2020
───────────────────────────────────────┼──────────────────────────
2121
3.28434f-21 9.37645f-26 3.28436f-218.05301f-24 9.39077f-26
@@ -30,7 +30,7 @@ The `block_type` should be an array type, it could for example be `Matrix{Float6
3030

3131
```jldoctest
3232
julia> BlockArray{Float32}(undef_blocks, [1,2], [3,2])
33-
3×5 BlockArray{Float32,2,Array{Float32,2}}:
33+
2×2-blocked 3×5 BlockArray{Float32,2}:
3434
#undef #undef #undef │ #undef #undef
3535
────────────────────────┼────────────────
3636
#undef #undef #undef │ #undef #undef
@@ -42,7 +42,7 @@ specifying it as the second argument:
4242

4343
```jl
4444
julia> BlockArray(undef_blocks, SparseVector{Float64, Int}, [1,2])
45-
2-blocked 3-element BlockArrays.BlockArray{Float64,1,SparseVector{Float64,Int64}}:
45+
2-blocked 3-element BlockArray{Float64,1,Array{SparseVector{Float64,Int64},1},BlockArrays.BlockSizes{1,Array{Int64,1}}}:
4646
#undef
4747
------
4848
#undef
@@ -59,14 +59,14 @@ An alternative syntax for this is `block_array[Block(i...)] = v` or
5959

6060
```jldoctest block_array
6161
julia> block_array = BlockArray{Float64}(undef_blocks, [1,2], [2,2])
62-
3×4 BlockArray{Float64,2,Array{Float64,2}}:
62+
2×2-blocked 3×4 BlockArray{Float64,2}:
6363
#undef #undef │ #undef #undef
6464
────────────────┼────────────────
6565
#undef #undef │ #undef #undef
6666
#undef #undef │ #undef #undef
6767
6868
julia> setblock!(block_array, rand(2,2), 2, 1)
69-
3×4 BlockArray{Float64,2,Array{Float64,2}}:
69+
2×2-blocked 3×4 BlockArray{Float64,2}:
7070
#undef #undef │ #undef #undef
7171
────────────────────────┼────────────────
7272
0.590845 0.566237 │ #undef #undef
@@ -75,7 +75,7 @@ julia> setblock!(block_array, rand(2,2), 2, 1)
7575
julia> block_array[Block(1, 1)] = [1 2];
7676
7777
julia> block_array
78-
3×4 BlockArray{Float64,2,Array{Float64,2}}:
78+
2×2-blocked 3×4 BlockArray{Float64,2}:
7979
1.0 2.0 │ #undef #undef
8080
────────────────────┼────────────────
8181
0.590845 0.566237 │ #undef #undef
@@ -112,7 +112,7 @@ We can also view and modify views of blocks of `BlockArray` using the `view` syn
112112
julia> A = BlockArray(ones(6), 1:3);
113113
114114
julia> view(A, Block(2))
115-
2-element view(::BlockArray{Float64,1,Array{Float64,1}}, BlockSlice(Block{1,Int64}((2,)),2:3)) with eltype Float64:
115+
2-element view(::BlockArray{Float64,1,Array{Array{Float64,1},1},BlockArrays.BlockSizes{1,Array{Int64,1}}}, BlockSlice(Block{1,Int64}((2,)),2:3)) with eltype Float64:
116116
1.0
117117
1.0
118118
@@ -130,7 +130,7 @@ An array can be repacked into a `BlockArray` with `BlockArray(array, block_sizes
130130

131131
```jl
132132
julia> block_array_sparse = BlockArray(sprand(4, 5, 0.7), [1,3], [2,3])
133-
2×2-blocked 4×5 BlockArrays.BlockArray{Float64,2,SparseMatrixCSC{Float64,Int64}}:
133+
2×2-blocked 4×5 BlockArray{Float64,2,Array{SparseMatrixCSC{Float64,Int64},2},BlockArrays.BlockSizes{2,Array{Int64,1}}}:
134134
0.0341601 0.3741870.0118196 0.299058 0.0
135135
----------------------------------------------------
136136
0.0945445 0.9311150.0460428 0.0 0.0
@@ -141,16 +141,19 @@ julia> block_array_sparse = BlockArray(sprand(4, 5, 0.7), [1,3], [2,3])
141141
To get back the underlying array use `Array`:
142142

143143
```jl
144-
julia> Array(block_array_sparse))
145-
4×5 SparseMatrixCSC{Float64,Int64} with 15 stored entries:
146-
[1, 1] = 0.0341601
147-
[2, 1] = 0.0945445
148-
[3, 1] = 0.314926
149-
[4, 1] = 0.12781
150-
151-
[3, 3] = 0.496169
152-
[4, 3] = 0.732
153-
[1, 4] = 0.299058
154-
[4, 4] = 0.449182
155-
[4, 5] = 0.875096
144+
julia> Array(block_array_sparse)
145+
4×5 SparseMatrixCSC{Float64,Int64} with 13 stored entries:
146+
[1, 1] = 0.30006
147+
[2, 1] = 0.451742
148+
[3, 1] = 0.243174
149+
[4, 1] = 0.156468
150+
[1, 2] = 0.94057
151+
[3, 2] = 0.544175
152+
[4, 2] = 0.598345
153+
[3, 3] = 0.737486
154+
[4, 3] = 0.929512
155+
[1, 4] = 0.539601
156+
[3, 4] = 0.757658
157+
[4, 4] = 0.44709
158+
[2, 5] = 0.514679
156159
```

docs/src/man/pseudoblockarrays.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Creating a `PseudoBlockArray` works in the same way as a `BlockArray`.
2323

2424
```jldoctest A
2525
julia> pseudo = PseudoBlockArray(rand(3,3), [1,2], [2,1])
26-
3×3 PseudoBlockArray{Float64,2,Array{Float64,2}}:
26+
2×2-blocked 3×3 PseudoBlockArray{Float64,2}:
2727
0.590845 0.460085 │ 0.200586
2828
────────────────────┼──────────
2929
0.766797 0.794026 │ 0.298614
@@ -39,11 +39,11 @@ A block array can be created with uninitialized entries using the `BlockArray{T}
3939
function. The block_sizes are each an `AbstractVector{Int}` which determines the size of the blocks in that dimension. We here create a `[1,2]×[3,2]` block matrix of `Float32`s:
4040
```julia
4141
julia> PseudoBlockArray{Float32}(undef, [1,2], [3,2])
42-
2×2-blocked 3×5 BlockArrays.BlockArray{Float32,2,Array{Float32,2}}:
43-
9.39116f-26 1.4013f-45 3.34245f-219.39064f-26 1.4013f-45
42+
2×2-blocked 3×5 PseudoBlockArray{Float32,2}:
43+
1.02295e-43 0.0 1.09301e-430.0 1.17709e-43
4444
───────────────────────────────────────┼──────────────────────────
45-
3.28434f-21 9.37645f-26 3.28436f-21 8.05301f-24 9.39077f-26
46-
1.4013f-45 1.4013f-45 1.4013f-45 1.4013f-45 1.4013f-45
45+
0.0 1.06499e-43 0.0 1.14906e-43 0.0
46+
1.05097e-43 0.0 1.13505e-43 0.0 1.1911e-43
4747
```
4848
We can also any other user defined array type that supports `similar`.
4949

@@ -83,7 +83,7 @@ We can also view and modify views of blocks of `PseudoBlockArray` using the `vie
8383
julia> A = PseudoBlockArray(ones(6), 1:3);
8484
8585
julia> view(A, Block(2))
86-
2-element view(::PseudoBlockArray{Float64,1,Array{Float64,1}}, BlockSlice(Block{1,Int64}((2,)),2:3)) with eltype Float64:
86+
2-element view(::PseudoBlockArray{Float64,1,Array{Float64,1},BlockArrays.BlockSizes{1,Array{Int64,1}}}, BlockSlice(Block{1,Int64}((2,)),2:3)) with eltype Float64:
8787
1.0
8888
1.0
8989

src/abstractblockarray.jl

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ const AbstractBlockVecOrMat{T} = Union{AbstractBlockMatrix{T}, AbstractBlockVect
2323

2424
block2string(b, s) = string(join(map(string,b), '×'), "-blocked ", Base.dims2string(s))
2525
Base.summary(a::AbstractBlockArray) = string(block2string(nblocks(a), size(a)), " ", typeof(a))
26+
_show_typeof(io, a) = show(io, typeof(a))
27+
function Base.summary(io::IO, a::AbstractBlockArray)
28+
print(io, block2string(nblocks(a), size(a)))
29+
print(io, ' ')
30+
_show_typeof(io, a)
31+
end
2632
Base.similar(block_array::AbstractBlockArray{T}) where {T} = similar(block_array, T)
2733
Base.IndexStyle(::Type{<:AbstractBlockArray}) = IndexCartesian()
2834

@@ -61,7 +67,7 @@ a single element.
6167
6268
```jldoctest; setup = quote using BlockArrays end
6369
julia> A = BlockArray(ones(2,3), [1, 1], [2, 1])
64-
2×3 BlockArray{Float64,2,Array{Float64,2}}:
70+
2-blocked 2×3 BlockArray{Float64,2}:
6571
1.0 1.0 │ 1.0
6672
──────────┼─────
6773
1.0 1.0 │ 1.0
@@ -134,7 +140,7 @@ julia> v = Array(reshape(1:6, (2, 3)))
134140
2 4 6
135141
136142
julia> A = BlockArray(v, [1,1], [2,1])
137-
2×3 BlockArray{Int64,2,Array{Int64,2}}:
143+
2-blocked 2×3 BlockArray{Int64,2}:
138144
1 3 │ 5
139145
──────┼───
140146
2 4 │ 6
@@ -162,7 +168,7 @@ attempted assigned block is out of bounds.
162168
163169
```jldoctest; setup = quote using BlockArrays end
164170
julia> A = PseudoBlockArray(ones(2, 3), [1, 1], [2, 1])
165-
2×3 PseudoBlockArray{Float64,2,Array{Float64,2}}:
171+
2-blocked 2×3 PseudoBlockArray{Float64,2}:
166172
1.0 1.0 │ 1.0
167173
──────────┼─────
168174
1.0 1.0 │ 1.0
@@ -196,7 +202,7 @@ julia> setblock!(A, [1 2], 1, 1);
196202
julia> A[Block(2, 1)] = [3 4];
197203
198204
julia> A
199-
2×3 PseudoBlockArray{Float64,2,Array{Float64,2}}:
205+
2-blocked 2×3 PseudoBlockArray{Float64,2}:
200206
1.0 2.0 │ 0.0
201207
──────────┼─────
202208
3.0 4.0 │ 0.0
@@ -246,7 +252,7 @@ specialize this method if they need to provide custom block bounds checking beha
246252
julia> A = BlockArray(rand(2,3), [1,1], [2,1]);
247253
248254
julia> blockcheckbounds(A, 3, 2)
249-
ERROR: BlockBoundsError: attempt to access 2×2-blocked 2×3 BlockArray{Float64,2,Array{Float64,2}} at block index [3,2]
255+
ERROR: BlockBoundsError: attempt to access 2×2-blocked 2×3 BlockArray{Float64,2,Array{Array{Float64,2},2},BlockArrays.BlockSizes{2,Array{Int64,1}}} at block index [3,2]
250256
[...]
251257
```
252258
"""
@@ -279,7 +285,7 @@ Returns the array stored in `A` as a `Array`.
279285
280286
```jldoctest; setup = quote using BlockArrays end
281287
julia> A = BlockArray(ones(2,3), [1,1], [2,1])
282-
2×3 BlockArray{Float64,2,Array{Float64,2}}:
288+
2-blocked 2×3 BlockArray{Float64,2}:
283289
1.0 1.0 │ 1.0
284290
──────────┼─────
285291
1.0 1.0 │ 1.0

0 commit comments

Comments
 (0)