Skip to content

Commit 83961fd

Browse files
traktofonandreasnoack
authored andcommitted
Add deepcopy and tests.
`deepcopy(::DArray)` calls `similar` and deepcopies the localparts. Tests to ensure that `copy` makes a shadllow copy and `deepcopy` makes a deep copy.
1 parent d627700 commit 83961fd

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

src/darray.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,16 @@ Base.copyto!(dest::SubOrDArray, src::SubOrDArray) = begin
574574
end
575575
Base.copy!(dest::SubOrDArray, src::SubOrDArray) = copyto!(dest, src)
576576

577+
function Base.deepcopy(src::DArray)
578+
dest = similar(src)
579+
asyncmap(procs(src)) do p
580+
remotecall_fetch(p) do
581+
dest[:L] = deepcopy(src[:L])
582+
end
583+
end
584+
return dest
585+
end
586+
577587
# local copies are obtained by convert(Array, ) or assigning from
578588
# a SubDArray to a local Array.
579589

test/darray.jl

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,51 @@ end
6060

6161
check_leaks()
6262

63-
@testset "test DArray equality" begin
63+
@testset "test DArray equality/copy/deepcopy" begin
6464
D = drand((200,200), [MYID, OTHERIDS])
65-
DC = copy(D)
6665

6766
@testset "test isequal(::DArray, ::DArray)" begin
67+
DC = copy(D)
6868
@test D == DC
69+
close(DC)
6970
end
7071

71-
@testset "test copy(::DArray) does a copy of each localpart" begin
72+
@testset "test [deep]copy(::DArray) does a copy of each localpart" begin
73+
DC = copy(D)
7274
@spawnat OTHERIDS localpart(DC)[1] = 0
7375
@test fetch(@spawnat OTHERIDS localpart(D)[1] != 0)
76+
DD = deepcopy(D)
77+
@spawnat OTHERIDS localpart(DD)[1] = 0
78+
@test fetch(@spawnat OTHERIDS localpart(D)[1] != 0)
79+
close(DC)
80+
close(DD)
81+
end
82+
83+
@testset "test copy(::DArray) is shallow" begin
84+
DA = @DArray [rand(100) for i=1:10]
85+
DC = copy(DA)
86+
id = procs(DC)[1]
87+
@test DA == DC
88+
fetch(@spawnat id localpart(DC)[1] .= -1.0)
89+
@test DA == DC
90+
@test fetch(@spawnat id all(localpart(DA)[1] .== -1.0))
91+
close(DA)
92+
close(DC)
93+
end
94+
95+
@testset "test deepcopy(::DArray) is not shallow" begin
96+
DA = @DArray [rand(100) for i=1:10]
97+
DC = deepcopy(DA)
98+
id = procs(DC)[1]
99+
@test DA == DC
100+
fetch(@spawnat id localpart(DC)[1] .= -1.0)
101+
@test DA != DC
102+
@test fetch(@spawnat id all(localpart(DA)[1] .>= 0.0))
103+
close(DA)
104+
close(DC)
74105
end
75106

76107
close(D)
77-
close(DC)
78108
end
79109

80110
check_leaks()

0 commit comments

Comments
 (0)