|
4 | 4 | CollapsedDocStrings = false |
5 | 5 | ``` |
6 | 6 |
|
7 | | -In this manual, we explain how to use the [`solve`](@ref) function from [OptimalControl.jl](https://control-toolbox.org/OptimalControl.jl) on GPU. We rely on [MadNLP](https://github.com/MadNLP/MadNLP.jl) and currently only provide support for NVIDIA thanks to [CUDA.jl](https://github.com/JuliaGPU/CUDA.jl). |
| 7 | +In this manual, we explain how to use the [`solve`](@ref) function from [OptimalControl.jl](https://control-toolbox.org/OptimalControl.jl) on GPU. We rely on [ExaModels.jl](https://exanauts.github.io/ExaModels.jl/stable) and [MadNLPGPU.jl](https://github.com/MadNLP/MadNLP.jl) and currently only provide support for NVIDIA thanks to [CUDA.jl](https://github.com/JuliaGPU/CUDA.jl). Consider the following simple Lagrange optimal control problem: |
8 | 8 |
|
9 | | -```@docs; canonical=false |
10 | | -solve(::CTModels.Model, ::Symbol...) |
| 9 | + ```julia |
| 10 | +using OptimalControl |
| 11 | +using MadNLPGPU |
| 12 | +using CUDA |
| 13 | + |
| 14 | +ocp = @def begin |
| 15 | + t ∈ [0, 1], time |
| 16 | + x ∈ R², state |
| 17 | + u ∈ R, control |
| 18 | + v ∈ R, variable |
| 19 | + x(0) == [0, 1] |
| 20 | + x(1) == [0, -1] |
| 21 | + ∂(x₁)(t) == x₂(t) |
| 22 | + ∂(x₂)(t) == u(t) |
| 23 | + 0 ≤ x₁(t) + v^2 ≤ 1.1 |
| 24 | + -10 ≤ u(t) ≤ 10 |
| 25 | + 1 ≤ v ≤ 2 |
| 26 | + ∫(u(t)^2 + v) → min |
| 27 | +end |
11 | 28 | ``` |
12 | 29 |
|
13 | | -TBC |
| 30 | +!!! note |
| 31 | + We have used MadNLPGPU instead of MadNLP, that is able to solve on GPU (leveraging [CUDSS.jl](https://github.com/exanauts/CUDSS.jl)) optimisation problems modelled with ExaModels.jl. As a direct transcription towards an `ExaModels.ExaModel` is performed, there are limitations on the syntax: |
| 32 | + - dynamics must be declared coordinate by coordinate (not globally as a vector valued expression) |
| 33 | + - nonlinear constraints (boundary, variable, control, state, mixed ones, see [Constraints](@ref manual-abstract-constraints) must also be scalar expressions (linear constraints *aka.* ranges, on the other hand, can be vectors) |
| 34 | + - all expressions must only involve algebraic operations that are known to ExaModels (check the [documentation](https://exanauts.github.io/ExaModels.jl/stable)), although one can provide additional user defined functions through *registration* (check [ExaModels API](https://exanauts.github.io/ExaModels.jl/stable/core/#ExaModels.@register_univariate-Tuple%7BAny,%2520Any,%2520Any%7D)) |
| 35 | + |
| 36 | +Computation on GPU is currently only tested with CUDA, and the associated backend must be passed to ExaModels as is done below (also note the `:exa` keyword to indicate the modeller, and `:madnlp` for the solver): |
| 37 | + |
| 38 | +```julia |
| 39 | +julia> sol = solve(ocp, :exa, :madnlp; exa_backend=CUDABackend()) |
| 40 | +▫ This is OptimalControl version v1.1.0 running with: direct, exa, madnlp. |
| 41 | + |
| 42 | +▫ The optimal control problem is solved with CTDirect version v0.16.0. |
| 43 | + |
| 44 | + ┌─ The NLP is modelled with ExaModels and solved with MadNLP. |
| 45 | + │ |
| 46 | + ├─ Number of time steps⋅: 250 |
| 47 | + └─ Discretisation scheme: trapeze |
| 48 | + |
| 49 | +▫ This is MadNLP version v0.8.7, running with cuDSS v0.4.0 |
| 50 | + |
| 51 | +Number of nonzeros in constraint Jacobian............: 2506 |
| 52 | +Number of nonzeros in Lagrangian Hessian.............: 2006 |
| 53 | + |
| 54 | +Total number of variables............................: 754 |
| 55 | + variables with only lower bounds: 0 |
| 56 | + variables with lower and upper bounds: 252 |
| 57 | + variables with only upper bounds: 0 |
| 58 | +Total number of equality constraints.................: 504 |
| 59 | +Total number of inequality constraints...............: 251 |
| 60 | + inequality constraints with only lower bounds: 0 |
| 61 | + inequality constraints with lower and upper bounds: 251 |
| 62 | + inequality constraints with only upper bounds: 0 |
| 63 | + |
| 64 | +iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls |
| 65 | + 0 1.0200000e+00 1.10e+00 1.00e+00 -1.0 0.00e+00 - 0.00e+00 0.00e+00 0 |
| 66 | + ... |
| 67 | + 26 9.8902986e+00 2.22e-16 7.11e-15 -9.0 1.32e-04 - 1.00e+00 1.00e+00h 1 |
| 68 | + |
| 69 | +Number of Iterations....: 26 |
| 70 | + |
| 71 | + (scaled) (unscaled) |
| 72 | +Objective...............: 9.8902986337530514e+00 9.8902986337530514e+00 |
| 73 | +Dual infeasibility......: 7.1054273576010019e-15 7.1054273576010019e-15 |
| 74 | +Constraint violation....: 2.2204460492503131e-16 2.2204460492503131e-16 |
| 75 | +Complementarity.........: 4.8363494304578671e-09 4.8363494304578671e-09 |
| 76 | +Overall NLP error.......: 4.8363494304578671e-09 4.8363494304578671e-09 |
| 77 | + |
| 78 | +... |
| 79 | + |
| 80 | +EXIT: Optimal Solution Found (tol = 1.0e-08). |
| 81 | +``` |
0 commit comments