Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single read index #10

Merged
merged 18 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions relaxed_concurrent_fifo/atomic_bitset.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,21 @@ constexpr bool set_bit_atomic(std::atomic<T>& data, std::size_t index, std::memo
}
}

template <typename T>
struct cache_aligned_t {
alignas(std::hardware_destructive_interference_size) std::atomic<T> atomic;
std::atomic<T>* operator->() { return &atomic; }
const std::atomic<T>* operator->() const { return &atomic; }
operator std::atomic<T>& () { return atomic; }
operator const std::atomic<T>& () const { return atomic; }
};

template <std::size_t N, typename ARR_TYPE = uint8_t>
class atomic_bitset {
private:
struct wrapper {
alignas(std::hardware_destructive_interference_size) std::atomic<ARR_TYPE> atomic;
std::atomic<ARR_TYPE>* operator->() { return &atomic; }
const std::atomic<ARR_TYPE>* operator->() const { return &atomic; }
operator std::atomic<ARR_TYPE>&() { return atomic; }
operator const std::atomic<ARR_TYPE>&() const { return atomic; }
};

static constexpr std::size_t bit_count = sizeof(ARR_TYPE) * 8;
static constexpr std::size_t array_members = N / bit_count;
std::array<wrapper, array_members> data;
std::array<cache_aligned_t<ARR_TYPE>, array_members> data;

// This requirement could be lifted in exchange for a more complicated implementation of the claim bit function.
static_assert(N % bit_count == 0, "Bit count must be dividable by size of array type!");
Expand Down
27 changes: 17 additions & 10 deletions relaxed_concurrent_fifo/benchmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ struct benchmark_bfs : benchmark_timed<> {
long long pushed_nodes{ 0 };
long long ignored_nodes{ 0 };
long long processed_nodes{ 0 };
bool err{ false };
};

#ifdef __GNUC__
Expand Down Expand Up @@ -331,7 +332,7 @@ struct benchmark_bfs : benchmark_timed<> {
while (d < old_d) {
if (distances[target].value.compare_exchange_weak(old_d, d, std::memory_order_relaxed)) {
if (!handle.push((static_cast<std::uint64_t>(d) << 32) | target)) {
throw std::runtime_error("Push failed!");
counter.err = true;
}
++counter.pushed_nodes;
break;
Expand Down Expand Up @@ -368,9 +369,15 @@ struct benchmark_bfs : benchmark_timed<> {
sum.pushed_nodes += counter.pushed_nodes;
sum.processed_nodes += counter.processed_nodes;
sum.ignored_nodes += counter.ignored_nodes;
sum.err |= counter.err;
return sum;
});

if (total_counts.err) {
stream << "ERR";
return;
}

auto longest_distance =
std::max_element(distances.begin(), distances.end(), [](auto const& a, auto const& b) {
auto a_val = a.value.load(std::memory_order_relaxed);
Expand Down Expand Up @@ -506,15 +513,15 @@ class benchmark_provider_relaxed : public benchmark_provider<BENCHMARK> {

BENCHMARK test(const benchmark_info& info, double prefill_amount) const override {
switch (info.num_threads) {
case 1: return test_helper<block_based_queue<std::size_t, 1 * BLOCK_MULTIPLIER, CELLS_PER_BLOCK, BITSET_TYPE>>(info, prefill_amount);
case 2: return test_helper<block_based_queue<std::size_t, 2 * BLOCK_MULTIPLIER, CELLS_PER_BLOCK, BITSET_TYPE>>(info, prefill_amount);
case 4: return test_helper<block_based_queue<std::size_t, 4 * BLOCK_MULTIPLIER, CELLS_PER_BLOCK, BITSET_TYPE>>(info, prefill_amount);
case 8: return test_helper<block_based_queue<std::size_t, 8 * BLOCK_MULTIPLIER, CELLS_PER_BLOCK, BITSET_TYPE>>(info, prefill_amount);
case 16: return test_helper<block_based_queue<std::size_t, 16 * BLOCK_MULTIPLIER, CELLS_PER_BLOCK, BITSET_TYPE>>(info, prefill_amount);
case 32: return test_helper<block_based_queue<std::size_t, 32 * BLOCK_MULTIPLIER, CELLS_PER_BLOCK, BITSET_TYPE>>(info, prefill_amount);
case 64: return test_helper<block_based_queue<std::size_t, 64 * BLOCK_MULTIPLIER, CELLS_PER_BLOCK, BITSET_TYPE>>(info, prefill_amount);
case 128: return test_helper<block_based_queue<std::size_t, 128 * BLOCK_MULTIPLIER, CELLS_PER_BLOCK, BITSET_TYPE>>(info, prefill_amount);
case 256: return test_helper<block_based_queue<std::size_t, 256 * BLOCK_MULTIPLIER, CELLS_PER_BLOCK, BITSET_TYPE>>(info, prefill_amount);
case 1: return test_helper<bbq_min_block_count<std::size_t, 1 * BLOCK_MULTIPLIER, CELLS_PER_BLOCK, BITSET_TYPE>>(info, prefill_amount);
case 2: return test_helper<bbq_min_block_count<std::size_t, 2 * BLOCK_MULTIPLIER, CELLS_PER_BLOCK, BITSET_TYPE>>(info, prefill_amount);
case 4: return test_helper<bbq_min_block_count<std::size_t, 4 * BLOCK_MULTIPLIER, CELLS_PER_BLOCK, BITSET_TYPE>>(info, prefill_amount);
case 8: return test_helper<bbq_min_block_count<std::size_t, 8 * BLOCK_MULTIPLIER, CELLS_PER_BLOCK, BITSET_TYPE>>(info, prefill_amount);
case 16: return test_helper<bbq_min_block_count<std::size_t, 16 * BLOCK_MULTIPLIER, CELLS_PER_BLOCK, BITSET_TYPE>>(info, prefill_amount);
case 32: return test_helper<bbq_min_block_count<std::size_t, 32 * BLOCK_MULTIPLIER, CELLS_PER_BLOCK, BITSET_TYPE>>(info, prefill_amount);
case 64: return test_helper<bbq_min_block_count<std::size_t, 64 * BLOCK_MULTIPLIER, CELLS_PER_BLOCK, BITSET_TYPE>>(info, prefill_amount);
case 128: return test_helper<bbq_min_block_count<std::size_t, 128 * BLOCK_MULTIPLIER, CELLS_PER_BLOCK, BITSET_TYPE>>(info, prefill_amount);
case 256: return test_helper<bbq_min_block_count<std::size_t, 256 * BLOCK_MULTIPLIER, CELLS_PER_BLOCK, BITSET_TYPE>>(info, prefill_amount);
default: throw std::runtime_error("Unsupported thread count!");
}
}
Expand Down
Loading