Finding where the time goes¶
How to find which phase, which entry and which solve a model spends its time on. Read this before Making a solve faster: the phase that is slow decides which way to try.
The spec is examples/dispatch.yaml.
import polars as pl
import specsolve as sps
pl.Config.set_tbl_rows(30)
MODEL = 'examples/dispatch.yaml'
GENERATORS = ['wind', 'solar', 'gas']
sources = {
'snapshot': range(4),
'generator': GENERATORS,
'p_max': pl.DataFrame({'generator': GENERATORS, 'value': [80.0, 0.0, 200.0]}),
'cost': pl.DataFrame({'generator': GENERATORS, 'value': [10.0, 25.0, 50.0]}),
'load': pl.DataFrame({'snapshot': range(4), 'value': [60.0, 120.0, 180.0, 90.0]}),
}
The phases of a solve¶
A model is clocked in nine phases, from the spec as you give it to the archive on disk. Each phase starts where the one before it ended, so the phases add up to the whole call:
flowchart TB
SPEC["the spec<br/>a YAML file or a mapping"] -->|load| PROGRAM["the checked spec and its program"]
DATA[("your sources<br/>parquet files and tables")] -->|tidy| TIDY["the tidy tables<br/>as sps.tidy returns them"]
PROGRAM --> ATTACHED
TIDY -->|attach| ATTACHED["the data on the program<br/>assumptions checked"]
ATTACHED -->|build| BUILT["the built model<br/>columns, rows and matrix"]
BUILT -->|write| FILE["an LP or MPS file"]
BUILT -->|handoff| LOADED["the solver, holding the model<br/>and any start="]
LOADED -->|solve| VALUES["the solver's values and duals"]
VALUES -->|readback| RESULT["the Result<br/>by coordinate"]
RESULT -->|archive| ARCHIVE[("the archive on disk<br/>metrics.parquet written last")]
Each arrow is one phase, and each box is what that phase produces.
Metrics defines each phase.
Every result carries its clocks. Read them as a table, one row per phase:
shape: (9, 2)
┌──────────┬──────────┐
│ phase ┆ seconds │
│ --- ┆ --- │
│ str ┆ f64 │
╞══════════╪══════════╡
│ load ┆ 0.000973 │
│ tidy ┆ 0.001629 │
│ attach ┆ 0.001179 │
│ build ┆ 0.006167 │
│ handoff ┆ 0.001286 │
│ solve ┆ 0.000384 │
│ readback ┆ 0.000185 │
│ write ┆ 0.0 │
│ archive ┆ 0.0 │
└──────────┴──────────┘
A phase that never ran, here write and archive, reads zero. On a model of
300,000 columns, solve took 2.4 s of a 2.5 to 2.6 s call
(#2180).
An archive keeps the same row, so you can read the clocks later or on another machine:
Every lap, one row each¶
model.timings() logs
every lap the model took, one row each:
shape: (28, 5)
┌─────┬──────────┬─────────────────┬───────────────┬──────────┐
│ run ┆ phase ┆ step ┆ entry ┆ seconds │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ u32 ┆ str ┆ str ┆ str ┆ f64 │
╞═════╪══════════╪═════════════════╪═══════════════╪══════════╡
│ 0 ┆ load ┆ null ┆ null ┆ 0.000916 │
│ 0 ┆ tidy ┆ read ┆ null ┆ 0.000073 │
│ 0 ┆ tidy ┆ shape ┆ generator ┆ 0.000471 │
│ 0 ┆ tidy ┆ shape ┆ snapshot ┆ 0.000444 │
│ 0 ┆ tidy ┆ shape ┆ cost ┆ 0.000232 │
│ 0 ┆ tidy ┆ shape ┆ load ┆ 0.000165 │
│ 0 ┆ tidy ┆ shape ┆ p_max ┆ 0.000158 │
│ 0 ┆ tidy ┆ shape ┆ null ┆ 0.000004 │
│ 0 ┆ attach ┆ encode ┆ cost ┆ 0.00014 │
│ 0 ┆ attach ┆ content ┆ cost ┆ 0.000123 │
│ 0 ┆ attach ┆ encode ┆ load ┆ 0.000058 │
│ 0 ┆ attach ┆ content ┆ load ┆ 0.000286 │
│ 0 ┆ attach ┆ encode ┆ p_max ┆ 0.000121 │
│ 0 ┆ attach ┆ content ┆ p_max ┆ 0.000096 │
│ 0 ┆ attach ┆ fill ┆ null ┆ 0.000042 │
│ 0 ┆ attach ┆ specialize ┆ null ┆ 0.00028 │
│ 0 ┆ attach ┆ assumptions ┆ null ┆ 0.000008 │
│ 0 ┆ build ┆ variable ┆ p ┆ 0.001912 │
│ 0 ┆ build ┆ constraint ┆ power_balance ┆ 0.002787 │
│ 0 ┆ build ┆ objective ┆ null ┆ 0.000976 │
│ 0 ┆ build ┆ matrix ┆ null ┆ 0.000238 │
│ 0 ┆ build ┆ sink_tables ┆ null ┆ 0.000181 │
│ 0 ┆ handoff ┆ match ┆ null ┆ 0.000017 │
│ 0 ┆ handoff ┆ capability ┆ null ┆ 0.000061 │
│ 0 ┆ handoff ┆ solver_instance ┆ null ┆ 0.001126 │
│ 0 ┆ handoff ┆ start ┆ null ┆ 0.000004 │
│ 0 ┆ solve ┆ null ┆ null ┆ 0.000291 │
│ 0 ┆ readback ┆ null ┆ null ┆ 0.000153 │
└─────┴──────────┴─────────────────┴───────────────┴──────────┘
runcounts the results handed back before the lap. Run 0 ends with the first result.stepsays what a lap did inside its phase, andentrynames the entry it built. The tidy, the attach, the build and the handoff have steps. Elsewhere both are null.- The rows add up to the metrics. A phase's rows sum to its clock in
model.metrics(), and a run's rows to themetricsof the result that ends it.
The source a tidy spends¶
Filter the log to the tidy to find the source that took the time:
shape: (7, 3)
┌───────┬───────────┬──────────┐
│ step ┆ entry ┆ seconds │
│ --- ┆ --- ┆ --- │
│ str ┆ str ┆ f64 │
╞═══════╪═══════════╪══════════╡
│ read ┆ null ┆ 0.000073 │
│ shape ┆ generator ┆ 0.000471 │
│ shape ┆ snapshot ┆ 0.000444 │
│ shape ┆ cost ┆ 0.000232 │
│ shape ┆ load ┆ 0.000165 │
│ shape ┆ p_max ┆ 0.000158 │
│ shape ┆ null ┆ 0.000004 │
└───────┴───────────┴──────────┘
read is every path read at once, so one row for all the files. Then each
dimension, relation and parameter has a shape row, for its columns and their
types. The last shape row has no entry: it is the refusal that needs every
source read, a dimension that got no labels.
The attach has a row per step as well. encode turns a table's labels into
its dimensions' own type, and content checks what that shows: every label
one of its dimension's, no coordinate given twice, and a row wherever the spec
refuses to lack one. Each relation and parameter has one row of each. fill,
specialize and assumptions follow, once each: the missing rows a divisor
or a power reads written in, the program rewritten with the conditions the
data decides, and the spec's assumptions checked.
The entry a build spends¶
Filter the log to the build, and sort it, to find the entry that took the time:
shape: (5, 5)
┌─────┬───────┬─────────────┬───────────────┬──────────┐
│ run ┆ phase ┆ step ┆ entry ┆ seconds │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ u32 ┆ str ┆ str ┆ str ┆ f64 │
╞═════╪═══════╪═════════════╪═══════════════╪══════════╡
│ 0 ┆ build ┆ constraint ┆ power_balance ┆ 0.002787 │
│ 0 ┆ build ┆ variable ┆ p ┆ 0.001912 │
│ 0 ┆ build ┆ objective ┆ null ┆ 0.000976 │
│ 0 ┆ build ┆ matrix ┆ null ┆ 0.000238 │
│ 0 ┆ build ┆ sink_tables ┆ null ┆ 0.000181 │
└─────┴───────┴─────────────┴───────────────┴──────────┘
The build's steps follow it: one variable, sos or constraint row per entry,
the objective, then matrix, which stacks every constraint into one matrix,
and sink_tables, the rest of what the solver is handed. An
entry's seconds are its own: they hold when the entries build in another
order.
An update that gives only new numbers at the old coordinates reads again
just the entries those numbers feed. Its build logs splice, those entries
and sink_tables, with no matrix row: a coefficient read again is spliced
into the matrix within its constraint's row.
The steps of a handoff¶
The handoff's rows name its steps, the same for every solver:
A first solve runs match, capability, solver_instance and start.
match matches your start= to this build, capability checks the solver
can take the model, solver_instance is a new solver instance taking it, and
start gives the solver the start. The first solver_instance in a process
also imports the solver's package. There are as many solver_instance rows as
metrics().solver_instances counts.
A later solve keeps the solver it holds where only numbers moved, and pushes
them onto it; otherwise it closes it and takes a new solver_instance. A
digest
row comes only from an update that rebuilds the model while a solver is held:
the held solver reads its fingerprint of its model before the rebuild. Two
solves with no update between them log no digest.
Each solve of a loop¶
A model that you update and solve again logs one run per result. Group the log by run to compare the solves:
for load in ([90.0, 150.0, 200.0, 60.0], [30.0, 60.0, 90.0, 45.0]):
model.update({'load': pl.DataFrame({'snapshot': range(4), 'value': load})})
model.solve()
per_run = model.timings().group_by('run', 'phase', maintain_order=True).agg(pl.col('seconds').sum())
print(per_run.pivot('phase', index='run', values='seconds'))
model.close()
shape: (3, 8)
┌─────┬──────────┬──────────┬──────────┬──────────┬──────────┬──────────┬──────────┐
│ run ┆ load ┆ tidy ┆ attach ┆ build ┆ handoff ┆ solve ┆ readback │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ u32 ┆ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 │
╞═════╪══════════╪══════════╪══════════╪══════════╪══════════╪══════════╪══════════╡
│ 0 ┆ 0.000916 ┆ 0.001547 ┆ 0.001153 ┆ 0.006093 ┆ 0.001207 ┆ 0.000291 ┆ 0.000153 │
│ 1 ┆ null ┆ 0.001199 ┆ 0.001012 ┆ 0.002518 ┆ 0.001588 ┆ 0.000295 ┆ 0.000165 │
│ 2 ┆ null ┆ 0.001129 ┆ 0.000737 ┆ 0.002412 ┆ 0.001075 ┆ 0.000279 ┆ 0.000178 │
└─────┴──────────┴──────────┴──────────┴──────────┴──────────┴──────────┴──────────┘
Run 0 holds the load and the first build. Each later run holds its update, its build and its solve. An archive written by a solve lands in that solve's run. The result's own row is written into the archive before it lands, so the run holds the landing and the result does not.
What the slow phase points to¶
| Where the time goes | What to try |
|---|---|
solve |
start from an earlier answer, accept a gap, and the ways after it |
build and handoff, again on every update |
keep the model loaded |
handoff, in solver_instance on every update |
keep the model loaded: only numbers may move |
build on many small models |
build many small models faster |
build on a large model |
the entry that holds the time, then make the model smaller |
Report a slow phase¶
Open an issue at github.com/fluxopt/specsolve/issues when one phase takes longer than you expect for the size of your model. The clocks tell a maintainer where to look, and an archive lets them run your model again. Write the report in five steps:
-
Solve the model with an archive. One file then holds the spec, the data, the answer and the clocks.
-
Paste the phases, the model's size and the versions.
result = sps.read_archive('runs/slow.zip') print(result.metrics.phases) print(result.metrics.columns, result.metrics.rows, result.metrics.nonzeros) print(result.provenance)The provenance names the solver, its version and options, and the specsolve and mathspec versions. Where
buildis slow, paste the build's rows ofmodel.timings()too. -
Say which phase is slow, and why you expected less. The best evidence is the same model at two sizes. "
buildtook 2 s at 1,000 snapshots and 90 s at 10,000" shows a phase that grows faster than the model. -
Attach the archive if you may share the data. Anyone can solve it again from the file alone, with
sps.solve(result.spec, result.sources). If you may not, attach the spec and give the number of labels in each dimension. -
Name the machine. Give its cores, its memory and its operating system, and say whether other work ran during the solve.
A slow solve is the solver's own run. Try the ways
before you report it. Report it when another tool hands the same solver the
same model and it solves faster.