diff --git a/perf/array.jl b/perf/array.jl index 857a05970..954014ccc 100644 --- a/perf/array.jl +++ b/perf/array.jl @@ -2,7 +2,7 @@ const m = 512 const n = 1000 for (S, smname) in [(Metal.PrivateStorage,"private"), (Metal.SharedStorage,"shared")] - group = addgroup!(SUITE, "$smname array") + local group = addgroup!(SUITE, "$smname array") # generate some arrays cpu_mat = rand(rng, Float32, m, n) diff --git a/src/array.jl b/src/array.jl index bad371f46..a288a271d 100644 --- a/src/array.jl +++ b/src/array.jl @@ -556,7 +556,7 @@ Base.unsafe_convert(::Type{MTL.MTLBuffer}, A::PermutedDimsArray) = ## unsafe_wrap function Base.unsafe_wrap(::Type{<:Array}, arr::MtlArray{T,N}, dims=size(arr); own=false) where {T,N} - return unsafe_wrap(Array{T,N}, arr.data[], dims; own) + return unsafe_wrap(Array{T,N}, pointer(arr), dims; own) end function Base.unsafe_wrap(t::Type{<:Array{T}}, buf::MTLBuffer, dims; own=false) where T diff --git a/src/device/array.jl b/src/device/array.jl index 036f6d3d3..ca075e728 100644 --- a/src/device/array.jl +++ b/src/device/array.jl @@ -149,7 +149,7 @@ Base.show(io::IO, a::MtlDeviceArray) = Base.show(io::IO, mime::MIME"text/plain", a::MtlDeviceArray) = show(io, a) @inline function Base.unsafe_view(A::MtlDeviceVector{T}, I::Vararg{Base.ViewIndex,1}) where {T} - ptr = pointer(A) + (I[1].start-1)*sizeof(T) + ptr = pointer(A, I[1].start) len = I[1].stop - I[1].start + 1 return MtlDeviceArray(len, ptr) end diff --git a/test/array.jl b/test/array.jl index e2aaaacc2..3333f5dab 100644 --- a/test/array.jl +++ b/test/array.jl @@ -370,6 +370,15 @@ end arr2 .+= 1; @test all(arr2 .== 2) @test all(marr2 .== 2) + + @testset "Issue #451" begin + a = mtl(reshape(Float32.(1:60), 5,4,3);storage=Metal.SharedStorage) + view_a = @view a[:,1:4,2] + b = copy(unsafe_wrap(Array, view_a)) + c = Array(view_a) + + @test b == c + end end @testset "ReshapedArray" begin