feat: multi thread

This commit is contained in:
tqcq 2024-10-24 04:29:15 +00:00
parent c9c6095c76
commit ba58f0b485

View File

@ -10,9 +10,14 @@
#include <time.h>
#include <algorithm>
#include <atomic>
#include <bitset>
#include <condition_variable>
#include <functional>
#include <mutex>
#include <queue>
#include <set>
#include <thread>
#include <vector>
#include "rev-mc.h"
@ -296,28 +301,50 @@ find_row_mask(std::vector<set_t> &sets,
}
uint64_t row_mask = LS_BITMASK(16);// use 16 bits for the row
uint64_t last_mask = (row_mask << (40 - 16));
const uint64_t last_mask = (row_mask << (40 - 16));
row_mask <<= CL_SHIFT;// skip the lowest 6 bits since they're used for CL addressing
while (row_mask < last_mask) {
uint64_t next_row_mask = next_bit_permutation(row_mask);
if (row_mask & LS_BITMASK(CL_SHIFT)) {
row_mask = next_row_mask;
continue;
}
auto resolve = [=](uint64_t row_mask) -> bool {
if (row_mask & LS_BITMASK(CL_SHIFT)) { return false; }
for (auto addr_pool : same_row_sets) {
addr_tuple base_addr = addr_pool[0];
for (int i = 1; i < addr_pool.size(); i++) {
addr_tuple tmp = addr_pool[i];
if ((tmp.p_addr & row_mask) != (base_addr.p_addr & row_mask)) { goto next_mask; }
if ((tmp.p_addr & row_mask) != (base_addr.p_addr & row_mask)) { return false; }
}
}
return true;
};
break;
std::atomic_bool found{false};
std::mutex lock;
std::queue<uint64_t> mask_task;
next_mask:
row_mask = next_row_mask;
while (row_mask < last_mask) {
mask_task.push(row_mask);
row_mask = next_bit_permutation(row_mask);
}
std::vector<std::thread> worker;
for (int i = 0; i < 8; ++i) {
worker.emplace_back([&] {
while (!found) {
uint64_t cur_mask;
{
std::lock_guard<std::mutex> _(lock);
if (mask_task.empty()) { break; }
cur_mask = mask_task.front();
mask_task.pop();
}
if (resolve(cur_mask)) {
std::lock_guard<std::mutex> _(lock);
found.store(true);
row_mask = cur_mask;
while (!mask_task.empty()) mask_task.pop();
}
}
});
}
// super hackish way to recover the real row mask