1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
#include <cute/tensor.hpp> #include <cute/arch/copy_sm90_desc.hpp> #include <cstdio>
using namespace cute;
__global__ void kernel_thread_arrive_wait(int num_threads) { __shared__ __align__(8) uint64_t barrier;
int tid = threadIdx.x;
if (tid == 0) { cute::initialize_barrier(barrier, num_threads); printf("[实验1] 初始化 mbarrier, 期望线程数 = %d\n", num_threads); } __syncthreads();
if (tid < num_threads) { int result = tid * tid; printf(" 线程 %d: 计算完成 (result=%d), 准备 arrive\n", tid, result);
cute::arrive_barrier(barrier); }
if (tid < num_threads) { cute::wait_barrier(barrier, 0); printf(" 线程 %d: wait 通过!所有线程都已 arrive\n", tid); } }
__global__ void kernel_producer_consumer() { __shared__ __align__(8) uint64_t barrier; __shared__ float data[32];
int tid = threadIdx.x;
if (tid == 0) { cute::initialize_barrier(barrier, 1); printf("\n[实验2] Producer/Consumer 模式, thread_count=1 (只有 producer arrive)\n"); } __syncthreads();
if (tid == 0) { for (int i = 0; i < 32; ++i) { data[i] = float(i) * 1.5f; } printf(" Producer(线程0): 数据准备完成, arrive\n");
cute::arrive_barrier(barrier); }
if (tid >= 1 && tid <= 4) { cute::wait_barrier(barrier, 0); printf(" Consumer(线程%d): wait 通过, data[%d] = %.1f (期望 %.1f)\n", tid, tid, data[tid], float(tid) * 1.5f); }
if (tid == 0) { printf("\n ★ 关于 Transaction Bytes(TMA 中使用):\n"); printf(" set_barrier_transaction_bytes(mbar, N):\n"); printf(" = mbarrier.arrive.expect_tx (同时 arrive + 设置预期搬运量)\n"); printf(" TMA 硬件搬运完成后自动 complete_tx\n"); printf(" 当 arrive_count 完成 + tx_bytes 完成 → phase 翻转\n"); } }
__device__ int probe_barrier_phase(uint64_t& barrier) { uint32_t smem_addr = cute::cast_smem_ptr_to_uint(&barrier); int result; asm volatile( "{\n" " .reg .pred P1;\n" " mbarrier.test_wait.parity.shared::cta.b64 P1, [%1], 0;\n" " selp.s32 %0, 1, 0, P1;\n" "}\n" : "=r"(result) : "r"(smem_addr)); return result; }
__global__ void kernel_phase_flip() { __shared__ __align__(8) uint64_t barrier;
int tid = threadIdx.x; constexpr int kRounds = 4; constexpr int kThreads = 32;
if (tid == 0) { cute::initialize_barrier(barrier, kThreads); printf("\n[实验3] Phase 翻转观察, %d 轮, %d 线程\n", kRounds, kThreads); printf(" ★ 用 mbarrier.test_wait.parity PTX 探测硬件真实 phase\n\n"); } __syncthreads();
if (tid < kThreads) { int current_phase = 0; if (tid == 0) { int hw_phase = probe_barrier_phase(barrier); printf(" 初始化后: 硬件 phase = %d\n\n", hw_phase); }
for (int round = 0; round < kRounds; ++round) { if (tid == 0) { printf(" --- 第 %d 轮 (当前 phase=%d, 等待翻转) ---\n", round, current_phase); }
cute::arrive_barrier(barrier);
cute::wait_barrier(barrier, current_phase);
int new_phase = 1 - current_phase; if (tid == 0) { int hw_phase = probe_barrier_phase(barrier); printf(" 第 %d 轮完成! phase: %d → %d (硬件探测: phase=%d)\n", round, current_phase, new_phase, hw_phase); }
current_phase = new_phase; } }
if (tid == 0) { printf("\n ★ 在 pipeline 中的应用:\n"); printf(" 每个 stage 有一对 barrier (full + empty)\n"); printf(" full_barrier: producer arrive → consumer wait\n"); printf(" empty_barrier: consumer arrive → producer wait\n"); printf(" phase 自动交替,实现环形 buffer 的多轮复用\n"); } }
int main() { printf("=== 第 10 课:MBarrier 基础 ===\n\n");
printf("========================================\n"); printf("实验 1:线程到达同步\n"); printf("========================================\n"); printf(" 类似 __syncthreads,但 mbarrier 支持:\n"); printf(" - 只让部分线程参与同步\n"); printf(" - 与 TMA 异步搬运配合\n"); printf(" - Phase 翻转实现流水线\n\n");
kernel_thread_arrive_wait<<<1, 8>>>(4); cudaDeviceSynchronize();
auto err = cudaGetLastError(); if (err != cudaSuccess) { printf(" CUDA error: %s\n", cudaGetErrorString(err)); return 1; }
printf("\n========================================\n"); printf("实验 2:Producer/Consumer 模式\n"); printf("========================================\n"); printf(" mbarrier 的灵活性:\n"); printf(" - 只有 producer 需要 arrive\n"); printf(" - consumer 只需 wait\n"); printf(" - 这是 Hopper warp-specialized kernel 的基础\n\n");
kernel_producer_consumer<<<1, 32>>>(); cudaDeviceSynchronize();
err = cudaGetLastError(); if (err != cudaSuccess) { printf(" CUDA error: %s\n", cudaGetErrorString(err)); return 1; }
printf("\n========================================\n"); printf("实验 3:Phase 翻转\n"); printf("========================================\n"); printf(" 同一个 barrier 重复使用:\n"); printf(" - 第 0 轮等 phase 0→1\n"); printf(" - 第 1 轮等 phase 1→0\n"); printf(" - phase 自动交替,这是流水线的核心机制\n\n");
kernel_phase_flip<<<1, 32>>>(); cudaDeviceSynchronize();
err = cudaGetLastError(); if (err != cudaSuccess) { printf(" CUDA error: %s\n", cudaGetErrorString(err)); return 1; }
printf("\n========================================\n"); printf("总结\n"); printf("========================================\n"); printf(" mbarrier vs __syncthreads:\n"); printf(" ┌───────────────────┬──────────────────┬──────────────────┐\n"); printf(" │ 特性 │ __syncthreads │ mbarrier │\n"); printf(" ├───────────────────┼──────────────────┼──────────────────┤\n"); printf(" │ 同步范围 │ 整个 CTA │ 可选线程子集 │\n"); printf(" │ 异步搬运支持 │ ✗ │ ✓ (tx bytes) │\n"); printf(" │ Phase 翻转 │ ✗ │ ✓ (0↔1 循环) │\n"); printf(" │ 流水线支持 │ ✗ │ ✓ (多 stage) │\n"); printf(" │ TMA 配合 │ ✗ │ ✓ (硬件自动) │\n"); printf(" └───────────────────┴──────────────────┴──────────────────┘\n");
printf("\n=== 练习完成 ===\n"); return 0; }
|