Hard
A cross-warp flag-and-payload spin-wait in shared memory
A block has 4 warps (128 threads). Thread 0 (in warp 0) writes a flag and a value into shared memory; thread 32 (the first thread of warp 1) reads them, with no barrier or fence between the write and the read. The programmer assumes warp 0 always 'runs first' since it has the lower thread index. What actually happens?
kernel.cu
__shared__ int flag;
__shared__ float payload;
__global__ void crossWarp() {
if (threadIdx.x == 0) {
payload = computeExpensiveValue();
flag = 1; // no fence/barrier after this
}
if (threadIdx.x == 32) {
while (flag == 0) { /* spin, hoping to see it flip */ }
use(payload);
}
}26 of 46 in GPU / CUDA