14 * Groups of threads executing a collective operation together, with the same interface for the current thread, a warp, a block, the warps of a block and a grid.
15 *
16 * A group provides `rank()`, `size()`, `bool any_sync(bool p)`, a barrier returning the logical or of `p` over all its threads, and `sync()`, a plain barrier.
17 * For instance, the loop `for(int k = g.rank(); k < n; k += g.size())` splits the iterations `0, ..., n-1` among the members of the group `g`.
21 * | `ThisBlock` | the threads of the block | `__syncthreads_or` |
22 * | `WarpsOfBlock` | the warps of the block | `__syncthreads_or` |
23 * | `ThisWarp` | the lanes of the warp | `__any_sync` |
24 * | `GridGroup` | the threads of the grid | `__syncthreads_or` and `grid.sync()` |
25 *
26 * Synchronization convention for the collective methods of an object, i.e., the methods called by all the threads of a group:
27 * A collective method does not synchronize the threads when it returns: add a barrier before reading its results.
28 * It does not synchronize on entry either: the caller guarantees that all the threads have exited the other methods of the same object (e.g., with a barrier) before calling it.
84/** All the threads of the grid, the kernel must be launched with `cudaLaunchCooperativeKernel`.
85 * Allocate the group in global memory and share it among the blocks (e.g., with `battery::make_unique_grid`, or in managed memory).
86 * It holds no data specific to a launch, so it can be reused across kernel launches.
87 *
88 * There is no reduction over a grid in cooperative groups (`cooperative_groups::reduce` only supports tiles), hence `any_sync` reduces in three shared flags, `any_true`, used in rotation.
89 * It costs one `grid.sync()` per call, and consecutive loops calling it do not need a barrier in between. */
90class GridGroup {
91 /** `any_true[t % 3]` is `true` if a thread called `any_sync(true)` in the call `t`, where `t` counts the calls of `any_sync` since the construction.
92 * We do not use atomics because tearing is seemingly not possible in CUDA (according to information given by Nvidia engineers during a hackathon). */
93bool any_true[3];
94
95 /** `t % 3`, the flag of the current call, see `any_sync`. */
106 /** The call `t` writes and reads `any_true[t % 3]`, so a thread reading the flag of the call `t` is not disturbed by a fast thread already in the call `t + 1`.
107 * The flag of the call `t + 1` is reset during the call `t`, after all the threads read it in the call `t - 2`. */
108 __device__ bool any_sync(bool p) {
109// It also makes the write of `slot` by the thread 0 of this block, in the previous call, visible to the threads of this block.
110bool block_any = __syncthreads_or(p);
111int s = slot;
112int next = s == 2 ? 0 : s + 1;
113if(block_any && threadIdx.x == 0) {
114 any_true[s] = true;
115 }
116// `any_true[next]` is written in the next call, and was last read two calls ago.
117if(rank() == 0) {
118 any_true[next] = false;
119 }
120 sync();
121bool cont = any_true[s];
122// All the threads read `slot` in this call before `sync()`, and they read it again in the next call after `__syncthreads_or`.