Skip to content

Commit

Permalink
Fix static initialization order dependency in stim_benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
Strilanc committed Oct 16, 2023
1 parent 7bb8fb7 commit 5d25ef6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
16 changes: 11 additions & 5 deletions src/stim/benchmark_main.perf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
using namespace stim;

RegisteredBenchmark *running_benchmark = nullptr;
std::vector<RegisteredBenchmark> all_registered_benchmarks{};
std::vector<RegisteredBenchmark> *all_registered_benchmarks_data = nullptr;
uint64_t registry_initialized = 0;

/// Describe quantity as an SI-prefixed value with two significant figures.
std::string si2(double val) {
Expand Down Expand Up @@ -83,14 +84,14 @@ void find_benchmarks(const std::string &filter, std::vector<RegisteredBenchmark>

if (!filter.empty() && filter[filter.size() - 1] == '*') {
std::string start = filter.substr(0, filter.size() - 1);
for (const auto &benchmark : all_registered_benchmarks) {
for (const auto &benchmark : *all_registered_benchmarks_data) {
if (benchmark.name.substr(0, start.size()) == start) {
out.push_back(benchmark);
found = true;
}
}
} else {
for (const auto &benchmark : all_registered_benchmarks) {
for (const auto &benchmark : *all_registered_benchmarks_data) {
if (benchmark.name == filter) {
out.push_back(benchmark);
found = true;
Expand All @@ -100,7 +101,7 @@ void find_benchmarks(const std::string &filter, std::vector<RegisteredBenchmark>

if (!found) {
std::cerr << "No benchmark matching filter '" << filter << "'. Available benchmarks are:\n";
for (auto &benchmark : all_registered_benchmarks) {
for (auto &benchmark : *all_registered_benchmarks_data) {
std::cerr << " " << benchmark.name << "\n";
}
exit(EXIT_FAILURE);
Expand All @@ -116,7 +117,7 @@ int main(int argc, const char **argv) {

std::vector<RegisteredBenchmark> chosen_benchmarks;
if (only == nullptr) {
chosen_benchmarks = all_registered_benchmarks;
chosen_benchmarks = *all_registered_benchmarks_data;
} else {
std::string filter_text = only;
std::vector<std::string> filters{};
Expand Down Expand Up @@ -180,5 +181,10 @@ int main(int argc, const char **argv) {
}
}
}

if (all_registered_benchmarks_data != nullptr) {
delete all_registered_benchmarks_data;
all_registered_benchmarks_data = nullptr;
}
return 0;
}
12 changes: 10 additions & 2 deletions src/stim/benchmark_util.perf.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,21 @@ struct RegisteredBenchmark {
std::vector<BenchmarkResult> results;
};
extern RegisteredBenchmark *running_benchmark;
extern std::vector<RegisteredBenchmark> all_registered_benchmarks;
extern std::vector<RegisteredBenchmark> *all_registered_benchmarks_data;
extern uint64_t registry_initialized;
inline void add_benchmark(RegisteredBenchmark benchmark) {
if (all_registered_benchmarks_data == nullptr || registry_initialized != 4620243525989388168ULL) {
registry_initialized = 4620243525989388168ULL;
all_registered_benchmarks_data = new std::vector<RegisteredBenchmark>();
}
all_registered_benchmarks_data->push_back(benchmark);
}

#define BENCHMARK(name) \
void BENCH_##name##_METHOD(); \
struct BENCH_STARTUP_TYPE_##name { \
BENCH_STARTUP_TYPE_##name() { \
all_registered_benchmarks.push_back({#name, BENCH_##name##_METHOD}); \
add_benchmark({#name, BENCH_##name##_METHOD}); \
} \
}; \
static BENCH_STARTUP_TYPE_##name BENCH_STARTUP_INSTANCE_##name; \
Expand Down

0 comments on commit 5d25ef6

Please sign in to comment.