Skip to content

Commit

Permalink
Merge pull request #248 from sebapersson/add_pmap
Browse files Browse the repository at this point in the history
Minor doc updates and fix bug in sparse_jacobian default option
  • Loading branch information
sebapersson authored Oct 14, 2024
2 parents 6b4a72d + 38c7cbd commit 3852e92
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/src/pest_method.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ In principle, `x0s` can now be used together with `calibrate` to perform multi-s
calibrate_multistart
```

Two important keyword arguments for `calibrate_multistart` are `dirsave` and `nprocs`. If `nprocs > 1`, the parameter estimation runs are performed in parallel using the [`pmap`](https://docs.julialang.org/en/v1/stdlib/Distributed/#Distributed.pmap) function from Distributed.jl with `nprocs` processes. While `pmap` requires that everything is loaded on each process, which creates an overhead, if the multistart parameter estimation on a single process takes longer than 5 minutes, running in parallel often greatly reduces the total runtime. Meanwhile, `dirsave` specifies an optional directory to continuously save the results from each individual run. We **strongly recommend** providing such a directory, as parameter estimation for larger models can take hours or even days. If something goes wrong with the computer during that time, it is, to put it mildly, frustrating to lose all the results. For our working example, we can perform 50 multistarts in parallel on two processes with:
Two important keyword arguments for `calibrate_multistart` are `dirsave` and `nprocs`. If `nprocs > 1`, the parameter estimation runs are performed in parallel using the [`pmap`](https://docs.julialang.org/en/v1/stdlib/Distributed/#Distributed.pmap) function from Distributed.jl with `nprocs` processes. Even though `pmap` introduces some overhead because it must load and compile the code on each process, setting `nprocs > 1` often reduces runtime when the parameter estimation is expected to take longer than 5 minutes. Meanwhile, `dirsave` specifies an optional directory to continuously save the results from each individual run. We **strongly recommend** providing such a directory, as parameter estimation for larger models can take hours or even days. If something goes wrong with the computer during that time, it is, to put it mildly, frustrating to lose all the results. For our working example, we can perform 50 multistarts in parallel on two processes with:

```@example 1
ms_res = calibrate_multistart(petab_prob, IPNewton(), 50; nprocs = 2,
Expand Down
4 changes: 2 additions & 2 deletions docs/src/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ ms_res = calibrate_multistart(petab_prob, IPNewton(), 50)

The printout shows parameter estimation statistics, such as the best objective value `fmin` across all runs. For further details on what is stored in `ms_res` see the [API](@ref API) documentation for `PEtabMultistartResult`.

!!! tip "Parallelize parameter estimation"
Runtime for `calibrate_multistart` can often be reduced by performing parameter estimation runs in parallel by setting the number of parallel processes with the `nprocs` keyword argument. More details can be found [here](@ref multistart_est).
!!! tip "Parallelize Parameter Estimation"
Runtime of `calibrate_multistart` can often be reduced by performing parameter estimation runs in parallel. To do this, set the `nprocs` keyword argument, more details can be found [here](@ref multistart_est).

Following multi-start parameter estimation, it is important to evaluate the results. One common evaluation approach is plotting, and a frequently used evaluation plot is the waterfall plot, which in a sorted manner shows the final objective values for each run:

Expand Down
11 changes: 6 additions & 5 deletions src/parameter_estimation/multistart.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ Perform `nmultistarts` parameter estimation runs from randomly sampled starting
the optimization algorithm `alg` to estimate the unknown model parameters in `prob`.
A list of available and recommended optimisation algorithms (`alg`) can be found in the
package documentation and in the [`calibrate`](@ref) documentation. If `nprocs > 1`, the
parameter estimation runs are performed in parallel using the `pmap` function from
Distributed.jl with `nprocs` processes. If parameter estimation on a single process
package documentation and in the [`calibrate`](@ref) documentation.
If `nprocs > 1`, the parameter estimation runs are performed in parallel using the
[`pmap`](https://docs.julialang.org/en/v1/stdlib/Distributed/#Distributed.pmap) function
from Distributed.jl with `nprocs` processes. If parameter estimation on a single process
(`nprocs = 1`) takes longer than 5 minutes, we **strongly** recommend setting `nprocs > 1`,
because since multi-start parameter estimation is inherently parallel, performing it in
parallel can greatly reduce runtime. Note that `nprocs` should not be larger than the number
as this can greatly reduce runtime. Note that `nprocs` should not be larger than the number
of cores on the computer.
If `dirsave` is provided, intermediate results for each run are saved in `dirsave`. It is
Expand Down
4 changes: 3 additions & 1 deletion src/petab_odeproblem/defaults.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,10 @@ function _get_ss_solver(ss_solver::Union{SteadyStateSolver, Nothing})::SteadySta
return SteadyStateSolver(:Simulate)
end

function _get_sparse_jacobian(sparse::Union{Bool, Nothing}, model_size::Symbol)::Bool
function _get_sparse_jacobian(sparse::Union{Bool, Nothing}, gradient_method::Symbol,
model_size::Symbol)::Bool
!isnothing(sparse) && return sparse
gradient_method in [:ForwardDiff, :ForwardEquations] && return false
model_size == :Large && return true
return false
end
Expand Down
3 changes: 2 additions & 1 deletion src/petab_odeproblem/problem_info.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ function PEtabODEProblemInfo(model::PEtabModel, model_info::ModelInfo, odesolver
default_solver = odesolver_use)
_ss_solver = _get_ss_solver(ss_solver)
_ss_solver_gradient = _get_ss_solver(ss_solver_gradient)
sparse_jacobian_use = _get_sparse_jacobian(sparse_jacobian, model_size)
sparse_jacobian_use = _get_sparse_jacobian(sparse_jacobian, gradient_method_use,
model_size)
chunksize_use = isnothing(chunksize) ? 0 : chunksize

# Cache to avoid allocations to as large degree as possible. TODO: Refactor into single
Expand Down
1 change: 1 addition & 0 deletions test/defaults.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ using PEtab, SciMLSensitivity, OrdinaryDiffEq, Sundials, Test
@test prob.probinfo.split_over_conditions == false
prob = PEtabODEProblem(model; gradient_method=:ForwardDiff, verbose=false)
@test prob.probinfo.solver.solver isa KenCarp4
@test prob.probinfo.sparse_jacobian == false
end

0 comments on commit 3852e92

Please sign in to comment.