Skip to content

Commit 361b7eb

Browse files
committed
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 2be6577 commit 361b7eb

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
@@ -564,6 +564,16 @@ Base.copyto!(dest::SubOrDArray, src::SubOrDArray) = begin
564564
end
565565
Base.copy!(dest::SubOrDArray, src::SubOrDArray) = copyto!(dest, src)
566566

567+
function Base.deepcopy(src::DArray)
568+
dest = similar(src)
569+
asyncmap(procs(src)) do p
570+
remotecall_fetch(p) do
571+
dest[:L] = deepcopy(src[:L])
572+
end
573+
end
574+
return dest
575+
end
576+
567577
# local copies are obtained by convert(Array, ) or assigning from
568578
# a SubDArray to a local Array.
569579

test/darray.jl

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

6262
check_leaks()
6363

64-
@testset "test DArray equality" begin
64+
@testset "test DArray equality/copy/deepcopy" begin
6565
D = drand((200,200), [MYID, OTHERIDS])
66-
DC = copy(D)
6766

6867
@testset "test isequal(::DArray, ::DArray)" begin
68+
DC = copy(D)
6969
@test D == DC
70+
close(DC)
7071
end
7172

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

77108
close(D)
78-
close(DC)
79109
end
80110

81111
check_leaks()

0 commit comments

Comments
 (0)