Skip to content

Commit

Permalink
Use CPU copy with SharedStorage
Browse files Browse the repository at this point in the history
[skip tests]
  • Loading branch information
christiangnrd committed Oct 4, 2024
1 parent 7579820 commit d72ce7d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,12 @@ function Base.unsafe_copyto!(dev::MTLDevice, dest::MtlArray{T}, doffs, src::Arra
end
return dest
end
function Base.unsafe_copyto!(::MTLDevice, dest::MtlArray{T,<:Any,Metal.SharedStorage}, doffs, src::Array{T}, soffs, n) where T
# these copies are implemented using pure memcpy's, not API calls, so aren't ordered.
synchronize()
GC.@preserve src dest unsafe_copyto!(pointer(unsafe_wrap(Array,dest), doffs), pointer(src, soffs), n)
return dest
end

# GPU -> CPU
function Base.unsafe_copyto!(dev::MTLDevice, dest::Array{T}, doffs, src::MtlArray{T}, soffs, n) where T
Expand All @@ -414,6 +420,12 @@ function Base.unsafe_copyto!(dev::MTLDevice, dest::Array{T}, doffs, src::MtlArra
end
return dest
end
function Base.unsafe_copyto!(::MTLDevice, dest::Array{T}, doffs, src::MtlArray{T,<:Any,Metal.SharedStorage}, soffs, n) where T
# these copies are implemented using pure memcpy's, not API calls, so aren't ordered.
synchronize()
GC.@preserve src dest unsafe_copyto!(pointer(dest, doffs), pointer(unsafe_wrap(Array,src), soffs), n)
return dest
end

# GPU -> GPU
function Base.unsafe_copyto!(dev::MTLDevice, dest::MtlArray{T}, doffs, src::MtlArray{T}, soffs, n) where T
Expand All @@ -427,6 +439,12 @@ function Base.unsafe_copyto!(dev::MTLDevice, dest::MtlArray{T}, doffs, src::MtlA
end
return dest
end
function Base.unsafe_copyto!(::MTLDevice, dest::MtlArray{T,<:Any,Metal.SharedStorage}, doffs, src::MtlArray{T,<:Any,Metal.SharedStorage}, soffs, n) where T
# these copies are implemented using pure memcpy's, not API calls, so aren't ordered.
synchronize()
GC.@preserve src dest unsafe_copyto!(pointer(unsafe_wrap(Array,dest), doffs), pointer(unsafe_wrap(Array,src), soffs), n)
return dest
end


## regular gpu array adaptor
Expand Down
23 changes: 23 additions & 0 deletions test/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,29 @@ end
@test collect(Metal.fill(1, 2, 2)) == ones(Float32, 2, 2)
end

@testset "copyto!: $T, $S" for S in [Metal.PrivateStorage, Metal.SharedStorage], T in [Float16, Float32, Bool, Int16, Int32, Int64, Int8, UInt16, UInt32, UInt64, UInt8]
function testcopyto!(out, in)
copyto!(out,in)
return Array(in) == Array(out)
end

dim = (1000,17,10)
A = rand(T,dim)
mtlA = mtl(A;storage=S)

#cpu -> gpu
res = Metal.zeros(T,dim;storage=S)
@test testcopyto!(res,A)

#gpu -> cpu
res = zeros(T,dim)
@test testcopyto!(res,mtlA)

#gpu -> gpu
res = Metal.zeros(T,dim;storage=S)
@test testcopyto!(res,mtlA)
end

check_storagemode(arr, smode) = Metal.storagemode(arr) == smode

# There is some repetition to the GPUArrays tests to test for different storagemodes
Expand Down

0 comments on commit d72ce7d

Please sign in to comment.