Memory Minimization

Written by: Paul Rubin

Primary Source: OR in an OB World

As I grow older, I’m starting to forget things (such as all the math I ever learned) … but that’s not the reason for the title of this post.

A somewhat interesting question popped up on Mathematics StackExchange. It combines a basic sequencing problem (ordering the processing of computational tasks) with a single resource constraint (memory) and a min-max criterion (minimize the largest amount of memory consumed anywhere in the sequence). What differentiates the problem from other resource-constrained scheduling problems I’ve seen is that the output of each task eats a specified amount of memory and must stay there until every successor task that needs it as input completes, after which the memory can be recycled. In particular, the output of the last task using the result of task \(n\) will coexist momentarily with the output of \(n\), before \(n\)’s output can be sent to the bit-recycler. Also, there is no time dimension here (we neither know nor care how much CPU time tasks need), just sequencing.

The data for the problem is a precedence graph (directed acyclic graph, one node for each task) plus an integer weight \(w_n\) for each task \(n\), representing the memory consumption of the value computed by task \(n\). There are several ways to attack a problem like this, including (mixed) integer programming (MIP), constraint programming (CP), and all sorts of metaheuristics. MIP and CP guarantee optimal solutions given enough resources (time, memory, computing power). Metaheuristics do not guarantee optimality, but also do not require commercial software to implement and may scale better than MIP and possibly CP.

I thought I’d publish my MIP model for the problem, scaling be damned.

Index sets and functions

Let \(N\) be the number of tasks (nodes).

  • I will use \(V=\left\{ 1,\dots,N\right\}\) as the set of tasks and \(S=\left\{ 1,\dots,N\right\}\) as the set of schedule slots. (Yes, I know they are the same set, but the dual notation may help distinguish things in context.)
  • \(A\subset V\times V\) will represent the set of arcs, where \((u,v)\in A\) means that task \(v\) takes the output of task \(u\) as an input.
  • \(\pi:V\rightarrow2^{V}\) and \(\sigma:V\rightarrow2^{V}\) will map each task to its immediate predecessors (the inputs to that task) and immediate successors (tasks that need its value as inputs) respectively.

Parameters

The only parameter is \(w:V\rightarrow\mathbb{N}\), which maps tasks to their memory footprints.

Variables

There are a few ways to express assignments in MIP models, the two most common of which are “does this critter go in this slot” and “does this critter immediately follow this other critter”. I’ll use the former.

  • \(x_{ij}\in\left\{ 0,1\right\} \ \forall i\in V,\forall j\in S\) will designate assignments, taking the value 1 if task \(i\) is scheduled into slot \(j\) in the sequence and 0 otherwise.
  • \(r_{ij}\in\left[0,1\right]\ \forall i\in V,\forall j\in S\) will take the value 1 if the output of task \(i\) is resident in memory immediately prior to performing the computational task in slot \(j\) and 0 otherwise.
  • \(d_{ij}\in\left[0,1\right]\ \forall i\in V,\forall j\in S\) indicates whether all tasks requiring the output of task \(i\) have been completed prior to the execution of the task in slot \(j\) (1) or not (0).
  • \(z>0\) is the maximum memory usage anywhere in the schedule.

We can actually winnow a few of these variables. If task \(i\) has any predecessors, then clearly \(x_{i1}=0\), since there is no opportunity before the first slot to execute a predecessor task. Similarly, \(d_{i1}=0\,\forall i\in V\), since nothing has been completed prior to the first slot.

You may think at this point that you have spotted a typo in the second and third bullet points (brackets rather than braces). You haven’t. Read on.

Objective

The easiest part of the model is the objective function: minimize \(z\).

Constraints

The assignment constraints are standard. Every task is assigned to exactly one slot, and every slot is filled with exactly one task.

\(\displaystyle \sum_{j\in S}x_{ij}=1\quad\forall i\in V\)
\(\displaystyle \sum_{i\in V}x_{ij}=1\quad\forall j\in S\)

Precedence constraints are also fairly straightforward. No task can be scheduled before each of its predecessors has completed.

\(\displaystyle x_{ij}\le\sum_{k <j}x_{hk}\quad\forall i\in V,\forall j\in S,\forall h\in\pi(i)\)

Next, we define memory consumption. Immediately after executing the task in any slot \(j\), the memory footprint will be the combined consumption of the output of that task and the output of any earlier task still in memory when slot \(j\) executes.

\(\displaystyle z\ge\sum_{i\in V}w(i)\left(r_{ij}+x_{ij}\right)\quad\forall j\in S\)

Defining whether the output of some task \(i\) is no longer needed prior to executing the task in some slot \(j>1\) is fairly straightforward.

\(\displaystyle d_{ij}\le\sum_{k<j}x_{hk}\quad\forall i\in V,\forall 1<j\in S,\forall h\in\sigma(i)\)

As noted above, there is no “before slot 1”, so we start this set of constraints with \(j=2\).

Finally, the result of task \(i\) remains in memory at the point where the task in slot \(j\) executes if task \(i\) executed before slot \(j\) and the memory has not yet been freed.

\(\displaystyle r_{ij}\ge\sum_{k<j}x_{ik}-d_{ij}\quad\forall i\in V,\forall 1<j\in S\)

Again, this is nonsensical for \(j=1\), so we start with \(j=2\).

Comments

I’ll end with a few observations about the model.

  • It has \(O(N^2)\) binary variables, so, as I noted above, it will not scale all that gracefully.
  • As mentioned early, the relaxation of \(d_{ij}\) and \(r_{ij}\) from binary to continuous in the interval \([0,1]\) was deliberate, not accidental. Why is this justified? \(d_{ij}\) is bounded above by an integer quantity, and the solver “wants” it to be \(1\): the sooner something can be removed from memory, the better for the solution. Similarly, \(r_{ij}\) is bounded below by an integer expression, and the solver “wants” it to be \(0\): the less cruft hanging around in memory, the better. So the solution will be binary even if the variables are not.
  • The solution might be “lazy” about clearing memory. From the model perspective, there is no rush to remove unneeded results as long as they are not contributing to the maximum memory footprint. The maximum memory load \(z\) may occur at multiple points in the schedule. At least one would contain nothing that could be removed (else the solution would not be optimal). At points \(j\) where memory use is below maximum, and possibly at points where memory use equals the maximum, there may be tasks \(i\) for which \(d_{ij}=0\) in the solution but could be set to \(1\), without however reducing \(z\). This can be avoided by adding constraints that force \(d_{ij}=1\) wherever possible, but those constraints would enlarge the model (quite possibly slowing the solver) without improving the final solution.
The following two tabs change content below.
I'm an apostate mathematician, retired from a business school after 33 years of teaching mostly (but not exclusively) quantitative methods courses. My academic interests lie in operations research. I also study Tae Kwon Do a bit on the side.

Latest posts by Paul Rubin (see all)