Add a layout independent metric so a result can be cached and reused
%synth could optimise the real tape characters, but that price depends on where the cells sit, so the answer was only good for one layout and the cache could only be shared between call sites with the same addresses. metric chars tape characters including the .tp walk layout dependent metric flat characters without the walk layout independent metric blocks block count layout independent 'flat' is the step-free mode: it counts biz/flb/rnz and the ops, ignores how far apart the cells are, so one search result and one cache entry serve every layout. The price is a slightly longer tape when the layout happens to suit a different program: 90 characters for chars, 95 for flat on the full adder. Checked: examples/full_add_flat.r8asm hits the cache on the second run, and the same block with 'layout 0 2 4 6 8 10' added hits the *same* entry and emits a byte-identical tape (the key drops the layout for the layout-free metrics). Both pass the eight combination check.
This commit is contained in:
1 parent
505fe8af86
commit
da16c111cf
8 files changed
+118
-32
No files matched your search
@@ -58,6 +58,31 @@ all eight combinations ok
|
||||
`map` 的项数决定输入个数:2^K 项就是 `%1..%K` 为输入,值的第 j 位落到 `%(K+j+1)`,剩下
|
||||
的参数是必须结束为 0 的草稿。想要完全显式(或输入不在最前面)就改用 `stat`,那是整张状态表。
|
||||
|
||||
## 三种代价口径
|
||||
|
||||
%synth 里用 metric 选优化目标:
|
||||
|
||||
| metric | 优化什么 | 依赖 cell 布局? | 缓存能否跨调用点复用 |
|
||||
|---|---|---|---|
|
||||
| chars(默认) | 真实纸带字符数,含 .tp 行走 | 是(可用 layout 指定) | 同一个布局才能 |
|
||||
| lat | 字符数,但**不计 .tp 行走** | 否 | **能** |
|
||||
| locks | 块数 | 否 | 能 |
|
||||
|
||||
lat 就是"步长无关模式":它只数 iz/lb/␍nz 和 op 本身,不看 cell 之间隔多远,
|
||||
所以同一张真值表在任何布局下都用同一份结果、同一份缓存。代价是纸带会比按实际布局最优的
|
||||
长一点 —— full_add 上 chars 是 90 字符,lat 是 95 字符。
|
||||
|
||||
`
|
||||
%synth fulladd_flat
|
||||
params 6
|
||||
map 0 1 1 2 1 2 2 3
|
||||
metric flat
|
||||
%endsynth
|
||||
`
|
||||
|
||||
同一个 metric flat 的块配上 layout 0 2 4 6 8 10 再汇编,会直接命中同一份缓存并且
|
||||
发射出**完全相同**的纸带(已验证)。
|
||||
|
||||
## 从零开始
|
||||
|
||||
```sh
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
%include "asm/lib.r8asm"
|
||||
|
||||
; ????:metric flat ?? .tp ?????,??? cell ????,
|
||||
; ??????????????(?????????????????)?
|
||||
%synth fulladd_flat
|
||||
params 6
|
||||
map 0 1 1 2 1 2 2 3
|
||||
metric flat
|
||||
%endsynth
|
||||
|
||||
.var A 0
|
||||
.var B 1
|
||||
.var Ci 2
|
||||
.var S 3
|
||||
.var Co 4
|
||||
.var T 5
|
||||
|
||||
fulladd_flat A B Ci S Co T
|
||||
output S
|
||||
output Co
|
||||
@@ -0,0 +1,22 @@
|
||||
%include "asm/lib.r8asm"
|
||||
|
||||
; ????:metric flat ?? .tp ?????,??? cell ????,
|
||||
; ??????????????(?????????????????)?
|
||||
%synth fulladd_flat
|
||||
params 6
|
||||
map 0 1 1 2 1 2 2 3
|
||||
layout 0 2 4 6 8 10
|
||||
metric flat
|
||||
%endsynth
|
||||
|
||||
.var A 0
|
||||
.var B 1
|
||||
.var Ci 2
|
||||
.var S 3
|
||||
.var Co 4
|
||||
.var T 5
|
||||
|
||||
fulladd_flat A B Ci S Co T
|
||||
output S
|
||||
output Co
|
||||
|
||||
@@ -21,7 +21,7 @@ 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);
|
||||
const std::vector<int> &layout, int mode, int metric);
|
||||
|
||||
class r8asm {
|
||||
using data = int16_t;
|
||||
@@ -127,7 +127,9 @@ private:
|
||||
std::vector<int> map; // 2^K entries: output bits of that input
|
||||
std::vector<int> stat; // or the whole table: 2^params values
|
||||
std::vector<int> layout; // optional: cell address of each parameter
|
||||
int mode = 0; // 0 = shortest tape, 1 = fewest layers only
|
||||
int mode = 0; // 0 = shortest tape, 1 = fewest layers only
|
||||
int metric = 0; // 0 = tape chars (needs the layout), 1 = chars without
|
||||
// the .tp walk (any layout), 2 = blocks
|
||||
};
|
||||
|
||||
private:
|
||||
|
||||
@@ -70,7 +70,7 @@ template <int N, int K> struct engine {
|
||||
// refuses to convert that to an int non-type parameter (MSVC lets it pass)
|
||||
template <std::size_t N>
|
||||
inline int block_cost(const std::array<int, N> &addr, int sacrifice, int mask,
|
||||
bool or_mode) {
|
||||
bool or_mode, bool count_travel) {
|
||||
int lo = addr[size_t(sacrifice)], hi = lo, cnt = 0;
|
||||
for (int b = 0; b < N; b++)
|
||||
if (mask & (1 << b)) {
|
||||
@@ -78,7 +78,10 @@ inline int block_cost(const std::array<int, N> &addr, int sacrifice, int mask,
|
||||
lo = std::min(lo, addr[size_t(b)]);
|
||||
hi = std::max(hi, addr[size_t(b)]);
|
||||
}
|
||||
return 3 + 2 * (hi - lo) + cnt * (or_mode ? 4 : 1);
|
||||
// count_travel = false drops the .tp walk, which is the only part of the
|
||||
// price that depends on where the cells sit: the result then works for any
|
||||
// layout and can be cached once for all call sites
|
||||
return 3 + (count_travel ? 2 * (hi - lo) : 0) + cnt * (or_mode ? 4 : 1);
|
||||
}
|
||||
|
||||
struct result_t {
|
||||
@@ -96,7 +99,7 @@ result_t search(const typename engine<N, K>::key &start,
|
||||
const typename engine<N, K>::key &goal,
|
||||
const std::vector<std::array<int, 3>> &ins,
|
||||
const std::array<int, N> &addr, int limit, unsigned nthreads,
|
||||
size_t max_states) {
|
||||
size_t max_states, bool count_travel = true) {
|
||||
using E = engine<N, K>;
|
||||
using key = typename E::key;
|
||||
result_t r;
|
||||
@@ -112,7 +115,7 @@ result_t search(const typename engine<N, K>::key &start,
|
||||
std::vector<int> w(ni);
|
||||
int wmax = 0;
|
||||
for (size_t i = 0; i < ni; i++) {
|
||||
w[i] = block_cost(addr, ins[i][0], ins[i][1], ins[i][2] != 0);
|
||||
w[i] = block_cost(addr, ins[i][0], ins[i][1], ins[i][2] != 0, count_travel);
|
||||
if (w[i] > wmax)
|
||||
wmax = w[i];
|
||||
}
|
||||
|
||||
@@ -34,7 +34,8 @@ template <int N, int K>
|
||||
result_t search_k(const typename r8veritas<N>::truthtable &tt,
|
||||
const std::vector<int> &bit_of,
|
||||
const std::array<int, N> &addr, int limit,
|
||||
unsigned threads, size_t max_states) {
|
||||
unsigned threads, size_t max_states,
|
||||
bool count_travel = true) {
|
||||
using E = engine<N, K>;
|
||||
using key = typename E::key;
|
||||
const size_t entries = size_t(1) << K;
|
||||
@@ -54,13 +55,13 @@ result_t search_k(const typename r8veritas<N>::truthtable &tt,
|
||||
}
|
||||
|
||||
return search<N, K>(start, goal, classic_blocks<N>(), addr, limit, threads,
|
||||
max_states);
|
||||
max_states, count_travel);
|
||||
}
|
||||
|
||||
template <int N>
|
||||
result_t search_tt(const typename r8veritas<N>::truthtable &tt,
|
||||
const std::array<int, N> &addr, int limit, unsigned threads,
|
||||
size_t max_states) {
|
||||
size_t max_states, bool count_travel = true) {
|
||||
std::vector<int> bit_of;
|
||||
for (int i = 0; i < N; i++)
|
||||
for (size_t j = 0; j < (size_t(1) << N); j++)
|
||||
@@ -70,17 +71,17 @@ result_t search_tt(const typename r8veritas<N>::truthtable &tt,
|
||||
}
|
||||
switch (bit_of.size()) {
|
||||
case 1:
|
||||
return search_k<N, 1>(tt, bit_of, addr, limit, threads, max_states);
|
||||
return search_k<N, 1>(tt, bit_of, addr, limit, threads, max_states, count_travel);
|
||||
case 2:
|
||||
return search_k<N, 2>(tt, bit_of, addr, limit, threads, max_states);
|
||||
return search_k<N, 2>(tt, bit_of, addr, limit, threads, max_states, count_travel);
|
||||
case 3:
|
||||
return search_k<N, 3>(tt, bit_of, addr, limit, threads, max_states);
|
||||
return search_k<N, 3>(tt, bit_of, addr, limit, threads, max_states, count_travel);
|
||||
case 4:
|
||||
return search_k<N, 4>(tt, bit_of, addr, limit, threads, max_states);
|
||||
return search_k<N, 4>(tt, bit_of, addr, limit, threads, max_states, count_travel);
|
||||
case 5:
|
||||
return search_k<N, 5>(tt, bit_of, addr, limit, threads, max_states);
|
||||
return search_k<N, 5>(tt, bit_of, addr, limit, threads, max_states, count_travel);
|
||||
case 6:
|
||||
return search_k<N, 6>(tt, bit_of, addr, limit, threads, max_states);
|
||||
return search_k<N, 6>(tt, bit_of, addr, limit, threads, max_states, count_travel);
|
||||
default: {
|
||||
result_t r;
|
||||
r.message = "the answer depends on " + std::to_string(bit_of.size()) +
|
||||
|
||||
+12
-1
@@ -466,6 +466,17 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) {
|
||||
main_tree.fail("'" + kw +
|
||||
"' is gone: map already says which parameter is "
|
||||
"which, use stat for the raw table");
|
||||
} else if (kw == "metric") {
|
||||
const std::string &m = it->tokens[1].value;
|
||||
if (m == "chars")
|
||||
spec.metric = 0;
|
||||
else if (m == "flat")
|
||||
spec.metric = 1;
|
||||
else if (m == "blocks")
|
||||
spec.metric = 2;
|
||||
else
|
||||
main_tree.fail("unknown metric '" + m +
|
||||
"' (chars, flat or blocks)");
|
||||
} else if (kw == "mode") {
|
||||
const std::string &m = it->tokens[1].value;
|
||||
if (m == "layers")
|
||||
@@ -513,7 +524,7 @@ 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);
|
||||
spec.mode, spec.metric);
|
||||
if (prog.empty()) {
|
||||
main_tree.fail("%synth '" + name + "': nothing emitted");
|
||||
} else {
|
||||
|
||||
+18
-16
@@ -16,7 +16,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 mode, int metric) {
|
||||
using V = r8veritas<P>;
|
||||
V v;
|
||||
v.initialize_nicknames();
|
||||
@@ -55,12 +55,14 @@ r8asm_synth_run(const std::vector<int> &in, const std::vector<int> &out,
|
||||
|
||||
// ---- cache: the same table always gives the same program, and the tape
|
||||
// search is measured in minutes, so keep the answer around
|
||||
std::string key = std::to_string(P) + "|" + std::to_string(mode) + "|";
|
||||
std::string key = std::to_string(P) + "|" + std::to_string(mode) + "|" +
|
||||
std::to_string(metric) + "|";
|
||||
for (int val : stat)
|
||||
key += std::to_string(val) + ",";
|
||||
key += "#";
|
||||
for (int val : layout)
|
||||
key += std::to_string(val) + ",";
|
||||
if (metric == 0) // only the character metric cares where the cells are
|
||||
for (int val : layout)
|
||||
key += std::to_string(val) + ",";
|
||||
uint64_t h = 0xcbf29ce484222325ULL;
|
||||
for (unsigned char ch : key) {
|
||||
h ^= ch;
|
||||
@@ -110,7 +112,7 @@ r8asm_synth_run(const std::vector<int> &in, const std::vector<int> &out,
|
||||
<< " layers" << (layers.proven_optimal ? " (shortest)" : "")
|
||||
<< ", " << layers.states << " states, " << layers.seconds << "s\n";
|
||||
|
||||
if (mode == 1) { // fewest layers only: skip the tape search
|
||||
if (mode == 1 || metric == 2) { // blocks only: skip the tape search
|
||||
for (const auto &i : layers.program)
|
||||
out_ins.push_back({int(i.sacrifice), int(i.mask), int(i.or_mode)});
|
||||
write_cache(out_ins);
|
||||
@@ -146,8 +148,8 @@ r8asm_synth_run(const std::vector<int> &in, const std::vector<int> &out,
|
||||
|
||||
const unsigned threads = std::thread::hardware_concurrency();
|
||||
const auto t0 = std::chrono::steady_clock::now();
|
||||
const auto tape =
|
||||
r8par::search_tt<P>(tt, addr, bound, threads, copt.max_states);
|
||||
const auto tape = r8par::search_tt<P>(tt, addr, bound, threads,
|
||||
copt.max_states, metric != 1);
|
||||
const double tape_seconds =
|
||||
std::chrono::duration<double>(std::chrono::steady_clock::now() - t0)
|
||||
.count();
|
||||
@@ -172,7 +174,7 @@ 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) {
|
||||
const std::vector<int> &layout, int mode, int metric) {
|
||||
if (params < 1 || params > 8) {
|
||||
std::cerr << "r8asm: %synth " << name << ": supports 1..8 parameters\n";
|
||||
return {};
|
||||
@@ -192,21 +194,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);
|
||||
return r8asm_synth_run<1>(in, out, map, stat, name, layout, mode, metric);
|
||||
case 2:
|
||||
return r8asm_synth_run<2>(in, out, map, stat, name, layout, mode);
|
||||
return r8asm_synth_run<2>(in, out, map, stat, name, layout, mode, metric);
|
||||
case 3:
|
||||
return r8asm_synth_run<3>(in, out, map, stat, name, layout, mode);
|
||||
return r8asm_synth_run<3>(in, out, map, stat, name, layout, mode, metric);
|
||||
case 4:
|
||||
return r8asm_synth_run<4>(in, out, map, stat, name, layout, mode);
|
||||
return r8asm_synth_run<4>(in, out, map, stat, name, layout, mode, metric);
|
||||
case 5:
|
||||
return r8asm_synth_run<5>(in, out, map, stat, name, layout, mode);
|
||||
return r8asm_synth_run<5>(in, out, map, stat, name, layout, mode, metric);
|
||||
case 6:
|
||||
return r8asm_synth_run<6>(in, out, map, stat, name, layout, mode);
|
||||
return r8asm_synth_run<6>(in, out, map, stat, name, layout, mode, metric);
|
||||
case 7:
|
||||
return r8asm_synth_run<7>(in, out, map, stat, name, layout, mode);
|
||||
return r8asm_synth_run<7>(in, out, map, stat, name, layout, mode, metric);
|
||||
default:
|
||||
return r8asm_synth_run<8>(in, out, map, stat, name, layout, mode);
|
||||
return r8asm_synth_run<8>(in, out, map, stat, name, layout, mode, metric);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user