[REQ] Add a block barrier for tile kernels, or order tile_scatter_masked after earlier reads
Description
wp.tile_scatter_masked() synchronizes the block only after its write. When a kernel reuses a shared tile, for example a block-wide counter across loop iterations, nothing orders that write after other threads' earlier reads of the same tile. Warp also has no block barrier that a kernel could call itself to close this write-after-read hazard.
Either of these would solve it:
- A public block barrier for tile kernels, for example
wp.tile_sync()(__syncthreads()on CUDA, a fiber switch for CPU blocks), so a kernel can place the barrier where it needs it. - A barrier before the write in
wp.tile_scatter_masked()(andwp.tile_scatter_add()). At minimum, a note in their docstrings that the write is not ordered after earlier reads of the tile by other threads.
Minimal example. Warps 1-3 do more work than warp 0 before reading the shared value. Warp 0 meanwhile reaches the next tile_scatter_masked() and overwrites the value.
import numpy as np
import warp as wp
BLOCK_DIM = 128
@wp.kernel
def overwrite_after_read(out: wp.array(dtype=int), use_workaround: int):
_, t = wp.tid()
flag = wp.tile_zeros(shape=1, dtype=int, storage="shared")
stack = wp.tile_stack(capacity=1, dtype=int)
wp.tile_scatter_masked(flag, 0, 1, t == 0) # all threads see 1 after this call
# Uneven work before the read: warps 1-3 take longer than warp 0.
if t >= 32:
x = float(0.0)
for k in range(20000):
x = wp.sin(x + float(k))
if x > 1.0e30:
wp.printf("%f\n", x)
v = wp.tile_extract(flag, 0)
if use_workaround != 0:
wp.tile_stack_clear(stack) # only used for its leading barrier
wp.tile_scatter_masked(flag, 0, 2, t == 0) # thread 0 overwrites the value the others read
out[t] = v
for use_workaround in (0, 1):
out = wp.zeros(BLOCK_DIM, dtype=int)
wp.launch_tiled(overwrite_after_read, dim=[1], inputs=[out, use_workaround], block_dim=BLOCK_DIM)
values = out.numpy()
print(f"workaround={use_workaround}: {int(np.sum(values == 1))} threads read 1, {int(np.sum(values == 2))} read 2")
Output (warp-lang 1.18.0, RTX 4090, driver 580.178.04; tile_scatter_masked in tile.h on main is unchanged):
workaround=0: 32 threads read 1, 96 read 2
workaround=1: 128 threads read 1, 0 read 2
With use_workaround, tile_stack_clear() on an otherwise unused stack supplies the missing barrier, because it synchronizes before its write. Kernels currently rely on that implementation detail.
Context
Newton's mesh-mesh SDF narrow phase keeps a block-shared edge counter. Thread 0 resets it with tile_scatter_masked() at the start of each shape pair, which is the use case behind #1298. Without a barrier before the reset, a warp that had not yet read the counter for the previous loop's exit test read the new start. It re-entered the finished loop and spun forever, so CollisionPipeline.collide() occasionally never returned. On an RTX 4090 shared with other GPU jobs this happened about once per 10^5 to 10^6 calls.
The fix in newton-physics/newton#4859 calls wp.tile_stack_clear() before the reset purely for its leading barrier. Newton's contact_reduction_global.py similarly uses a cooperative tile_scatter_masked() clear for its trailing barrier before reusing a shared tile. An explicit barrier would make that intent visible and would not depend on how these functions happen to synchronize.
Thanks for the detailed report and reproducer. While we investigate, a temporary workaround for CUDA kernels is to define an explicit block barrier using wp.func_native:
import warp as wp
@wp.func_native("""
#if defined(__CUDA_ARCH__)
__syncthreads();
#endif
""")
def _sync_threads(): ...
Call _sync_threads() after the earlier reads and before the next write to the shared tile. All threads in the block must reach the call; avoid placing it inside a branch that only some threads take.
It provides a barrier on CUDA only—the CPU implementation is a no-op.
@daedalus5 I recommend using the WP_TILE_SYNC(); macro instead. __syncthreads() is exclusive to CUDA, while the macro also works for logical CPU thread blocks and potential other future backends.