Cuda battery library
Loading...
Searching...
No Matches
group.hpp
Go to the documentation of this file.
1// Copyright 2026 Pierre Talbot
2
3#ifndef CUDA_BATTERY_GROUP_HPP
4#define CUDA_BATTERY_GROUP_HPP
5
6#include "utility.hpp"
7
8#ifdef __CUDACC__
9 #include <cooperative_groups.h>
10 #include <cooperative_groups/reduce.h>
11#endif
12
13/** \file group.hpp
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`.
18 * | Group | Threads | `any_sync` |
19 * |----------------|--------------------------------------|--------------------------------------|
20 * | `Sequential` | the current thread | none |
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.
29 */
30
31namespace battery {
32
33/** A group made of the current thread only. */
34struct Sequential {
35 CUDA INLINE constexpr int rank() const { return 0; }
36 CUDA INLINE constexpr int size() const { return 1; }
37 CUDA INLINE constexpr bool any_sync(bool p) const { return p; }
38 CUDA INLINE constexpr void sync() const {}
39};
40
41#ifdef __CUDACC__
42
43/** All the threads of a (one-dimensional) block. */
44struct ThisBlock {
45 __device__ INLINE int rank() const { return threadIdx.x; }
46 __device__ INLINE int size() const { return blockDim.x; }
47 __device__ INLINE bool any_sync(bool p) const { return __syncthreads_or(p); }
48 __device__ INLINE void sync() const { __syncthreads(); }
49
50 /** A barrier returning the number of threads of the block for which `p` is `true`. */
51 __device__ INLINE int count_sync(bool p) const { return __syncthreads_count(p); }
52};
53
54/** All the warps of a (one-dimensional) block: the rank of a thread is the index of its warp.
55 * In the loop `for(int k = rank(); k < n; k += size())`, the 32 lanes of the warp `w` execute the iterations `k = w, w + num_warps, ...` together.
56 * Hence, the body of the loop can use warp-level primitives, such as the methods of `ThisWarp`.
57 * `blockDim.x` must be a multiple of 32. */
58struct WarpsOfBlock {
59 __device__ INLINE int rank() const { return threadIdx.x / 32; }
60 __device__ INLINE int size() const { return blockDim.x / 32; }
61 __device__ INLINE bool any_sync(bool p) const { return __syncthreads_or(p); }
62 __device__ INLINE void sync() const { __syncthreads(); }
63};
64
65/** All the lanes of the warp of the current thread.
66 * All the 32 lanes must call these methods together (no divergence). */
67struct ThisWarp {
68 __device__ INLINE int rank() const { return threadIdx.x % 32; }
69 __device__ INLINE int size() const { return 32; }
70 __device__ INLINE bool any_sync(bool p) const { return __any_sync(0xffffffffu, p); }
71 __device__ INLINE void sync() const { __syncwarp(); }
72
73 /** \return A mask whose bit `r` is the value of `p` in the lane `r`. */
74 __device__ INLINE unsigned int ballot_sync(bool p) const { return __ballot_sync(0xffffffffu, p); }
75
76 /** \return The sum of `x` over the lanes of the warp.
77 * It uses the `redux` instruction on sm_80 and later, and shuffles before. */
78 __device__ INLINE int reduce_add_sync(int x) const {
79 namespace cg = cooperative_groups;
80 return cg::reduce(cg::tiled_partition<32>(cg::this_thread_block()), x, cg::plus<int>());
81 }
82};
83
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). */
93 bool any_true[3];
94
95 /** `t % 3`, the flag of the current call, see `any_sync`. */
96 int slot;
97
98public:
99 CUDA GridGroup(): any_true{false, false, false}, slot(0) {}
100
101 /** `this_grid()` reads two special registers (no memory load), and its result is specific to the launch, hence we do not store it. */
102 __device__ INLINE int rank() const { return static_cast<int>(cooperative_groups::this_grid().thread_rank()); }
103 __device__ INLINE int size() const { return static_cast<int>(cooperative_groups::this_grid().num_threads()); }
104 __device__ INLINE void sync() const { cooperative_groups::this_grid().sync(); }
105
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.
110 bool block_any = __syncthreads_or(p);
111 int s = slot;
112 int next = s == 2 ? 0 : s + 1;
113 if(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.
117 if(rank() == 0) {
118 any_true[next] = false;
119 }
120 sync();
121 bool 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`.
123 // Every block writes the same value.
124 if(threadIdx.x == 0) {
125 slot = next;
126 }
127 return cont;
128 }
129};
130
131#endif // __CUDACC__
132
133} // namespace battery
134
135#endif
Definition algorithm.hpp:10
Definition group.hpp:34
CUDA INLINE constexpr int size() const
Definition group.hpp:36
CUDA INLINE constexpr int rank() const
Definition group.hpp:35
CUDA INLINE constexpr bool any_sync(bool p) const
Definition group.hpp:37
CUDA INLINE constexpr void sync() const
Definition group.hpp:38
#define INLINE
Definition utility.hpp:77
#define CUDA
Definition utility.hpp:73