Skip to content

Commit

Permalink
Add bench
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Applencourt committed Jul 24, 2023
1 parent 80af942 commit b357691
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 40 deletions.
1 change: 0 additions & 1 deletion .secret_cpp/.clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
Language: Cpp
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 2
Expand Down
58 changes: 58 additions & 0 deletions .secret_cpp/include/qpx.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <array>
#include <sul/dynamic_bitset.hpp>
#include <unordered_map>


typedef sul::dynamic_bitset<> spin_det_t;

template<>
struct std::hash<spin_det_t> {
std::size_t operator()(spin_det_t const& s) const noexcept {
return std::hash<std::string>()(s.to_string());
}
};
#define N_SPIN_SPECIES 2
//typedef std::array<spin_det_t, 2> 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<det_t> {
std::size_t operator()(det_t const& s) const noexcept {
std::size_t h1 = std::hash<spin_det_t>{}(s.alpha);
std::size_t h2 = std::hash<spin_det_t>{}(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<spin_occupancy_mask_t, N_SPIN_SPECIES> occupancy_mask_t;

typedef sul::dynamic_bitset<> spin_unoccupancy_mask_t;
typedef std::array<spin_unoccupancy_mask_t, N_SPIN_SPECIES> unoccupancy_mask_t;

typedef std::array<uint64_t, 4> eri_4idx_t;


109 changes: 109 additions & 0 deletions .secret_cpp/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include <iostream>
#include <fstream>
#include <random>
#include <algorithm>
#include "qpx.hpp"
#include <benchmark/benchmark.h>
#include <unordered_set>

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;
}

bool get_element_binary_search(det_t target, std::vector<det_t> &psi) {
return std::binary_search(psi.begin(), psi.end(), target);
}

bool get_element_hash(det_t target, std::unordered_set<det_t> &psi) {
return psi.find(target) != psi.end();
}

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)
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<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)
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<det_t> psi_m;
for (auto d: psi) {
psi_m.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)
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();

40 changes: 2 additions & 38 deletions .secret_cpp/not_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,7 @@
#include <array>
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/doctest.h>

typedef sul::dynamic_bitset<> spin_det_t;

template<>
struct std::hash<spin_det_t> {
std::size_t operator()(spin_det_t const& s) const noexcept {
return std::hash<std::string>()(s.to_string());
}
};

#define N_SPIN_SPECIES 2
// enum spin_type_e {
// ALPHA = 0,
// BETA = 1,
// N_SPIN_SPECIES,
// };

typedef std::array<spin_det_t, 2> det_t;
template<>
struct std::hash<det_t> {
std::size_t operator()(det_t const& s) const noexcept {
std::size_t h1 = std::hash<spin_det_t>{}(s[0]);
std::size_t h2 = std::hash<spin_det_t>{}(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<spin_occupancy_mask_t, 2> occupancy_mask_t;

typedef sul::dynamic_bitset<> spin_unoccupancy_mask_t;
typedef std::array<spin_unoccupancy_mask_t, 2> unoccupancy_mask_t;

typedef std::array<uint64_t, 4> eri_4idx_t;
#include <qpx.hpp>


// Utils
Expand Down Expand Up @@ -337,7 +301,7 @@ void category_D_iiil(uint64_t N_orb, eri_4idx_t idx, std::vector<det_t>& 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];
Expand Down
1 change: 0 additions & 1 deletion data/f2_631g.10det.wf
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,3 @@
-0.0306196143382
++++++-+-+------+-----------------------------------------------
+++++++++-------------------------------------------------------

0 comments on commit b357691

Please sign in to comment.