From b35769167fbaed621959902aaa50d596ecb1f87b Mon Sep 17 00:00:00 2001 From: Thomas Applencourt Date: Mon, 24 Jul 2023 23:27:11 +0000 Subject: [PATCH] Add bench --- .secret_cpp/.clang-format | 1 - .secret_cpp/include/qpx.hpp | 58 +++++++++++++++++++ .secret_cpp/main.cpp | 109 ++++++++++++++++++++++++++++++++++++ .secret_cpp/not_main.cpp | 40 +------------ data/f2_631g.10det.wf | 1 - 5 files changed, 169 insertions(+), 40 deletions(-) create mode 100644 .secret_cpp/include/qpx.hpp create mode 100644 .secret_cpp/main.cpp diff --git a/.secret_cpp/.clang-format b/.secret_cpp/.clang-format index 9d0fc47..2dc9354 100644 --- a/.secret_cpp/.clang-format +++ b/.secret_cpp/.clang-format @@ -73,7 +73,6 @@ IndentWrappedFunctionNames: false JavaScriptQuotes: Leave JavaScriptWrapImports: true KeepEmptyLinesAtTheStartOfBlocks: false -Language: Cpp MacroBlockBegin: '' MacroBlockEnd: '' MaxEmptyLinesToKeep: 2 diff --git a/.secret_cpp/include/qpx.hpp b/.secret_cpp/include/qpx.hpp new file mode 100644 index 0000000..da29ca1 --- /dev/null +++ b/.secret_cpp/include/qpx.hpp @@ -0,0 +1,58 @@ +#include +#include +#include + + +typedef sul::dynamic_bitset<> spin_det_t; + +template<> +struct std::hash { + std::size_t operator()(spin_det_t const& s) const noexcept { + return std::hash()(s.to_string()); + } +}; +#define N_SPIN_SPECIES 2 +//typedef std::array det_t; + +class det_t { // The class + + public: // Access specifier + spin_det_t alpha; + spin_det_t beta; + + det_t(spin_det_t _alpha, spin_det_t _beta) { + alpha = _alpha; + beta = _beta; + } + + bool operator < ( const det_t &b ) const { + return (alpha < b.alpha) && (beta < b.beta) ; + } + + bool operator == ( const det_t &b ) const { + return (alpha == b.alpha) && (beta == b.beta) ; + } + +}; + +template<> +struct std::hash { + std::size_t operator()(det_t const& s) const noexcept { + std::size_t h1 = std::hash{}(s.alpha); + std::size_t h2 = std::hash{}(s.beta); + return h1 ^ (h2 << 1); + } +}; +std::ostream& operator<<(std::ostream& os, const det_t& obj) { + return os << "(" << obj.alpha << "," << obj.beta << ")"; +} + +typedef sul::dynamic_bitset<> spin_occupancy_mask_t; +typedef std::array occupancy_mask_t; + +typedef sul::dynamic_bitset<> spin_unoccupancy_mask_t; +typedef std::array unoccupancy_mask_t; + +typedef std::array eri_4idx_t; + + diff --git a/.secret_cpp/main.cpp b/.secret_cpp/main.cpp new file mode 100644 index 0000000..98f0abe --- /dev/null +++ b/.secret_cpp/main.cpp @@ -0,0 +1,109 @@ +#include +#include +#include +#include +#include "qpx.hpp" +#include +#include + +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 setup() { + std::vector 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& psi) { + for (auto &det : psi) { + if (det == target) + return true; + } + return false; +} + +bool get_element_binary_search(det_t target, std::vector &psi) { + return std::binary_search(psi.begin(), psi.end(), target); +} + +bool get_element_hash(det_t target, std::unordered_set &psi) { + return psi.find(target) != psi.end(); +} + +static void NaiveLookup(benchmark::State& state) { + auto psi = setup(); + std::sort(psi.begin(), psi.end()); + + + std::vector 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) + get_element_naive(d, psi); + } + state.counters["LookupRate"] = benchmark::Counter(psi.size(), benchmark::Counter::kIsRate); + +} + +static void BinarySearchLookup(benchmark::State& state) { + auto psi = setup(); + std::sort(psi.begin(), psi.end()); + + + std::vector 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) + get_element_binary_search(d, psi); + } + state.counters["LookupRate"] = benchmark::Counter(psi.size(), benchmark::Counter::kIsRate); +} + +static void HashLookup(benchmark::State& state) { + auto psi = setup(); + std::unordered_set psi_m; + for (auto d: psi) { + psi_m.insert(d); + } + + std::vector 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) + get_element_hash(d, psi_m); + } + state.counters["LookupRate"] = benchmark::Counter(psi.size(), benchmark::Counter::kIsRate); +} + +// Register the function as a benchmark +BENCHMARK(NaiveLookup); +BENCHMARK(BinarySearchLookup); +BENCHMARK(HashLookup); +// Run the benchmark +BENCHMARK_MAIN(); + diff --git a/.secret_cpp/not_main.cpp b/.secret_cpp/not_main.cpp index 1226e2c..68a3c70 100644 --- a/.secret_cpp/not_main.cpp +++ b/.secret_cpp/not_main.cpp @@ -8,43 +8,7 @@ #include #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN #include - -typedef sul::dynamic_bitset<> spin_det_t; - -template<> -struct std::hash { - std::size_t operator()(spin_det_t const& s) const noexcept { - return std::hash()(s.to_string()); - } -}; - -#define N_SPIN_SPECIES 2 -// enum spin_type_e { -// ALPHA = 0, -// BETA = 1, -// N_SPIN_SPECIES, -// }; - -typedef std::array det_t; -template<> -struct std::hash { - std::size_t operator()(det_t const& s) const noexcept { - std::size_t h1 = std::hash{}(s[0]); - std::size_t h2 = std::hash{}(s[1]); - return h1 ^ (h2 << 1); - } -}; -std::ostream& operator<<(std::ostream& os, const det_t& obj) { - return os << "(" << obj[0] << "," << obj[1] << ")"; -} - -typedef sul::dynamic_bitset<> spin_occupancy_mask_t; -typedef std::array occupancy_mask_t; - -typedef sul::dynamic_bitset<> spin_unoccupancy_mask_t; -typedef std::array unoccupancy_mask_t; - -typedef std::array eri_4idx_t; +#include // Utils @@ -337,7 +301,7 @@ void category_D_iiil(uint64_t N_orb, eri_4idx_t idx, std::vector& psi, // aα aβ <- aα bβ for(size_t spin_ph = 0; spin_ph < N_SPIN_SPECIES; spin_ph++) { size_t spin_aa = !spin_ph; - for(size_t exc_fwd = 0; exc_fwd < 2; exc_fwd++) { + for(size_t exc_fwd = 0; exc_fwd < N_SPIN_SPECIES; exc_fwd++) { size_t exc_rev = !exc_fwd; auto p = ph[exc_fwd]; auto h = ph[exc_rev]; diff --git a/data/f2_631g.10det.wf b/data/f2_631g.10det.wf index df7d4bf..2a09630 100644 --- a/data/f2_631g.10det.wf +++ b/data/f2_631g.10det.wf @@ -37,4 +37,3 @@ -0.0306196143382 ++++++-+-+------+----------------------------------------------- +++++++++------------------------------------------------------- -