Created
November 26, 2022 19:48
-
-
Save L0laapk3/049215bc02e5434b55528955e3e29f11 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <benchmark/benchmark.h> | |
#include <limits> | |
#include <random> | |
#include <chrono> | |
#include <iostream> | |
// based on https://github.com/BUHPCv2/PerfDemo/blob/master/CacheBench.cpp | |
template<typename data_t> | |
static void cacheBench(benchmark::State &state) | |
{ | |
uint32_t bytes = 1 << state.range(0); | |
uint32_t access_size = bytes / sizeof(uint32_t) / 2; | |
uint32_t data_size = bytes / sizeof(data_t); | |
std::vector<data_t> data; | |
data.reserve(data_size); | |
std::vector<uint32_t> indices; | |
indices.reserve(access_size); | |
// rng for the data and indices | |
std::default_random_engine random_engine ( | |
std::chrono::steady_clock::now().time_since_epoch().count() | |
); | |
std::uniform_int_distribution<data_t> data_dist { | |
std::numeric_limits<data_t>::min(), | |
std::numeric_limits<data_t>::max() | |
}; | |
std::uniform_int_distribution<uint32_t> idx_dist {0, data_size}; | |
// insert random data into the vector and random indexes to the index array | |
for (auto i = 0; i < data_size; ++i) | |
data.push_back(data_dist(random_engine)); | |
// now pick random indices and insert into index array | |
for (auto i = 0; i < access_size; ++i) | |
indices.push_back(idx_dist(random_engine)); | |
// thrash all cache!! | |
while (state.KeepRunning()) | |
{ | |
auto sum = 0LL; | |
for (auto i : indices) | |
sum += data[i]; | |
benchmark::DoNotOptimize(sum); | |
__asm__ volatile | |
("" : : "g"(sum) : "memory"); | |
} | |
state.SetItemsProcessed((long long)state.iterations() * data_size * sizeof(data[0])); | |
state.SetLabel("\t" + (bytes >> 20 == 0 ? std::to_string(bytes >> 10) + "KB" : std::to_string(bytes >> 20) + "MB")); | |
} | |
BENCHMARK(cacheBench<uint64_t>)->Threads(1)->DenseRange(24, 31)->ReportAggregatesOnly(true); | |
BENCHMARK(cacheBench<uint32_t>)->Threads(1)->DenseRange(24, 31)->ReportAggregatesOnly(true); | |
BENCHMARK_MAIN(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment