Skip to content

Better overloading of similar of block sparse broadcasting expressions #61

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

Merged
merged 3 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "BlockSparseArrays"
uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4"
authors = ["ITensor developers <[email protected]> and contributors"]
version = "0.2.20"
version = "0.2.21"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
6 changes: 3 additions & 3 deletions src/blocksparsearrayinterface/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ function Broadcast.BroadcastStyle(
return DefaultArrayStyle{N}()
end

function Base.similar(bc::Broadcasted{<:BlockSparseArrayStyle}, elt::Type)
# TODO: Make sure this handles GPU arrays properly.
function Base.similar(bc::Broadcasted{<:BlockSparseArrayStyle}, elt::Type, ax)
# TODO: Make this more generic, base it off sure this handles GPU arrays properly.
m = Mapped(bc)
return similar(first(m.args), elt, combine_axes(axes.(m.args)...))
return similar(first(m.args), elt, ax)
end

# Catches cases like `dest .= value` or `dest .= value1 .+ value2`.
Expand Down
18 changes: 18 additions & 0 deletions test/test_basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,24 @@ arrayts = (Array, JLArray)
@test blockstoredlength(a) == 1
@test storedlength(a) == 2 * 4

# Test similar on broadcasted expressions.
a = dev(BlockSparseArray{elt}(undef, ([2, 3], [3, 4])))
bc = Broadcast.broadcasted(+, a, a)
a′ = similar(bc, Float32)
@test a′ isa BlockSparseArray{Float32}
@test blocktype(a′) <: arrayt{Float32,2}
@test axes(a) == (blockedrange([2, 3]), blockedrange([3, 4]))

# Test similar on broadcasted expressions with axes specified.
a = dev(BlockSparseArray{elt}(undef, ([2, 3], [3, 4])))
bc = Broadcast.broadcasted(+, a, a)
a′ = similar(
bc, Float32, (blockedrange([2, 4]), blockedrange([2, 5]), blockedrange([2, 2]))
)
@test a′ isa BlockSparseArray{Float32}
@test blocktype(a′) <: arrayt{Float32,3}
@test axes(a′) == (blockedrange([2, 4]), blockedrange([2, 5]), blockedrange([2, 2]))

a = dev(BlockSparseArray{elt}(undef, ([2, 3], [3, 4])))
@views for b in [Block(1, 2), Block(2, 1)]
a[b] = dev(randn(elt, size(a[b])))
Expand Down
Loading