okami.mom/strata

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

Niko1221Merge f-022-int into f-022-gyrocuda: Pascal path kept for cc 6.0, 8-lane trellis rows for every other CUDA card01f8c3255

main
17.4 KiB279 linesraw
1// pool_plan_test: the default CPU expert worker count on mock CPUs (include/strata/kernels/cpu/pool_plan.hpp).
2// Pure arithmetic, no threads and no OS calls: it runs on any machine.
3#include "strata/kernels/cpu/pool_plan.hpp"
4
5#include <cstdio>
6#include <string>
7
8namespace cpu = strata::kernels::cpu;
9
10static int g_fail = 0;
11
12static void expect(bool ok, const char* what) {
13    if (!ok) {
14        ++g_fail;
15        std::fprintf(stderr, "FAIL: %s\n", what);
16    }
17}
18
19static cpu::PoolPlanInput cpu_of(int logical, std::vector<int> per_package, double quota = 0) {
20    cpu::PoolPlanInput in;
21    in.logical = logical;
22    in.cores_per_package = std::move(per_package);
23    in.cpu_quota = quota;
24    return in;
25}
26
27static void line(const char* name, const cpu::PoolPlanInput& in) {
28    const cpu::PoolPlan p = cpu::plan_pool_workers(in);
29    std::printf("  %-34s logical %3d  cores %3d (host socket %3d, %d socket%s)  -> %2d workers  [%s]\n", name, in.logical,
30                p.cores, p.cores_local, p.packages, p.packages == 1 ? "" : "s", p.workers, p.limit);
31}
32
33int main() {
34    std::printf("pool_plan_test: cap %d, reserve %d from %d workers\n", cpu::kPoolWorkersCap, cpu::kPoolReserve,
35                cpu::kPoolReserveFrom);
36
37    // 8 cores / 16 threads: every physical core but the host's, never the SMT siblings
38    const auto c8 = cpu_of(16, {8});
39    line("8c/16t", c8);
40    expect(cpu::plan_pool_workers(c8).workers == 7, "8c/16t -> 7 workers");
41
42    // 6 cores / 12 threads (the small test PCs) and a CPU without SMT
43    expect(cpu::plan_pool_workers(cpu_of(12, {6})).workers == 5, "6c/12t -> 5 workers");
44    expect(cpu::plan_pool_workers(cpu_of(4, {4})).workers == 3, "4c/4t (no SMT) -> 3 workers");
45    expect(cpu::plan_pool_workers(cpu_of(1, {1})).workers == 1, "1 core -> 1 worker (never 0)");
46    expect(cpu::plan_pool_workers(cpu_of(2, {2})).workers == 1, "2 cores -> 1 worker");
47
48    // 16 cores / 32 threads
49    const auto c16 = cpu_of(32, {16});
50    line("16c/32t", c16);
51    {
52        const int w = cpu::plan_pool_workers(c16).workers;
53        expect(w == 15 - (15 >= cpu::kPoolReserveFrom ? cpu::kPoolReserve : 0) || w == cpu::kPoolWorkersCap,
54               "16c/32t -> physical cores minus the host (and the reserve)");
55        expect(w <= 15, "16c/32t never above the physical cores minus the host");
56    }
57
58    // 64 cores / 128 threads: the cap, far below the logical count
59    const auto c64 = cpu_of(128, {64});
60    line("64c/128t", c64);
61    expect(cpu::plan_pool_workers(c64).workers == cpu::kPoolWorkersCap, "64c/128t -> the cap");
62    expect(cpu::plan_pool_workers(c64).workers < 64, "64c/128t is not near the logical count");
63
64    // 2 sockets x 64 cores / 256 threads: the user report - never 255, one socket's cores at most, under the cap
65    const auto c2s = cpu_of(256, {64, 64});
66    line("2-socket 128c/256t", c2s);
67    expect(cpu::plan_pool_workers(c2s).workers == cpu::kPoolWorkersCap, "2-socket 128c/256t -> the cap");
68    expect(cpu::plan_pool_workers(c2s).packages == 2, "2-socket: two packages");
69    expect(cpu::plan_pool_workers(c2s).cores == 128, "2-socket: 128 physical cores counted");
70
71    // 2 sockets x 20 cores (the Xeons of bench #674 #707: 17-18 workers beat 35): the host's socket only
72    const auto xeon = cpu_of(80, {20, 20});
73    line("2-socket 40c/80t (Xeon)", xeon);
74    expect(cpu::plan_pool_workers(xeon).workers == 18, "2x20c: 18 workers (bench #674 #707: 17-18 beat 35): the host socket only, minus the host and the reserve");
75    // ... and a smaller second socket does not matter; the host's socket comes first in the input
76    expect(cpu::plan_pool_workers(cpu_of(48, {6, 18})).workers == 5, "host socket of 6 cores: 5 workers");
77
78    // an unknown core layout (no sysfs, a container): half the logical processors for a many-thread CPU, never all
79    const auto unk = cpu_of(256, {});
80    line("256 logical, layout unknown", unk);
81    expect(!cpu::plan_pool_workers(unk).topology_known, "unknown layout is flagged");
82    expect(cpu::plan_pool_workers(unk).workers <= cpu::kPoolWorkersCap, "unknown 256 -> at most the cap");
83    expect(cpu::plan_pool_workers(unk).workers < 255, "unknown 256 -> never 255");
84    expect(cpu::plan_pool_workers(cpu_of(8, {})).workers == 7, "unknown, 8 logical -> 7 workers");
85
86    // hybrid CPUs: with more E-cores than P-cores, the P-cores but the host's plus half of the E-cores
87    {
88        auto h = cpu_of(20, {20});
89        h.p_cores = 8;
90        h.e_cores = 12;
91        line("hybrid 8P+12E (Core Ultra 7 265)", h);
92        expect(cpu::plan_pool_workers(h).workers == 13, "8P+12E -> 13 workers (measured: 12-16 beat the 19 of one per core)");
93        auto h2 = cpu_of(32, {24});
94        h2.p_cores = 8;
95        h2.e_cores = 16;
96        expect(cpu::plan_pool_workers(h2).workers == 15, "8P+16E -> 15 workers (#642)");
97        auto h3 = cpu_of(24, {16});
98        h3.p_cores = 8;
99        h3.e_cores = 8;
100        expect(cpu::plan_pool_workers(h3).workers == 15, "8P+8E: not more E-cores than P-cores, every core but the host's");
101    }
102
103    // #3262: Core Ultra 7 255H (6P + 8E + 2 LP-E, no SMT).  The LP-E cores (CPUs 14-15) are in cpu_lowpower/cpus, in
104    // neither the core nor the atom list; they are efficiency cores.
105    {
106        const std::vector<int> core = {0, 1, 2, 3, 4, 5}, atom = {6, 7, 8, 9, 10, 11, 12, 13}, lp = {14, 15};
107        int e = 0, p = 0;
108        for (int c = 0; c < 16; ++c) {
109            const long cap = c < 6 ? 1024 : c < 14 ? 774 : 312;
110            (cpu::hybrid_cpu_is_efficiency(c, cap, 1024, true, core, atom, lp) ? e : p)++;
111        }
112        expect(p == 6 && e == 10, "255H: 6 P-cores, 8 E + 2 LP-E counted as efficiency cores");
113        expect(cpu::hybrid_cpu_is_efficiency(14, 312, 1024, true, core, atom, lp), "LP-E core 14 is an E-core");
114        expect(!cpu::hybrid_cpu_is_efficiency(5, 1024, 1024, true, core, atom, lp), "P-core 5 stays a P-core");
115        // a CPU in the lists of no kind falls back to its capacity (312 < 90% of 1024: an E-core)
116        expect(cpu::hybrid_cpu_is_efficiency(14, 312, 1024, true, core, atom, {}), "LP-E core with no third list: capacity says E");
117        // a CPU in no list with a favoured-P capacity is a P-core
118        expect(!cpu::hybrid_cpu_is_efficiency(20, 1012, 1024, true, core, atom, lp), "unlisted CPU, capacity 1012/1024: P");
119        // the plan with the right counts: 6P + 10E -> 5 + 5 workers, not the 15 of "every core but the host's"
120        auto h = cpu_of(16, {16});
121        h.p_cores = 6;
122        h.e_cores = 10;
123        expect(cpu::plan_pool_workers(h).workers == 10, "255H -> 10 workers (the measured 9-13 are flat; 15 is -35%)");
124    }
125
126    // a container quota
127    const auto q = cpu_of(128, {64}, 4.0);
128    line("64c, container quota 4 CPUs", q);
129    expect(cpu::plan_pool_workers(q).workers == 3, "quota 4 CPUs -> 3 workers");
130    expect(cpu::plan_pool_workers(cpu_of(128, {64}, 200.0)).workers == cpu::kPoolWorkersCap, "a big quota changes nothing");
131
132    // warnings: only above the plan, and each says what to do
133    const cpu::PoolPlan p2s = cpu::plan_pool_workers(c2s);
134    expect(cpu::pool_workers_warning(0, c2s, p2s).empty(), "0 (the default) never warns");
135    expect(cpu::pool_workers_warning(p2s.workers, c2s, p2s).empty(), "the plan itself never warns");
136    expect(cpu::pool_workers_warning(4, c2s, p2s).empty(), "fewer than the plan never warns");
137    {
138        const std::string w = cpu::pool_workers_warning(256, c2s, p2s);
139        expect(!w.empty(), "256 workers on 256 threads warns");
140        expect(w.find("--pool-workers " + std::to_string(p2s.workers)) != std::string::npos, "the warning names the recommendation");
141        expect(w.find("still runs") != std::string::npos, "the warning says it still runs");
142    }
143    expect(cpu::pool_workers_warning(200, c2s, p2s).find("physical cores") != std::string::npos,
144           "200 workers on 128 cores: more than the physical cores");
145    expect(cpu::pool_workers_warning(300, c2s, p2s).find("256 CPU threads") != std::string::npos,
146           "300 workers on 256 threads: more than the CPU threads");
147    expect(cpu::pool_workers_warning(30, xeon, cpu::plan_pool_workers(xeon)).find("socket") != std::string::npos,
148           "30 workers on 2x20c: crosses the socket");
149    expect(!cpu::pool_workers_warning(40, c64, cpu::plan_pool_workers(c64)).empty(), "40 workers on 64c is above the cap: warns");
150    expect(cpu::pool_workers_warning(8, q, cpu::plan_pool_workers(q)).find("container") != std::string::npos,
151           "8 workers under a 4-CPU quota: names the container limit");
152
153    // STRATA_NUMA_SPLIT (span_packages): the default spans every socket, the other bounds stay; off = the plan above
154    {
155        auto x = xeon;   // 2 x 20 cores: 39 but the host's, one kept free -> 38, then the cap
156        x.span_packages = true;
157        line("2-socket 40c/80t, NUMA split", x);
158        const cpu::PoolPlan px = cpu::plan_pool_workers(x);
159        expect(px.workers == cpu::kPoolWorkersCap, "2x20c split: both sockets, then the cap");
160        expect(px.cores_local == 40, "2x20c split: every socket counts as local");
161        auto b = cpu_of(48, {12, 12});   // the 2 x Xeon Gold 6226 box: 23 but the host's, one kept free
162        expect(cpu::plan_pool_workers(b).workers == 11, "2x12c no split: the host socket's 11 (unchanged)");
163        b.span_packages = true;
164        expect(cpu::plan_pool_workers(b).workers == 22, "2x12c split: 22 workers across both sockets");
165        auto one = cpu_of(16, {8});
166        one.span_packages = true;
167        expect(cpu::plan_pool_workers(one).workers == 7, "one socket: the split changes nothing");
168        // the socket warning is silenced while the split is on; the others stay
169        expect(cpu::pool_workers_warning(30, x, px).empty(), "30 workers on 2x20c under the split: no socket warning");
170        auto bx = cpu_of(80, {20, 20});
171        bx.span_packages = true;
172        const std::string w = cpu::pool_workers_warning(36, bx, cpu::plan_pool_workers(bx));
173        expect(!w.empty() && w.find("socket") == std::string::npos, "36 on 2x20c under the split: the cap warning, not the socket one");
174        expect(cpu::pool_workers_warning(60, bx, cpu::plan_pool_workers(bx)).find("physical cores") != std::string::npos,
175               "60 on 2x20c under the split: still more than the physical cores");
176    }
177    // interleave_packages: round-robin over the packages (host's first), stable inside each
178    {
179        const std::vector<size_t> o = cpu::interleave_packages({0, 0, 0, 1, 1, 1, 1});
180        const std::vector<size_t> want = {0, 3, 1, 4, 2, 5, 6};
181        expect(o == want, "interleave 3+4: 0 3 1 4 2 5 6");
182        expect(cpu::interleave_packages({}).empty(), "interleave of nothing");
183        const std::vector<size_t> same = cpu::interleave_packages({1, 1, 1});
184        expect(same == std::vector<size_t>({0, 1, 2}), "one package: the order is kept");
185    }
186    // a configured count under the split, on the 2 x 12-core box: the worker cores as fill_plan_fields orders them
187    // (the host's socket first: its 11 cores but the host's, then the other socket's 12), taken in interleave order;
188    // the host thread drains on socket 0
189    {
190        std::vector<int> pkg(11, 0);
191        pkg.insert(pkg.end(), 12, 1);
192        const std::vector<size_t> o = cpu::interleave_packages(pkg);
193        auto per = [&](int n, bool interleaved, int s) {
194            int c = 0;
195            for (int i = 0; i < n; ++i) c += pkg[interleaved ? o[(size_t) i] : (size_t) i] == s;
196            return c;
197        };
198        expect(per(12, true, 0) == 6 && per(12, true, 1) == 6, "--pool-workers 12 under the split: 6 + 6 workers");
199        expect(cpu::numa_slices_balanced(per(12, true, 0) + 1, per(12, true, 1)), "12: 7 / 6 threads, balanced");
200        expect(per(13, true, 0) == 7 && per(13, true, 1) == 6, "--pool-workers 13 under the split: 7 + 6 workers");
201        expect(cpu::numa_slices_balanced(per(13, true, 0) + 1, per(13, true, 1)), "13: 8 / 6 threads, balanced");
202        expect(!cpu::numa_slices_balanced(per(12, false, 0) + 1, per(12, false, 1)),
203               "12 in the host-first order would be 12 / 1 threads: refused (what the interleave avoids)");
204        expect(per(22, true, 0) == 11 && per(22, true, 1) == 11, "22 (the split default here): 11 + 11 workers");
205    }
206
207    // ---- --pool-cores and the start-up line (pure helpers) ----
208    {
209        std::vector<int> v;
210        std::string err;
211        expect(cpu::parse_pool_cores("0,2,4,6", v, err) && v == std::vector<int>({0, 2, 4, 6}), "list: 0,2,4,6");
212        expect(cpu::parse_pool_cores("2-14:2", v, err) && v == std::vector<int>({2, 4, 6, 8, 10, 12, 14}), "list: 2-14:2");
213        expect(cpu::parse_pool_cores("4-6", v, err) && v == std::vector<int>({4, 5, 6}), "list: 4-6");
214        expect(cpu::parse_pool_cores(" 6 , 2 ,2, 0-1 ", v, err) && v == std::vector<int>({6, 2, 0, 1}),
215               "list: spaces ok, the person's order kept, a repeated number once");
216        expect(!cpu::parse_pool_cores("", v, err) && v.empty() && !err.empty(), "list: empty is an error with a message");
217        expect(!cpu::parse_pool_cores("0,,2", v, err) && v.empty(), "list: an empty item");
218        expect(!cpu::parse_pool_cores("0,a", v, err), "list: letters");
219        expect(!cpu::parse_pool_cores("6-2", v, err), "list: a range that goes down");
220        expect(!cpu::parse_pool_cores("0-8:0", v, err), "list: a step of 0");
221        expect(!cpu::parse_pool_cores("-3", v, err), "list: a negative number");
222        expect(!cpu::parse_pool_cores("0-99999", v, err) && v.empty(), "list: a silly range is refused with a message, not expanded");
223        expect(cpu::parse_pool_cores("0-4095", v, err) && v.size() == 4096, "list: 4096 entries are fine");
224        expect(cpu::format_cpu_list({2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30}) == "2,4,...,30", "format: a stride");
225        expect(cpu::format_cpu_list({0, 1, 2, 3, 4}) == "0-4", "format: a run");
226        expect(cpu::format_cpu_list({2, 4, 7}) == "2,4,7", "format: short and irregular lists in full");
227        expect(cpu::format_cpu_list({1, 2, 4, 5, 9}) == "1,2,4,5,9", "format: irregular");
228        expect(cpu::format_cpu_list({}) == "none", "format: none");
229
230        // Windows-style numbering of 16 cores / 32 threads: core k = logical 2k and 2k+1
231        std::vector<cpu::LogicalCpuInfo> win;
232        for (int i = 0; i < 32; ++i) win.push_back({i, i / 2});
233        std::vector<int> w;
234        for (int c = 1; c < 16; ++c) w.push_back(2 * c);
235        const std::string line = cpu::describe_worker_placement(w, 0, win, false);
236        std::printf("  %s\n", line.c_str());
237        expect(line == "CPU workers on logical processors 2,4,...,30 (15 workers, one per physical core; host on 0)",
238               "the default line on 16c/32t");
239        // the mistake the person worried about: workers on logical 0..7 of an adjacent-numbering SMT PC
240        const std::string bad = cpu::describe_worker_placement({1, 2, 3, 4, 5, 6, 7}, 0, win, false);
241        std::printf("  %s\n", bad.c_str());
242        expect(bad.find("one per physical core") == std::string::npos && bad.find("carry more than one worker") != std::string::npos,
243               "workers on logical 1..7 of 16c/32t are reported as sharing cores");
244        expect(bad.find("shares the host's core") != std::string::npos, "... and the worker on the host's core is named");
245        const std::string unk = cpu::describe_worker_placement({1, 2, 3}, 0, {}, false);
246        expect(unk.find("did not say") != std::string::npos && unk.find("one per physical core") == std::string::npos,
247               "unknown layout: no claim");
248        expect(cpu::describe_worker_placement({2, 4}, -1, win, true).find("your --pool-cores list") != std::string::npos &&
249               cpu::describe_worker_placement({2, 4}, -1, win, true).find("host not pinned") != std::string::npos,
250               "a given list and an unpinned host are said");
251
252        // check_pool_cores: drop what cannot be used, warn about siblings and the host's processor, never refuse
253        cpu::PoolCoresCheck c = cpu::check_pool_cores({2, 4, 6, 8}, win, 0);
254        expect(c.cpus == std::vector<int>({2, 4, 6, 8}) && c.warnings.empty(), "check: one per core, no warning");
255        c = cpu::check_pool_cores({2, 3, 6}, win, 0);
256        expect(c.cpus == std::vector<int>({2, 3, 6}) && c.warnings.size() == 1 && c.warnings[0].find("two hardware threads") != std::string::npos,
257               "check: SMT siblings are kept with one warning");
258        c = cpu::check_pool_cores({0, 2}, win, 0);
259        expect(c.cpus == std::vector<int>({0, 2}) && c.warnings.size() == 1 && c.warnings[0].find("host thread's") != std::string::npos,
260               "check: the host's processor is kept with a warning");
261        c = cpu::check_pool_cores({1, 2}, win, 0);
262        expect(c.cpus == std::vector<int>({1, 2}) && c.warnings.size() == 1 && c.warnings[0].find("shares a core with the host") != std::string::npos,
263               "check: the host's SMT sibling is kept with a warning");
264        c = cpu::check_pool_cores({2, 40, 99}, win, 0);
265        expect(c.cpus == std::vector<int>({2}) && c.warnings.size() == 1 && c.warnings[0].find("40,99") != std::string::npos,
266               "check: processors this process may not use are left out and named");
267        c = cpu::check_pool_cores({40}, win, 0);
268        expect(c.cpus.empty() && c.warnings.size() == 1, "check: nothing usable");
269        c = cpu::check_pool_cores({2, 3}, {{2, -1}, {3, -1}}, 0);
270        expect(c.cpus.size() == 2 && c.warnings.empty(), "check: an unknown layout cannot tell siblings: no warning");
271    }
272
273    if (g_fail) {
274        std::fprintf(stderr, "pool_plan_test: %d failure(s)\n", g_fail);
275        return 1;
276    }
277    std::printf("pool_plan_test: OK\n");
278    return 0;
279}