Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

metal: persistent-kernel probe (grid barrier between phases): correct only with device-scope fences, not faster

Niko12214e5ea96d2370
diff --git a/metal/kernels/gridbar.metal b/metal/kernels/gridbar.metal
new file mode 100644
index 000000000..5cd9a0fac
--- /dev/null
+++ b/metal/kernels/gridbar.metal
@@ -0,0 +1,54 @@
+// metal/kernels/gridbar.metal - feasibility probe for a persistent kernel: phases separated by an in-kernel grid
+// barrier (a device atomic counter) instead of dispatch boundaries. Metal does not promise that all threadgroups of
+// a grid are resident at once; the probe launches at most one threadgroup per GPU core.
+//
+// Each phase p: every thread streams its share of chunk p (n uint4), and threadgroup g writes a token value
+// (p * 1000 + g) to flag[p][g]; after the barrier, phase p + 1 checks every flag of phase p (written by the OTHER
+// threadgroups) and counts mismatches into bad[0].
+
+static inline void grid_barrier(device atomic_uint* bar, uint target, ushort tid) {
+ threadgroup_barrier(mem_flags::mem_device);
+#ifdef GB_FENCE
+ atomic_thread_fence(mem_flags::mem_device, memory_order_seq_cst, thread_scope_device);
+#endif
+ if (tid == 0) {
+ atomic_fetch_add_explicit(bar, 1u, memory_order_relaxed);
+ while (atomic_load_explicit(bar, memory_order_relaxed) < target) {}
+ }
+ threadgroup_barrier(mem_flags::mem_device);
+#ifdef GB_FENCE
+ atomic_thread_fence(mem_flags::mem_device, memory_order_seq_cst, thread_scope_device);
+#endif
+}
+
+kernel void k_phases(device const uint4* src [[buffer(0)]], device uint* out [[buffer(1)]],
+ device atomic_uint* bar [[buffer(2)]], device volatile uint* flag [[buffer(3)]],
+ device atomic_uint* bad [[buffer(4)]], constant uint& n [[buffer(5)]],
+ constant uint& phases [[buffer(6)]],
+ uint tgpx [[threadgroup_position_in_grid]], uint ntgx [[threadgroups_per_grid]],
+ ushort tid [[thread_index_in_threadgroup]], uint gid [[thread_position_in_grid]],
+ uint nt [[threads_per_grid]]) {
+ const uint g = tgpx, ntg = ntgx;
+ uint4 acc = 0;
+ for (uint p = 0; p < phases; ++p) {
+ if (p > 0 && tid < ntg) {
+ const uint v = flag[(p - 1) * ntg + tid];
+ if (v != (p - 1) * 1000 + tid) atomic_fetch_add_explicit(bad, 1u, memory_order_relaxed);
+ }
+ device const uint4* s = src + ulong(p) * n;
+ for (uint i = gid; i < n; i += nt) acc += s[i];
+ if (tid == 0) flag[p * ntg + g] = p * 1000 + g;
+ grid_barrier(bar, (p + 1) * ntg, tid);
+ }
+ out[gid] = acc.x ^ acc.y ^ acc.z ^ acc.w;
+}
+
+// one phase per dispatch, for comparison
+kernel void k_phase1(device const uint4* src [[buffer(0)]], device uint* out [[buffer(1)]],
+ constant uint& n [[buffer(2)]], constant uint& p [[buffer(3)]],
+ uint gid [[thread_position_in_grid]], uint nt [[threads_per_grid]]) {
+ uint4 acc = 0;
+ device const uint4* s = src + ulong(p) * n;
+ for (uint i = gid; i < n; i += nt) acc += s[i];
+ out[gid] = acc.x ^ acc.y ^ acc.z ^ acc.w;
+}
diff --git a/metal/tests/gridbar_bench.cpp b/metal/tests/gridbar_bench.cpp
new file mode 100644
index 000000000..6420be4ea
--- /dev/null
+++ b/metal/tests/gridbar_bench.cpp
@@ -0,0 +1,66 @@
+// metal/tests/gridbar_bench.cpp - phases of a streaming read separated by dispatch boundaries vs by an in-kernel
+// grid barrier (gridbar.metal), and whether a phase sees the previous phase's writes of the other threadgroups.
+// gridbar_bench [MiB per phase] [phases]
+#include "mtl_rt.h"
+
+#include <algorithm>
+#include <cstdio>
+#include <cstdlib>
+#include <vector>
+
+int main(int argc, char** argv) {
+ const double mib = argc > 1 ? std::atof(argv[1]) : 4.5;
+ const int phases = argc > 2 ? std::atoi(argv[2]) : 96;
+ mtl::Context cx;
+ const int lib = cx.compile({"gridbar.metal"}, std::getenv("GB_FENCE") ? "#define NO_GGML_COMMON 1\n#define GB_FENCE 1\n"
+ : "#define NO_GGML_COMMON 1\n");
+ mtl::Pipeline pp = cx.pipeline(lib, "k_phases"), p1 = cx.pipeline(lib, "k_phase1");
+ const uint32_t n = (uint32_t)(mib * 1048576 / 16);
+ mtl::Buffer src = cx.alloc((size_t)n * 16 * phases), out = cx.alloc(4u << 20), bar = cx.alloc(4),
+ flag = cx.alloc(4 * 64 * (size_t)phases), bad = cx.alloc(4);
+ for (size_t i = 0; i < src.bytes / 4; i += 1024) src.as<uint32_t>()[i] = (uint32_t)i;
+ std::printf("device %s: %d phases of %.1f MiB\n", cx.device_name().c_str(), phases, mib);
+ // dispatch boundaries
+ for (uint32_t groups : {40u, 160u, 640u}) {
+ std::vector<mtl::Dispatch> ds;
+ for (int p = 0; p < phases; ++p) {
+ mtl::Dispatch d;
+ d.p = &p1;
+ const uint32_t pu = p;
+ d.args = {mtl::Arg::B(src), mtl::Arg::B(out), mtl::Arg::V(n), mtl::Arg::V(pu)};
+ d.grid[0] = groups;
+ d.tg[0] = 1024;
+ ds.push_back(d);
+ }
+ cx.warm(ds, 0.2);
+ std::vector<double> t;
+ for (int i = 0; i < 5; ++i) t.push_back(cx.run(ds));
+ std::sort(t.begin(), t.end());
+ std::printf("dispatch per phase, %4u x 1024 threads: %7.2f us a phase %6.1f GB/s\n", groups,
+ t[2] / phases * 1e6, n * 16.0 * phases / t[2] / 1e9);
+ }
+ // grid barrier, at most one threadgroup a core
+ for (uint32_t groups : {10u, 20u, 40u}) {
+ mtl::Dispatch d;
+ d.p = &pp;
+ const uint32_t ph = phases;
+ d.args = {mtl::Arg::B(src), mtl::Arg::B(out), mtl::Arg::B(bar), mtl::Arg::B(flag), mtl::Arg::B(bad),
+ mtl::Arg::V(n), mtl::Arg::V(ph)};
+ d.grid[0] = groups;
+ d.tg[0] = 1024;
+ std::vector<double> t;
+ uint32_t badc = 0;
+ for (int i = 0; i < 6; ++i) {
+ bar.as<uint32_t>()[0] = 0;
+ bad.as<uint32_t>()[0] = 0;
+ for (size_t k = 0; k < flag.bytes / 4; ++k) flag.as<uint32_t>()[k] = 0xFFFFFFFFu;
+ const double s = cx.run({d});
+ if (i) t.push_back(s);
+ badc += bad.as<uint32_t>()[0];
+ }
+ std::sort(t.begin(), t.end());
+ std::printf("grid barrier, %4u x 1024 threads: %7.2f us a phase %6.1f GB/s stale flags seen: %u\n",
+ groups, t[2] / phases * 1e6, n * 16.0 * phases / t[2] / 1e9, badc);
+ }
+ return 0;
+}