Skip to content

Parse by value type and keep the ExaModelsPower bridge trim-safe - #184

Merged
samtalki merged 2 commits into
mainfrom
trim-safe-bridge
Oct 27, 2026
Merged

samtalki merged 2 commits into
mainfrom
trim-safe-bridge

Conversation

@samtalki

@samtalki samtalki commented Oct 26, 2026 •

Copy link
Copy Markdown
Member

ExaModelsPower compiles its case reader into a shared library with juliac --trim=safe (compile_all), and the compiled library parses cases at run time. 0.9 had a trim-safe typed path for this (309e05e). The 0.11 rewrite dropped it: a trimmed ExaModelsPower build over 0.11.6 fails verification with 90 errors.

The first-level failures were:

  • parse choosing the value type at run time;
  • to_powerdata's keyword T arriving as a DataType;
  • @with_handles reading operation_lock through the abstract Handle;
  • unique! over a Vector{Handle};
  • calculation instances and solutions borrowing through a Symbol looked up in a Dict.

Typed parse

As parse(T, str) does in Base, naming the value type returns that value:

net  = parse(BalancedNetwork, "case14.m")             # ::BalancedNetwork
m    = parse(PioModule{BalancedNetwork}, "case14.m")  # ::PioModule{BalancedNetwork}, for emit and updates
inst = parse(AcScucInstance, "scenario.json")         # ::AcScucInstance
parse("case14.m")                                     # ::PioModule, unchanged; also parse(PioModule, …)
  • Sources: each form takes a path, an IO, or bytes.
  • Inference: for a concrete V, both typed forms are inferred and compile under --trim=safe. The value keeps its module's data alive, as parse(path).value already did.
  • Families: V may be a family such as TimeSeries, CalculationInstance, or a Union. The value type is then resolved at run time and checked against V. PioModule{V} takes a concrete V, since a module type names one value type.
  • Errors are ArgumentErrors:
    • a source holding another kind of value names the source and what it holds, and the module is released at once;
    • a type parameter that is not a PowerIO value type, such as TimeSeries{Int}, is refused plainly.
  • deserialize takes the same forms for PowerIO IR.

Typed results elsewhere

  • Wrapping: one typed _wrap_value(V, …) serves parse, deserialize, the to_*_instance constructions, and time series and scenario set entries.
    • The to_*_instance constructions now return a concrete PioModule{I}.
    • Calculation instances and solutions borrow through static entry points.
  • ExaModelsPower bridge:
    • to_powerdata(x, T) and to_ac_power_data(x, T) take the element type positionally for a network, a module, or a path, and infer a concrete result, empty tables included. The keyword forms forward to them.
    • LoadSeries{T}(net, …) is the inferable form of each LoadSeries constructor.
  • Path helpers: those in ybus.jl and exa.jl use parse(BalancedNetwork, path; format) instead of repeating an untyped parse and an isa check.

Handles and tables

  • lib and operation_lock are read through typed accessors.
  • Handles are collected by dispatch over the roots, and repeats are dropped after the objectid sort instead of with unique!. Locking a single handle allocates nothing; net.base_mva went from 80 bytes to 0.
  • The bridge reads element tables through _table(net, Val(name)), which getproperty also uses, instead of relying on constant propagation.
  • base_mva and the value type name are each read in one place.

Trim check

test/trim builds and runs a trimmed app that uses:

  • parse(BalancedNetwork, …) with the positional bridge, on case14;
  • parse(PioModule{AcScucInstance}, …) on a GO Challenge 3 instance.

CI runs it on the 1 leg. The project pins JuliaC 0.3 and Julia 1.12, since its Manifest isn't committed.

Verification

  • Pkg.test(): on Julia 1.13.1, 2,296 passed and 1 broken; on 1.10, 1,976 passed and 1 broken. The broken one is the existing @test_skip in the C ABI coverage test.
  • julia --project=test/trim test/trim/build.jl (Julia 1.13.1, macOS): trim verification passes and the app prints 14 5 20 40 0 100.0 2.
  • Downstream: ExaModelsPower (Read cases through PowerIO (replaces #60) madsuite-org/ExaModelsPower.jl#68) calls parse(BalancedNetwork, …) and to_powerdata(net, T). With them, compile_all(ExaModelsPower) verifies and links all six models, and each compiled model's obj and cons match the in-Julia model exactly.

This rides the next paired release (0.11.7). ExaModelsPower #68 depends on it.

🤖 Generated with Claude Code

ExaModelsPower compiles its case reader into a library with
`juliac --trim=safe`, which refuses any call it cannot resolve statically.
PowerIO 0.9 had a typed path for that; the 0.11 rewrite dropped it, and a
trimmed build of `parse` plus `to_powerdata` failed verification with 90
errors.

- `parse(V, source)` (path, IO, bytes) checks the value's structural type
  name and wraps it as a concrete `PioModule{V}`, refusing a source that
  holds another kind of value with an `ArgumentError` that names it.
- `to_powerdata(net, T)` and `to_ac_power_data(net, T)` take the element
  type positionally; the keyword forms forward to them.
- Handle locking reads `lib` and `operation_lock` through typed accessors
  and collects handles by dispatch rather than `isa` tests and `unique!`
  over the abstract `Handle` type.
- The bridge reads element tables through `_table(net, Val(name))`, whose
  row type is static, instead of the large `getproperty`.

`test/trim` builds and runs a trimmed app that parses case14 and calls the
bridge; CI runs it on the `1` leg.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…rap path

- parse(V, source) returns a V, as parse(T, str) does in Base;
  parse(PioModule{V}, source) returns the module and parse(PioModule, source)
  is the untyped form. deserialize takes the same forms.
- A family V (TimeSeries, CalculationInstance, a Union) is resolved at run time
  and checked; a parameter that names no PowerIO value type is refused; a
  mismatch names the source and releases the module at once.
- Calculation instances and solutions borrow through static entry points, so
  parse(AcScucInstance, path) compiles under --trim=safe. One typed
  _wrap_value serves parse, deserialize, the to_*_instance constructions
  (now PioModule{I}), and time series and scenario set entries.
- to_powerdata and to_ac_power_data take T positionally for modules and paths;
  LoadSeries{T}(net, ...) is the inferable constructor. The path helpers use
  the typed parse.
- Locking a single handle no longer allocates; base_mva and the value type
  name are each read in one place.
- The trimmed app also parses a GO Challenge 3 instance; test/trim pins
  JuliaC 0.3 and Julia 1.12.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@frederikgeth frederikgeth left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved reviewed head b3eeb09a472461b532fce6eac70a8eab940d279a against current PowerIO.jl main 1a941f0893277a7e6f7d64803e06e1fd24e152a5. No blocking findings after reviewing typed parse/deserialize dispatch, static value and calculation wrapping, owner-rooted handle lifetimes, deduplication/lock ordering, keyword forwarding, typed table access and the positional ExaModelsPower bridge. Existing untyped forms and ABI 7 remain compatible; the typed overloads are additive.

Local full Pkg.test() against the published 0.11.6 macOS aarch64 C library passed on Julia 1.13.3 (2,296 passes, one existing platform skip) and Julia 1.10.12 (1,976 passes, one existing skip), with POWERIO_REQUIRE_LIBRARY=1 and four Julia threads. Eighteen supplemental assertions passed for inferred typed IO/byte-view parsing, typed IR IO/bytes, values surviving explicit module/collection handle release and GC, typed time-series/scenario entries and solution families, mismatch errors, mixed-root deterministic handle deduplication, and concrete empty generator/branch/arc/storage bridge tables.

Exact-head CI and Benchmarks passed. Hosted CI actually built the native main library, checked raw-layer regeneration, ran both Julia version legs and the documentation build, and executed the trimmed-app compile/run gate on the current Julia leg. Its output was trimmed app: 14 5 20 40 0 100.0 2. Trim coverage is verified from those hosted logs; a fresh local trim build is not claimed because JuliaC 0.3 is absent from the offline package cache.

This is an approved paired-0.11.7 candidate for the ExaModelsPower dependency. Final cross-repository integration, release preparation and maintainer approval remain separate gates. No code was changed or pushed and nothing was merged, registered, tagged or released.

@samtalki
samtalki merged commit 79815c2 into main Oct 27, 2026
4 checks passed
@samtalki
samtalki deleted the trim-safe-bridge branch October 27, 2026 02:32
sshin23 pushed a commit to samtalki/ExaModelsPower.jl that referenced this pull request Nov 17, 2026
Every case is parsed by PowerIO instead of ExaPowerIO, so any single-phase
network PowerIO reads (MATPOWER, PSS/E, PowerModels JSON, pandapower,
CGMES, ...) becomes an OPF case, and a network already parsed with PowerIO
can be passed wherever a file name was. ExaPowerIO is no longer a
dependency; the PGLib-OPF artifact it provided is declared here, with the
same tree hash.

`parse_ac_power_data` reads `PowerIO.to_powerdata(net, T)` and keeps what
is in service, in file order, as named isbits row types with ExaPowerIO's
field names, so no model body changes. It matches ExaPowerIO 0.3.4 field
for field on every PGLib-OPF case, except that `va` is in radians, storage
is remapped to model bus positions, and a lost reference bus moves to the
largest-Pmax generator (ExaPowerIO compared PMIN).

A load curve no longer scales bus shunts (madsuite-org#52). The MATPOWER writer
refuses a solution whose row counts do not match the case file.

The compiled library parses with `parse(BalancedNetwork, path)` and the
positional `to_powerdata(net, T)`, which pass `juliac --trim=safe` from
eigenergy/PowerIO.jl#184; PowerIO is pinned to that branch until 0.11.7.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants