Parallel cost search: measured, and left off by default

Tried the obvious split -- one worker per possible first block, since a
program's first block is unique and the answers cannot overlap -- and measured
it on the full adder at limit 82:

    workers   wall    total CPU   peak memory
    1         1350s    1350s       11 GB
    8          ~10min  4600s       14 GB
    32         --      4870s       17 GB

So the answers do not overlap but the *search* does: every prefix walks into the
same middle of the graph, and with a table per worker that middle is explored
once per worker instead of once overall.  Three to four times the work and more
memory, for a modest wall clock win.

Kept the code (opt.threads > 1) because it is correct and it is the right shape
for a machine with more memory, but the default is one worker.  The parallel
split that should actually pay off is inside one shared table: expand the
current Dial bucket across threads, insert through a sharded table.  That is
the next attempt, not this one.
This commit is contained in:
starcells committed 2026-11-06 11:40:07 +08:00
1 parent 810aef6fe4
commit a363351b5b
1 file changed
+173 -116
+173 -116
View File
@@ -48,7 +48,10 @@
#include <optional>
#include <ostream>
#include <set>
#include <atomic>
#include <mutex>
#include <string>
#include <thread>
#include <unordered_set>
#include <utility>
#include <vector>
@@ -85,6 +88,7 @@ public:
std::array<int, CELL_SIZE> addr{}; // cell address of each plane
int max_cost = 4096; // give up above this tape length
size_t max_states = 600'000'000;
unsigned threads = 0; // 0 = one worker per hardware thread
bool verbose = false;
};
@@ -473,137 +477,190 @@ private:
E::put(goal, b, E::get(goal, b) | (uint64_t(1) << j));
}
// ---- arena (stable ids) + open addressing id table
std::vector<key> keys;
std::vector<uint16_t> dist;
std::vector<uint32_t> par;
std::vector<uint16_t> how;
std::vector<int32_t> tab;
size_t tabmask = (1 << 18) - 1;
tab.assign(tabmask + 1, -1);
// ---- split by first block and run the workers in parallel.
// A program's first block is unique, so the answers do not overlap; the
// workers only have to agree on the best cost found so far, which doubles
// as every other worker's bound.
const unsigned hw = std::thread::hardware_concurrency();
// One worker by default: splitting by first block duplicates the whole
// middle of the search (every prefix reaches it), which measured three to
// four times the work and sixteen gigabytes. opt.threads > 1 still runs
// the split, it is just not the default.
unsigned nthreads = opt.threads ? unsigned(opt.threads) : 1u;
if (nthreads < 1u)
nthreads = 1u;
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;
std::atomic<int> next_first{0};
std::atomic<int> best_done{0x7FFFFFFF};
std::mutex best_mutex;
std::vector<instruction> best_prog;
std::atomic<unsigned long long> states_total{0};
std::atomic<bool> budget_hit{false};
auto record = [&](int cost, std::vector<instruction> prog) {
std::lock_guard<std::mutex> guard(best_mutex);
if (cost < best_done.load()) {
best_done.store(cost);
best_prog = std::move(prog);
}
return -1;
};
auto rehash = [&]() {
tab.assign((tabmask + 1) * 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;
auto worker = [&]() {
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 tabmask = (1u << 16) - 1;
tab.assign(tabmask + 1, -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 ins) -> 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(ins);
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);
const size_t budget = opt.max_states / nthreads;
for (;;) {
const int first = next_first.fetch_add(1);
if (first >= int(insns.size()))
break;
if (w[size_t(first)] >= best_done.load())
continue;
keys.clear();
dists.clear();
pars.clear();
hows.clear();
tab.assign(tabmask + 1, -1);
for (auto &b : ring)
b.clear();
const int limit = std::min(opt.max_cost, best_done.load() - 1);
if (w[size_t(first)] > limit)
continue;
const key s0 = E::step(start, insns[size_t(first)]);
const int d0 = w[size_t(first)];
if (s0 == goal) {
record(d0, {insns[size_t(first)]});
continue;
}
add(s0, d0, 0, 0xFFFE); // root of this sub search
ring[size_t(d0) % ring.size()].push_back(0);
auto backtrack = [&](uint32_t id) {
std::vector<instruction> p;
while (hows[id] != 0xFFFE) {
p.push_back(insns[hows[id]]);
id = pars[id];
}
p.push_back(insns[size_t(first)]);
std::reverse(p.begin(), p.end());
return p;
};
int d = d0;
while (d <= limit) {
auto &bucket = ring[size_t(d) % ring.size()];
if (bucket.empty()) {
d++; // plain Dial: the ring reuses slots every ring.size() steps
continue;
}
const uint32_t id = bucket.back();
bucket.pop_back();
if (dists[id] != d)
continue; // stale
if (keys[id] == goal) {
record(d, backtrack(id));
break;
}
if (d >= limit)
continue;
const key cur = keys[id];
for (size_t ii = 0; ii < insns.size(); ii++) {
if (E::get(cur, insns[ii].sacrifice) == 0)
continue;
const int nd = d + w[ii];
if (nd > limit)
continue;
const key nx = E::step(cur, insns[ii]);
const int32_t f = find_id(nx);
if (f < 0) {
const uint32_t nid = add(nx, nd, id, uint16_t(ii));
ring[size_t(nd) % ring.size()].push_back(nid);
} else if (nd < dists[size_t(f)]) {
dists[size_t(f)] = uint16_t(nd);
pars[size_t(f)] = id;
hows[size_t(f)] = uint16_t(ii);
ring[size_t(nd) % ring.size()].push_back(uint32_t(f));
}
}
if (keys.size() > budget) {
budget_hit.store(true);
break;
}
}
states_total.fetch_add(keys.size());
}
};
auto add = [&](const key &k) -> uint32_t {
if ((keys.size() + 1) * 10 > (tabmask + 1) * 7)
rehash();
const uint32_t id = uint32_t(keys.size());
keys.push_back(k);
dist.push_back(0xFFFF);
par.push_back(0);
how.push_back(0);
size_t j = size_t(E::hash(k)) & tabmask;
while (tab[j] >= 0)
j = (j + 1) & tabmask;
tab[j] = int32_t(id);
return id;
};
const uint32_t start_id = add(start);
dist[start_id] = 0;
how[start_id] = 0xFFFE; // root
std::vector<std::thread> pool;
for (unsigned t = 1; t < nthreads; t++)
pool.emplace_back(worker);
worker();
for (auto &t : pool)
t.join();
std::vector<std::vector<uint32_t>> ring(size_t(wmax) + 1);
auto push = [&](int d, uint32_t id) {
ring[size_t(d) % ring.size()].push_back(id);
};
push(0, start_id);
auto backtrack = [&](uint32_t id) {
std::vector<instruction> p;
while (how[id] != 0xFFFE) {
p.push_back(insns[how[id]]);
id = par[id];
}
std::reverse(p.begin(), p.end());
return p;
};
if (start == goal) {
r.found = r.proven = true;
r.states = size_t(states_total.load());
if (!best_prog.empty()) {
r.program = best_prog;
r.cost = best_done.load();
r.found = true;
r.proven = !budget_hit.load();
if (!r.proven)
r.message = "state budget exhausted before the search was complete";
return r;
}
int d = 0;
size_t pops_last = 0;
const int limit = opt.max_cost;
while (d <= limit) {
auto &bucket = ring[size_t(d) % ring.size()];
if (bucket.empty()) {
// plain Dial: step one unit at a time. Scanning ahead is not safe --
// the ring reuses slots every ring.size() steps, so a far bucket can
// sit where a near one would be.
d++;
continue;
}
const uint32_t id = bucket.back();
bucket.pop_back();
if (dist[id] != d)
continue; // stale
if (keys[id] == goal) {
r.program = backtrack(id);
r.cost = d;
r.found = r.proven = true;
r.states = keys.size();
return r;
}
if (d >= limit)
continue;
const key cur = keys[id];
for (size_t ii = 0; ii < insns.size(); ii++) {
if (E::get(cur, insns[ii].sacrifice) == 0)
continue;
const int nd = d + w[ii];
if (nd > limit)
continue;
const key nx = E::step(cur, insns[ii]);
const int32_t f = find_id(nx);
if (f < 0) {
const uint32_t nid = add(nx);
dist[nid] = uint16_t(nd);
par[nid] = id;
how[nid] = uint16_t(ii);
push(nd, nid);
} else if (nd < dist[size_t(f)]) {
dist[size_t(f)] = uint16_t(nd);
par[size_t(f)] = id;
how[size_t(f)] = uint16_t(ii);
push(nd, uint32_t(f));
}
}
if (opt.verbose && keys.size() / 20'000'000 != pops_last) {
pops_last = keys.size() / 20'000'000;
std::cerr << " cost " << d << ", " << keys.size() << " states\n";
}
if (keys.size() > opt.max_states) {
r.message = "state budget exhausted";
r.states = keys.size();
return r;
}
}
r.states = keys.size();
r.proven = true; // nothing at or below the limit
r.message = "no program with cost <= " + std::to_string(limit);
r.proven = !budget_hit.load();
r.message =
r.proven ? ("no program with cost <= " + std::to_string(opt.max_cost))
: "state budget exhausted";
return r;
}
template <int K>
result solve_with(const truthtable &raw, const std::vector<size_t> &full,
const options &opt, result &r,