Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 54 additions & 3 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,56 @@ jobs:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}

# Master (pushes, the nightly) restores and saves the depot cache: the
# artifact store, package store, registries and the compiled-modules
# directory. The compiled cache is the biggest warm-run win - without it
# precompile reruns from scratch.
- name: Cache Julia depot
if: github.event_name != 'pull_request'
uses: julia-actions/cache@v2
with:
# Cache the artifact store, package store, registries and the
# compiled-modules directory. The compiled cache is the biggest
# warm-run win - without it precompile reruns from scratch.
cache-name: ombackend
include-matrix: true

# A pull request restores master's cache and saves none: each PR run
# saved 0.8-2.6 GB per OS, the repository went over its 10 GB and GitHub
# evicted master's caches. The paths and the key prefix are those of
# julia-actions/cache above (a cache is found only under the same paths).
- name: Depot cache paths (pull request)
if: github.event_name == 'pull_request'
id: depot
shell: bash
run: |
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" ]]; then
depot="${USERPROFILE//\\//}/.julia"
else
depot="$HOME/.julia"
fi
{
echo "paths<<EOF"
printf "%s\n" "$depot/artifacts" "$depot/packages" "$depot/registries" "$depot/compiled" \
"$depot/scratchspaces" "$depot/logs"
echo "EOF"
} >> "$GITHUB_OUTPUT"
echo "key=ombackend;os=${{ runner.os }};os=${{ matrix.os }};arch=${{ matrix.arch }};version=${{ matrix.version }};" >> "$GITHUB_OUTPUT"

- name: Restore Julia depot from master (pull request)
if: github.event_name == 'pull_request'
uses: actions/cache/restore@v4
with:
path: ${{ steps.depot.outputs.paths }}
key: ${{ steps.depot.outputs.key }}
restore-keys: ${{ steps.depot.outputs.key }}
enableCrossOsArchive: false

- name: Add OpenModelica Julia registry
shell: julia --color=yes {0}
run: |
import Pkg
Pkg.Registry.add("General")
Pkg.Registry.add(Pkg.RegistrySpec(url = "https://github.com/OpenModelica/OpenModelicaRegistry.git"))
# a restored registry is as old as its cache (julia-actions/cache updates it on master; a PR's restore does not)
Pkg.Registry.update()

- name: Develop the sibling checkouts
shell: julia --color=yes --project=OMBackend.jl {0}
Expand All @@ -100,6 +135,22 @@ jobs:
with:
project: OMBackend.jl

# Which of our packages the restored depot has precompiled for the test
# run (its flags: coverage on Linux), and why a cache is rejected
# (JULIA_DEBUG=loading: "Rejecting ... because ..."). Unchanged siblings
# should come from the cache; OMBackend itself is rebuilt when it changed.
- name: Precompile cache report
shell: bash
env:
JULIA_DEBUG: loading
COVERAGE_FLAG: ${{ runner.os == 'Linux' && format('--code-coverage=@{0}/OMBackend.jl', github.workspace) || '' }}
run: |
julia --color=yes --project=OMBackend.jl --depwarn=yes $COVERAGE_FLAG -e '
for name in ("ImmutableList", "MetaModelica", "OMParser", "OMFrontend", "OMRuntimeExternalC", "OMBackend")
local id = Base.identify_package(name)
println(name, ": ", Base.isprecompiled(id; ignore_loaded = true) ? "cached" : "to precompile")
end'

- name: Run tests
uses: julia-actions/julia-runtest@v1
env:
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "OMBackend"
uuid = "9400bd5f-7561-42f6-8760-03756753da50"
version = "0.11.2"
version = "0.11.3"
authors = ["John Tinnerholm <johti17@liu.se>", "Karim Abdelhak <karim.abdelhak@fh-bielefeld.de>", "AnHeuermann <andreas.heuermann@fh-bielefeld.de>"]

[deps]
Expand Down
2 changes: 1 addition & 1 deletion ext/OMBackendOMFrontendExt/translate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ function _translateKept(fm::F.FlatModel, model::OMJLIR.Model, path::String, subj
local result::Tuple{String, Expr} =
translate(fm; functionList, BackendMode, warnMissingStartValues, eliminateNonDynamic, observedFilter,
checkSimCode, omjlir = model)
(BackendMode == IMTK_MODE && !haskey(IMTKGen.BUILT, name)) || return result
(BackendMode == IMTK_MODE && !haskey(IMTKGen.BUILT, name) && !(name in IMTKGen.BUILT_AT_SIMULATE)) || return result
reason = "does not build"
catch err
isFatal(err) && rethrow()
Expand Down
113 changes: 96 additions & 17 deletions src/Backend/Passes/IndexReduction/arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ element's reference as the scalarized model names it (`c[2].p.T` of the array `c
record array's; `\$DER.c[2].T` of the backend's `\$DER.c.T`). `passesFollow`: the view is of a model as lowered, for
the scalar passes (`scalarizeArrays(model; passesFollow = true)`), and keeps what they take as the frontend's
scalarization does: a reference by a subscript known at run time only (`chooseRuntimeIndices!`), a record equation
of a call (`fieldEquations`). The view the array form's index reduction plans on does not.
of a call (`fieldEquations`). The view the array form's index reduction plans on does not. `structural[p]` the value
of the structural Integer parameter `p` (`structuralValues`): a range over it (`1:N`) has its translated value.
"""
struct Elements
elements::Dict{Symbol, Vector{Symbol}}
sizes::Dict{Symbol, Vector{Int}}
array::Dict{Symbol, Tuple{Symbol, Vector{Int}}}
crefs::Dict{Symbol, IR.ComponentRef}
structural::Dict{Symbol, Int}
passesFollow::Bool
end

Expand Down Expand Up @@ -175,7 +177,7 @@ already (a record field array of a parameter, `expandRecordFieldArrays`). `passe
"""
function elements(variables::IR.VariableSet; passesFollow::Bool = false)::Elements
local E = Elements(Dict{Symbol, Vector{Symbol}}(), Dict{Symbol, Vector{Int}}(), Dict{Symbol, Tuple{Symbol, Vector{Int}}}(),
Dict{Symbol, IR.ComponentRef}(), passesFollow)
Dict{Symbol, IR.ComponentRef}(), structuralValues(variables), passesFollow)
local derived::Vector{Tuple{IR.Variable, Vector{IR.CrefPart}, Symbol}} = Tuple{IR.Variable, Vector{IR.CrefPart}, Symbol}[]
#= the arrays of rolled components first (their elements may be another array's, kept beside them: rollComponents!) =#
local rolledFirst::Vector{IR.Variable} = IR.Variable[v for v in variables if IR.isRolled(v.cref.parts[1].ty)]
Expand All @@ -201,6 +203,50 @@ function elements(variables::IR.VariableSet; passesFollow::Bool = false)::Elemen
return E
end

"""
structuralValues(variables) -> Dict{Symbol, Int}

The values of the structural Integer parameters of `variables` (`IR.PARAMETER(true)`: the frontend evaluated them,
the generated code holds their translated values) whose bindings fold to an Integer, also through other structural
parameters (`M = 2 * N`).
"""
function structuralValues(variables::IR.VariableSet)::Dict{Symbol, Int}
local values::Dict{Symbol, Int} = Dict{Symbol, Int}()
local todo::Vector{IR.Variable} = IR.Variable[v for v in variables
if v.kind isa IR.PARAMETER && v.kind.structural && v.binding !== nothing]
local progress::Bool = true
while progress && !isempty(todo)
progress = false
for k in length(todo):-1:1
local value::Union{Nothing, Int} = foldInteger(withStructural(todo[k].binding, values))
value === nothing && continue
values[todo[k].name] = value
deleteat!(todo, k)
progress = true
end
end
return values
end

"`e` with the references to the structural parameters of `values` (`structuralValues`) replaced by their values."
function withStructural(e::IR.Expression, values::Dict{Symbol, Int})::IR.Expression
isempty(values) && return e
local replace = (@nospecialize(x::IR.Expression)) -> begin
(x isa IR.CREF && !IR.hasSubscripts(x.cref) && haskey(values, x.cref.name)) || return x
return IR.INTEGER(values[x.cref.name])
end
return IR.mapExp(replace, e)
end

"""
iterationRange(range, values, E) -> Union{Nothing, StepRange{Int, Int}}

The values of a loop's `range` at the iterators `values`: literal once the iterators and the structural parameters
(`E.structural`) are replaced (`1:N`); `nothing` where it is not.
"""
iterationRange(range::IR.Expression, values::Dict{Symbol, Int}, E::Elements)::Union{Nothing, StepRange{Int, Int}} =
literalRange(withStructural(scalarExpression(range, values, E), E.structural))

"""
addDerivedElements!(E, v, operators, of)

Expand Down Expand Up @@ -233,7 +279,7 @@ end
made of), a new `Elements`; `E` not changed. NotHandled for an added array that is no backend-made one.
"""
function extendedElements(E::Elements, added::Vector{IR.Variable}, variables::IR.VariableSet)::Elements
local out = Elements(copy(E.elements), copy(E.sizes), copy(E.array), copy(E.crefs), E.passesFollow)
local out = Elements(copy(E.elements), copy(E.sizes), copy(E.array), copy(E.crefs), E.structural, E.passesFollow)
local derived::Vector{Tuple{IR.Variable, Vector{IR.CrefPart}, Symbol}} = Tuple{IR.Variable, Vector{IR.CrefPart}, Symbol}[]
for v in added
v.ty isa IR.T_ARRAY || continue
Expand Down Expand Up @@ -883,7 +929,7 @@ by `scalarExpression`). NotHandled for a for-equation over a range that is not c
"""
function scalarEquations!(out::Vector{IR.Equation}, origins::Vector{EquationOrigin}, eq::IR.FOR_EQUATION,
origin::EquationOrigin, values::Dict{Symbol, Int}, E::Elements)::Nothing
local range = literalRange(scalarExpression(eq.iterator.range, values, E))
local range::Union{Nothing, StepRange{Int, Int}} = iterationRange(eq.iterator.range, values, E)
range === nothing && throw(NotHandled("a for-equation over a range that is not constant ($(IR.toString(eq.iterator.range)))"))
local inner = copy(values)
local paths = Vector{Int}[vcat(origin.path, b) for b in eachindex(eq.body)]
Expand Down Expand Up @@ -980,7 +1026,7 @@ function scalarEquations!(out::Vector{IR.Equation}, origins::Vector{EquationOrig
origin::EquationOrigin, values::Dict{Symbol, Int}, E::Elements)::Nothing
local loop::Union{Nothing, IR.FOR_STATEMENT} = E.passesFollow ? independentLoop(eq.statements) : nothing
local range::Union{Nothing, StepRange{Int, Int}} =
loop === nothing ? nothing : literalRange(scalarExpression(loop.iterator.range, values, E))
loop === nothing ? nothing : iterationRange(loop.iterator.range, values, E)
if loop !== nothing && range !== nothing
#= one algorithm per iteration, as the frontend's scalarization has one per component =#
local inner::Dict{Symbol, Int} = copy(values)
Expand Down Expand Up @@ -1285,27 +1331,60 @@ function scalarizeArrays(model::IR.Model; passesFollow::Bool = false,
cache::Union{Nothing, ScalarCache} = nothing)::Scalarized
(length(model.continuous) == 1 && isempty(model.clocked) && isempty(model.modes)) ||
throw(NotHandled("arrays in a model of several partitions or structural modes"))
local E = elements(model.variables; passesFollow)
local variables = scalarVariables(model.variables, E)
local p = model.continuous[1]
local equations = IR.Equation[]
local origins = EquationOrigin[]
local E::Elements = elements(model.variables; passesFollow)
local (partition::IR.Partition, origins::Vector{EquationOrigin}) = scalarPartition(model.continuous[1], E, cache)
local scalar::IR.Model = IR.Model(model.name, scalarVariables(model.variables, E), IR.Partition[partition], model.clocked,
model.baseClocks, model.functions, model.initialMode, model.modes, model.transitions,
model.frontendModel)
return Scalarized(scalar, origins, E)
end

"""
scalarPartition(p, E, cache) -> (partition, origins)

The partition `p` with its equations and initial equations as their elements (`scalarEquationsOf!`) and its array
unknowns as their elements (`E`), and the origins of its element equations.
"""
function scalarPartition(p::IR.Partition, E::Elements,
cache::Union{Nothing, ScalarCache})::Tuple{IR.Partition, Vector{EquationOrigin}}
local equations::Vector{IR.Equation} = IR.Equation[]
local origins::Vector{EquationOrigin} = EquationOrigin[]
for (i, eq) in enumerate(p.equations)
scalarEquationsOf!(equations, origins, eq, i, E, cache)
end
local initialEquations = IR.Equation[]
local initialOrigins = EquationOrigin[]
local initialEquations::Vector{IR.Equation} = IR.Equation[]
local initialOrigins::Vector{EquationOrigin} = EquationOrigin[]
for (i, eq) in enumerate(p.initialEquations)
scalarEquationsOf!(initialEquations, initialOrigins, eq, i, E, cache)
end
local unknowns = Symbol[]
local unknowns::Vector{Symbol} = Symbol[]
for n in p.unknowns
haskey(E.elements, n) ? append!(unknowns, E.elements[n]) : push!(unknowns, n)
end
local partition = IR.Partition(p.index, p.association, unknowns, equations, initialEquations)
local scalar = IR.Model(model.name, variables, IR.Partition[partition], model.clocked, model.baseClocks, model.functions,
model.initialMode, model.modes, model.transitions, model.frontendModel)
return Scalarized(scalar, origins, E)
return (IR.Partition(p.index, p.association, unknowns, equations, initialEquations), origins)
end

"""
scalarizeModel(model) -> IR.Model

`model` as lowered with its arrays as their elements, for the scalar passes (`scalarizeArrays` with `passesFollow`)
in every part of it: its continuous and clocked partitions, and each structural mode with its own variables. The
backend's scalarization (`backendScalarizedModel`) of a model the array form does not take. The model is not
changed.
"""
function scalarizeModel(model::IR.Model)::IR.Model
local E::Elements = elements(model.variables; passesFollow = true)
return IR.Model(model.name, scalarVariables(model.variables, E),
IR.Partition[first(scalarPartition(p, E, nothing)) for p in model.continuous],
IR.Partition[first(scalarPartition(p, E, nothing)) for p in model.clocked], model.baseClocks,
model.functions, model.initialMode, IR.StructuralMode[scalarMode(m) for m in model.modes],
model.transitions, model.frontendModel)
end

"The structural mode `m` with its variables and its system as their elements (`scalarizeModel`)."
function scalarMode(m::IR.StructuralMode)::IR.StructuralMode
local E::Elements = elements(m.variables; passesFollow = true)
return IR.StructuralMode(m.name, scalarVariables(m.variables, E), first(scalarPartition(m.system, E, nothing)))
end

"""
Expand Down
6 changes: 5 additions & 1 deletion src/CodeGeneration/IMTKGen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ const DUMP_ENABLED = Ref(false)
by the generated `<name>Model(tspan)`: (problem, callbacks, ivs, _ivs_all,
reducedSystem, tspan, pars, vars, irreducibleSyms). =#
const BUILT = Dict{String, Tuple}()
#= The models whose build is their module's own simulate (structural transitions, sub-models: `generateIMTKCode`
skips the build at translate), so no `BUILT` entry: translated all the same. =#
const BUILT_AT_SIMULATE = Set{String}()
#= Hash of (modelCode, build-affecting flags) for each cached build. A repeat
translate (e.g. overwriteCache) whose regenerated code and flags are
unchanged reuses the live module + cached build instead of re-eval'ing, which
Expand All @@ -100,7 +103,7 @@ another mode): an older build must not answer for the current one.
"""
function forgetBuild(cname::String)
_forgetReinit(cname)
for cache in (BUILT, BUILT_HASH, REDUCED_SYSTEMS, PRISTINE_P, TUNABLE_SETS)
for cache in (BUILT, BUILT_AT_SIMULATE, BUILT_HASH, REDUCED_SYSTEMS, PRISTINE_P, TUNABLE_SETS)
delete!(cache, cname)
end
#= and its external objects (tables): kept, every translate of the model left its tables
Expand Down Expand Up @@ -147,6 +150,7 @@ function generateIMTKCode(simCode::SC.SIM_CODE)
_buildAndCache(modelName, modelCode)
else
forgetBuild(cname)
push!(BUILT_AT_SIMULATE, cname)
@info "[IMTK GEN] structural / sub-model path; build-cache skipped (iMTK delegates to MTK simulate)" model = modelName
end
return (modelName, modelCode)
Expand Down
20 changes: 16 additions & 4 deletions src/Runtime/Runtime.jl
Original file line number Diff line number Diff line change
Expand Up @@ -391,11 +391,11 @@ function solve(omProblem::OM_ProblemStructural, tspan, alg; kwargs...)
local u0Dict = _buildTransferU0Dict(newSystem, oldSystem, integrator,
cb.name, activeModeName,
omProblem.topVariables)
#= MTK 10+: one operating point, the inherited unknowns and the new mode's parameter values =#
local daeProb = ModelingToolkit.ODEProblem(
newSystem.f.sys,
u0Dict,
(i.t, tspan[2]),
newSystem.p;
merge(u0Dict, _parameterValues(newSystem)),
(i.t, tspan[2]);
build_initializeprob = true,
warn_initialize_determined = false,
callback = CallbackSet(cb.systemSpecificCallbacks, omProblem.callbackConditions...)
Expand Down Expand Up @@ -501,6 +501,17 @@ function _isDAETransferTarget(newSystem)::Bool
end
end

"""
_parameterValues(problem::ODEProblem) -> Dict{Any, Any}

The values of the parameters of the MTK system of `problem` in `problem.p` (symbolic indexing), for the operating
point of a problem rebuilt from that system (`ODEProblem(sys, op, tspan)`).
"""
function _parameterValues(problem::ODEProblem)::Dict{Any, Any}
local sys = problem.f.sys
return Dict{Any, Any}(p => problem.ps[p] for p in ModelingToolkit.parameters(sys))
end

"""
Build a Dict mapping MTK symbolic variables of the new mode to inherited
numeric values pulled from the old mode's integrator (state first, then
Expand Down Expand Up @@ -676,8 +687,9 @@ function _switchMode(change::AbstractStructuralChange, problem::ODEProblem, inte
_fill_observed_u0!(newU0, oldSyms, newProblem, change.solutionAtChange)
@VSS_DEBUG @info "[solve OM_ProblemRecompilation] new u0" newU0 anyNaN = any(isnan, newU0)
#= In the latest world, as the steps: init runs the new mode's callbacks' initialization (a table's time events). =#
#= the solver options (`kwargs`: tolerances) go to init; reinit! takes none of them =#
local newIntegrator::DEIntegrator = Base.invokelatest(init, newProblem, alg; force_dtmin = true, kwargs...)
Base.invokelatest(reinit!, newIntegrator, newU0; t0 = tChange, tf = last(tspan), reset_dt = true, kwargs...)
Base.invokelatest(reinit!, newIntegrator, newU0; t0 = tChange, tf = last(tspan), reset_dt = true)
#= The structural event may trigger again in this step: reset after it. =#
newIntegrator.dt = min(newIntegrator.dt, 1e-6)
Base.invokelatest(step!, newIntegrator, newIntegrator.dt, true)
Expand Down
Loading
Loading