Metal.jl icon indicating copy to clipboard operation
Metal.jl copied to clipboard

Create MtlArray using memory allocated by Array

Open christiangnrd opened this issue 1 year ago • 5 comments

Feedback on implementation, interface, tests, etc. is welcome.

Closes #62

christiangnrd avatar Mar 19 '24 20:03 christiangnrd

Maybe a check to see if a pointer is page-aligned and throw otherwise? And then for unsafe_copyto! it could fallback to the old implementation if the pointer is not page-aligned?

christiangnrd avatar Mar 19 '24 21:03 christiangnrd

In theory the function is already unsafe. Might as well be up to the user to check? Not sure.

But yeah either documenting or asserting is a good idea.

tgymnich avatar Mar 19 '24 21:03 tgymnich

I believe that this code works in MacOS 14.4 but not MacOS 13.x. For some reason, on 14.4, alloc_buffer_nobytes always works, even if the array is not page-aligned, while in MacOS 13, it always returns a null pointer.

Does anyone have any ideas as to why?

christiangnrd avatar Mar 20 '24 16:03 christiangnrd

FWIW, https://developer.apple.com/documentation/metal/mtldevice/1433382-newbufferwithbytesnocopy still mentions that the pointer has to be page-aligned.

maleadt avatar Mar 20 '24 17:03 maleadt

Apparently the array/buffer size also has to be divisible by 4096 to work in MacOS 13.

https://developer.apple.com/library/archive/documentation/Performance/Conceptual/ManagingMemory/Articles/MemoryAlloc.html#//apple_ref/doc/uid/20001881-99117

christiangnrd avatar Mar 21 '24 01:03 christiangnrd

I think we always have to enforce pointer and buffer alignment:

pointer

    A page-aligned pointer to the starting memory address.
length

    The size of the new buffer, in bytes, that results in a page-aligned region of memory.

Even though it seems to work on macOS 14, it does seem to violate the docs. I'll push a commit, if you don't mind.

maleadt avatar Apr 10 '24 13:04 maleadt

@christiangnrd I tightened the check as mentioned above, and also simplified the implementation a little: determining nocopy within unsafe_copyto so that it doesn't have to be passed as a kwarg that doesn't really make much sense IMO (because it's about the temporary buffer, which is an implementation detail to the unsafe_copyto! caller).

maleadt avatar Apr 10 '24 13:04 maleadt

All the changes you made seem like improvements to me.

christiangnrd avatar Apr 10 '24 14:04 christiangnrd

Thanks!

maleadt avatar Apr 10 '24 14:04 maleadt