%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 <file.r8asm>                N workers
    r8forge --synth-threads=N <file.r8asm>   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.
This commit is contained in:
dsh committed 2026-11-07 23:56:37 +08:00
1 parent b4ea79ba91
commit b666799950
7 files changed
+92 -19

No files matched your search

+5
View File
@@ -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 会把一些可行的表误判成不可能)。
+9
View File
@@ -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
```
层数搜索是串行的,这个参数对它没用(它本来就只有秒级)。
## 从零开始
+5
View File
@@ -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/` —— 总则
+14 -2
View File
@@ -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<std::array<int, 3>>
r8asm_synth_program(int params, const std::vector<int> &in,
const std::vector<int> &out, const std::vector<int> &map,
const std::vector<int> &stat, const std::string &name,
const std::vector<int> &layout, int mode, int metric);
const std::vector<int> &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; }
};
+2 -1
View File
@@ -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 {
+21 -13
View File
@@ -17,7 +17,7 @@ std::vector<std::array<int, 3>>
r8asm_synth_run(const std::vector<int> &in, const std::vector<int> &out,
const std::vector<int> &map, const std::vector<int> &stat,
const std::string &name, const std::vector<int> &layout,
int mode, int metric) {
int mode, int metric, unsigned threads) {
using V = r8veritas<P>;
V v;
v.initialize_nicknames();
@@ -169,9 +169,15 @@ r8asm_synth_run(const std::vector<int> &in, const std::vector<int> &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<P>(tt, addr, bound, threads,
const auto tape = r8par::search_tt<P>(tt, addr, bound, workers,
copt.max_states, metric != 1);
const double tape_seconds =
std::chrono::duration<double>(std::chrono::steady_clock::now() - t0)
@@ -186,7 +192,8 @@ r8asm_synth_run(const std::vector<int> &in, const std::vector<int> &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<std::array<int, 3>>
r8asm_synth_program(int params, const std::vector<int> &in,
const std::vector<int> &out, const std::vector<int> &map,
const std::vector<int> &stat, const std::string &name,
const std::vector<int> &layout, int mode, int metric) {
const std::vector<int> &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<int> &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);
}
}
+36 -3
View File
@@ -1,6 +1,6 @@
// The assembler CLI.
//
// r8forge [--synth-chars] <file.r8asm>
// r8forge [--synth-chars] [-j N] <file.r8asm>
//
// %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 <cerrno>
#include <cstdlib>
#include <iostream>
#include <string>
namespace {
void usage() {
std::cerr << "usage: r8forge [--synth-chars] <file.r8asm>\n";
std::cerr << "usage: r8forge [--synth-chars] [-j N] <file.r8asm>\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();