Skip to content

Commit

Permalink
Implement WindowedChooseExpression.
Browse files Browse the repository at this point in the history
  • Loading branch information
sukritkalra committed Nov 16, 2023
1 parent 0b6daef commit 56a5997
Show file tree
Hide file tree
Showing 6 changed files with 467 additions and 3 deletions.
54 changes: 54 additions & 0 deletions schedulers/tetrisched/include/tetrisched/Expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ enum ExpressionType {
/// specialization is extremely effective to lower, and whenever possible
/// should be used insteado of the generalized choose expression.
EXPR_MALLEABLE_CHOOSE = 7,
/// A `WindowedChoose` expression represents a choice of a required number of
/// machines from the set of resource partitions for the given duration
/// starting after the provided start time, and finishing before the required
/// endTime. Note that, a WindowedChooseExpression is equivalent to a set of
/// ChooseExpressions modulated by a MaxExpression. We define this
/// higher-level abstraction since it makes it easier for us to dynamically
/// discretize the start times of the ChooseExpressions.
EXPR_WINDOWED_CHOOSE = 8,
};
using ExpressionType = enum ExpressionType;

Expand Down Expand Up @@ -282,6 +290,52 @@ class ChooseExpression : public Expression {
ExpressionTimeBounds getTimeBounds() const override;
};

/// A `WindowedChooseExpression` represents a choice of a required number of
/// machines from the set of resource partitions for the given duration starting
/// after the provided start time, and finishing before the required endTime.
class WindowedChooseExpression : public Expression {
/// The Resource partitions that the WindowedChooseExpression is being asked
/// to choose resources from.
Partitions resourcePartitions;
/// The number of partitions that this WindowedChooseExpression needs to
/// choose.
uint32_t numRequiredMachines;
/// The start time after which the choice needs to be placed.
Time startTime;
/// The duration for which the choice needs to be placed.
Time duration;
/// The end time before which the choice needs to finish placement.
Time endTime;
/// The granularity at which the choices need to be generated.
Time granularity;
// The utility of the choice represented by this Expression.
TETRISCHED_ILP_TYPE utility;
// A map from the placement time to a Variable that represents if
// the placement is chosen or not.
std::unordered_map<Time, VariablePtr> placementTimeVariables;
// A map from the placement time to a vector of <PartitionId, Variable>
// signifying how many Placement variables are chosen from each Partition.
std::unordered_map<Time, std::vector<std::pair<uint32_t, VariablePtr>>>
placementPartitionVariables;

public:
WindowedChooseExpression(std::string taskName, Partitions resourcePartitions,
uint32_t numRequiredMachines, Time startTime,
Time duration, Time endTime, Time granularity,
TETRISCHED_ILP_TYPE utility);
void addChild(ExpressionPtr child) override;
ParseResultPtr parse(SolverModelPtr solverModel,
Partitions availablePartitions,
CapacityConstraintMap& capacityConstraints,
Time currentTime) override;
SolutionResultPtr populateResults(SolverModelPtr solverModel) override;
std::string getDescriptiveName() const override;
ExpressionTimeBounds getTimeBounds() const override;
};

/// A MalleableChooseExpression represents a choice of a flexible set of
/// requirements of resources at each time that sums up to the total required
/// space-time allocations from the given start to the given end time.
class MalleableChooseExpression : public Expression {
private:
/// The Resource partitions that the Expression is being asked to
Expand Down
8 changes: 8 additions & 0 deletions schedulers/tetrisched/include/tetrisched/SolverModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,14 @@ class SolverModelT {
/// Throws an error if the model was not solved first.
T getObjectiveValue() const;

/// Retrieves the Variable with the given name, if available.
std::optional<std::shared_ptr<VariableT<T>>> getVariableByName(
std::string variableName) const;

/// Retrieves the Constraint with the given name, if available.
std::optional<std::shared_ptr<ConstraintT<T>>> getConstraintByName(
std::string constraintName) const;

/// Clears all the Variables and Constraints from this Model.
void clear();

Expand Down
24 changes: 22 additions & 2 deletions schedulers/tetrisched/python/Expressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ void defineSTRLExpressions(py::module_& tetrisched_m) {
.value("EXPR_ALLOCATION", tetrisched::ExpressionType::EXPR_ALLOCATION)
.value("EXPR_MALLEABLE_CHOOSE",
tetrisched::ExpressionType::EXPR_MALLEABLE_CHOOSE)
.value("EXPR_WINDOWED_CHOOSE",
tetrisched::ExpressionType::EXPR_WINDOWED_CHOOSE)
.export_values();

// Define the Placement object.
Expand Down Expand Up @@ -110,13 +112,31 @@ void defineSTRLExpressions(py::module_& tetrisched_m) {
uint32_t numRequiredMachines, tetrisched::Time startTime,
tetrisched::Time duration, TETRISCHED_ILP_TYPE utility) {
return std::make_shared<tetrisched::ChooseExpression>(
taskName, partitions, numRequiredMachines, startTime,
duration, utility);
taskName, partitions, numRequiredMachines, startTime, duration,
utility);
}),
"Initializes a ChooseExpression for the given task to be placed on "
"`numRequiredMachines` from the given partition at the given "
"startTime, running for the given duration.");

// Define the WindowedChooseExpression.
py::class_<tetrisched::WindowedChooseExpression, tetrisched::Expression,
std::shared_ptr<tetrisched::WindowedChooseExpression>>(
tetrisched_m, "WindowedChooseExpression")
.def(py::init([](std::string taskName, tetrisched::Partitions partitions,
uint32_t numRequiredMachines, tetrisched::Time startTime,
tetrisched::Time duration, tetrisched::Time endTime,
tetrisched::Time granularity,
TETRISCHED_ILP_TYPE utility) {
return std::make_shared<tetrisched::WindowedChooseExpression>(
taskName, partitions, numRequiredMachines, startTime, duration,
endTime, granularity, utility);
}),
"Initializes a WindowedChooseExpression for the given task to be "
"placed on `numRequiredMachines` from the given partition at the "
"given startTime, ending before the given end time and "
"running for the given duration.");

// Define the MalleableChooseExpression.
py::class_<tetrisched::MalleableChooseExpression, tetrisched::Expression,
std::shared_ptr<tetrisched::MalleableChooseExpression>>(
Expand Down
Loading

0 comments on commit 56a5997

Please sign in to comment.