You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The package has already the support to create an Array given the memory allocated by a MtlArray, but not the other way around. It is possible, but required that the allocated memory is page-aligned. The following code is a MWE of how this can be achieved (without worrying about object destruction):
using Metal
# Obtain the page size.
pagesize =ccall(:getpagesize, Cint, ())
# Dimensions and type of the desired array.
dims = (1000, 1000)
T = Float32
# Compute how many pages we need to store the array.
npages =ceil(Int, prod(dims) *sizeof(T) / pagesize)
# Compute the total number of bytes.
nbytes = npages * pagesize
# Allocate memory aligned with the page.
addr =Ref(C_NULL)
ccall(
:posix_memalign,
Cint,
(Ptr{Ptr{Cvoid}}, Csize_t, Csize_t),
addr,
pagesize,
prod(dims) *sizeof(T)
)
# Wrap the allocated memory to a Julia array.
array =unsafe_wrap(Array{T, length(dims)}, reinterpret(Ptr{T}, addr[]), dims, own =false)
# Create the MtlArray.
pbuf = Metal.MTL.mtDeviceNewBufferWithBytesNoCopy(
current_device(),
addr[],
nbytes,
Metal.Shared | Metal.MTL.DefaultTracking | Metal.MTL.DefaultCPUCache
)
buf =MtlBuffer(pbuf)
marray =MtlArray{T, length(dims)}(buf, dims)
julia> array[1, 1] =100.0100.0
julia> marray[1, 1]
100.0
julia> array[1000, 1000] =19861968
julia> marray[1000, 1000]
1986.0
julia> marray[1000, 1000] =19871987
julia> array[1000, 1000]
1987.0
The text was updated successfully, but these errors were encountered:
Thanks for creating this issue. Not being an expert I was confused why this package wasn't taking advantage of the shared memory of M1/2 chips and this answered that question.
Perhaps a comment in the README would be worthwhile for others interested in this?
Hi!
The package has already the support to create an
Array
given the memory allocated by aMtlArray
, but not the other way around. It is possible, but required that the allocated memory is page-aligned. The following code is a MWE of how this can be achieved (without worrying about object destruction):The text was updated successfully, but these errors were encountered: