Skip to content

Commit

Permalink
Super fast binaries_search
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Applencourt committed Jul 25, 2023
1 parent b357691 commit 868d484
Show file tree
Hide file tree
Showing 8 changed files with 3,108 additions and 129 deletions.
147 changes: 147 additions & 0 deletions .secret_cpp/bench_psi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@

#include <random>
#include <algorithm>
#include "qpx.hpp"
#include "psi.hpp"
#include <benchmark/benchmark.h>
#include <unordered_set>
#include <tsl/hopscotch_set.h>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <fstream>

spin_det_t parse_line(std::string s) {
spin_det_t sd;
for(int i = 0; i < s.size(); i++) {
if(s[i] == '-') sd.push_back(0);
if(s[i] == '+') sd.push_back(1);
}
return sd;
}

std::vector<det_t> setup() {
std::vector<det_t> psi;

std::ifstream fs("/home/applenco/QuantumEnvelope/data/c2_eq_hf_dz_15.101224det.wf");
while(true) {
std::string coef_str_or_empty;
if(!std::getline(fs, coef_str_or_empty)) break;

std::string alpha_str;
std::getline(fs, alpha_str);
parse_line(alpha_str);
std::string beta_str;
std::getline(fs, beta_str);
std::string emptyline_str;
std::getline(fs, emptyline_str);

psi.push_back({parse_line(alpha_str), parse_line(beta_str)});
}
return psi;
}

bool get_element_naive(det_t target, std::vector<det_t>& psi) {
for(auto& det : psi) {
if(det == target) return true;
}
return false;
}

static void NaiveLookup(benchmark::State& state) {
auto psi = setup();
std::sort(psi.begin(), psi.end());


std::vector<det_t> psi_random{psi.begin(), psi.end()};
std::shuffle(psi_random.begin(), psi_random.end(), std::default_random_engine{});

for(auto _ : state) {
for(auto d : psi_random) {
bool f = get_element_naive(d, psi);
benchmark::DoNotOptimize(f);
}
}
state.counters["LookupRate"] =
benchmark::Counter(psi.size(), benchmark::Counter::kIsIterationInvariantRate);
}

static void BinarySearchLookup(benchmark::State& state) {
auto psi = setup();
std::sort(psi.begin(), psi.end());


std::vector<det_t> psi_random{psi.begin(), psi.end()};
std::shuffle(psi_random.begin(), psi_random.end(), std::default_random_engine{});

for(auto _ : state) {
for(auto d : psi_random) {
bool f = std::binary_search(psi.begin(), psi.end(), d);
benchmark::DoNotOptimize(f);
}
}
state.counters["LookupRate"] =
benchmark::Counter(psi.size(), benchmark::Counter::kIsIterationInvariantRate);
}

static void BinarySearchesLookup(benchmark::State& state) {
auto psi = setup();
std::sort(psi.begin(), psi.end());

std::vector<det_t> vals{psi.begin(), psi.end()};
std::vector<bool> result(vals.size());

for(auto _ : state) {
binary_searchs(psi.begin(), psi.end(), vals.begin(), vals.end(), vals.begin() + vals.size() / 2,
result.begin() + vals.size() / 2);
}

state.counters["LookupRate"] =
benchmark::Counter(psi.size(), benchmark::Counter::kIsIterationInvariantRate);
}


static void stdHashLookup(benchmark::State& state) {
auto psi = setup();
std::unordered_set<det_t> psi_s;
for(auto d : psi) { psi_s.insert(d); }

std::vector<det_t> psi_random{psi.begin(), psi.end()};
std::shuffle(psi_random.begin(), psi_random.end(), std::default_random_engine{});

for(auto _ : state) {
for(auto d : psi_random) {
bool f = (psi_s.find(d) != psi_s.end());
benchmark::DoNotOptimize(f);
}
}
state.counters["LookupRate"] =
benchmark::Counter(psi.size(), benchmark::Counter::kIsIterationInvariantRate);
}

static void tslHashLookup(benchmark::State& state) {
auto psi = setup();
tsl::hopscotch_set<det_t> psi_s;
for(auto d : psi) { psi_s.insert(d); }

std::vector<det_t> psi_random{psi.begin(), psi.end()};
std::shuffle(psi_random.begin(), psi_random.end(), std::default_random_engine{});

for(auto _ : state) {
for(auto d : psi_random) {
bool f = (psi_s.find(d) != psi_s.end());
benchmark::DoNotOptimize(f);
}
}
state.counters["LookupRate"] =
benchmark::Counter(psi.size(), benchmark::Counter::kIsIterationInvariantRate);
}

// Register the function as a benchmark
//BENCHMARK(NaiveLookup);
BENCHMARK(BinarySearchLookup);
BENCHMARK(BinarySearchesLookup);
BENCHMARK(stdHashLookup);
BENCHMARK(tslHashLookup);
// Run the benchmark
BENCHMARK_MAIN();
1 change: 0 additions & 1 deletion .secret_cpp/not_main.cpp → .secret_cpp/category.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ spin_det_t vec_to_spin_det(std::vector<T> idx, size_t n_orb) {


// Phase

spin_det_t get_phase_mask(spin_det_t p) {
size_t i = 0;
while(true) {
Expand Down
Loading

0 comments on commit 868d484

Please sign in to comment.