Skip to content

Commit

Permalink
Move OptimizationPass to const Expression* instead of weak_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
sukritkalra committed Dec 26, 2023
1 parent 5580178 commit af72cfc
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 33 deletions.
6 changes: 3 additions & 3 deletions schedulers/tetrisched/include/tetrisched/Expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class Expression : public std::enable_shared_from_this<Expression> {
/// The children of this Expression.
std::vector<ExpressionPtr> children;
/// The parents of this Expression.
std::vector<std::weak_ptr<Expression>> parents;
std::vector<const Expression*> parents;
/// The type of this Expression.
ExpressionType type;
/// The Solution result from this Expression.
Expand All @@ -210,7 +210,7 @@ class Expression : public std::enable_shared_from_this<Expression> {
std::mutex expressionMutex;

/// Adds a parent to this expression.
void addParent(ExpressionPtr parent);
void addParent(const Expression* parent);

public:
/// Construct the Expression class of the given type.
Expand Down Expand Up @@ -250,7 +250,7 @@ class Expression : public std::enable_shared_from_this<Expression> {
size_t getNumParents() const;

/// Returns the parents of this Expression.
std::vector<ExpressionPtr> getParents() const;
std::vector<const Expression*> getParents() const;

/// Returns the number of children of this Expression.
size_t getNumChildren() const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,13 @@ class CapacityConstraintMapPurgingOptimizationPass : public OptimizationPass {
/// A HashMap of the Expression ID to the cliques in the Expression tree.
/// A clique is defined as a set of Expressions that are known to be
/// non-concurrent with each other.
tbb::concurrent_hash_map<ExpressionPtr, std::unordered_set<ExpressionPtr>>
tbb::concurrent_hash_map<const Expression*,
std::unordered_set<const Expression*>>
cliques;

/// A map from an Expression to the set of entire leaf Expressions that are
/// resident under that Expression.
std::unordered_map<ExpressionPtr, std::unordered_set<ExpressionPtr>>
std::unordered_map<const Expression*, std::unordered_set<const Expression*>>
childLeafExpressions;

/// Computes the cliques from a bottom-up traversal of the STRL.
Expand Down
19 changes: 10 additions & 9 deletions schedulers/tetrisched/src/Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void Expression::addChild(ExpressionPtr child) {
throw tetrisched::exceptions::ExpressionConstructionException(
"Cannot add a null child to the Expression " + name + ".");
}
child->addParent(shared_from_this());
child->addParent(this);
children.push_back(child);
}

Expand Down Expand Up @@ -269,17 +269,18 @@ void Expression::setTimeBounds(ExpressionTimeBounds timeBounds) {
this->timeBounds = timeBounds;
}

void Expression::addParent(ExpressionPtr parent) { parents.push_back(parent); }
void Expression::addParent(const Expression* parent) {
if (parent == nullptr) {
throw tetrisched::exceptions::ExpressionConstructionException(
"Cannot add a null parent to the Expression " + name + ".");
}
parents.push_back(parent);
}

size_t Expression::getNumParents() const { return parents.size(); }

std::vector<ExpressionPtr> Expression::getParents() const {
std::vector<ExpressionPtr> returnParents;
returnParents.reserve(parents.size());
for (decltype(parents)::size_type i = 0; i < parents.size(); i++) {
returnParents.push_back(parents[i].lock());
}
return returnParents;
std::vector<const Expression*> Expression::getParents() const {
return parents;
}

SolutionResultPtr Expression::populateResults(SolverModelPtr solverModel) {
Expand Down
33 changes: 14 additions & 19 deletions schedulers/tetrisched/src/OptimizationPasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -754,12 +754,10 @@ void CapacityConstraintMapPurgingOptimizationPass::computeCliques(
// This is a leaf node, we just add it to its own clique.
{
decltype(cliques)::accessor cliquesAccessor;
cliques.insert(cliquesAccessor, currentExpression);
cliquesAccessor->second =
std::unordered_set<ExpressionPtr>({currentExpression});
cliques.insert(cliquesAccessor, currentExpression.get());
cliquesAccessor->second = {currentExpression.get()};
}
childLeafExpressions[currentExpression] =
std::unordered_set<ExpressionPtr>({currentExpression});
childLeafExpressions[currentExpression.get()] = {currentExpression.get()};
TETRISCHED_DEBUG("[" << name << "] " << currentExpression->getId()
<< " (" << currentExpression->getName() << ") has "
<< currentExpression->getNumChildren()
Expand All @@ -774,13 +772,11 @@ void CapacityConstraintMapPurgingOptimizationPass::computeCliques(
// in our current STRL generation, a MaxExpression is never more than a
// single level away from the leaves. We can both construct the cliques
// and the child leaf expressions in one go.
childLeafExpressions[currentExpression] =
std::unordered_set<ExpressionPtr>({currentExpression});
childLeafExpressions[currentExpression.get()] = {currentExpression.get()};
{
decltype(cliques)::accessor cliquesAccessor;
cliques.insert(cliquesAccessor, currentExpression);
cliquesAccessor->second =
std::unordered_set<ExpressionPtr>({currentExpression});
cliques.insert(cliquesAccessor, currentExpression.get());
cliquesAccessor->second = {currentExpression.get()};
}
// {
// TETRISCHED_SCOPE_TIMER(
Expand Down Expand Up @@ -809,19 +805,18 @@ void CapacityConstraintMapPurgingOptimizationPass::computeCliques(
currentExpression->getTypeString());
// Construct the leaf expressions under the current expression as
// a merger of all the leaf expressions under its children.
childLeafExpressions[currentExpression] =
std::unordered_set<ExpressionPtr>();
childLeafExpressions[currentExpression.get()] = {currentExpression.get()};

for (auto &child : currentExpression->getChildren()) {
// Add the child expression set to the current expression.
auto childLeafExpressionSet = childLeafExpressions.find(child);
auto childLeafExpressionSet = childLeafExpressions.find(child.get());
if (childLeafExpressionSet == childLeafExpressions.end()) {
throw exceptions::RuntimeException(
"[" + name + "] Child " + child->getId() + "(" +
child->getName() + ") of " + currentExpression->getId() + "(" +
currentExpression->getName() + ") does not have a clique.");
}
childLeafExpressions[currentExpression].insert(
childLeafExpressions[currentExpression.get()].insert(
childLeafExpressionSet->second.begin(),
childLeafExpressionSet->second.end());
TETRISCHED_DEBUG("[" << name << "] Adding child expressions from "
Expand All @@ -843,7 +838,7 @@ void CapacityConstraintMapPurgingOptimizationPass::computeCliques(
auto currentExpressionChildren = currentExpression->getChildren();

auto leftChild = currentExpressionChildren[0];
auto leftChildLeafExpressions = childLeafExpressions.find(leftChild);
auto leftChildLeafExpressions = childLeafExpressions.find(leftChild.get());
if (leftChildLeafExpressions == childLeafExpressions.end()) {
throw exceptions::RuntimeException(
"[" + name + "] Left child " + leftChild->getId() + "(" +
Expand All @@ -852,7 +847,7 @@ void CapacityConstraintMapPurgingOptimizationPass::computeCliques(
}

auto rightChild = currentExpressionChildren[1];
auto rightChildLeafExpressions = childLeafExpressions.find(rightChild);
auto rightChildLeafExpressions = childLeafExpressions.find(rightChild.get());
if (rightChildLeafExpressions == childLeafExpressions.end()) {
throw exceptions::RuntimeException(
"[" + name + "] Right child " + rightChild->getId() + "(" +
Expand Down Expand Up @@ -925,7 +920,7 @@ void CapacityConstraintMapPurgingOptimizationPass::
// Expression adds a higher usage than previously seen. If no such
// clique exists, we create a new one.
std::vector<
std::pair<std::unordered_set<ExpressionPtr>, TETRISCHED_ILP_TYPE>>
std::pair<std::unordered_set<const Expression*>, TETRISCHED_ILP_TYPE>>
cliqueUsages;
cliqueUsages.reserve(capacityConstraint->usageVector.size());

Expand Down Expand Up @@ -1099,8 +1094,8 @@ OptimizationPassRunner::OptimizationPassRunner(bool debug,
}

// Register the CapacityConstraintMapPurging optimization pass.
registeredPasses.push_back(
std::make_shared<CapacityConstraintMapPurgingOptimizationPass>());
// registeredPasses.push_back(
// std::make_shared<CapacityConstraintMapPurgingOptimizationPass>());
}

void OptimizationPassRunner::runPreTranslationPasses(
Expand Down

0 comments on commit af72cfc

Please sign in to comment.