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.
This commit is contained in:
dsh committed 2026-11-06 13:24:09 +08:00
1 parent 5b492b4ef5
commit 0bc5f1e6b8
5 files changed
+462 -5

No files matched your search

+3
View File
@@ -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)
+283
View File
@@ -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 <algorithm>
#include <array>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <string>
#include <thread>
#include <vector>
namespace r8par {
template <int N, int K> 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<uint64_t, KEYW>;
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 <int N>
inline int block_cost(const std::array<int, N> &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<std::array<int, 3>> prog;
size_t states = 0;
double seconds = 0;
std::string message;
};
template <int N, int K>
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) {
using E = engine<N, K>;
using key = typename E::key;
result_t r;
const auto t0 = std::chrono::steady_clock::now();
auto finish = [&]() {
r.seconds =
std::chrono::duration<double>(std::chrono::steady_clock::now() - t0)
.count();
return r;
};
const size_t ni = ins.size();
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);
if (w[i] > wmax)
wmax = w[i];
}
if (nthreads < 1)
nthreads = 1;
// ---- the table; only the serial pass ever writes to it
std::vector<key> keys;
std::vector<uint16_t> dists;
std::vector<uint32_t> pars;
std::vector<uint16_t> hows;
std::vector<int32_t> 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<std::vector<uint32_t>> 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<std::vector<cand>> cbuf(nthreads);
std::vector<std::vector<upd>> ubuf(nthreads);
auto backtrack = [&](uint32_t id) {
std::vector<std::array<int, 3>> 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<std::thread> 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
+95
View File
@@ -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 <array>
#include <cstddef>
#include <vector>
namespace r8par {
// every block of the classic model: one sacrifice plus every mask, both ops
template <int N>
std::vector<std::array<int, 3>> classic_blocks() {
std::vector<std::array<int, 3>> 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 <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) {
using E = engine<N, K>;
using key = typename E::key;
const size_t entries = size_t(1) << K;
std::vector<size_t> 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<N, K>(start, goal, classic_blocks<N>(), addr, limit, threads,
max_states);
}
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) {
std::vector<int> 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<N, 1>(tt, bit_of, addr, limit, threads, max_states);
case 2:
return search_k<N, 2>(tt, bit_of, addr, limit, threads, max_states);
case 3:
return search_k<N, 3>(tt, bit_of, addr, limit, threads, max_states);
case 4:
return search_k<N, 4>(tt, bit_of, addr, limit, threads, max_states);
case 5:
return search_k<N, 5>(tt, bit_of, addr, limit, threads, max_states);
case 6:
return search_k<N, 6>(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
+12 -5
View File
@@ -1,4 +1,5 @@
#include "r8forge/r8asm.hh"
#include "r8forge/r8veritas_phase_tt.hh"
#include <filesystem>
#include <fstream>
#include <chrono>
@@ -139,8 +140,14 @@ r8asm_synth_run(const std::vector<int> &in, const std::vector<int> &out,
}
copt.max_cost = bound;
std::array<int, P> 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<P>(tt, addr, bound, threads, copt.max_states);
const double tape_seconds =
std::chrono::duration<double>(std::chrono::steady_clock::now() - t0)
.count();
@@ -153,10 +160,10 @@ r8asm_synth_run(const std::vector<int> &in, const std::vector<int> &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;
}
+69
View File
@@ -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 <cstdlib>
#include <iostream>
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<std::array<int, 3>> 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<int, 6> 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;
}