okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-mac-rel (Mac Metal support) into f-144-rel6a74320ca

main
3.0 KiB66 linesraw
1// metal/tests/gridbar_bench.cpp - phases of a streaming read separated by dispatch boundaries vs by an in-kernel
2// grid barrier (gridbar.metal), and whether a phase sees the previous phase's writes of the other threadgroups.
3//   gridbar_bench [MiB per phase] [phases]
4#include "mtl_rt.h"
5
6#include <algorithm>
7#include <cstdio>
8#include <cstdlib>
9#include <vector>
10
11int main(int argc, char** argv) {
12    const double mib = argc > 1 ? std::atof(argv[1]) : 4.5;
13    const int phases = argc > 2 ? std::atoi(argv[2]) : 96;
14    mtl::Context cx;
15    const int lib = cx.compile({"gridbar.metal"}, std::getenv("GB_FENCE") ? "#define NO_GGML_COMMON 1\n#define GB_FENCE 1\n"
16                                                                        : "#define NO_GGML_COMMON 1\n");
17    mtl::Pipeline pp = cx.pipeline(lib, "k_phases"), p1 = cx.pipeline(lib, "k_phase1");
18    const uint32_t n = (uint32_t)(mib * 1048576 / 16);
19    mtl::Buffer src = cx.alloc((size_t)n * 16 * phases), out = cx.alloc(4u << 20), bar = cx.alloc(4),
20                flag = cx.alloc(4 * 64 * (size_t)phases), bad = cx.alloc(4);
21    for (size_t i = 0; i < src.bytes / 4; i += 1024) src.as<uint32_t>()[i] = (uint32_t)i;
22    std::printf("device %s: %d phases of %.1f MiB\n", cx.device_name().c_str(), phases, mib);
23    // dispatch boundaries
24    for (uint32_t groups : {40u, 160u, 640u}) {
25        std::vector<mtl::Dispatch> ds;
26        for (int p = 0; p < phases; ++p) {
27            mtl::Dispatch d;
28            d.p = &p1;
29            const uint32_t pu = p;
30            d.args = {mtl::Arg::B(src), mtl::Arg::B(out), mtl::Arg::V(n), mtl::Arg::V(pu)};
31            d.grid[0] = groups;
32            d.tg[0] = 1024;
33            ds.push_back(d);
34        }
35        cx.warm(ds, 0.2);
36        std::vector<double> t;
37        for (int i = 0; i < 5; ++i) t.push_back(cx.run(ds));
38        std::sort(t.begin(), t.end());
39        std::printf("dispatch per phase, %4u x 1024 threads: %7.2f us a phase  %6.1f GB/s\n", groups,
40                    t[2] / phases * 1e6, n * 16.0 * phases / t[2] / 1e9);
41    }
42    // grid barrier, at most one threadgroup a core
43    for (uint32_t groups : {10u, 20u, 40u}) {
44        mtl::Dispatch d;
45        d.p = &pp;
46        const uint32_t ph = phases;
47        d.args = {mtl::Arg::B(src), mtl::Arg::B(out), mtl::Arg::B(bar), mtl::Arg::B(flag), mtl::Arg::B(bad),
48                  mtl::Arg::V(n), mtl::Arg::V(ph)};
49        d.grid[0] = groups;
50        d.tg[0] = 1024;
51        std::vector<double> t;
52        uint32_t badc = 0;
53        for (int i = 0; i < 6; ++i) {
54            bar.as<uint32_t>()[0] = 0;
55            bad.as<uint32_t>()[0] = 0;
56            for (size_t k = 0; k < flag.bytes / 4; ++k) flag.as<uint32_t>()[k] = 0xFFFFFFFFu;
57            const double s = cx.run({d});
58            if (i) t.push_back(s);
59            badc += bad.as<uint32_t>()[0];
60        }
61        std::sort(t.begin(), t.end());
62        std::printf("grid barrier,       %4u x 1024 threads: %7.2f us a phase  %6.1f GB/s  stale flags seen: %u\n",
63                    groups, t[2] / phases * 1e6, n * 16.0 * phases / t[2] / 1e9, badc);
64    }
65    return 0;
66}