Skip to content

Commit

Permalink
[issue1105] Refactor SCC merge strategy.
Browse files Browse the repository at this point in the history
The strategy now longer needs to compute at which indices of the FTS
the products corresponding to the SCC-components will be. Instead,
after having handled all SCC-components, the strategy will simply use
all available factors to compute the set of merge candidates.
  • Loading branch information
Silvan Sievers committed Jan 29, 2024
1 parent 5d7a505 commit 9ac0165
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 34 deletions.
28 changes: 10 additions & 18 deletions src/search/merge_and_shrink/merge_strategy_factory_sccs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,30 +74,23 @@ unique_ptr<MergeStrategy> MergeStrategyFactorySCCs::compute_merge_strategy(
break;
}

/*
Compute the indices at which the merged SCCs can be found when all
SCCs have been merged.
*/
int index = num_vars - 1;
log << "SCCs of the causal graph:" << endl;
if (log.is_at_least_normal()) {
log << "SCCs of the causal graph:" << endl;
}
vector<vector<int>> non_singleton_cg_sccs;
vector<int> indices_of_merged_sccs;
indices_of_merged_sccs.reserve(sccs.size());
for (const vector<int> &scc : sccs) {
log << scc << endl;
if (log.is_at_least_normal()) {
log << scc << endl;
}
int scc_size = scc.size();
if (scc_size == 1) {
indices_of_merged_sccs.push_back(scc.front());
} else {
index += scc_size - 1;
indices_of_merged_sccs.push_back(index);
if (scc_size != 1) {
non_singleton_cg_sccs.push_back(scc);
}
}
if (sccs.size() == 1) {
if (log.is_at_least_normal() && sccs.size() == 1) {
log << "Only one single SCC" << endl;
}
if (static_cast<int>(sccs.size()) == num_vars) {
if (log.is_at_least_normal() && static_cast<int>(sccs.size()) == num_vars) {
log << "Only singleton SCCs" << endl;
assert(non_singleton_cg_sccs.empty());
}
Expand All @@ -111,8 +104,7 @@ unique_ptr<MergeStrategy> MergeStrategyFactorySCCs::compute_merge_strategy(
task_proxy,
merge_tree_factory,
merge_selector,
move(non_singleton_cg_sccs),
move(indices_of_merged_sccs));
move(non_singleton_cg_sccs));
}

bool MergeStrategyFactorySCCs::requires_init_distances() const {
Expand Down
31 changes: 19 additions & 12 deletions src/search/merge_and_shrink/merge_strategy_sccs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,53 @@ MergeStrategySCCs::MergeStrategySCCs(
const TaskProxy &task_proxy,
const shared_ptr<MergeTreeFactory> &merge_tree_factory,
const shared_ptr<MergeSelector> &merge_selector,
vector<vector<int>> non_singleton_cg_sccs,
vector<int> indices_of_merged_sccs)
vector<vector<int>> &&non_singleton_cg_sccs)
: MergeStrategy(fts),
task_proxy(task_proxy),
merge_tree_factory(merge_tree_factory),
merge_selector(merge_selector),
non_singleton_cg_sccs(move(non_singleton_cg_sccs)),
indices_of_merged_sccs(move(indices_of_merged_sccs)),
current_merge_tree(nullptr) {
}

MergeStrategySCCs::~MergeStrategySCCs() {
}

pair<int, int> MergeStrategySCCs::get_next() {
// We did not already start merging an SCC/all finished SCCs, so we
// do not have a current set of indices we want to finish merging.
if (current_ts_indices.empty()) {
// Get the next indices we need to merge
/*
We are currently not dealing with merging all factors of an SCC, so
we need to either get the next one or allow merging any existing
factors of the FTS if there is no SCC left.
*/
if (non_singleton_cg_sccs.empty()) {
assert(indices_of_merged_sccs.size() > 1);
current_ts_indices = move(indices_of_merged_sccs);
// We are done dealing with all SCCs, allow merging any factors.
current_ts_indices.reserve(fts.get_num_active_entries());
for (int ts_index: fts) {
current_ts_indices.push_back(ts_index);
}
} else {
/*
There is another SCC we have to deal with. Store its factors so
that we merge them over the next iterations.
*/
vector<int> &current_scc = non_singleton_cg_sccs.front();
assert(current_scc.size() > 1);
current_ts_indices = move(current_scc);
non_singleton_cg_sccs.erase(non_singleton_cg_sccs.begin());
}

// If using a merge tree factory, compute a merge tree for this set
// If using a merge tree factory, compute a merge tree for this set.
if (merge_tree_factory) {
current_merge_tree = merge_tree_factory->compute_merge_tree(
task_proxy, fts, current_ts_indices);
}
} else {
// Add the most recent merge to the current indices set
// Add the most recent product to the current index set.
current_ts_indices.push_back(fts.get_size() - 1);
}

// Select the next merge for the current set of indices, either using the
// Select the next merge from the current index set, either using the
// tree or the selector.
pair<int, int > next_pair;
int merged_ts_index = fts.get_size();
Expand All @@ -72,7 +79,7 @@ pair<int, int> MergeStrategySCCs::get_next() {
next_pair = merge_selector->select_merge(fts, current_ts_indices);
}

// Remove the two merged indices from the current set of indices.
// Remove the two merged indices from the current index set.
for (vector<int>::iterator it = current_ts_indices.begin();
it != current_ts_indices.end();) {
if (*it == next_pair.first || *it == next_pair.second) {
Expand Down
5 changes: 1 addition & 4 deletions src/search/merge_and_shrink/merge_strategy_sccs.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ class MergeStrategySCCs : public MergeStrategy {
std::shared_ptr<MergeTreeFactory> merge_tree_factory;
std::shared_ptr<MergeSelector> merge_selector;
std::vector<std::vector<int>> non_singleton_cg_sccs;
std::vector<int> indices_of_merged_sccs;

// Active "merge strategies" while merging a set of indices
std::unique_ptr<MergeTree> current_merge_tree;
std::vector<int> current_ts_indices;
public:
Expand All @@ -28,8 +26,7 @@ class MergeStrategySCCs : public MergeStrategy {
const TaskProxy &task_proxy,
const std::shared_ptr<MergeTreeFactory> &merge_tree_factory,
const std::shared_ptr<MergeSelector> &merge_selector,
std::vector<std::vector<int>> non_singleton_cg_sccs,
std::vector<int> indices_of_merged_sccs);
std::vector<std::vector<int>> &&non_singleton_cg_sccs);
virtual ~MergeStrategySCCs() override;
virtual std::pair<int, int> get_next() override;
};
Expand Down

0 comments on commit 9ac0165

Please sign in to comment.