forked from starcells/R8VeritasCC
Compare commits
1
Commits
main
...
parallel-bfs
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
64aeb29956 |
No files matched your search
+203
-40
@@ -3,30 +3,67 @@
|
|||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <atomic>
|
||||||
#include <bit>
|
#include <bit>
|
||||||
#include <bitset>
|
#include <bitset>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <mutex>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <stack>
|
#include <stack>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <thread>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#ifndef CELL_SIZE_BITWIDTH
|
||||||
#define CELL_SIZE_BITWIDTH std::bit_width(unsigned(CELL_SIZE))
|
#define CELL_SIZE_BITWIDTH std::bit_width(unsigned(CELL_SIZE))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef INS_SIZE
|
||||||
#define INS_SIZE (CELL_SIZE + 1)
|
#define INS_SIZE (CELL_SIZE + 1)
|
||||||
|
#endif
|
||||||
// mask and opcode
|
// mask and opcode
|
||||||
|
#ifndef INS_BYTECODE_SIZE
|
||||||
#define INS_BYTECODE_SIZE (INS_SIZE + CELL_SIZE_BITWIDTH)
|
#define INS_BYTECODE_SIZE (INS_SIZE + CELL_SIZE_BITWIDTH)
|
||||||
|
#endif
|
||||||
// mask,opcode and sacrifice
|
// mask,opcode and sacrifice
|
||||||
|
|
||||||
|
#ifndef OPCODE_SET1
|
||||||
#define OPCODE_SET1 (static_cast<int>(r8veritas::ins_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))
|
#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 {
|
template <int CELL_SIZE> class r8veritas {
|
||||||
public:
|
public:
|
||||||
@@ -58,6 +95,9 @@ public:
|
|||||||
std::array<std::string, CELL_SIZE> tp_paths;
|
std::array<std::string, CELL_SIZE> tp_paths;
|
||||||
|
|
||||||
private:
|
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;
|
std::array<r8veritas::influence, CELL_SIZE> all_influences;
|
||||||
|
|
||||||
const std::set<r8veritas::ins> all_ins = []() -> std::set<r8veritas::ins> {
|
const std::set<r8veritas::ins> all_ins = []() -> std::set<r8veritas::ins> {
|
||||||
@@ -79,9 +119,6 @@ private:
|
|||||||
return s;
|
return s;
|
||||||
}();
|
}();
|
||||||
|
|
||||||
std::unordered_map<r8veritas::stat_ref,
|
|
||||||
std::pair<r8veritas::stat_ref, r8veritas::ins_bytecode>>
|
|
||||||
visited_op;
|
|
||||||
struct stat_hash {
|
struct stat_hash {
|
||||||
size_t operator()(const stat &s) const {
|
size_t operator()(const stat &s) const {
|
||||||
size_t h = 0;
|
size_t h = 0;
|
||||||
@@ -93,8 +130,22 @@ private:
|
|||||||
return h;
|
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:
|
public:
|
||||||
r8veritas::ins to_ins(const r8veritas::ins_bytecode &bc) {
|
r8veritas::ins to_ins(const r8veritas::ins_bytecode &bc) {
|
||||||
@@ -116,17 +167,18 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
r8veritas::stat_ref generate_empty_stat() {
|
r8veritas::stat_ref generate_empty_stat() {
|
||||||
r8veritas::stat arr;
|
r8veritas::stat arr{};
|
||||||
for (r8veritas::index i = 0; i < (1 << CELL_SIZE); i++) {
|
size_t s = shard_of(arr);
|
||||||
arr[i] = 0;
|
std::lock_guard<std::mutex> g(pool_locks[s]);
|
||||||
}
|
auto tmp_ptr = pool_shards[s].insert(std::move(arr)).first;
|
||||||
auto tmp_ptr = stat_pool.insert(std::move(arr)).first;
|
|
||||||
return &(*tmp_ptr);
|
return &(*tmp_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
// WARNING:ST WILL BE DESTROYED
|
// WARNING:ST WILL BE DESTROYED
|
||||||
std::pair<r8veritas::stat_ref, bool> save_stat(r8veritas::stat &&st) {
|
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};
|
return {&(*(t.first)), !t.second};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,14 +190,14 @@ public:
|
|||||||
out[i] = operation(r8veritas::cell(i));
|
out[i] = operation(r8veritas::cell(i));
|
||||||
}
|
}
|
||||||
return (r8veritas::truthtable(save_stat(std::move(inp)).first,
|
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,
|
// Pure operate: no pool/map side effects. The row loop is lock-free and
|
||||||
bool> // true means the next stat has been visited.
|
// embarrassingly parallel by construction.
|
||||||
operate_ins(const r8veritas::stat_ref &last_stat, const r8veritas::ins &ins) {
|
static r8veritas::stat
|
||||||
auto key = std::pair(last_stat, r8veritas::ins_bytecode(ins));
|
operate_stat(const r8veritas::stat &last, const r8veritas::ins &ins) {
|
||||||
r8veritas::stat next_stat = *last_stat;
|
r8veritas::stat next_stat = last;
|
||||||
int opcode = ins.bc & 1;
|
int opcode = ins.bc & 1;
|
||||||
|
|
||||||
r8veritas::cell cell_mask = (ins.bc >> 1);
|
r8veritas::cell cell_mask = (ins.bc >> 1);
|
||||||
@@ -160,9 +212,16 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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));
|
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;
|
return tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -270,38 +329,142 @@ public:
|
|||||||
return (new_truthtable);
|
return (new_truthtable);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool bfs(const r8veritas::truthtable &truthtable,
|
private:
|
||||||
bool enable_debug_output = false, r8veritas::index layer = 1) {
|
// 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) {
|
// Expand frontier[begin, end), multi-threaded. Each thread appends its
|
||||||
stats.push(truthtable.first);
|
// 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);
|
return (false);
|
||||||
}
|
}
|
||||||
|
|
||||||
r8veritas::index i = stats.size();
|
for (;; ++layer) {
|
||||||
if (enable_debug_output)
|
r8veritas::index fsz = frontier.size();
|
||||||
std::cout << "LAYER=" << layer << ",stats_size=" << i << std::endl;
|
if (enable_debug_output)
|
||||||
r8veritas::stat_ref tmp_stat_ref;
|
std::cout << "LAYER=" << layer << ",stats_size=" << fsz << std::endl;
|
||||||
std::pair<stat_ref, bool> ans;
|
|
||||||
for (; i > 0; i--) {
|
if (fsz < PARALLEL_THRESHOLD || nthreads <= 1) {
|
||||||
tmp_stat_ref = stats.front();
|
if (expand_serial(0, fsz, truthtable)) {
|
||||||
for (auto j : r8veritas::all_ins) {
|
return true;
|
||||||
ans = operate_ins(tmp_stat_ref, j);
|
|
||||||
if (ans.first == truthtable.second) {
|
|
||||||
while (!stats.empty())
|
|
||||||
stats.pop();
|
|
||||||
return (true);
|
|
||||||
}
|
}
|
||||||
if (!ans.second) {
|
} else {
|
||||||
stats.push(ans.first);
|
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) {
|
r8veritas::path track_path(const r8veritas::truthtable &truthtable) {
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
@@ -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
|
||||||
|
}
|
||||||
Reference in new issue
Block a user