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

Add PrecompileTools precompilation #49

Open
wants to merge 3 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: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
NCDatasets = "85f8d34a-cbdd-5861-8df4-14fed0d494ab"
NLsolve = "2774e3e8-f4cf-5e23-947b-6d7e65073b56"
PALEOboxes = "804b410e-d900-4b2a-9ecd-f5a06d4c1fd4"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
Expand All @@ -39,6 +40,7 @@ JLD2 = "0.4"
NCDatasets = "0.12"
NLsolve = "4.5"
PALEOboxes = "0.21.7"
PrecompileTools = "1.0"
RecipesBase = "1.2"
Requires = "1.0"
Revise = "3.1"
Expand Down
74 changes: 70 additions & 4 deletions src/PALEOmodel.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
module PALEOmodel

import PALEOboxes as PB

# autodiff setup
import ForwardDiff
import Logging
import ForwardDiff
import StaticArrays
import SparsityTracing
import PrecompileTools

import PALEOboxes as PB



import TimerOutputs: @timeit, @timeit_debug

Expand Down Expand Up @@ -58,5 +61,68 @@ include("ReactionNetwork.jl")

include("ForwardDiffWorkarounds.jl")

# workload for PrecompileTools
function run_example_workload()

# Minimal model
model = PB.create_model_from_config(
joinpath(@__DIR__, "../test/configreservoirs.yaml"),
"model1",
)

initial_state, modeldata = PALEOmodel.initialize!(model)

# DAE solver
paleorun = PALEOmodel.Run(model=model, output = PALEOmodel.OutputWriters.OutputMemory())
PALEOmodel.ODE.integrateDAEForwardDiff(
paleorun, initial_state, modeldata, (0.0, 1.0),
solvekwargs=(reltol=1e-5,),
)

# ODE solver
paleorun = PALEOmodel.Run(model=model, output = PALEOmodel.OutputWriters.OutputMemory())
PALEOmodel.ODE.integrateForwardDiff(
paleorun, initial_state, modeldata, (0.0, 1.0),
solvekwargs=(reltol=1e-5,),
)


# save and load
tmpfile = tempname(; cleanup=true)
output = paleorun.output
PALEOmodel.OutputWriters.save_netcdf(output, tmpfile; check_ext=false)
load_output = PALEOmodel.OutputWriters.load_netcdf!(PALEOmodel.OutputWriters.OutputMemory(), tmpfile; check_ext=false)

# FieldArray
O_array = PALEOmodel.get_array(load_output, "global.O")
T_conc_array = PALEOmodel.get_array(load_output, "ocean.T_conc", (cell=1,))

return nothing
end


@PrecompileTools.setup_workload begin

# Putting some things in `setup` can reduce the size of the
# precompile file and potentially make loading faster.

logger = Logging.NullLogger()
# logger = Logging.ConsoleLogger()

@PrecompileTools.compile_workload begin
# all calls in this block will be precompiled, regardless of whether
# they belong to your package or not (on Julia 1.8 and higher)

try
Logging.with_logger(logger) do
run_example_workload()
end

catch ex
@info "precompile failed with exception:" ex
end
end

end

end