Skip to content

Commit 2416b0f

Browse files
authored
Merge pull request #264 from devmotion/dw/view
Don't send `view`s between workers
2 parents e2ef5c9 + f551205 commit 2416b0f

File tree

1 file changed

+11
-29
lines changed

1 file changed

+11
-29
lines changed

src/darray.jl

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ function locate(d::DArray, I::Int...)
455455
end
456456
end
457457

458-
chunk(d::DArray{T,N,A}, i...) where {T,N,A} = remotecall_fetch(localpart, d.pids[i...], d)::A
458+
chunk(d::DArray{T,N,A}, pid::Int) where {T,N,A} = remotecall_fetch(localpart, pid, d)::A
459459

460460
## convenience constructors ##
461461

@@ -573,22 +573,22 @@ DArray{T,N,S}(A::S) where {T,N,S<:AbstractArray} = distribute(convert(AbstractAr
573573

574574
function Array{S,N}(d::DArray{T,N}) where {S,T,N}
575575
a = Array{S}(undef, size(d))
576-
@sync begin
577-
for i = 1:length(d.pids)
578-
@async a[d.indices[i]...] = chunk(d, i)
576+
@sync for (pid, indices) in zip(d.pids, d.indices)
577+
if !any(isempty, indices)
578+
@async a[indices...] = chunk(d, pid)
579579
end
580580
end
581581
return a
582582
end
583583

584584
function Array{S,N}(s::SubDArray{T,N}) where {S,T,N}
585585
I = s.indices
586-
d = s.parent
586+
d = parent(s)
587587
if isa(I,Tuple{Vararg{UnitRange{Int}}}) && S<:T && T<:S && !isempty(s)
588588
l = locate(d, map(first, I)...)
589589
if isequal(d.indices[l...], I)
590590
# SubDArray corresponds to a chunk
591-
return chunk(d, l...)
591+
return chunk(d, d.pids[l...])
592592
end
593593
end
594594
a = Array{S}(undef, size(s))
@@ -695,20 +695,6 @@ function Base.deepcopy(src::DArray)
695695
end
696696
return dest
697697
end
698-
699-
# local copies are obtained by convert(Array, ) or assigning from
700-
# a SubDArray to a local Array.
701-
702-
function Base.setindex!(a::Array, d::DArray,
703-
I::Union{UnitRange{Int},Colon,Vector{Int},StepRange{Int,Int}}...)
704-
n = length(I)
705-
@sync for i = 1:length(d.pids)
706-
K = d.indices[i]
707-
@async a[[I[j][K[j]] for j=1:n]...] = chunk(d, i)
708-
end
709-
return a
710-
end
711-
712698
# We also want to optimize setindex! with a SubDArray source, but this is hard
713699
# and only works on 0.5.
714700

@@ -813,24 +799,20 @@ function Base.setindex!(a::Array, s::SubDArray,
813799
I::Union{UnitRange{Int},Colon,Vector{Int},StepRange{Int,Int}}...)
814800
Inew = Base.to_indices(a, I)
815801
Base.setindex_shape_check(s, Base.index_lengths(Inew...)...)
816-
n = length(Inew)
817-
d = s.parent
802+
d = parent(s)
818803
J = Base.to_indices(d, s.indices)
819-
@sync for i = 1:length(d.pids)
820-
K_c = d.indices[i]
804+
@sync for (pid, K_c) in zip(d.pids, d.indices)
821805
K = map(intersect, J, K_c)
822806
if !any(isempty, K)
823807
K_mask = map(indexin_mask, J, K_c)
824808
idxs = restrict_indices(Inew, K_mask)
825809
if isequal(K, K_c)
826810
# whole chunk
827-
@async a[idxs...] = chunk(d, i)
811+
@async a[idxs...] = chunk(d, pid)
828812
else
829813
# partial chunk
830-
@async a[idxs...] =
831-
remotecall_fetch(d.pids[i]) do
832-
view(localpart(d), [K[j].-first(K_c[j]).+1 for j=1:length(J)]...)
833-
end
814+
localidxs = map((Kj, K_cj) -> Kj .- (first(K_cj) - 1), K, K_c)
815+
@async a[idxs...] = remotecall_fetch((d, idxs) -> localpart(d)[idxs...], pid, d, localidxs)
834816
end
835817
end
836818
end

0 commit comments

Comments
 (0)