How to sync streaming bands in Metal

I want to sync records with MTLBuffer

between thread groups in Metal core. I tried to use the function atomic_uint

and atomic_fetch_add_explicit

. This should work, but my problem is that I don't know how to interpret the values ​​on the CPU side. I would prefer a locking mechanism that I could use to synchronize writes to the same buffer from different thread groups. Is there such a mechanism or some other workaround that is not related to atomic types?

+3


source to share


2 answers


I faced similar problems in 16 bit floating point conversion.



You can try to interpret the values ​​on the processor side by fetching the original bits from the buffer, comparing against the internal format of atom_uint, and then try to use the resulting information to process as the data type you want. Low-level approach for sure, but works when nothing happens.

+2


source


Locking usually does not work because not all thread groups are necessarily updated at the same time. For example, if you try to implement spin locking, you can just revive because the producer of your data may not be running yet if everything is the same core. Another problem with blocking schemes is that you cannot force the GPU to block or context switch, so you just waste energy spinning, potentially forever, waiting for the resource to become available.



They prefer to use global atoms instead for operations that traditionally use atomic operators. For example, if you want to write a simple malloc () that will be allocated from a pre-allocated MTLBuffer, you can use a global atom to increase the offset in the MTLBuffer by the size of each allocation. Return the original address of the old offset + MTLBuffer as the start of the allocation.

+1


source







All Articles