1

I am using an MTLBuffer in Metal that I created by allocating several memory pages (using vm_allocate) with

device.makeBuffer(bytesNoCopy:length:options:deallocator:).

I write the buffer with CPU and the GPU only reads it. I know that generally I need to synchronise between CPU and GPU.

However, I have more knowledge about where in the MTLBuffer write (by CPU) and read (by GPU) happens and in my case writing is into different memory pages than the read (in a given time interval).

My question: Do I need to sync between CPU and GPU even if the relevant data that is written and read are on different memory pages (but in the same MTLBuffer)? Intuitively, I would think not, but then MTLBuffer is a bit opaque and I don't really know what kind of processing/requirement the GPU actually does/has with the MTLBuffer.

Additional info: This is a question for iOS and MTLStorageMode is shared.

Thank you very much for help!

Céline
  • 185
  • 7
  • The way you want to use the buffer is called "false sharing". If your buffers are hazard tracked, it will make the driver serialize work against it unnecessarily. – JustSomeGuy Jul 02 '22 at 05:13
  • 1
    Unless you are using manual hazard tracking, at which point it should probably be fine, as long as the values aren't in the same cache line – JustSomeGuy Jul 02 '22 at 05:14
  • Thank you for your comments! Indeed I want to manually hazard track the buffer. I also thought that even avoiding the same cache lines might be probably sufficient, but in my case I even can avoid R&W on the same memory page, so I can assume quite a lot. But I wondered if there is an actual guarantee for it being ok. – Céline Jul 02 '22 at 11:33
  • I am pretty sure that you need to either wait for a command buffer to complete, or have a shared event fire after modification happened on a GPU timeline to *guarantee* that you will see side effects on CPU. – JustSomeGuy Jul 03 '22 at 03:07
  • Thank you for the insights, that makes a lot of sense! It is indeed plausible that things would go fine, but a guarantee is a different matter, and since not officially stated in the docs that it would be ok, I will not go there... Thank you again for your help! – Céline Jul 04 '22 at 13:17

1 Answers1

0

Assuming the buffer was created with MTLStorageModeManaged, you can use the function didModifyRange to sync CPU to GPU for only a portion (a page for example) of the buffer.