From b666799950a88756432060edd6364842fb759ad5 Mon Sep 17 00:00:00 2001 From: dsh Date: Sat, 7 Nov 2026 23:56:37 +0800 Subject: [PATCH] %synth: make the tape search thread count a run option (-j N) The tape search always used std::thread::hardware_concurrency() workers with no way to ask for fewer (or more). It is now a run option like the cost model: r8forge -j N N workers r8forge --synth-threads=N same thing 0 (the default) means one worker per hardware thread, as before. r8asm::set_synth_threads(unsigned) is the C++ side; the CLI parses -j, --synth-threads and --synth-threads=. The layer search stays serial (it only takes seconds and has no split), so the knob only affects the expensive tape search, which splits by first block (see r8veritas_phase.hh). The search line on stderr now ends with ", N threads", which makes the setting verifiable without timing: with a freshly written table (params 4, or4) and the cache entry removed between runs, -j 1 -> 230 states, 0.0009s, 1 threads -j 4 -> ..., 4 threads --synth-threads=2 -> ..., 2 threads (nothing) / -j 0 -> ..., 32 threads (hardware_concurrency here) Documented in SPEC 3.7, README's cost model section and the AGENTS %synth section. asm/test.r8asm is unchanged at 28729 characters; byte_op_test, examples_test (all 13), macro_docs, synth_test (95 chars, 8/8) and r8forge_derive all still green. --- AGENTS.md | 5 +++++ README.md | 9 +++++++++ SPEC.md | 5 +++++ include/r8forge/r8asm.hh | 16 ++++++++++++++-- src/r8asm.cc | 3 ++- src/synth.cc | 34 +++++++++++++++++++++------------- tests/asm_test.cc | 39 ++++++++++++++++++++++++++++++++++++--- 7 files changed, 92 insertions(+), 19 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 6fcbe16..88f3eac 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -456,6 +456,11 @@ W 轮之后 `(PH:PL)` 就是积;`PL`(原来的 Q)被移空,所以 **MUL * 缓存:`.r8forge_cache/`(**已入库**,克隆下来直接命中)。键 = 模型版本 + params + mode + metric + **解析后的整张目标表**(+ layout,仅 chars)。改指令模型时要 bump `kModelVersion`(`src/synth.cc`)。 +* **纸带搜索的线程数**:`-j N` / `--synth-threads N` / `--synth-threads=N`(`0` = 每个硬件 + 线程一个,是默认);C++ 侧是 `r8asm::set_synth_threads(unsigned)`。搜索按"第一个块"切 + (`r8veritas_phase.hh` 的 `search_tt`),所以多 worker 安全;**层数搜索是串行的**。 + 搜完在 stderr 那行末尾会打 `, N threads`,可以直接确认参数生效(实测 `-j 1` → 1、 + `-j 4` → 4、`-j 0` → 32(本机硬件线程数))。 * 缓存代价参考:`bit_raw_add` 的 flat 搜索 990,636,137 状态 / 680 秒 / ~22 GB 内存。 层数上限现在是 8(原来是 5,5 会把一些可行的表误判成不可能)。 diff --git a/README.md b/README.md index 4421c14..82dbbb3 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,15 @@ all eight combinations ok 早先那个 `metric blocks`(只按块数)**删掉了**:块数不是纸带长度的好代理 —— 层数最少不等 于纸带最短,块数最少也不是。 +纸带搜索默认**每个硬件线程开一个 worker**,`-j N`(`--synth-threads N`)可以手动限流, +`-j 0` 就是回到自动: + +```sh +r8forge -j 4 examples/full_add.r8asm # 纸带搜索最多 4 个 worker +``` + +层数搜索是串行的,这个参数对它没用(它本来就只有秒级)。 + ## 从零开始 diff --git a/SPEC.md b/SPEC.md index 3f07225..e876c3a 100644 --- a/SPEC.md +++ b/SPEC.md @@ -201,6 +201,11 @@ R8Forge 唯一的语言扩展:在汇编期现场搜出一段最短的块序列 调用;CLI 在 `tests/asm_test.cc`(`r8forge`)里解析 `--synth-chars` / `--synth-flat`。 其它入口(`r8forge_derive`、几个测试)不传就一律 flat。 +**线程数**也是同一条路子:`-j N`(`--synth-threads N` / `--synth-threads=N`)限制纸带搜索的 +worker 数,`0`(默认)是每个硬件线程一个。纸带搜索按"第一个块"切分(见 +`r8veritas_phase.hh` 的 `search_tt`),所以多 worker 是安全的;**层数搜索是串行的**,这个 +参数对它没用(它本来就只有秒级)。`r8asm::set_synth_threads(unsigned)` 是 C++ 侧的开关。 + --- ## 4. 标准库 `asm/` —— 总则 diff --git a/include/r8forge/r8asm.hh b/include/r8forge/r8asm.hh index 67fac58..81d4b06 100644 --- a/include/r8forge/r8asm.hh +++ b/include/r8forge/r8asm.hh @@ -19,12 +19,14 @@ // (the whole 2^params state table) says what to compute. `layout` is the cell // address of each parameter (empty means adjacent, 0..n-1). `metric` picks the // tape cost model: 1 = layout free (the .tp walk is not counted, one cache -// entry serves every caller), 0 = real characters for this layout. +// entry serves every caller), 0 = real characters for this layout. `threads` +// is how many workers the tape search may use; 0 = one per hardware thread. std::vector> r8asm_synth_program(int params, const std::vector &in, const std::vector &out, const std::vector &map, const std::vector &stat, const std::string &name, - const std::vector &layout, int mode, int metric); + const std::vector &layout, int mode, int metric, + unsigned threads); class r8asm { using data = int16_t; @@ -117,6 +119,10 @@ private: // every call site; `r8forge --synth-chars` flips it for the whole run. int synth_metric = 1; + // Workers the tape search may use: 0 = one per hardware thread. Only the + // tape search splits (it splits by first block); the layer search is serial. + unsigned synth_threads = 0; + // The assembler has no exceptions: anything that cannot be assembled // sets this flag and says why on stderr. bool error = false; @@ -243,6 +249,12 @@ public: // tape characters for the layout written in the source. Set it before // import_file; `r8forge --synth-chars` is the command line spelling. void set_synth_metric(int metric) { main_tree.synth_metric = metric; } + // Workers the %synth tape search may use; 0 (default) = one per hardware + // thread. `r8forge -j N` is the command line spelling. The layer search is + // serial either way. + void set_synth_threads(unsigned threads) { + main_tree.synth_threads = threads; + } // True once anything (a missing file, an unknown name, ...) has failed. bool failed() const { return main_tree.error; } }; diff --git a/src/r8asm.cc b/src/r8asm.cc index 429ce9d..1a653f2 100644 --- a/src/r8asm.cc +++ b/src/r8asm.cc @@ -586,7 +586,8 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) { } auto prog = r8asm_synth_program(spec.params, spec.in, spec.out, spec.map, spec.stat, name, spec.layout, - spec.mode, main_tree.synth_metric); + spec.mode, main_tree.synth_metric, + main_tree.synth_threads); if (prog.empty()) { main_tree.fail("%synth '" + name + "': nothing emitted"); } else { diff --git a/src/synth.cc b/src/synth.cc index 9e56366..8e780bc 100644 --- a/src/synth.cc +++ b/src/synth.cc @@ -17,7 +17,7 @@ std::vector> r8asm_synth_run(const std::vector &in, const std::vector &out, const std::vector &map, const std::vector &stat, const std::string &name, const std::vector &layout, - int mode, int metric) { + int mode, int metric, unsigned threads) { using V = r8veritas

; V v; v.initialize_nicknames(); @@ -169,9 +169,15 @@ r8asm_synth_run(const std::vector &in, const std::vector &out, for (int i = 0; i < P; i++) addr[size_t(i)] = copt.addr[size_t(i)]; - const unsigned threads = std::thread::hardware_concurrency(); + // The tape search splits by first block (see r8veritas_phase.hh), so it can + // use several workers; hardware_concurrency() is only a guess and can return + // 0, so a run that asks for 0 workers gets one per hardware thread and at + // least one either way. + unsigned workers = threads ? threads : std::thread::hardware_concurrency(); + if (workers == 0) + workers = 1; const auto t0 = std::chrono::steady_clock::now(); - const auto tape = r8par::search_tt

(tt, addr, bound, threads, + const auto tape = r8par::search_tt

(tt, addr, bound, workers, copt.max_states, metric != 1); const double tape_seconds = std::chrono::duration(std::chrono::steady_clock::now() - t0) @@ -186,7 +192,8 @@ r8asm_synth_run(const std::vector &in, const std::vector &out, } std::cerr << "r8asm: %synth " << name << ": tape cost " << tape.cost << " in " << tape.prog.size() << " blocks (layer answer was " << bound << "), " - << tape.states << " states, " << tape_seconds << "s\n"; + << tape.states << " states, " << tape_seconds << "s, " << workers + << " threads\n"; for (const auto &i : tape.prog) out_ins.push_back({i[0], i[1], i[2]}); write_cache(out_ins); @@ -197,7 +204,8 @@ std::vector> r8asm_synth_program(int params, const std::vector &in, const std::vector &out, const std::vector &map, const std::vector &stat, const std::string &name, - const std::vector &layout, int mode, int metric) { + const std::vector &layout, int mode, int metric, + unsigned threads) { if (params < 1 || params > 8) { std::cerr << "r8asm: %synth " << name << ": supports 1..8 parameters\n"; return {}; @@ -217,21 +225,21 @@ r8asm_synth_program(int params, const std::vector &in, } switch (params) { case 1: - return r8asm_synth_run<1>(in, out, map, stat, name, layout, mode, metric); + return r8asm_synth_run<1>(in, out, map, stat, name, layout, mode, metric, threads); case 2: - return r8asm_synth_run<2>(in, out, map, stat, name, layout, mode, metric); + return r8asm_synth_run<2>(in, out, map, stat, name, layout, mode, metric, threads); case 3: - return r8asm_synth_run<3>(in, out, map, stat, name, layout, mode, metric); + return r8asm_synth_run<3>(in, out, map, stat, name, layout, mode, metric, threads); case 4: - return r8asm_synth_run<4>(in, out, map, stat, name, layout, mode, metric); + return r8asm_synth_run<4>(in, out, map, stat, name, layout, mode, metric, threads); case 5: - return r8asm_synth_run<5>(in, out, map, stat, name, layout, mode, metric); + return r8asm_synth_run<5>(in, out, map, stat, name, layout, mode, metric, threads); case 6: - return r8asm_synth_run<6>(in, out, map, stat, name, layout, mode, metric); + return r8asm_synth_run<6>(in, out, map, stat, name, layout, mode, metric, threads); case 7: - return r8asm_synth_run<7>(in, out, map, stat, name, layout, mode, metric); + return r8asm_synth_run<7>(in, out, map, stat, name, layout, mode, metric, threads); default: - return r8asm_synth_run<8>(in, out, map, stat, name, layout, mode, metric); + return r8asm_synth_run<8>(in, out, map, stat, name, layout, mode, metric, threads); } } diff --git a/tests/asm_test.cc b/tests/asm_test.cc index 9f79d84..8755f3b 100644 --- a/tests/asm_test.cc +++ b/tests/asm_test.cc @@ -1,6 +1,6 @@ // The assembler CLI. // -// r8forge [--synth-chars] +// r8forge [--synth-chars] [-j N] // // %synth charges a block for a layout free tape by default: the block is // copied to many call sites, so the .tp walk is not part of the search and one @@ -8,21 +8,41 @@ // for the layout written in the source instead (much slower, and the result is // only optimal for that layout). It is a property of the run, not of a block, // which is why there is no `metric` line inside %synth any more. +// +// -j N (or --synth-threads N) caps the workers of the %synth tape search; 0 +// (the default) means one per hardware thread. Only the tape search is +// parallel -- the layer search is serial whatever this says. #include "r8forge/r8asm.hh" +#include +#include #include #include namespace { void usage() { - std::cerr << "usage: r8forge [--synth-chars] \n"; + std::cerr << "usage: r8forge [--synth-chars] [-j N] \n"; +} + +// 0 is allowed (one worker per hardware thread); absurd counts are not. +bool parse_count(const std::string &text, unsigned &out) { + if (text.empty()) + return false; + char *end = nullptr; + errno = 0; + const unsigned long value = std::strtoul(text.c_str(), &end, 10); + if (end == nullptr || *end != '\0' || errno != 0 || value > 4096) + return false; + out = unsigned(value); + return true; } } // namespace int main(int argc, char **argv) { const char *file = nullptr; - int metric = 1; // layout free + int metric = 1; // layout free + unsigned threads = 0; // one worker per hardware thread for (int i = 1; i < argc; i++) { const std::string arg = argv[i]; if (arg == "--synth-chars") { @@ -32,6 +52,18 @@ int main(int argc, char **argv) { } else if (arg == "-h" || arg == "--help") { usage(); return (0); + } else if (arg == "-j" || arg == "--synth-threads") { + if (i + 1 >= argc || !parse_count(argv[++i], threads)) { + std::cerr << "'" << arg << "' wants a thread count\n"; + usage(); + return (2); + } + } else if (arg.rfind("--synth-threads=", 0) == 0) { + if (!parse_count(arg.substr(16), threads)) { + std::cerr << "bad thread count in '" << arg << "'\n"; + usage(); + return (2); + } } else if (arg.size() > 1 && arg[0] == '-') { std::cerr << "unknown option '" << arg << "'\n"; usage(); @@ -49,6 +81,7 @@ int main(int argc, char **argv) { } r8asm a; a.set_synth_metric(metric); + a.set_synth_threads(threads); if (!a.import_file(file, kDefaultCellSize)) return (1); std::string tape = a.export_rot8();