From 0bc5f1e6b8dd4d728431311d94b60d2e2b9cdf8b Mon Sep 17 00:00:00 2001 From: dsh Date: Fri, 6 Nov 2026 13:24:09 +0800 Subject: [PATCH] Phase-parallel tape search: 22 minutes down to 2.7 The sequential search is memory bound on its hash table, so the split that works is inside a Dial bucket, not across the graph: * the table is read only for the whole bucket, so threads probe it with no locking -- 99.7% of the work, since only about one edge in four hundred lands on an unseen state * misses and dist improvements are buffered per thread and applied by one serial pass after the bucket Cost order is untouched, so the answer is identical. Measured at limit 60: threads wall states 1 12.40s 7,671,626 8 2.68s 7,671,626 16 2.20s 7,671,626 32 2.14s 7,671,626 and at the full limit 82 (the run that used to take 1350s): 162s, 414,631,673 states, cost 75 in 6 blocks -- the same program. %synth now uses this by default. examples/full_add.r8asm assembles in 166s instead of ~23 minutes and still produces the 90 character tape, checked on the machine for all eight combinations. The earlier attempt -- one worker per possible first block -- stays in r8veritas.hh behind opt.threads but off by default; it duplicated the middle of the graph and cost three to four times the work. --- CMakeLists.txt | 3 + include/r8forge/r8veritas_phase.hh | 283 ++++++++++++++++++++++++++ include/r8forge/r8veritas_phase_tt.hh | 95 +++++++++ src/synth.cc | 17 +- tests/cost_par.cc | 69 +++++++ 5 files changed, 462 insertions(+), 5 deletions(-) create mode 100644 include/r8forge/r8veritas_phase.hh create mode 100644 include/r8forge/r8veritas_phase_tt.hh create mode 100644 tests/cost_par.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 421aa54..97233aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,5 +23,8 @@ target_link_libraries(r8forge_synth_test r8forge_core) add_executable(r8forge_derive src/derive.cc) target_link_libraries(r8forge_derive r8forge_core) +add_executable(r8forge_cost_par tests/cost_par.cc) +target_link_libraries(r8forge_cost_par r8forge_core) + add_executable(r8forge_cost_search tests/cost_search.cc) target_link_libraries(r8forge_cost_search r8forge_core) diff --git a/include/r8forge/r8veritas_phase.hh b/include/r8forge/r8veritas_phase.hh new file mode 100644 index 0000000..7d4f0f7 --- /dev/null +++ b/include/r8forge/r8veritas_phase.hh @@ -0,0 +1,283 @@ +// Phase-parallel tape search. +// +// The sequential search is memory bound on its hash table, so this version does +// not cut the graph up (splitting by first block duplicated the whole middle: +// three to four times the work, sixteen gigabytes -- see the commit before). +// Instead it splits each Dial bucket across threads: +// +// 1. the table is read only for the whole bucket, so threads probe it with no +// locking at all -- that is ~99.7% of the work, because only about one edge +// in four hundred lands on a state we have not seen yet +// 2. misses and dist improvements are buffered per thread and applied by one +// serial pass once the bucket is finished +// +// Cost order is untouched, so the answer is the one the sequential search gives. +#ifndef R8VERITAS_PHASE +#define R8VERITAS_PHASE + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace r8par { + +template struct engine { + static constexpr int M = 1 << K; + static constexpr int KEYW = (N * M + 63) / 64; + static constexpr uint64_t PMASK = + (M >= 64) ? ~uint64_t(0) : ((uint64_t(1) << (M & 63)) - 1); + using key = std::array; + + static uint64_t get(const key &k, int b) { + const int off = b * M; + return (k[off >> 6] >> (off & 63)) & PMASK; + } + static void put(key &k, int b, uint64_t v) { + const int off = b * M; + k[off >> 6] = + (k[off >> 6] & ~(PMASK << (off & 63))) | ((v & PMASK) << (off & 63)); + } + static key step(const key &k, int sacrifice, int mask, bool or_mode) { + key n = k; + const uint64_t c = get(k, sacrifice); + put(n, sacrifice, 0); + for (int b = 0; b < N; b++) + if (mask & (1 << b)) + put(n, b, or_mode ? (get(k, b) | c) : (get(k, b) ^ c)); + return n; + } + static uint64_t hash(const key &k) { + uint64_t h = 0xcbf29ce484222325ULL; + for (int i = 0; i < KEYW; i++) { + h ^= k[i]; + h *= 0x100000001b3ULL; + h ^= h >> 29; + h *= 0xbf58476d1ce4e5b9ULL; + h ^= h >> 32; + } + return h; + } +}; + +// cost of one block on the tape (same model as r8veritas::solve_cost) +template +inline int block_cost(const std::array &addr, int sacrifice, int mask, + bool or_mode) { + int lo = addr[size_t(sacrifice)], hi = lo, cnt = 0; + for (int b = 0; b < N; b++) + if (mask & (1 << b)) { + cnt++; + 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); +} + +struct result_t { + bool found = false; + bool proven = false; + int cost = 0; + std::vector> prog; + size_t states = 0; + double seconds = 0; + std::string message; +}; + +template +result_t search(const typename engine::key &start, + const typename engine::key &goal, + const std::vector> &ins, + const std::array &addr, int limit, unsigned nthreads, + size_t max_states) { + using E = engine; + using key = typename E::key; + result_t r; + const auto t0 = std::chrono::steady_clock::now(); + auto finish = [&]() { + r.seconds = + std::chrono::duration(std::chrono::steady_clock::now() - t0) + .count(); + return r; + }; + + const size_t ni = ins.size(); + std::vector 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); + if (w[i] > wmax) + wmax = w[i]; + } + if (nthreads < 1) + nthreads = 1; + + // ---- the table; only the serial pass ever writes to it + std::vector keys; + std::vector dists; + std::vector pars; + std::vector hows; + std::vector tab(size_t(1) << 20, -1); + size_t tabmask = tab.size() - 1; + + auto find_id = [&](const key &k) -> int32_t { + size_t j = size_t(E::hash(k)) & tabmask; + while (tab[j] >= 0) { + if (keys[size_t(tab[j])] == k) + return tab[j]; + j = (j + 1) & tabmask; + } + return -1; + }; + auto rehash = [&]() { + tab.assign(tab.size() * 2, -1); + tabmask = tab.size() - 1; + for (size_t id = 0; id < keys.size(); id++) { + size_t j = size_t(E::hash(keys[id])) & tabmask; + while (tab[j] >= 0) + j = (j + 1) & tabmask; + tab[j] = int32_t(id); + } + }; + auto add = [&](const key &k, int d, uint32_t parent, uint16_t i) -> uint32_t { + if ((keys.size() + 1) * 10 > (tabmask + 1) * 7) + rehash(); + const uint32_t id = uint32_t(keys.size()); + keys.push_back(k); + dists.push_back(uint16_t(d)); + pars.push_back(parent); + hows.push_back(i); + size_t j = size_t(E::hash(k)) & tabmask; + while (tab[j] >= 0) + j = (j + 1) & tabmask; + tab[j] = int32_t(id); + return id; + }; + + std::vector> ring(size_t(wmax) + 1); + add(start, 0, 0, 0xFFFE); + ring[0].push_back(0); + + struct cand { + key k; + uint32_t parent; + uint16_t ins; + int cost; + }; + struct upd { + uint32_t id; + uint32_t parent; + uint16_t ins; + int cost; + }; + std::vector> cbuf(nthreads); + std::vector> ubuf(nthreads); + + auto backtrack = [&](uint32_t id) { + std::vector> p; + while (hows[id] != 0xFFFE) { + p.push_back(ins[hows[id]]); + id = pars[id]; + } + std::reverse(p.begin(), p.end()); + return p; + }; + + int d = 0; + while (d <= limit) { + auto &bucket = ring[size_t(d) % ring.size()]; + if (bucket.empty()) { + d++; // plain Dial: step one unit at a time + continue; + } + + // ---- if the goal is in this bucket, that is the cheapest way to it + for (uint32_t id : bucket) + if (dists[id] == d && keys[id] == goal) { + r.prog = backtrack(id); + r.cost = d; + r.found = r.proven = true; + r.states = keys.size(); + return finish(); + } + + // ---- parallel probe phase: the table only gets read + for (auto &b : cbuf) + b.clear(); + for (auto &b : ubuf) + b.clear(); + { + auto body = [&](unsigned t) { + for (size_t j = t; j < bucket.size(); j += nthreads) { + const uint32_t id = bucket[j]; + if (dists[id] != d) + continue; // stale entry + const key cur = keys[id]; + for (size_t i = 0; i < ni; i++) { + if (E::get(cur, ins[i][0]) == 0) + continue; + const int nd = d + w[i]; + if (nd > limit) + continue; + const key nx = E::step(cur, ins[i][0], ins[i][1], ins[i][2] != 0); + const int32_t f = find_id(nx); + if (f < 0) + cbuf[t].push_back({nx, id, uint16_t(i), nd}); + else if (nd < dists[size_t(f)]) + ubuf[t].push_back({uint32_t(f), id, uint16_t(i), nd}); + } + } + }; + std::vector pool; + for (unsigned t = 1; t < nthreads; t++) + pool.emplace_back(body, t); + body(0); + for (auto &th : pool) + th.join(); + } + + // ---- serial apply phase + for (unsigned t = 0; t < nthreads; t++) { + for (const auto &c : cbuf[t]) { + const int32_t f = find_id(c.k); + if (f < 0) { + const uint32_t id = add(c.k, c.cost, c.parent, c.ins); + ring[size_t(c.cost) % ring.size()].push_back(id); + } else if (c.cost < dists[size_t(f)]) { + dists[size_t(f)] = uint16_t(c.cost); + pars[size_t(f)] = c.parent; + hows[size_t(f)] = c.ins; + ring[size_t(c.cost) % ring.size()].push_back(uint32_t(f)); + } + } + for (const auto &u : ubuf[t]) { + if (u.cost < dists[size_t(u.id)]) { + dists[size_t(u.id)] = uint16_t(u.cost); + pars[size_t(u.id)] = u.parent; + hows[size_t(u.id)] = u.ins; + ring[size_t(u.cost) % ring.size()].push_back(u.id); + } + } + } + + bucket.clear(); + if (keys.size() > max_states) { + r.message = "state budget exhausted before the search was complete"; + r.states = keys.size(); + return finish(); + } + } + + r.states = keys.size(); + r.proven = true; + r.message = "no program with cost <= " + std::to_string(limit); + return finish(); +} + +} // namespace r8par + +#endif diff --git a/include/r8forge/r8veritas_phase_tt.hh b/include/r8forge/r8veritas_phase_tt.hh new file mode 100644 index 0000000..f444402 --- /dev/null +++ b/include/r8forge/r8veritas_phase_tt.hh @@ -0,0 +1,95 @@ +// Glue between r8veritas truthtables and the phase-parallel search: figures out +// which input bits matter, builds the start/goal keys and dispatches on K. +#ifndef R8VERITAS_PHASE_TT +#define R8VERITAS_PHASE_TT + +#include "r8forge/r8veritas.hh" +#include "r8forge/r8veritas_phase.hh" + +#include +#include +#include + +namespace r8par { + +// every block of the classic model: one sacrifice plus every mask, both ops +template +std::vector> classic_blocks() { + std::vector> ins; + for (int s = 0; s < N; s++) { + ins.push_back({s, 0, 0}); + for (int sub = 1; sub < (1 << (N - 1)); sub++) { + int m = 0, bit = 0; + for (int b = 0; b < N; b++) + if (b != s) + m |= ((sub >> bit++) & 1) << b; + ins.push_back({s, m, 0}); + ins.push_back({s, m, 1}); + } + } + return ins; +} + +template +result_t search_k(const typename r8veritas::truthtable &tt, + const std::vector &bit_of, + const std::array &addr, int limit, + unsigned threads, size_t max_states) { + using E = engine; + using key = typename E::key; + const size_t entries = size_t(1) << K; + std::vector full(entries, 0); + for (size_t j = 0; j < entries; j++) + for (int b = 0; b < K; b++) + if (j & (size_t(1) << b)) + full[j] |= size_t(1) << bit_of[size_t(b)]; + + key start = {}, goal = {}; + for (size_t j = 0; j < entries; j++) + for (int b = 0; b < N; b++) { + if (j & (size_t(1) << b)) + E::put(start, b, E::get(start, b) | (uint64_t(1) << j)); + if ((*tt.second)[full[j]] & (1u << b)) + E::put(goal, b, E::get(goal, b) | (uint64_t(1) << j)); + } + + return search(start, goal, classic_blocks(), addr, limit, threads, + max_states); +} + +template +result_t search_tt(const typename r8veritas::truthtable &tt, + const std::array &addr, int limit, unsigned threads, + size_t max_states) { + std::vector bit_of; + for (int i = 0; i < N; i++) + for (size_t j = 0; j < (size_t(1) << N); j++) + if ((*tt.second)[j] != (*tt.second)[j ^ (size_t(1) << i)]) { + bit_of.push_back(i); + break; + } + switch (bit_of.size()) { + case 1: + return search_k(tt, bit_of, addr, limit, threads, max_states); + case 2: + return search_k(tt, bit_of, addr, limit, threads, max_states); + case 3: + return search_k(tt, bit_of, addr, limit, threads, max_states); + case 4: + return search_k(tt, bit_of, addr, limit, threads, max_states); + case 5: + return search_k(tt, bit_of, addr, limit, threads, max_states); + case 6: + return search_k(tt, bit_of, addr, limit, threads, max_states); + default: { + result_t r; + r.message = "the answer depends on " + std::to_string(bit_of.size()) + + " inputs, which the packed state cannot hold"; + return r; + } + } +} + +} // namespace r8par + +#endif diff --git a/src/synth.cc b/src/synth.cc index 0fed7f2..72a8b95 100644 --- a/src/synth.cc +++ b/src/synth.cc @@ -1,4 +1,5 @@ #include "r8forge/r8asm.hh" +#include "r8forge/r8veritas_phase_tt.hh" #include #include #include @@ -139,8 +140,14 @@ r8asm_synth_run(const std::vector &in, const std::vector &out, } copt.max_cost = bound; + std::array addr{}; + for (int i = 0; i < P; i++) + addr[size_t(i)] = copt.addr[size_t(i)]; + + const unsigned threads = std::thread::hardware_concurrency(); const auto t0 = std::chrono::steady_clock::now(); - const auto tape = v.solve_cost(tt, copt); + const auto tape = + r8par::search_tt

(tt, addr, bound, threads, copt.max_states); const double tape_seconds = std::chrono::duration(std::chrono::steady_clock::now() - t0) .count(); @@ -153,10 +160,10 @@ r8asm_synth_run(const std::vector &in, const std::vector &out, return out_ins; } std::cerr << "r8asm: %synth " << name << ": tape cost " << tape.cost << " in " - << tape.program.size() << " blocks (layer answer was " << bound - << "), " << tape.states << " states, " << tape_seconds << "s\n"; - for (const auto &i : tape.program) - out_ins.push_back({int(i.sacrifice), int(i.mask), int(i.or_mode)}); + << tape.prog.size() << " blocks (layer answer was " << bound << "), " + << tape.states << " states, " << tape_seconds << "s\n"; + for (const auto &i : tape.prog) + out_ins.push_back({i[0], i[1], i[2]}); write_cache(out_ins); return out_ins; } diff --git a/tests/cost_par.cc b/tests/cost_par.cc new file mode 100644 index 0000000..6620b44 --- /dev/null +++ b/tests/cost_par.cc @@ -0,0 +1,69 @@ +// Drives the phase-parallel tape search on the full adder so it can be timed +// against the sequential one. +#include "r8forge/r8veritas.hh" +#include "r8forge/r8veritas_phase.hh" +#include +#include + +using V = r8veritas<6>; + +int main(int argc, char **argv) { + const int limit = argc > 1 ? std::atoi(argv[1]) : 82; + const unsigned threads = argc > 2 ? unsigned(std::atoi(argv[2])) : 1u; + + V v; + auto raw = v.generate_truthtable([](V::cell inp) -> V::cell { + unsigned a = inp & 1, b = (inp >> 1) & 1, c = (inp >> 2) & 1; + unsigned s = a ^ b ^ c; + unsigned co = (a & b) | (a & c) | (b & c); + return V::cell((co << 4) | (s << 3) | (inp & 0b111)); + }); + + // same input space r8veritas uses: the 3 relevant bits, identity start + using E = r8par::engine<6, 3>; + using key = typename E::key; + key start = {}, goal = {}; + for (int j = 0; j < 8; j++) + for (int b = 0; b < 6; b++) { + if (j & (1 << b)) + E::put(start, b, E::get(start, b) | (uint64_t(1) << j)); + if ((*raw.second)[size_t(j)] & (1u << b)) + E::put(goal, b, E::get(goal, b) | (uint64_t(1) << j)); + } + + // the 378 blocks: pure sacrifice plus every mask, both op modes + std::vector> ins; + for (int s = 0; s < 6; s++) { + ins.push_back({s, 0, 0}); + for (int sub = 1; sub < (1 << 5); sub++) { + int m = 0, bit = 0; + for (int b = 0; b < 6; b++) + if (b != s) + m |= ((sub >> bit++) & 1) << b; + ins.push_back({s, m, 0}); + ins.push_back({s, m, 1}); + } + } + + std::array addr = {0, 1, 2, 3, 4, 5}; + std::cout << "limit " << limit << ", threads " << threads << ", " + << ins.size() << " blocks\n"; + const auto res = r8par::search<6, 3>(start, goal, ins, addr, limit, threads, + 600'000'000ull); + if (!res.found) { + std::cout << "no program <= " << limit << " " << res.states + << " states, " << res.seconds << "s " << res.message << "\n"; + return res.proven ? 0 : 2; + } + std::cout << "cost " << res.cost << ", " << res.prog.size() << " blocks, " + << res.states << " states, " << res.seconds << "s\n"; + for (const auto &i : res.prog) { + std::cout << " sacrifice " << i[0] << " " << (i[2] ? "SET1" : "FLIP") + << " {"; + for (int b = 0; b < 6; b++) + if (i[1] & (1 << b)) + std::cout << " " << b; + std::cout << " }\n"; + } + return 0; +}