3

我想将写入同步到MTLBufferMetal 内核中的线程组之间。我尝试使用atomic_uint类型和atomic_fetch_add_explicit功能。这应该可行,但我的问题是我不知道如何解释 CPU 端的值。我更喜欢一种锁定机制,我可以使用它来同步从不同线程组对同一缓冲区的写入。是否有这种机制或其他一些不受原子类型约束的解决方法?

4

2 回答 2

2

我在 16 位浮点转换中遇到了类似的问题。

您可以尝试通过从缓冲区中提取原始位来解释 CPU 端的值,与 atomic_uint 的内部格式进行比较,然后尝试使用获得的信息将其重新转换为您需要的数据类型。肯定是一种低级方法,但在没有其他方法的情况下有效。

于 2015-04-24T14:24:56.223 回答
2

Locking generally doesn't work because not all thread groups are necessarily alive concurrently. If you try to implement a spin lock, for example, you may just livelock because the producer of your data may not have launched yet, assuming everything is the same kernel. Another problem with locking schemes is you can't cause the GPU to block or context switch, so you'll just be wasting power spinning, potentially forever, waiting for the resource to become available.

Prefer instead using global atomics for operations that are what atomic ops are traditionally used for. For example, if you want to write a simple malloc() that suballocates from a preallocated MTLBuffer, you can use a global atomic to increment the offset into the MTLBuffer by the size of each allocation. Return the old offset+MTLBuffer base address as the start of the allocation.

于 2017-08-15T23:07:03.350 回答