Skip to content

Commit

Permalink
some refactoring to get more efficient implementations of core algori…
Browse files Browse the repository at this point in the history
…thms
  • Loading branch information
Sam committed Jul 24, 2020
1 parent 4f5d6e4 commit af1a810
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/SynthesiserRelation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ std::unique_ptr<SynthesiserRelation> SynthesiserRelation::getSynthesiserRelation
}

assert(rel != nullptr && "relation type not specified");
/*
/*
auto chains = indexSet.getAllChains();
size_t inequalities = 0;
Expand Down
73 changes: 30 additions & 43 deletions src/ram/analysis/RamIndexAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,28 +317,26 @@ void MinIndexSelection::solve() {
SearchSignature initDelta = *(chain.begin());
insertIndex(ids, initDelta);

// build the lex-order from back to front ensuring no duplication with prev delta
for (auto iit = chain.begin(); next(iit) != chain.end(); ++iit) {
SearchSignature delta = SearchSignature::getDelta(*next(iit), *iit);
insertIndex(ids, delta);
}

// if we see duplicates in the lex-order then remove duplicates
bool changed = true;
while (changed) {
changed = false;
for (size_t i = 0; i < ids.size(); ++i) {
for (size_t j = i + 1; j < ids.size(); ++j) {
if (ids[i] == ids[j]) {
ids.erase(ids.begin() + i);
changed = true;
break;
}
}
if (changed) {
break;
}
// prune repeated deltas from right to left
std::reverse(ids.begin(), ids.end());
std::unordered_set<uint32_t> seen;
auto newEnd = std::remove_if(ids.begin(), ids.end(), [&seen](uint32_t value) {
if (seen.find(value) != std::end(seen)) {
return true;
} else {
seen.insert(value);
return false;
}
}
});

ids.erase(newEnd, ids.end());
std::reverse(ids.begin(), ids.end());

assert(!ids.empty());
orders.push_back(ids);
Expand Down Expand Up @@ -428,13 +426,12 @@ const MinIndexSelection::ChainOrderMap MinIndexSelection::getChainsFromMatching(

const MinIndexSelection::ChainOrderMap MinIndexSelection::mergeChains(
MinIndexSelection::ChainOrderMap& chains) {

bool changed = true;
while (changed) {
changed = false;
for (auto lhs_it = chains.begin(); lhs_it != chains.end(); ++lhs_it) {
for (auto lhs_it = chains.begin(); !changed && lhs_it != chains.end(); ++lhs_it) {
const auto lhs = *lhs_it;
for (auto rhs_it = std::next(lhs_it); rhs_it != chains.end(); ++rhs_it) {
for (auto rhs_it = std::next(lhs_it); !changed && rhs_it != chains.end(); ++rhs_it) {
const auto rhs = *rhs_it;

// merge the two chains
Expand Down Expand Up @@ -565,31 +562,26 @@ const MinIndexSelection::ChainOrderMap MinIndexSelection::mergeChains(
changed = true;

// remove previous 2 chains
chains.erase(std::remove(chains.begin(), chains.end(), lhs), chains.end());
chains.erase(std::remove(chains.begin(), chains.end(), rhs), chains.end());
chains.erase(lhs_it);
chains.erase(rhs_it);

// insert merge chain
chains.push_back(mergedChain);
break;
}
if (changed) {
break;
}
}
}

return dischargeToMergeChains(chains);
}

const MinIndexSelection::ChainOrderMap MinIndexSelection::dischargeToMergeChains(
MinIndexSelection::ChainOrderMap& chains) {

bool changed = true;
while (changed) {
changed = false;
for (auto lhs_it = chains.begin(); lhs_it != chains.end(); ++lhs_it) {
for (auto lhs_it = chains.begin(); !changed && lhs_it != chains.end(); ++lhs_it) {
const auto lhs = *lhs_it;
for (auto rhs_it = std::next(lhs_it); rhs_it != chains.end(); ++rhs_it) {
for (auto rhs_it = std::next(lhs_it); !changed && rhs_it != chains.end(); ++rhs_it) {
const auto rhs = *rhs_it;

// merge the two chains
Expand Down Expand Up @@ -744,19 +736,15 @@ const MinIndexSelection::ChainOrderMap MinIndexSelection::dischargeToMergeChains
changed = true;

// remove previous 2 chains
chains.erase(std::remove(chains.begin(), chains.end(), lhs), chains.end());
chains.erase(std::remove(chains.begin(), chains.end(), rhs), chains.end());
chains.erase(lhs_it);
chains.erase(rhs_it);

// insert merge chain
// insert merged chain
chains.push_back(mergedChain);
break;
}
if (changed) {
break;
}
}
}

return chains;
}

Expand All @@ -778,9 +766,8 @@ MinIndexSelection::AttributeSet MinIndexSelection::getAttributesToDischarge(
}
if (Global::config().has("provenance")) {
return attributesToDischarge;
}


}

auto chains = getAllChains();
// find the chain that the operation lives inside
for (auto chain : chains) {
Expand All @@ -802,7 +789,7 @@ MinIndexSelection::AttributeSet MinIndexSelection::getAttributesToDischarge(
break; // we only care about the chain that the operation belongs to
}
}

return attributesToDischarge;
}

Expand Down Expand Up @@ -899,8 +886,8 @@ void RamIndexAnalysis::print(std::ostream& os) const {
/* print searches */
for (auto& search : indexes.getSearches()) {
os << "\t\t";
os << search;
os << "\n";
os << search;
os << "\n";
}

os << "\tNumber of Indexes: " << indexes.getAllOrders().size() << "\n";
Expand Down
3 changes: 2 additions & 1 deletion src/ram/analysis/RamIndexAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <cstdlib>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <set>
Expand Down Expand Up @@ -219,7 +220,7 @@ class MinIndexSelection {
using Chain = std::vector<SearchSignature>;
// A chain is a vector of SearchSignature to support inserting incomparable elements later
// E.g. 1 --> 2 we don't have 1 and 2 as comparable but they form a valid lex-order
using ChainOrderMap = std::vector<Chain>;
using ChainOrderMap = std::list<Chain>;

class SearchComparator {
public:
Expand Down
4 changes: 2 additions & 2 deletions tests/provenance/high_arity/B.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
1 2 3 4 5 6 7
2 4 6 8 10 12 14
1 2 2 2 2 2 2
1 2 3 4 5 6 7
2 2 2 2 2 2 2
3 2 2 2 2 2 2
2 4 6 8 10 12 14
3 4 4 4 4 4 4

0 comments on commit af1a810

Please sign in to comment.