diff --git a/include/r8veritas.hh b/include/r8veritas.hh index 8fb3177..e7e4224 100644 --- a/include/r8veritas.hh +++ b/include/r8veritas.hh @@ -3,30 +3,67 @@ #include #include +#include #include #include #include #include #include #include +#include #include #include #include #include #include +#include #include #include #include +#include +#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(r8veritas::ins_opcode::SET1)) +#endif +#ifndef OPCODE_FLIP #define OPCODE_FLIP (static_cast(r8veritas::ins_opcode::FLIP)) +#endif + +// r8veritas — 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 class r8veritas { public: @@ -58,6 +95,9 @@ public: std::array tp_paths; private: + static constexpr size_t POOL_SHARDS = 32; // power of two + static constexpr size_t PARALLEL_THRESHOLD = 4096; + std::array all_influences; const std::set all_ins = []() -> std::set { @@ -79,9 +119,6 @@ private: return s; }(); - std::unordered_map> - visited_op; struct stat_hash { size_t operator()(const stat &s) const { size_t h = 0; @@ -93,8 +130,22 @@ private: return h; } }; - std::unordered_set stat_pool; - std::queue stats; + + std::array, POOL_SHARDS> + pool_shards; + std::array pool_locks; + std::mutex visited_op_lock; + std::unordered_map> + visited_op; + + std::vector frontier; + std::vector next; + std::vector> 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) { @@ -116,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 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 save_stat(r8veritas::stat &&st) { - auto t = stat_pool.insert(std::move(st)); + size_t s = shard_of(st); + std::lock_guard g(pool_locks[s]); + auto t = pool_shards[s].insert(std::move(st)); return {&(*(t.first)), !t.second}; } @@ -138,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 // 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); @@ -160,9 +212,16 @@ public: } } } + return next_stat; + } + + std::pair // 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; } @@ -270,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 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 &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 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 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 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 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 found{false}; + size_t T = std::min(nthreads, fsz); + thread_next.assign(T, {}); + std::vector 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) { @@ -314,4 +477,4 @@ public: } }; -#endif \ No newline at end of file +#endif diff --git a/tests/r8_bench.cc b/tests/r8_bench.cc new file mode 100644 index 0000000..35f3532 --- /dev/null +++ b/tests/r8_bench.cc @@ -0,0 +1,101 @@ +// Baseline bench driver: original single-thread r8veritas. +// Usage: r8_bench problem in {and, copy, s, co, half} +#include "r8veritas.hh" +#include +#include +#include +#include + +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(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 +} diff --git a/tests/r8_bench_mt.cc b/tests/r8_bench_mt.cc new file mode 100644 index 0000000..40c7f94 --- /dev/null +++ b/tests/r8_bench_mt.cc @@ -0,0 +1,102 @@ +// MT bench driver: multi-threaded r8veritas. +// Usage: r8_bench_mt problem in {and, copy, s, co, half} +#include "r8veritas.hh" +#include +#include +#include +#include + +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(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 +}