216 lines
6.2 KiB
C++
216 lines
6.2 KiB
C++
#include "catch_amalgamated.hpp"
|
|
|
|
#include <bitserial/bitserial.hpp>
|
|
|
|
#include <algorithm>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <iomanip>
|
|
#include <iostream>
|
|
#include <random>
|
|
#include <span>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
|
|
constexpr std::size_t kElementCount = 1024 * 1024;
|
|
|
|
struct BandwidthBenchmark {
|
|
std::string name;
|
|
std::size_t bytes_per_operation;
|
|
};
|
|
|
|
std::vector<BandwidthBenchmark> &bandwidth_benchmarks() {
|
|
static std::vector<BandwidthBenchmark> benchmarks;
|
|
return benchmarks;
|
|
}
|
|
|
|
void record_bandwidth_benchmark(
|
|
std::string const &name,
|
|
std::size_t bytes_per_operation
|
|
) {
|
|
auto &benchmarks = bandwidth_benchmarks();
|
|
auto it = std::ranges::find(benchmarks, name, &BandwidthBenchmark::name);
|
|
if (it == benchmarks.end()) {
|
|
benchmarks.push_back({name, bytes_per_operation});
|
|
} else {
|
|
it->bytes_per_operation = bytes_per_operation;
|
|
}
|
|
}
|
|
|
|
std::size_t bytes_per_operation_for(std::string const &name) {
|
|
auto const &benchmarks = bandwidth_benchmarks();
|
|
auto it = std::ranges::find(benchmarks, name, &BandwidthBenchmark::name);
|
|
return it == benchmarks.end() ? 0 : it->bytes_per_operation;
|
|
}
|
|
|
|
struct ScaledBandwidth {
|
|
double value;
|
|
char const *unit;
|
|
};
|
|
|
|
ScaledBandwidth scale_bandwidth(double bytes_per_second) {
|
|
char const *units[] = {"B/s", "KB/s", "MB/s", "GB/s", "TB/s"};
|
|
std::size_t unit_index = 0;
|
|
while (bytes_per_second >= 1000.0 && unit_index + 1 < std::size(units)) {
|
|
bytes_per_second /= 1000.0;
|
|
++unit_index;
|
|
}
|
|
return {bytes_per_second, units[unit_index]};
|
|
}
|
|
|
|
int bandwidth_precision(double value) {
|
|
if (value >= 100.0) {
|
|
return 1;
|
|
}
|
|
if (value >= 10.0) {
|
|
return 2;
|
|
}
|
|
return 3;
|
|
}
|
|
|
|
class BandwidthReporter : public Catch::EventListenerBase {
|
|
public:
|
|
using Catch::EventListenerBase::EventListenerBase;
|
|
|
|
void benchmarkEnded(Catch::BenchmarkStats<> const &stats) override {
|
|
auto const bytes_per_operation = bytes_per_operation_for(stats.info.name);
|
|
auto const seconds_per_operation = stats.mean.point.count() * 1e-9;
|
|
if (bytes_per_operation == 0 || seconds_per_operation <= 0.0) {
|
|
return;
|
|
}
|
|
|
|
auto const bandwidth =
|
|
scale_bandwidth(static_cast<double>(bytes_per_operation) /
|
|
seconds_per_operation);
|
|
auto const old_flags = std::cout.flags();
|
|
auto const old_precision = std::cout.precision();
|
|
std::cout << "\n bandwidth: " << std::fixed
|
|
<< std::setprecision(bandwidth_precision(bandwidth.value))
|
|
<< bandwidth.value << ' ' << bandwidth.unit << " ("
|
|
<< bytes_per_operation << " bytes/op)\n";
|
|
std::cout.flags(old_flags);
|
|
std::cout.precision(old_precision);
|
|
}
|
|
};
|
|
|
|
CATCH_REGISTER_LISTENER(BandwidthReporter);
|
|
|
|
template <std::size_t Bits>
|
|
struct BaqFixture {
|
|
static constexpr std::uint8_t max_value =
|
|
static_cast<std::uint8_t>((1u << Bits) - 1u);
|
|
|
|
std::vector<std::uint8_t> src;
|
|
std::vector<std::uint8_t> packed_ref;
|
|
std::vector<std::uint8_t> packed_bulk;
|
|
std::vector<std::uint8_t> unpacked_ref;
|
|
std::vector<std::uint8_t> unpacked_bulk;
|
|
|
|
explicit BaqFixture(std::size_t n = kElementCount) :
|
|
src(n),
|
|
packed_ref((n * Bits + 7) / 8, 0),
|
|
packed_bulk((n * Bits + 7) / 8, 0),
|
|
unpacked_ref(n),
|
|
unpacked_bulk(n) {
|
|
std::mt19937 gen(0xB17u + static_cast<unsigned>(Bits));
|
|
std::uniform_int_distribution<std::uint16_t> dist(0, max_value);
|
|
for (std::uint8_t &x : src) {
|
|
x = static_cast<std::uint8_t>(dist(gen));
|
|
}
|
|
|
|
pack_reference(src, packed_ref);
|
|
bitserial::pack_bits<Bits>(src, packed_bulk);
|
|
REQUIRE(packed_bulk == packed_ref);
|
|
|
|
unpack_reference(packed_ref, unpacked_ref);
|
|
bitserial::unpack_bits<Bits>(packed_ref, unpacked_bulk);
|
|
REQUIRE(unpacked_bulk == unpacked_ref);
|
|
}
|
|
|
|
static void pack_reference(
|
|
std::span<const std::uint8_t> in,
|
|
std::span<std::uint8_t> out
|
|
) noexcept {
|
|
std::size_t bit_pos = 0;
|
|
for (const std::uint8_t x : in) {
|
|
bitserial::write_bits(out, bit_pos, Bits, x & max_value);
|
|
bit_pos += Bits;
|
|
}
|
|
}
|
|
|
|
static void unpack_reference(
|
|
std::span<const std::uint8_t> in,
|
|
std::span<std::uint8_t> out
|
|
) noexcept {
|
|
std::size_t bit_pos = 0;
|
|
for (std::uint8_t &x : out) {
|
|
x = static_cast<std::uint8_t>(bitserial::read_bits(in, bit_pos, Bits));
|
|
bit_pos += Bits;
|
|
}
|
|
}
|
|
};
|
|
|
|
template <std::size_t Bits>
|
|
void check_edges() {
|
|
for (std::size_t n = 0; n <= 257; ++n) {
|
|
BaqFixture<Bits> f(n);
|
|
CAPTURE(Bits, n);
|
|
CHECK(f.packed_bulk == f.packed_ref);
|
|
CHECK(f.unpacked_bulk == f.unpacked_ref);
|
|
}
|
|
}
|
|
|
|
template <std::size_t Bits>
|
|
void run_benchmarks(char const *label) {
|
|
BaqFixture<Bits> f;
|
|
CAPTURE(label, Bits, f.src.size(), f.packed_ref.size());
|
|
auto const bytes_per_operation = f.src.size() + f.packed_ref.size();
|
|
|
|
auto const pack_reference_name =
|
|
std::string(label) + " pack read/write_bits loop";
|
|
auto const pack_bulk_name = std::string(label) + " pack bitserial bulk";
|
|
auto const unpack_reference_name =
|
|
std::string(label) + " unpack read_bits loop";
|
|
auto const unpack_bulk_name = std::string(label) + " unpack bitserial bulk";
|
|
|
|
record_bandwidth_benchmark(pack_reference_name, bytes_per_operation);
|
|
BENCHMARK(std::string(pack_reference_name)) {
|
|
BaqFixture<Bits>::pack_reference(f.src, f.packed_ref);
|
|
return f.packed_ref.front();
|
|
};
|
|
|
|
record_bandwidth_benchmark(pack_bulk_name, bytes_per_operation);
|
|
BENCHMARK(std::string(pack_bulk_name)) {
|
|
bitserial::pack_bits<Bits>(f.src, f.packed_bulk);
|
|
return f.packed_bulk.front();
|
|
};
|
|
|
|
record_bandwidth_benchmark(unpack_reference_name, bytes_per_operation);
|
|
BENCHMARK(std::string(unpack_reference_name)) {
|
|
BaqFixture<Bits>::unpack_reference(f.packed_ref, f.unpacked_ref);
|
|
return f.unpacked_ref.front();
|
|
};
|
|
|
|
record_bandwidth_benchmark(unpack_bulk_name, bytes_per_operation);
|
|
BENCHMARK(std::string(unpack_bulk_name)) {
|
|
bitserial::unpack_bits<Bits>(f.packed_ref, f.unpacked_bulk);
|
|
return f.unpacked_bulk.front();
|
|
};
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST_CASE("BAQ bulk pack/unpack edge cases", "[baq][bulk]") {
|
|
check_edges<3>();
|
|
check_edges<4>();
|
|
check_edges<5>();
|
|
}
|
|
|
|
TEST_CASE("BAQ bulk benchmarks", "[!benchmark][baq][bulk]") {
|
|
run_benchmarks<3>("3:8");
|
|
run_benchmarks<4>("4:8");
|
|
run_benchmarks<5>("5:8");
|
|
}
|