Skip to content

Commit 75a931e

Browse files
committed
DArray: Add view ctor
1 parent d0cf5cc commit 75a931e

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/array/alloc.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ function Base.zero(x::DArray{T,N}) where {T,N}
8181
return _to_darray(a)
8282
end
8383

84+
function Base.view(A::AbstractArray{T,N}, p::Blocks{N}) where {T,N}
85+
d = ArrayDomain(Base.index_shape(A))
86+
dc = partition(p, d)
87+
# N.B. We use `tochunk` because we only want to take the view locally, and
88+
# taking views should be very fast
89+
chunks = [tochunk(view(A, x.indexes...)) for x in dc]
90+
return DArray(T, d, dc, chunks, p)
91+
end
92+
8493
function sprand(p::Blocks, m::Integer, n::Integer, sparsity::Real)
8594
s = rand(UInt)
8695
f = function (idx, t,sz)

test/array.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ end
3838
@test r[1:10] != r[11:20]
3939
end
4040

41+
@testset "view" begin
42+
A = rand(64, 64)
43+
DA = view(A, Blocks(8, 8))
44+
@test collect(DA) == A
45+
@test size(DA) == (64, 64)
46+
A_v = fetch(first(DA.chunks))
47+
@test A_v isa SubArray
48+
@test A_v == A[1:8, 1:8]
49+
end
50+
4151
@testset "map" begin
4252
X1 = ones(Blocks(10, 10), 100, 100)
4353
X2 = map(x->x+1, X1)

0 commit comments

Comments
 (0)