Skip to content

Commit

Permalink
Optimize clues by moving allocations out of Build* methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ol-imorozko committed Oct 5, 2024
1 parent 3b2b34f commit 3cc3060
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 20 deletions.
21 changes: 16 additions & 5 deletions src/core/algorithms/dc/FastADC/util/clue_set_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,29 @@ namespace algos::fastadc {

inline ClueSet BuildClueSet(std::vector<PliShard> const& pliShards, PredicatePacks const& packs) {
ClueSet clue_set;
ClueSet partial_clue_set;
size_t task_count = (pliShards.size() * (pliShards.size() + 1)) / 2;
LOG(DEBUG) << " [CLUE] task count: " << task_count;

// Range of all pliShards is equal, so it's safe to pass a pre-allocated vector of
// pliShards[0]'s range
size_t range = pliShards[0].Range();
size_t evidence_count = range * range;

clue_set.reserve(range * 2);
partial_clue_set.reserve(range * 2);

std::vector<Clue> forward_clues(evidence_count, 0);
std::vector<Clue> reverse_clues(evidence_count, 0);

for (size_t i = 0; i < pliShards.size(); i++) {
for (size_t j = i; j < pliShards.size(); j++) {
ClueSet partial_clue_set;

if (i == j) {
partial_clue_set = SingleClueSetBuilder{pliShards[i]}.BuildClueSet(packs);
SingleClueSetBuilder{pliShards[i]}.BuildClueSet(packs, forward_clues,
partial_clue_set);
} else {
partial_clue_set =
CrossClueSetBuilder{pliShards[i], pliShards[j]}.BuildClueSet(packs);
CrossClueSetBuilder{pliShards[i], pliShards[j]}.BuildClueSet(
packs, forward_clues, reverse_clues, partial_clue_set);
}

for (auto const& [clue, count] : partial_clue_set) {
Expand Down
6 changes: 2 additions & 4 deletions src/core/algorithms/dc/FastADC/util/common_clue_set_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@ inline ClueSet AccumulateClues(Vectors const&... vectors) {
}
#else
template <typename... Vectors>
ClueSet AccumulateClues(Vectors const&... vectors) {
ClueSet clue_set;
ClueSet AccumulateClues(ClueSet& clue_set, Vectors const&... vectors) {
clue_set.clear();
int64_t clue_zero_count = 0;

clue_set.reserve((vectors.size() + ...));

auto insert_clues = [&](std::vector<Clue> const& clues) {
for (auto const& clue : clues) {
if (clue.none()) {
Expand Down
10 changes: 6 additions & 4 deletions src/core/algorithms/dc/FastADC/util/cross_clue_set_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ namespace algos::fastadc {
CrossClueSetBuilder::CrossClueSetBuilder(PliShard const& shard1, PliShard const& shard2)
: plis1_(shard1.plis), plis2_(shard2.plis), evidence_count_(shard1.Range() * shard2.Range()) {}

ClueSet CrossClueSetBuilder::BuildClueSet(PredicatePacks const& packs) {
std::vector<Clue> forward_clues(evidence_count_, 0);
std::vector<Clue> reverse_clues(evidence_count_, 0);
void CrossClueSetBuilder::BuildClueSet(PredicatePacks const& packs,
std::vector<Clue>& forward_clues,
std::vector<Clue>& reverse_clues, ClueSet& clue_set) {
forward_clues.assign(evidence_count_, Clue());
reverse_clues.assign(evidence_count_, Clue());

for (auto const& cat_pack : packs.str_single) {
CorrectStrSingle(forward_clues, reverse_clues, plis1_[cat_pack.left_idx],
Expand All @@ -33,7 +35,7 @@ ClueSet CrossClueSetBuilder::BuildClueSet(PredicatePacks const& packs) {
num_pack.eq_mask, num_pack.gt_mask);
}

return AccumulateClues(forward_clues, reverse_clues);
AccumulateClues(clue_set, forward_clues, reverse_clues);
}

void CrossClueSetBuilder::SetSingleEQ(std::vector<Clue>& clues1, std::vector<Clue>& clues2,
Expand Down
3 changes: 2 additions & 1 deletion src/core/algorithms/dc/FastADC/util/cross_clue_set_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class CrossClueSetBuilder {
CrossClueSetBuilder(CrossClueSetBuilder&& other) noexcept = default;
CrossClueSetBuilder& operator=(CrossClueSetBuilder&& other) noexcept = delete;

ClueSet BuildClueSet(PredicatePacks const& packs);
void BuildClueSet(PredicatePacks const& packs, std::vector<Clue>& forward_clues,
std::vector<Clue>& reverse_clues, ClueSet& clue_set);

private:
std::vector<Pli> const& plis1_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ SingleClueSetBuilder::SingleClueSetBuilder(PliShard const& shard)
tid_range_(shard.Range()),
evidence_count_(tid_range_ * tid_range_) {}

ClueSet SingleClueSetBuilder::BuildClueSet(PredicatePacks const& packs) {
std::vector<Clue> clues(evidence_count_, 0);
void SingleClueSetBuilder::BuildClueSet(PredicatePacks const& packs, std::vector<Clue>& clues,
ClueSet& clue_set) {
clues.assign(evidence_count_, Clue());

for (auto const& cat_pack : packs.str_single) {
CorrectStrSingle(clues, plis_[cat_pack.left_idx], cat_pack.eq_mask);
Expand All @@ -29,14 +30,12 @@ ClueSet SingleClueSetBuilder::BuildClueSet(PredicatePacks const& packs) {
num_pack.eq_mask, num_pack.gt_mask);
}

ClueSet clue_set = AccumulateClues(clues);
AccumulateClues(clue_set, clues);

// Reflex evidence check and removal:
Clue reflex_clue{}; // All bits zero
clue_set[reflex_clue] -= tid_range_;
if (clue_set[reflex_clue] == 0) clue_set.erase(clue_set.find(reflex_clue));

return clue_set;
}

void SingleClueSetBuilder::SetSingleEQ(std::vector<Clue>& clues, Pli::Cluster const& cluster,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class SingleClueSetBuilder {
SingleClueSetBuilder(SingleClueSetBuilder&& other) noexcept = default;
SingleClueSetBuilder& operator=(SingleClueSetBuilder&& other) noexcept = delete;

ClueSet BuildClueSet(PredicatePacks const& packs);
void BuildClueSet(PredicatePacks const& packs, std::vector<Clue>& clues, ClueSet& clue_set);

private:
std::vector<Pli> const& plis_;
Expand Down

0 comments on commit 3cc3060

Please sign in to comment.