Skip to content

Commit 7ba392b

Browse files
committed
More slicing improvements
1 parent 6994d05 commit 7ba392b

3 files changed

Lines changed: 25 additions & 36 deletions

File tree

src/abstractblocksparsearray/arraylayouts.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using ArrayLayouts: ArrayLayouts, DualLayout, MemoryLayout, MulAdd
2-
using BlockArrays: BlockLayout
2+
using BlockArrays: Block, BlockLayout, blocks
33
using SparseArraysBase: SparseLayout
44
using TypeParameterAccessors: parenttype, similartype
55

@@ -62,7 +62,15 @@ function ArrayLayouts.sub_materialize(layout::BlockLayout{<:SparseLayout}, a, ax
6262
# TODO: Use `similar`?
6363
blocktype_a = blocktype(parent(a))
6464
a_dest = BlockSparseArray{eltype(a), length(axes), blocktype_a}(undef, axes)
65-
a_dest .= a
65+
for I in CartesianIndices(blocks(a_dest))
66+
b = Block(Tuple(I))
67+
block_a = @view a[b]
68+
if !iszero(block_a)
69+
block_dest = similar(a_dest[b])
70+
copyto!(block_dest, block_a)
71+
a_dest[b] = block_dest
72+
end
73+
end
6674
return a_dest
6775
end
6876

src/blocksparsearrayinterface/blocksparsearrayinterface.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,7 @@ end
442442

443443
# TODO: Define `getstoredindex`, `getunstoredindex` instead.
444444
function Base.getindex(a::SparseSubArrayBlocks{<:Any, N}, I::Vararg{Int, N}) where {N}
445-
# TODO: Should this be defined as `@view a.array[Block(I)]` instead?
446-
return @view a.array[Block(I)]
445+
return BlockArrays.viewblock(a.array, Block(I))
447446

448447
## parent_blocks = @view blocks(parent(a.array))[blockrange(a)...]
449448
## parent_block = parent_blocks[I...]

test/test_abstract_blocktype.jl

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -56,36 +56,22 @@ arrayts = (Array, JLArray)
5656
a = BlockSparseMatrix{elt, AbstractMatrix{elt}}(undef, [2, 3], [2, 3])
5757
a[Block(1, 1)] = dev(randn(elt, 2, 2))
5858
for f in (eig_full, eig_trunc)
59-
if arrayt === Array
60-
d, v = f(a)
61-
@test a * v v * d
62-
else
63-
@test_broken f(a)
64-
end
65-
end
66-
if arrayt === Array
67-
d = eig_vals(a)
68-
@test sort(Vector(d); by = abs) sort(eig_vals(Matrix(a)); by = abs)
69-
else
70-
@test_broken eig_vals(a)
59+
res = f(a)
60+
d, v = res[1:2]
61+
@test a * v v * d
7162
end
63+
d = eig_vals(a)
64+
@test sort(Vector(d); by = abs) sort(eig_vals(Matrix(a)); by = abs)
7265

7366
a = BlockSparseMatrix{elt, AbstractMatrix{elt}}(undef, [2, 3], [2, 3])
7467
a[Block(1, 1)] = dev(parent(hermitianpart(randn(elt, 2, 2))))
7568
for f in (eigh_full, eigh_trunc)
76-
if arrayt === Array
77-
d, v = f(a)
78-
@test a * v v * d
79-
else
80-
@test_broken f(a)
81-
end
82-
end
83-
if arrayt === Array
84-
d = eigh_vals(a)
85-
@test sort(Vector(d); by = abs) sort(eig_vals(Matrix(a)); by = abs)
86-
else
87-
@test_broken eigh_vals(a)
69+
res = f(a)
70+
d, v = res[1:2]
71+
@test a * v v * d
8872
end
73+
d = eigh_vals(a)
74+
@test sort(Vector(d); by = abs) sort(eig_vals(Matrix(a)); by = abs)
8975

9076
a = BlockSparseMatrix{elt, AbstractMatrix{elt}}(undef, [2, 3], [2, 3])
9177
a[Block(1, 1)] = dev(randn(elt, 2, 2))
@@ -96,7 +82,7 @@ arrayts = (Array, JLArray)
9682
@test isisometric(u; side = :left)
9783
else
9884
# TODO: Fix comparison with UniformScaling on GPU.
99-
@test_broken isisometric(u; side = :left)
85+
@test isisometric(u; side = :left)
10086
end
10187
end
10288
for f in (right_orth, right_polar, lq_compact, lq_full)
@@ -106,15 +92,11 @@ arrayts = (Array, JLArray)
10692
@test isisometric(u; side = :right)
10793
else
10894
# TODO: Fix comparison with UniformScaling on GPU.
109-
@test_broken isisometric(u; side = :right)
95+
@test isisometric(u; side = :right)
11096
end
11197
end
11298
for f in (svd_compact, svd_full, svd_trunc)
113-
if arrayt Array && (f svd_full || f svd_trunc)
114-
@test_broken f(a)
115-
else
116-
u, s, v = f(a)
117-
@test u * s * v a
118-
end
99+
u, s, v = f(a)
100+
@test u * s * v a
119101
end
120102
end

0 commit comments

Comments
 (0)