Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use PrecompileTools to compile basic model on build. #2114

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2083,7 +2083,7 @@ weakdeps = ["Distributed"]
DistributedExt = "Distributed"

[[deps.Ribasim]]
deps = ["ADTypes", "Accessors", "Arrow", "BasicModelInterface", "CodecZstd", "Configurations", "DBInterface", "DataInterpolations", "DataStructures", "Dates", "DiffEqBase", "DiffEqCallbacks", "EnumX", "FiniteDiff", "Graphs", "HiGHS", "IterTools", "JuMP", "Legolas", "LineSearches", "LinearAlgebra", "LinearSolve", "Logging", "LoggingExtras", "MetaGraphsNext", "OrdinaryDiffEqBDF", "OrdinaryDiffEqCore", "OrdinaryDiffEqLowOrderRK", "OrdinaryDiffEqNonlinearSolve", "OrdinaryDiffEqRosenbrock", "OrdinaryDiffEqSDIRK", "OrdinaryDiffEqTsit5", "PreallocationTools", "SQLite", "SciMLBase", "SparseArrays", "SparseConnectivityTracer", "StructArrays", "Tables", "TerminalLoggers", "TranscodingStreams"]
deps = ["ADTypes", "Accessors", "Arrow", "BasicModelInterface", "CodecZstd", "Configurations", "DBInterface", "DataInterpolations", "DataStructures", "Dates", "DiffEqBase", "DiffEqCallbacks", "EnumX", "FiniteDiff", "Graphs", "HiGHS", "IterTools", "JuMP", "Legolas", "LineSearches", "LinearAlgebra", "LinearSolve", "Logging", "LoggingExtras", "MetaGraphsNext", "OrdinaryDiffEqBDF", "OrdinaryDiffEqCore", "OrdinaryDiffEqLowOrderRK", "OrdinaryDiffEqNonlinearSolve", "OrdinaryDiffEqRosenbrock", "OrdinaryDiffEqSDIRK", "OrdinaryDiffEqTsit5", "PreallocationTools", "PrecompileTools", "Preferences", "SQLite", "SciMLBase", "SparseArrays", "SparseConnectivityTracer", "StructArrays", "Tables", "TerminalLoggers", "TranscodingStreams"]
path = "core"
uuid = "aac5e3d9-0b8f-4d4f-8241-b1a7a9632635"
version = "2025.1.0"
Expand Down
3 changes: 3 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,6 @@ TranscodingStreams = "3bb67fe8-82b1-5028-8e26-92a6c54297fa"

[sources]
Ribasim = {path = "core"}

[preferences.Ribasim]
precompile_workload = false
26 changes: 26 additions & 0 deletions build/precompile.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
# Workflow that will compile a lot of the code we will need.
# With the purpose of reducing the latency for compiled binaries.

using Preferences, UUIDs
# Commented out due to segfault on building
# set_preferences!(
# UUID("0bca4576-84f4-4d90-8ffe-ffa030f20462"), # SciMLBase
# "SpecializationLevel" => "FullSpecialize";
# force = true,
# )
Comment on lines +5 to +10
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# Commented out due to segfault on building
# set_preferences!(
# UUID("0bca4576-84f4-4d90-8ffe-ffa030f20462"), # SciMLBase
# "SpecializationLevel" => "FullSpecialize";
# force = true,
# )

I made #2130.

set_preferences!(
UUID("aac5e3d9-0b8f-4d4f-8241-b1a7a9632635"), # Ribasim
"precompile_workload" => true;
force = true,
)
using Ribasim

toml_path = normpath(@__DIR__, "../generated_testmodels/basic/ribasim.toml")
# This should now only take a second or less
@assert Ribasim.main(toml_path) == 0

# Remove preferences to avoid affecting normal Ribasim usage
# Commented out due to segfault on building
# set_preferences!(
# UUID("0bca4576-84f4-4d90-8ffe-ffa030f20462"), # SciMLBase
# "SpecializationLevel" => missing;
# force = true,
# )
Comment on lines +23 to +28
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# Commented out due to segfault on building
# set_preferences!(
# UUID("0bca4576-84f4-4d90-8ffe-ffa030f20462"), # SciMLBase
# "SpecializationLevel" => missing;
# force = true,
# )

set_preferences!(
UUID("aac5e3d9-0b8f-4d4f-8241-b1a7a9632635"), # Ribasim
"precompile_workload" => missing;
force = true,
)
7 changes: 7 additions & 0 deletions core/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ OrdinaryDiffEqRosenbrock = "43230ef6-c299-4910-a778-202eb28ce4ce"
OrdinaryDiffEqSDIRK = "2d112036-d095-4a1e-ab9a-08536f3ecdbf"
OrdinaryDiffEqTsit5 = "b1df2697-797e-41e3-8120-5422d3b24e4a"
PreallocationTools = "d236fae5-4411-538c-8e31-a6e3d9e00b46"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
SQLite = "0aa819cd-b072-5ff4-a722-6bc24af294d9"
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Expand Down Expand Up @@ -85,6 +87,8 @@ OrdinaryDiffEqRosenbrock = "1.1"
OrdinaryDiffEqSDIRK = "1.1"
OrdinaryDiffEqTsit5 = "1.1"
PreallocationTools = "0.4"
PrecompileTools = "1.2.1"
Preferences = "1.4.3"
SQLite = "1.5.1"
SciMLBase = "2.36"
SparseArrays = "1"
Expand Down Expand Up @@ -114,5 +118,8 @@ TestItemRunner = "f8b46487-2199-4994-9208-9a1283c18c0a"
[preferences.LinearSolve]
LoadMKL_JLL = false

[preferences.Ribasim]
precompile_workload = false

[targets]
test = ["Aqua", "CSV", "DataFrames", "IOCapture", "Logging", "Statistics", "TerminalLoggers", "Test", "TOML", "TestItemRunner"]
10 changes: 10 additions & 0 deletions core/src/Ribasim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ For more granular access, see:
"""
module Ribasim

using PrecompileTools: @setup_workload, @compile_workload

# Algorithms for solving ODEs.
using OrdinaryDiffEqCore: OrdinaryDiffEqCore, get_du, AbstractNLSolver
using DiffEqBase: DiffEqBase, calculate_residuals!
Expand Down Expand Up @@ -150,4 +152,12 @@ include("concentration.jl")
include("main.jl")
include("libribasim.jl")

@setup_workload begin
toml_path = normpath(@__DIR__, "../../generated_testmodels/basic/ribasim.toml")
isfile(toml_path) || return
@compile_workload begin
Ribasim.main(toml_path)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Ribasim.main(toml_path)
main(toml_path)

end
end

end # module Ribasim
41 changes: 33 additions & 8 deletions core/src/parameter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,40 @@
5 Drainage = 6 Precipitation = 7
Base.to_index(id::Substance.T) = Int(id) # used to index into concentration matrices

@generated function config.snake_case(nt::NodeType.T)
ex = quote end
for (sym, _) in EnumX.symbol_map(NodeType.T)
sc = QuoteNode(config.snake_case(sym))
t = NodeType.T(sym)
push!(ex.args, :(nt === $t && return $sc))
function config.snake_case(nt::NodeType.T)::Symbol
if nt == NodeType.Basin
return :basin
elseif nt == NodeType.TabulatedRatingCurve
return :tabulated_rating_curve
elseif nt == NodeType.Pump
return :pump
elseif nt == NodeType.Outlet
return :outlet
elseif nt == NodeType.UserDemand
return :user_demand
elseif nt == NodeType.FlowDemand
return :flow_demand
elseif nt == NodeType.LevelDemand
return :level_demand
elseif nt == NodeType.FlowBoundary
return :flow_boundary
elseif nt == NodeType.LevelBoundary
return :level_boundary
elseif nt == NodeType.LinearResistance
return :linear_resistance
elseif nt == NodeType.ManningResistance
return :manning_resistance
elseif nt == NodeType.Terminal
return :terminal
elseif nt == NodeType.DiscreteControl
return :discrete_control
elseif nt == NodeType.ContinuousControl
return :continuous_control
elseif nt == NodeType.PidControl
return :pid_control
else
error("Unknown node type: $nt")

Check warning on line 56 in core/src/parameter.jl

View check run for this annotation

Codecov / codecov/patch

core/src/parameter.jl#L56

Added line #L56 was not covered by tests
end
push!(ex.args, :(return :nothing)) # type stability
ex
end

# Support creating a NodeType enum instance from a symbol or string
Expand Down