control-toolbox / control-toolbox/CTParser.jl
[Dev] Allocations ?
- Dominant language
- Julia
- Stars
- 3
- Forks
- 0
- Avg merge
- 4h 22m
- Merged PRs (30d)
- 2
Description
Hi @jbcaillau
Redoing some tests I noticed some allocations when using the abstract definition.
I don't think the performance impact is significant, but it is vexing ;-)
Below, example `goddard_all` uses the functional definition and `goddard` uses the abstract one. Types are still OK and no runtime dispatch is signaled, however some allocations seem to have crept back :D
```
julia> test_unit(goddard_all().ocp; test_trans=false, test_solve=false, jet=true)
Objective 4.516 ns (0 allocations: 0 bytes)
No errors detected
Constraints 5.923 μs (2 allocations: 3.38 KiB)
No errors detected
```
```
julia> test_unit(goddard().ocp; test_trans=false, test_solve=false, jet=true)
Objective 4.513 ns (0 allocations: 0 bytes)
No errors detected
Constraints 15.043 μs (406 allocations: 34.94 KiB)
No errors detected
```
Profiling (Source view) pinpoints the allocations at the dynamics calls in CTDirect
```
46 . 1 work = similar(xu, docp.dims.NLP_x * (docp.time.steps+1))
47 . .
48 . . # loop over time steps
49 . . for i = 1:docp.time.steps+1
50 . . offset = (i-1) * docp.dims.NLP_x
51 . . ti = time_grid[i]
52 . . xi = get_OCP_state_at_time_step(xu, docp, i)
53 . . ui = get_OCP_control_at_time_step(xu, docp, i)
54 . . # OCP dynamics
55 . 404 CTModels.dynamics(docp.ocp)((@view work[offset+1:offset+docp.dims.OCP_x]), ti, xi, ui, v)
```
with the underlying part in onepass.jl
```
Total: 0 404 (flat, cum) 99.51%
449 . . gs = gensym()
450 . . r = gensym()
451 . . args = [r, p.t, xt, ut, p.v]
452 . . code = quote
453 . . function $gs($(args...))
454 . 404 @views $r[:] .= $e
455 . . return nothing
456 . . end
457 . . $prefix.dynamics!($p_ocp, $gs)
458 . . end
459 . . return __wrap(code, p.lnum, p.line)
```
The dynamics for the goddard example involves aux functions F0 and F1, however I get the same kind of allocations for basic dynamics eg double integrator
```
julia> test_unit(double_integrator_mintf().ocp; test_obj=false, test_trans=false, test_solve=false, jet=true, profile=true)
```
gives the profile output
```
Total: 0 101 (flat, cum) 98.06%
449 . . gs = gensym()
450 . . r = gensym()
451 . . args = [r, p.t, xt, ut, p.v]
452 . . code = quote
453 . . function $gs($(args...))
454 . 101 @views $r[:] .= $e
455 . . return nothing
456 . . end
457 . . $prefix.dynamics!($p_ocp, $gs)
458 . . end
459 . . return __wrap(code, p.lnum, p.line)
```
Contributor guide
Assessment
This issue has not been assessed yet.