Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BlockSparseArrays] Redesign block views again #1513

Merged
merged 4 commits into from
Jun 27, 2024

Conversation

mtfishman
Copy link
Member

This PR is an iteration on the redesign of block views that was implemented in #1481, which implemented block views (i.e. @view a[Block(1, 1)]) as a SubArray wrapping the block sparse array and storing the location of the desired block. It is also related to #1498, which introduced view!/@view! that may instantiate blocks if they don't exist.

This PR defines a devoted type BlockView, and now @view a[Block(1, 1)] either directly outputs the data of the block if it exists, or a BlockView wrapping the block sparse array and the location of the desired block if the block doesn't exist.

So for example:

julia> using BlockArrays: Block

julia> using NDTensors.BlockSparseArrays: BlockSparseArray

julia> a = BlockSparseArray{Float64}([2, 3], [2, 3])
typeof(axes) = Tuple{BlockArrays.BlockedOneTo{Int64, Vector{Int64}}, BlockArrays.BlockedOneTo{Int64, Vector{Int64}}}

Warning: To temporarily circumvent a bug in printing BlockSparseArrays with mixtures of dual and non-dual axes, the types of the dual axes printed below might not be accurate. The types printed above this message are the correct ones.

2×2-blocked 5×5 BlockSparseArray{Float64, 2, Matrix{Float64}, NDTensors.SparseArrayDOKs.SparseArrayDOK{Matrix{Float64}, 2, NDTensors.BlockSparseArrays.BlockZero{Tuple{BlockArrays.BlockedOneTo{Int64, Vector{Int64}}, BlockArrays.BlockedOneTo{Int64, Vector{Int64}}}}}, Tuple{BlockArrays.BlockedOneTo{Int64, Vector{Int64}}, BlockArrays.BlockedOneTo{Int64, Vector{Int64}}}}:
 0.0  0.00.0  0.0  0.0
 0.0  0.00.0  0.0  0.0
 ──────────┼───────────────
 0.0  0.00.0  0.0  0.0
 0.0  0.00.0  0.0  0.0
 0.0  0.00.0  0.0  0.0

julia> a[Block(1, 1)] = randn(2, 2)
2×2 Matrix{Float64}:
  0.114978   3.56484
 -0.508884  -0.323752

julia> @view a[Block(1, 1)]
2×2 Matrix{Float64}:
  0.114978   3.56484
 -0.508884  -0.323752

julia> @view a[Block(2, 2)]
3×3 NDTensors.BlockSparseArrays.BlockView{Float64, 2, BlockSparseArray{Float64, 2, Matrix{Float64}, NDTensors.SparseArrayDOKs.SparseArrayDOK{Matrix{Float64}, 2, NDTensors.BlockSparseArrays.BlockZero{Tuple{BlockArrays.BlockedOneTo{Int64, Vector{Int64}}, BlockArrays.BlockedOneTo{Int64, Vector{Int64}}}}}, Tuple{BlockArrays.BlockedOneTo{Int64, Vector{Int64}}, BlockArrays.BlockedOneTo{Int64, Vector{Int64}}}}}:
 0.0  0.0  0.0
 0.0  0.0  0.0
 0.0  0.0  0.0

I think this will make some operations with block views easier to implement, and also help to simplify some code internally.

I'm not totally settled on the design that views of existing blocks directly returns the block data, an alternative would be that it also outputs a BlockView which is more comparable to the current behavior that it outputs a SubArray no matter what. However, I think it is worth trying out this design and seeing how it goes, it would be easy to change that later.

@ogauthe this is related to our discussion in ITensor/BlockSparseArrays.jl#2. I think this new design will make it a bit easier to fix some issues with block views you brought up, like using them in TensorAlgebra.contract and in Strided.@strided.

@mtfishman
Copy link
Member Author

@ogauthe with this PR, this now works:

using BlockArrays: Block
using NDTensors.TensorAlgebra: contract
using NDTensors.BlockSparseArrays: BlockSparseArray
a = BlockSparseArray{Float64}([1], [1])
a[1, 1] = 1.0
b = @view a[Block(1, 1)]
contract(b, (1, 2), ones((1, 1)), (2, 3))

whether or not the block is instantiated, though my guess is that if the block isn't instantiated it may not be doing something efficient.

@mtfishman mtfishman merged commit 2985e9b into main Jun 27, 2024
16 checks passed
@mtfishman mtfishman deleted the BlockSparseArrays_redesign_blockviews_again branch June 27, 2024 20:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant