Skip to content

Commit

Permalink
Rename PseudoBlockArray to BlockedArray
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman committed May 15, 2024
1 parent ee57d11 commit 9bf9651
Show file tree
Hide file tree
Showing 28 changed files with 408 additions and 408 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

A block array is a partition of an array into blocks or subarrays, see [wikipedia](https://en.wikipedia.org/wiki/Block_matrix) for a more extensive description. This package has two purposes. Firstly, it defines an interface for an `AbstractBlockArray` block arrays that can be shared among types representing different types of block arrays. The advantage to this is that it provides a consistent API for block arrays.

Secondly, it also implements two different type of block arrays that follow the `AbstractBlockArray` interface. The type `BlockArray` stores each block contiguously while the type `PseudoBlockArray` stores the full matrix contiguously. This means that `BlockArray` supports fast non copying extraction and insertion of blocks while `PseudoBlockArray` supports fast access to the full matrix to use in in for example a linear solver.
Secondly, it also implements two different type of block arrays that follow the `AbstractBlockArray` interface. The type `BlockArray` stores each block contiguously while the type `BlockedArray` stores the full matrix contiguously. This means that `BlockArray` supports fast non copying extraction and insertion of blocks while `BlockedArray` supports fast access to the full matrix to use in in for example a linear solver.

A simple way to produce `BlockArray`s is via `mortar`, which combines an array of arrays into a `BlockArray`:
```julia
Expand Down
2 changes: 1 addition & 1 deletion benchmark/runbenchmarks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ g = addgroup!(SUITE, "indexing")
g_size = addgroup!(SUITE, "size")

for n = (5,)
for BT in (BlockArray, PseudoBlockArray)
for BT in (BlockArray, BlockedArray)
block_vec = BT(rand(n), [1,3,1])
block_mat = BT(rand(n,n), [1,3,1], [4,1])
block_arr = BT(rand(n,n,n), [1,3,1], [4,1], [3, 2])
Expand Down
2 changes: 1 addition & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

A block array is a partition of an array into multiple blocks or subarrays, see [wikipedia](https://en.wikipedia.org/wiki/Block_matrix) for a more extensive description. This package has two purposes. Firstly, it defines an interface for an `AbstractBlockArray` block arrays that can be shared among types representing different types of block arrays. The advantage to this is that it provides a consistent API for block arrays.

Secondly, it also implements two concrete types of block arrays that follow the `AbstractBlockArray` interface. The type `BlockArray` stores each single block contiguously, by wrapping an `AbstractArray{<:AbstractArray{T,N},N}` to concatenate all blocks – the complete array is thus not stored contiguously. Conversely, a `PseudoBlockArray` stores the full matrix contiguously (by wrapping only one `AbstractArray{T, N}`) and only superimposes a block structure. This means that `BlockArray` supports fast non copying extraction and insertion of blocks, while `PseudoBlockArray` supports fast access to the full matrix to use in, for example, a linear solver.
Secondly, it also implements two concrete types of block arrays that follow the `AbstractBlockArray` interface. The type `BlockArray` stores each single block contiguously, by wrapping an `AbstractArray{<:AbstractArray{T,N},N}` to concatenate all blocks – the complete array is thus not stored contiguously. Conversely, a `BlockedArray` stores the full matrix contiguously (by wrapping only one `AbstractArray{T, N}`) and only superimposes a block structure. This means that `BlockArray` supports fast non copying extraction and insertion of blocks, while `BlockedArray` supports fast access to the full matrix to use in, for example, a linear solver.


## Terminology
Expand Down
8 changes: 4 additions & 4 deletions docs/src/lib/public.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ Base.popfirst!
```


## PseudoBlockArray
## BlockedArray

```@docs
PseudoBlockArray
PseudoBlockVector
PseudoBlockMatrix
BlockedArray
BlockedVector
BlockedMatrix
Base.resize!
```

Expand Down
24 changes: 12 additions & 12 deletions docs/src/man/pseudoblockarrays.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# PseudoBlockArrays
# BlockedArrays

```@meta
DocTestSetup = quote
Expand All @@ -8,22 +8,22 @@ DocTestSetup = quote
end
```

A `PseudoBlockArray` is similar to a [`BlockArray`](@ref) except the full array is stored
A `BlockedArray` is similar to a [`BlockArray`](@ref) except the full array is stored
contiguously instead of block by block. This means that is not possible to insert and retrieve
blocks without copying data. On the other hand, converting a `PseudoBlockArray` to the "full" underlying array is instead instant since
blocks without copying data. On the other hand, converting a `BlockedArray` to the "full" underlying array is instead instant since
it can just return the wrapped array.

When iteratively solving a set of equations with a gradient method the Jacobian typically has a block structure. It can be convenient
to use a `PseudoBlockArray` to build up the Jacobian block by block and then pass the resulting matrix to
to use a `BlockedArray` to build up the Jacobian block by block and then pass the resulting matrix to
a direct solver using `Matrix`.

## Creating PseudoBlockArrays
## Creating BlockedArrays

Creating a `PseudoBlockArray` works in the same way as a `BlockArray`.
Creating a `BlockedArray` works in the same way as a `BlockArray`.

```jldoctest A
julia> pseudo = PseudoBlockArray(reshape([1:9;], 3, 3), [1,2], [2,1])
2×2-blocked 3×3 PseudoBlockMatrix{Int64}:
julia> pseudo = BlockedArray(reshape([1:9;], 3, 3), [1,2], [2,1])
2×2-blocked 3×3 BlockedMatrix{Int64}:
1 4 │ 7
──────┼───
2 5 │ 8
Expand All @@ -38,8 +38,8 @@ This "takes ownership" of the passed in array so no copy of the array is made.
A block array can be created with uninitialized entries using the `BlockArray{T}(undef, block_sizes...)`
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:
```julia
julia> PseudoBlockArray{Float32}(undef, [1,2], [3,2])
2×2-blocked 3×5 PseudoBlockMatrix{Float32}:
julia> BlockedArray{Float32}(undef, [1,2], [3,2])
2×2-blocked 3×5 BlockedMatrix{Float32}:
1.02295e-43 0.0 1.09301e-430.0 1.17709e-43
───────────────────────────────────────┼──────────────────────────
0.0 1.06499e-43 0.01.14906e-43 0.0
Expand Down Expand Up @@ -79,9 +79,9 @@ The underlying array is accessed with `Array` just like for `BlockArray`.

## Views of blocks

We can also view and modify views of blocks of `PseudoBlockArray` using the `view` syntax:
We can also view and modify views of blocks of `BlockedArray` using the `view` syntax:
```jldoctest
julia> A = PseudoBlockArray(ones(6), 1:3);
julia> A = BlockedArray(ones(6), 1:3);
julia> view(A, Block(2))
2-element view(::Vector{Float64}, 2:3) with eltype Float64:
Expand Down
4 changes: 2 additions & 2 deletions ext/BlockArraysBandedMatricesExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import BlockArrays: blockcolsupport, blockrowsupport, AbstractBlockedUnitRange
import ArrayLayouts: sub_materialize


bandeddata(P::PseudoBlockMatrix) = bandeddata(P.blocks)
bandwidths(P::PseudoBlockMatrix) = bandwidths(P.blocks)
bandeddata(P::BlockedMatrix) = bandeddata(P.blocks)
bandwidths(P::BlockedMatrix) = bandwidths(P.blocks)

function blockcolsupport(::AbstractBandedLayout, B, j)
m,n = axes(B)
Expand Down
2 changes: 1 addition & 1 deletion src/BlockArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export blocksizes, blocklengths, blocklasts, blockfirsts, blockisequal
export BlockRange, blockedrange, BlockedUnitRange, BlockedOneTo

export BlockArray, BlockMatrix, BlockVector, BlockVecOrMat, mortar
export PseudoBlockArray, PseudoBlockMatrix, PseudoBlockVector, PseudoBlockVecOrMat
export BlockedArray, BlockedMatrix, BlockedVector, BlockedVecOrMat

export undef_blocks, undef, findblock, findblockindex

Expand Down
10 changes: 5 additions & 5 deletions src/blockarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -546,16 +546,16 @@ end

# Temporary work around
Base.reshape(block_array::BlockArray, axes::NTuple{N,AbstractUnitRange{Int}}) where N =
reshape(PseudoBlockArray(block_array), axes)
reshape(BlockedArray(block_array), axes)
Base.reshape(block_array::BlockArray, dims::Tuple{Int,Vararg{Int}}) =
reshape(PseudoBlockArray(block_array), dims)
reshape(BlockedArray(block_array), dims)
Base.reshape(block_array::BlockArray, axes::Tuple{Union{Integer,Base.OneTo}, Vararg{Union{Integer,Base.OneTo}}}) =
reshape(PseudoBlockArray(block_array), axes)
reshape(BlockedArray(block_array), axes)
Base.reshape(block_array::BlockArray, dims::Tuple{Vararg{Union{Int,Colon}}}) =
reshape(PseudoBlockArray(block_array), dims)
reshape(BlockedArray(block_array), dims)

"""
resize!(a::BlockVector, N::Block) -> PseudoBlockVector
resize!(a::BlockVector, N::Block) -> BlockedVector
Resize `a` to contain the first `N` blocks, returning a new `BlockVector` sharing
memory with `a`. If `N` is smaller than the current
Expand Down
32 changes: 16 additions & 16 deletions src/blockbroadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@

abstract type AbstractBlockStyle{N} <: AbstractArrayStyle{N} end
struct BlockStyle{N} <: AbstractBlockStyle{N} end
struct PseudoBlockStyle{N} <: AbstractBlockStyle{N} end
struct BlockedStyle{N} <: AbstractBlockStyle{N} end


BlockStyle(::Val{N}) where {N} = BlockStyle{N}()
PseudoBlockStyle(::Val{N}) where {N} = PseudoBlockStyle{N}()
BlockedStyle(::Val{N}) where {N} = BlockedStyle{N}()

Check warning on line 15 in src/blockbroadcast.jl

View check run for this annotation

Codecov / codecov/patch

src/blockbroadcast.jl#L15

Added line #L15 was not covered by tests
BlockStyle{M}(::Val{N}) where {N,M} = BlockStyle{N}()
PseudoBlockStyle{M}(::Val{N}) where {N,M} = PseudoBlockStyle{N}()
BlockedStyle{M}(::Val{N}) where {N,M} = BlockedStyle{N}()
BroadcastStyle(::Type{<:BlockArray{<:Any,N}}) where {N} = BlockStyle{N}()
BroadcastStyle(::Type{<:PseudoBlockArray{<:Any,N}}) where {N} = PseudoBlockStyle{N}()
BroadcastStyle(::Type{<:BlockedArray{<:Any,N}}) where {N} = BlockedStyle{N}()
BroadcastStyle(::Type{<:AdjOrTrans{<:Any,<:BlockArray{<:Any,N}}}) where {N} = BlockStyle{2}()
BroadcastStyle(::Type{<:AdjOrTrans{<:Any,<:PseudoBlockArray{<:Any,N}}}) where {N} = PseudoBlockStyle{2}()
BroadcastStyle(::Type{<:AdjOrTrans{<:Any,<:BlockedArray{<:Any,N}}}) where {N} = BlockedStyle{2}()

BroadcastStyle(::DefaultArrayStyle{N}, b::AbstractBlockStyle{M}) where {M,N} = typeof(b)(Val(max(M,N)))
BroadcastStyle(a::AbstractBlockStyle{N}, ::DefaultArrayStyle{M}) where {M,N} = typeof(a)(Val(max(M,N)))
BroadcastStyle(::StructuredMatrixStyle, b::AbstractBlockStyle{M}) where {M} = typeof(b)(Val(max(M,2)))
BroadcastStyle(a::AbstractBlockStyle{M}, ::StructuredMatrixStyle) where {M} = typeof(a)(Val(max(M,2)))
BroadcastStyle(::BlockStyle{M}, ::PseudoBlockStyle{N}) where {M,N} = BlockStyle(Val(max(M,N)))
BroadcastStyle(::PseudoBlockStyle{M}, ::BlockStyle{N}) where {M,N} = BlockStyle(Val(max(M,N)))
BroadcastStyle(::BlockStyle{M}, ::BlockedStyle{N}) where {M,N} = BlockStyle(Val(max(M,N)))
BroadcastStyle(::BlockedStyle{M}, ::BlockStyle{N}) where {M,N} = BlockStyle(Val(max(M,N)))


# sortedunion can assume inputs are already sorted so this could be improved
Expand All @@ -45,8 +45,8 @@ Base.Broadcast.axistype(a, b::AbstractBlockedUnitRange) = length(b) == 1 ? a : c
similar(bc::Broadcasted{<:AbstractBlockStyle{N}}, ::Type{T}) where {T,N} =
BlockArray{T,N}(undef, axes(bc))

similar(bc::Broadcasted{PseudoBlockStyle{N}}, ::Type{T}) where {T,N} =
PseudoBlockArray{T,N}(undef, axes(bc))
similar(bc::Broadcasted{BlockedStyle{N}}, ::Type{T}) where {T,N} =

Check warning on line 48 in src/blockbroadcast.jl

View check run for this annotation

Codecov / codecov/patch

src/blockbroadcast.jl#L48

Added line #L48 was not covered by tests
BlockedArray{T,N}(undef, axes(bc))

"""
SubBlockIterator(subblock_lasts::Vector{Int}, block_lasts::Vector{Int})
Expand Down Expand Up @@ -139,7 +139,7 @@ function _generic_blockbroadcast_copyto!(dest::AbstractArray,

bs = axes(bc)
if !blockisequal(axes(dest), bs)
copyto!(PseudoBlockArray(dest, bs), bc)
copyto!(BlockedArray(dest, bs), bc)
return dest
end

Expand Down Expand Up @@ -197,13 +197,13 @@ end
end

_removeblocks(a::Broadcasted) = broadcasted(a.f, map(_removeblocks,a.args)...)
_removeblocks(a::PseudoBlockArray) = a.blocks
_removeblocks(a::BlockedArray) = a.blocks
_removeblocks(a::BlockSlice) = a.indices
_removeblocks(a::Adjoint) = _removeblocks(parent(a))'
_removeblocks(a::Transpose) = transpose(_removeblocks(parent(a)))
_removeblocks(a::SubArray{<:Any,N,<:PseudoBlockArray}) where N = view(_removeblocks(parent(a)), map(_removeblocks, parentindices(a))...)
_removeblocks(a::SubArray{<:Any,N,<:BlockedArray}) where N = view(_removeblocks(parent(a)), map(_removeblocks, parentindices(a))...)
_removeblocks(a) = a
copy(bc::Broadcasted{PseudoBlockStyle{N}}) where N = PseudoBlockArray(Broadcast.materialize(_removeblocks(bc)), axes(bc))
copy(bc::Broadcasted{BlockedStyle{N}}) where N = BlockedArray(Broadcast.materialize(_removeblocks(bc)), axes(bc))

for op in (:+, :-, :*)
@eval function copy(bc::Broadcasted{BlockStyle{N},<:Any,typeof($op),<:Tuple{<:AbstractArray{<:Number,N}}}) where N
Expand Down Expand Up @@ -248,9 +248,9 @@ BroadcastStyle(::Type{<:SubArray{<:Any,N,<:Any,I}}) where {N,I<:Tuple{BlockSlice
BroadcastStyle(::Type{<:SubArray{<:Any,N,<:Any,I}}) where {N,I<:Tuple{BlockSlice{<:Any,<:AbstractBlockedUnitRange},BlockSlice{<:Any,<:AbstractBlockedUnitRange},Vararg{Any}}} = BlockStyle{N}()
BroadcastStyle(::Type{<:SubArray{<:Any,N,<:Any,I}}) where {N,I<:Tuple{Any,BlockSlice{<:Any,<:AbstractBlockedUnitRange},Vararg{Any}}} = BlockStyle{N}()

BroadcastStyle(::Type{<:SubArray{<:Any,N,<:PseudoBlockArray,I}}) where {N,I<:Tuple{BlockSlice{<:Any,<:AbstractBlockedUnitRange},Vararg{Any}}} = PseudoBlockStyle{N}()
BroadcastStyle(::Type{<:SubArray{<:Any,N,<:PseudoBlockArray,I}}) where {N,I<:Tuple{BlockSlice{<:Any,<:AbstractBlockedUnitRange},BlockSlice{<:Any,<:AbstractBlockedUnitRange},Vararg{Any}}} = PseudoBlockStyle{N}()
BroadcastStyle(::Type{<:SubArray{<:Any,N,<:PseudoBlockArray,I}}) where {N,I<:Tuple{Any,BlockSlice{<:Any,<:AbstractBlockedUnitRange},Vararg{Any}}} = PseudoBlockStyle{N}()
BroadcastStyle(::Type{<:SubArray{<:Any,N,<:BlockedArray,I}}) where {N,I<:Tuple{BlockSlice{<:Any,<:AbstractBlockedUnitRange},Vararg{Any}}} = BlockedStyle{N}()
BroadcastStyle(::Type{<:SubArray{<:Any,N,<:BlockedArray,I}}) where {N,I<:Tuple{BlockSlice{<:Any,<:AbstractBlockedUnitRange},BlockSlice{<:Any,<:AbstractBlockedUnitRange},Vararg{Any}}} = BlockedStyle{N}()
BroadcastStyle(::Type{<:SubArray{<:Any,N,<:BlockedArray,I}}) where {N,I<:Tuple{Any,BlockSlice{<:Any,<:AbstractBlockedUnitRange},Vararg{Any}}} = BlockedStyle{N}()



Expand Down
2 changes: 1 addition & 1 deletion src/blockdeque.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ end

function blockappend!(
dest::BlockVector{<:Any,<:AbstractArray{T}},
src::PseudoBlockVector{<:Any,T},
src::BlockedVector{<:Any,T},
) where {T}
if blocklength(src) == 1
return _blockpush!(dest, src.blocks)
Expand Down
4 changes: 2 additions & 2 deletions src/blockindices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ It can be used to index into `BlockArrays` in the following manner:
```jldoctest
julia> arr = Array(reshape(1:25, (5,5)));
julia> a = PseudoBlockArray(arr, [3,2], [1,4])
2×2-blocked 5×5 PseudoBlockMatrix{Int64}:
julia> a = BlockedArray(arr, [3,2], [1,4])
2×2-blocked 5×5 BlockedMatrix{Int64}:
1 │ 6 11 16 21
2 │ 7 12 17 22
3 │ 8 13 18 23
Expand Down
Loading

0 comments on commit 9bf9651

Please sign in to comment.