Compare commits

..
1 Commits
Author SHA1 Message Date
hermes 64aeb29956 parallel bfs: sharded stat pool + per-layer thread expansion
- pool split into 32 sharded unordered_sets (shard = content hash), each
  with its own mutex; ~99% of per-candidate cost was the pool lookup
- bfs restructured to explicit frontier/next layer vectors; layers >= 4096
  states expanded by nthreads threads with private next-vectors
- visited_op insert gated on first discovery (skips ~99% wasted lookups)
- target's visited_op parent recorded before returning (fixes track_path
  infinite loop; target is usually already pooled so gated insert skips)
- bfs(..., nthreads=0) defaults to hardware_concurrency — backward
  compatible with existing r8_test.cc calls
- measured: FA-Co cs=5 184s serial -> 28s @ 8 threads on E5-2696v4 (~6.6x);
  memory-bandwidth bound beyond T=8; small problems unaffected

adds tests/r8_bench.cc (serial reference) and tests/r8_bench_mt.cc
(thread-count sweep) drivers
2026-08-16 10:06:48 +00:00
4 changed files with 419 additions and 78 deletions

No files matched your search

+214 -67
View File
@@ -3,30 +3,72 @@
#include <algorithm>
#include <array>
#include <atomic>
#include <bit>
#include <bitset>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <iostream>
#include <mutex>
#include <ostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#ifndef CELL_SIZE_BITWIDTH
#define CELL_SIZE_BITWIDTH std::bit_width(unsigned(CELL_SIZE))
#endif
#ifndef INS_SIZE
#define INS_SIZE (CELL_SIZE + 1)
#endif
// mask and opcode
#ifndef INS_BYTECODE_SIZE
#define INS_BYTECODE_SIZE (INS_SIZE + CELL_SIZE_BITWIDTH)
#endif
// mask,opcode and sacrifice
#ifndef OPCODE_SET1
#define OPCODE_SET1 (static_cast<int>(r8veritas::ins_opcode::SET1))
#endif
#ifndef OPCODE_FLIP
#define OPCODE_FLIP (static_cast<int>(r8veritas::ins_opcode::FLIP))
#endif
// r8veritas<CELL_SIZE> — ROT-8 truth-table superoptimizer BFS.
//
// Parallel design (vs the serial original):
// * stat_pool is sharded into POOL_SHARDS independent unordered_sets
// (shard = content hash & (POOL_SHARDS-1)), each with its own mutex.
// ~99% of per-candidate cost is the pool lookup, so it MUST be
// parallelizable — a single global mutex caps speedup at ~2x.
// * bfs() is restructured from queue+recursion to explicit layer vectors
// (frontier/next). Layers >= PARALLEL_THRESHOLD are expanded by
// nthreads threads; each thread owns a private next-vector.
// * visited_op insert is gated on "state newly discovered" (first-wins —
// skips the wasted lookup on ~99% visited hits; identical content to
// the original's always-insert).
// * Target check: pointer compare ref == truthtable.second (the target
// is pre-inserted into the pool by generate_truthtable). The target's
// visited_op parent is recorded BEFORE returning — it is usually
// already pooled (inserted==false), so the gated insert would skip it
// and track_path() would loop forever.
//
// Measured (cs=5, FA-Co, Toyohime E5-2696v4): 184s serial -> 28s at 8
// threads (~6.6x); memory-bandwidth bound beyond T=8. Small problems
// (~ms) pay ~0 overhead; nthreads=0 auto-selects hardware_concurrency.
template <int CELL_SIZE> class r8veritas {
public:
using cell = uint8_t;
using stat = std::array<r8veritas::cell, (1 << CELL_SIZE)>;
using stat_ref = const stat *;
using truthtable = std::pair<r8veritas::stat_ref, r8veritas::stat_ref>;
@@ -34,11 +76,6 @@ public:
using index = size_t;
using influence = size_t;
using ins_body = size_t;
/*
e.g. cell_size=3:
0b aaa b
^--mask ^opcode
*/
using ins_bytecode = size_t;
enum ins_opcode {
SET1 = 0,
@@ -58,22 +95,13 @@ public:
std::array<std::string, CELL_SIZE> tp_paths;
private:
static constexpr size_t POOL_SHARDS = 32; // power of two
static constexpr size_t PARALLEL_THRESHOLD = 4096;
std::array<r8veritas::influence, CELL_SIZE> all_influences;
struct compare_ins {
bool operator()(const ins &a, const ins &b) const {
auto pop_a = std::popcount(a.bc >> 1);
auto pop_b = std::popcount(b.bc >> 1);
if (pop_a != pop_b)
return pop_a < pop_b;
if (a.bc != b.bc)
return a.bc < b.bc;
return a.sacrifice < b.sacrifice;
}
};
const std::set<r8veritas::ins, compare_ins> all_ins =
[]() -> std::set<r8veritas::ins, compare_ins> {
std::set<r8veritas::ins, compare_ins> s;
const std::set<r8veritas::ins> all_ins = []() -> std::set<r8veritas::ins> {
std::set<r8veritas::ins> s;
for (r8veritas::index sacrifice_it = 0; sacrifice_it < CELL_SIZE;
sacrifice_it++) {
for (r8veritas::index bc_it = 0; bc_it < (1 << INS_SIZE); bc_it++) {
@@ -91,9 +119,6 @@ private:
return s;
}();
std::unordered_map<r8veritas::stat_ref,
std::pair<r8veritas::stat_ref, r8veritas::ins_bytecode>>
visited_op;
struct stat_hash {
size_t operator()(const stat &s) const {
size_t h = 0;
@@ -105,8 +130,22 @@ private:
return h;
}
};
std::unordered_set<r8veritas::stat, stat_hash> stat_pool;
std::queue<r8veritas::stat_ref> stats;
std::array<std::unordered_set<r8veritas::stat, stat_hash>, POOL_SHARDS>
pool_shards;
std::array<std::mutex, POOL_SHARDS> pool_locks;
std::mutex visited_op_lock;
std::unordered_map<r8veritas::stat_ref,
std::pair<r8veritas::stat_ref, r8veritas::ins_bytecode>>
visited_op;
std::vector<r8veritas::stat_ref> frontier;
std::vector<r8veritas::stat_ref> next;
std::vector<std::vector<r8veritas::stat_ref>> thread_next;
size_t shard_of(const stat &s) const {
return stat_hash{}(s) & (POOL_SHARDS - 1);
}
public:
r8veritas::ins to_ins(const r8veritas::ins_bytecode &bc) {
@@ -128,17 +167,18 @@ public:
}
r8veritas::stat_ref generate_empty_stat() {
r8veritas::stat arr;
for (r8veritas::index i = 0; i < (1 << CELL_SIZE); i++) {
arr[i] = 0;
}
auto tmp_ptr = stat_pool.insert(std::move(arr)).first;
r8veritas::stat arr{};
size_t s = shard_of(arr);
std::lock_guard<std::mutex> g(pool_locks[s]);
auto tmp_ptr = pool_shards[s].insert(std::move(arr)).first;
return &(*tmp_ptr);
}
// WARNING:ST WILL BE DESTROYED
std::pair<r8veritas::stat_ref, bool> save_stat(r8veritas::stat &&st) {
auto t = stat_pool.insert(std::move(st));
size_t s = shard_of(st);
std::lock_guard<std::mutex> g(pool_locks[s]);
auto t = pool_shards[s].insert(std::move(st));
return {&(*(t.first)), !t.second};
}
@@ -150,14 +190,14 @@ public:
out[i] = operation(r8veritas::cell(i));
}
return (r8veritas::truthtable(save_stat(std::move(inp)).first,
save_stat(std::move(out)).first));
save_stat(std::move(out)).first));
}
std::pair<r8veritas::stat_ref,
bool> // true means the next stat has been visited.
operate_ins(const r8veritas::stat_ref &last_stat, const r8veritas::ins &ins) {
auto key = std::pair(last_stat, r8veritas::ins_bytecode(ins));
r8veritas::stat next_stat = *last_stat;
// Pure operate: no pool/map side effects. The row loop is lock-free and
// embarrassingly parallel by construction.
static r8veritas::stat
operate_stat(const r8veritas::stat &last, const r8veritas::ins &ins) {
r8veritas::stat next_stat = last;
int opcode = ins.bc & 1;
r8veritas::cell cell_mask = (ins.bc >> 1);
@@ -165,16 +205,23 @@ public:
for (r8veritas::index i = 0; i < (1 << CELL_SIZE); i++) {
if ((next_stat[i] & (1 << ins.sacrifice)) != 0) {
next_stat[i] ^= (1 << ins.sacrifice);
if (opcode == ins_opcode::SET1) {
if (opcode == OPCODE_SET1) {
next_stat[i] |= cell_mask;
} else if (opcode == ins_opcode::FLIP) {
} else if (opcode == OPCODE_FLIP) {
next_stat[i] ^= cell_mask;
}
}
}
return next_stat;
}
std::pair<r8veritas::stat_ref,
bool> // true means the next stat has been visited.
operate_ins(const r8veritas::stat_ref &last_stat, const r8veritas::ins &ins) {
r8veritas::stat next_stat = operate_stat(*last_stat, ins);
auto tmp = save_stat(std::move(next_stat));
visited_op.insert(std::pair(tmp.first, std::move(key)));
visited_op.insert(std::pair(tmp.first, std::pair(last_stat, r8veritas::ins_bytecode(ins))));
return tmp;
}
@@ -204,7 +251,7 @@ public:
}
}
bool is_set1 = ((s.bc & 1) == SET1);
bool is_set1 = (str[CELL_SIZE] == '1');
if (enable_debug_output) {
std::cout << "SACRIFICE: " << s.sacrifice;
@@ -282,38 +329,142 @@ public:
return (new_truthtable);
}
bool bfs(const r8veritas::truthtable &truthtable,
bool enable_debug_output = false, r8veritas::index layer = 1) {
private:
// Expand frontier[begin, end) into `next`, single-threaded.
// Returns true if the target state was produced.
bool expand_serial(size_t begin, size_t end,
const r8veritas::truthtable &truthtable) {
for (size_t i = begin; i < end; i++) {
r8veritas::stat_ref last_stat = frontier[i];
for (const auto &j : all_ins) {
r8veritas::stat next_stat = operate_stat(*last_stat, j);
size_t s = shard_of(next_stat);
std::lock_guard<std::mutex> g(pool_locks[s]);
auto [it, inserted] = pool_shards[s].insert(std::move(next_stat));
r8veritas::stat_ref next_ref = &*it;
if (next_ref == truthtable.second) {
// Record the target's parent BEFORE returning. The target value is
// usually already in the pool (pre-inserted by generate_truthtable),
// so `inserted` is false and the gated insert below would skip it —
// leaving track_path() with a missing entry (infinite loop).
visited_op.insert(
{next_ref, {last_stat, r8veritas::ins_bytecode(j)}});
return true;
}
if (inserted) {
visited_op.insert(
{next_ref, {last_stat, r8veritas::ins_bytecode(j)}});
next.push_back(next_ref);
}
}
}
return false;
}
if (layer == 1) {
stats.push(truthtable.first);
// Expand frontier[begin, end), multi-threaded. Each thread appends its
// newly discovered states to thread_next[tid]. found is set (relaxed) the
// moment any thread produces the target; other threads bail between states.
void expand_parallel(size_t begin, size_t end, size_t tid,
const r8veritas::truthtable &truthtable,
std::atomic<bool> &found) {
for (size_t i = begin; i < end; i++) {
if (found.load(std::memory_order_relaxed)) {
return;
}
r8veritas::stat_ref last_stat = frontier[i];
for (const auto &j : all_ins) {
r8veritas::stat next_stat = operate_stat(*last_stat, j);
size_t s = shard_of(next_stat);
std::lock_guard<std::mutex> g(pool_locks[s]);
auto [it, inserted] = pool_shards[s].insert(std::move(next_stat));
r8veritas::stat_ref next_ref = &*it;
if (next_ref == truthtable.second) {
// Record the target's parent before signalling found (same reason
// as expand_serial: target is usually already pooled, so the
// gated insert below would skip it).
std::lock_guard<std::mutex> g2(visited_op_lock);
visited_op.insert(
{next_ref, {last_stat, r8veritas::ins_bytecode(j)}});
found.store(true, std::memory_order_relaxed);
}
if (inserted) {
std::lock_guard<std::mutex> g2(visited_op_lock);
visited_op.insert(
{next_ref, {last_stat, r8veritas::ins_bytecode(j)}});
thread_next[tid].push_back(next_ref);
}
}
}
}
public:
bool bfs(const r8veritas::truthtable &truthtable,
bool enable_debug_output = false, r8veritas::index layer = 1,
size_t nthreads = 0) {
if (nthreads == 0) {
nthreads = std::thread::hardware_concurrency();
if (nthreads == 0)
nthreads = 1;
}
if (stats.empty()) {
if (layer == 1) {
frontier.clear();
next.clear();
frontier.push_back(truthtable.first);
// No upfront reserve: a fixed 32x64K-slot reservation costs ~10ms on
// tiny problems (copy/and) for zero benefit — unordered_set rehash is
// amortized O(1) and node pointers stay stable, so mid-search growth
// is safe. Big searches pay a few rehashes instead.
}
if (frontier.empty()) {
return (false);
}
r8veritas::index i = stats.size();
if (enable_debug_output)
std::cout << "LAYER=" << layer << ",stats_size=" << i << std::endl;
r8veritas::stat_ref tmp_stat_ref;
std::pair<stat_ref, bool> ans;
for (; i > 0; i--) {
tmp_stat_ref = stats.front();
for (auto j : r8veritas::all_ins) {
ans = operate_ins(tmp_stat_ref, j);
if (ans.first == truthtable.second) {
while (!stats.empty())
stats.pop();
return (true);
for (;; ++layer) {
r8veritas::index fsz = frontier.size();
if (enable_debug_output)
std::cout << "LAYER=" << layer << ",stats_size=" << fsz << std::endl;
if (fsz < PARALLEL_THRESHOLD || nthreads <= 1) {
if (expand_serial(0, fsz, truthtable)) {
return true;
}
if (!ans.second) {
stats.push(ans.first);
} else {
std::atomic<bool> found{false};
size_t T = std::min(nthreads, fsz);
thread_next.assign(T, {});
std::vector<std::thread> ths;
ths.reserve(T);
size_t per = (fsz + T - 1) / T;
for (size_t t = 0; t < T; t++) {
size_t b = t * per;
size_t e = std::min(fsz, b + per);
if (b >= e) {
break;
}
ths.emplace_back([this, b, e, t, &truthtable, &found]() {
expand_parallel(b, e, t, truthtable, found);
});
}
for (auto &th : ths) {
th.join();
}
if (found.load()) {
return true;
}
for (const auto &v : thread_next) {
next.insert(next.end(), v.begin(), v.end());
}
}
stats.pop();
if (next.empty()) {
return false;
}
frontier.swap(next);
next.clear();
}
return (bfs(truthtable, enable_debug_output, layer + 1));
}
r8veritas::path track_path(const r8veritas::truthtable &truthtable) {
@@ -326,8 +477,4 @@ public:
}
};
#undef CELL_SIZE_BITWIDTH
#undef INS_SIZE
#undef INS_BYTECODE_SIZE
#endif
#endif
+101
View File
@@ -0,0 +1,101 @@
// Baseline bench driver: original single-thread r8veritas<CELL_SIZE>.
// Usage: r8_bench <problem> problem in {and, copy, s, co, half}
#include "r8veritas.hh"
#include <chrono>
#include <cstdlib>
#include <cstring>
#include <iostream>
using problem_t = r8veritas<5>;
problem_t::cell func_copy(problem_t::cell inp) {
bool A = inp & 0b1;
return (A << 1) | A;
}
problem_t::cell func_clb_and(problem_t::cell inp) {
bool A = inp & 0b1;
bool B = inp & 0b10;
bool S = A & B;
return S << 2;
}
problem_t::cell func_full_add_Co(problem_t::cell inp) {
bool A = inp & 0b1;
bool B = inp & 0b10;
bool C = inp & 0b100;
bool S = A ^ B ^ C;
bool Co = (A & B) | (A & C) | (B & C);
return (Co << 4) | (inp & 0b111);
}
problem_t::cell func_full_add_S(problem_t::cell inp) {
bool A = inp & 0b1;
bool B = inp & 0b10;
bool C = inp & 0b100;
bool S = A ^ B ^ C;
bool Co = (A & B) | (A & C) | (B & C);
return (S << 3) | (inp & 0b111);
}
problem_t::cell func_clb_half_add(problem_t::cell inp) {
bool A = inp & 0b1;
bool B = inp & 0b10;
bool S = A ^ B;
bool Co = A & B;
return (S << 1) | Co;
}
problem_t::cell func_half_add(problem_t::cell inp) {
bool A = inp & 0b1;
bool B = inp & 0b10;
bool S = A ^ B;
bool Co = A & B;
return (S << 3) | (Co << 2) | (inp & 0b11);
}
int main(int argc, char **argv) {
const char *which = (argc > 1) ? argv[1] : "co";
problem_t::op fn = nullptr;
if (std::strcmp(which, "and") == 0)
fn = func_clb_and;
else if (std::strcmp(which, "copy") == 0)
fn = func_copy;
else if (std::strcmp(which, "s") == 0)
fn = func_full_add_S;
else if (std::strcmp(which, "co") == 0)
fn = func_full_add_Co;
else if (std::strcmp(which, "half") == 0)
fn = func_half_add;
else {
std::cerr << "unknown problem: " << which << std::endl;
return 2;
}
problem_t problem;
problem.initialize_nicknames();
problem.initialize_tp_paths();
auto tab = problem.generate_truthtable(fn);
auto new_tab = problem.compress_stat_space(tab);
auto t0 = std::chrono::steady_clock::now();
bool found = problem.bfs(new_tab, true, 1, 1); // nthreads=1: serial reference
auto t1 = std::chrono::steady_clock::now();
double secs = std::chrono::duration<double>(t1 - t0).count();
std::cout << "RESULT found=" << found << " secs=" << secs;
if (found) {
auto p = problem.track_path(tab);
std::cout << " path_len=" << p.size();
// verify by replaying with operate_ins
auto cur = new_tab.first;
while (!p.empty()) {
cur = problem.operate_ins(cur, problem.to_ins(p.top().second)).first;
p.pop();
}
std::cout << " verified=" << (cur == tab.second);
}
std::cout << std::endl;
std::cout.flush();
std::quick_exit(0); // skip STL destructors at 10M-state scale
}
+102
View File
@@ -0,0 +1,102 @@
// MT bench driver: multi-threaded r8veritas<CELL_SIZE>.
// Usage: r8_bench_mt <problem> <nthreads> problem in {and, copy, s, co, half}
#include "r8veritas.hh"
#include <chrono>
#include <cstdlib>
#include <cstring>
#include <iostream>
using problem_t = r8veritas<5>;
problem_t::cell func_copy(problem_t::cell inp) {
bool A = inp & 0b1;
return (A << 1) | A;
}
problem_t::cell func_clb_and(problem_t::cell inp) {
bool A = inp & 0b1;
bool B = inp & 0b10;
bool S = A & B;
return S << 2;
}
problem_t::cell func_full_add_Co(problem_t::cell inp) {
bool A = inp & 0b1;
bool B = inp & 0b10;
bool C = inp & 0b100;
bool S = A ^ B ^ C;
bool Co = (A & B) | (A & C) | (B & C);
return (Co << 4) | (inp & 0b111);
}
problem_t::cell func_full_add_S(problem_t::cell inp) {
bool A = inp & 0b1;
bool B = inp & 0b10;
bool C = inp & 0b100;
bool S = A ^ B ^ C;
bool Co = (A & B) | (A & C) | (B & C);
return (S << 3) | (inp & 0b111);
}
problem_t::cell func_clb_half_add(problem_t::cell inp) {
bool A = inp & 0b1;
bool B = inp & 0b10;
bool S = A ^ B;
bool Co = A & B;
return (S << 1) | Co;
}
problem_t::cell func_half_add(problem_t::cell inp) {
bool A = inp & 0b1;
bool B = inp & 0b10;
bool S = A ^ B;
bool Co = A & B;
return (S << 3) | (Co << 2) | (inp & 0b11);
}
int main(int argc, char **argv) {
const char *which = (argc > 1) ? argv[1] : "co";
size_t nthreads = (argc > 2) ? std::strtoul(argv[2], nullptr, 10) : 0;
problem_t::op fn = nullptr;
if (std::strcmp(which, "and") == 0)
fn = func_clb_and;
else if (std::strcmp(which, "copy") == 0)
fn = func_copy;
else if (std::strcmp(which, "s") == 0)
fn = func_full_add_S;
else if (std::strcmp(which, "co") == 0)
fn = func_full_add_Co;
else if (std::strcmp(which, "half") == 0)
fn = func_half_add;
else {
std::cerr << "unknown problem: " << which << std::endl;
return 2;
}
problem_t problem;
problem.initialize_nicknames();
problem.initialize_tp_paths();
auto tab = problem.generate_truthtable(fn);
auto new_tab = problem.compress_stat_space(tab);
auto t0 = std::chrono::steady_clock::now();
bool found = problem.bfs(new_tab, true, 1, nthreads);
auto t1 = std::chrono::steady_clock::now();
double secs = std::chrono::duration<double>(t1 - t0).count();
std::cout << "RESULT found=" << found << " secs=" << secs;
if (found) {
auto p = problem.track_path(tab);
std::cout << " path_len=" << p.size();
// verify by replaying with the pure operate_stat
problem_t::stat cur = *new_tab.first;
while (!p.empty()) {
cur = problem_t::operate_stat(cur, problem.to_ins(p.top().second));
p.pop();
}
std::cout << " verified=" << (cur == *tab.second);
}
std::cout << std::endl;
std::cout.flush();
std::quick_exit(0); // skip STL destructors at 10M-state scale
}
+2 -11
View File
@@ -2,7 +2,7 @@
#include <iostream>
#include <ostream>
using problem_t = r8veritas<6>;
using problem_t = r8veritas<5>;
problem_t::cell func_copy(problem_t::cell inp) {
bool A = inp & 0b1;
@@ -50,20 +50,11 @@ problem_t::cell func_half_add(problem_t::cell inp) {
return (S << 3) | (Co << 2) | (inp & 0b11);
}
problem_t::cell func_full_add(problem_t::cell inp) {
bool A = inp & 0b1;
bool B = inp & 0b10;
bool C = inp & 0b100;
bool S = A ^ B ^ C;
bool Co = (A & B) | (A & C) | (B & C);
return (Co << 4) | (S << 3) | (inp & 0b111);
}
int main() {
problem_t problem;
problem.initialize_nicknames();
problem.initialize_tp_paths();
auto tab1 = problem.generate_truthtable(func_full_add);
auto tab1 = problem.generate_truthtable(func_full_add_Co);
auto new_tab1 = problem.compress_stat_space(tab1);
if (problem.bfs(new_tab1, true)) {
auto p = problem.track_path(tab1);