-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Thomas Applencourt
committed
Jul 24, 2023
1 parent
80af942
commit b357691
Showing
5 changed files
with
169 additions
and
40 deletions.
There are no files selected for viewing
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
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
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; | ||
|
||
|
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
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(); | ||
|
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
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