JuMP Interface
This tutorial explains how to use ExaModels with JuMP.
As an Optimizer
ExaModels can be called from JuMP using ExaModels.Optimizer. The first argument to Optimizer is any NLPModels-compatible solver.
using JuMP
import ExaModels
import NLPModelsIpopt
N = 10
model = Model(() -> ExaModels.Optimizer(NLPModelsIpopt.ipopt))
@variable(model, x[i in 1:N], start = mod(i, 2) == 1 ? -1.2 : 1.0)
@constraint(
model,
[i in 1:(N-2)],
(3 * x[i+1]^3 + 2 * x[i+2] - 5) +
sin(x[i+1] - x[i+2]) * sin(x[i+1] + x[i+2]) +
4 * x[i+1] - x[i] * exp(x[i] - x[i+1]) - 3 == 0.0
)
@objective(
model,
Min,
sum(100 * (x[i-1]^2 - x[i])^2 + (x[i-1] - 1)^2 for i in 2:N),
)
optimize!(model)This is Ipopt version 3.14.19, running with linear solver MUMPS 5.9.0.
Number of nonzeros in equality constraint Jacobian...: 56
Number of nonzeros in inequality constraint Jacobian.: 0
Number of nonzeros in Lagrangian Hessian.............: 100
Total number of variables............................: 10
variables with only lower bounds: 0
variables with lower and upper bounds: 0
variables with only upper bounds: 0
Total number of equality constraints.................: 8
Total number of inequality constraints...............: 0
inequality constraints with only lower bounds: 0
inequality constraints with lower and upper bounds: 0
inequality constraints with only upper bounds: 0
iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls
0 2.0570000e+03 2.48e+01 2.73e+01 -1.0 0.00e+00 - 0.00e+00 0.00e+00 0
1 1.0953147e+03 1.49e+01 8.27e+01 -1.0 2.20e+00 - 1.00e+00 1.00e+00f 1
2 3.2865521e+02 4.28e+00 1.36e+02 -1.0 1.43e+00 - 1.00e+00 1.00e+00f 1
3 1.3995370e+01 3.09e-01 2.18e+01 -1.0 5.63e-01 - 1.00e+00 1.00e+00f 1
4 6.2325715e+00 1.73e-02 8.47e-01 -1.0 2.10e-01 - 1.00e+00 1.00e+00f 1
5 6.2324586e+00 1.15e-05 8.16e-04 -1.7 3.35e-03 - 1.00e+00 1.00e+00h 1
6 6.2324586e+00 8.36e-12 7.97e-10 -5.7 2.00e-06 - 1.00e+00 1.00e+00h 1
Number of Iterations....: 6
(scaled) (unscaled)
Objective...............: 7.8692659500473061e-01 6.2324586324374671e+00
Dual infeasibility......: 7.9746906147943578e-10 6.3159549669171321e-09
Constraint violation....: 8.3553164387240031e-12 8.3553164387240031e-12
Variable bound violation: 0.0000000000000000e+00 0.0000000000000000e+00
Complementarity.........: 0.0000000000000000e+00 0.0000000000000000e+00
Overall NLP error.......: 7.9746906147943578e-10 6.3159549669171321e-09
Number of objective function evaluations = 7
Number of objective gradient evaluations = 7
Number of equality constraint evaluations = 7
Number of inequality constraint evaluations = 0
Number of equality constraint Jacobian evaluations = 7
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations = 6
Total seconds in IPOPT = 2.772
EXIT: Optimal Solution Found.
Behind the scenes, ExaModels.Optimizer converts the JuMP model into an equivalent ExaModels.ExaModel before passing it to NLPModelsIpopt.
For large structured nonlinear models, using ExaModels.Optimizer can be significantly faster than using Ipopt.Optimizer directly because ExaModels.Optimizer uses ExaModel's automatic differentiation routines instead using JuMP's default automatic differentiation library.
Accessing the ExaModel
You can also construct the ExaModel directly:
exa_model = ExaModels.ExaModel(model)An ExaModel{Float64, Vector{Float64}, ...}
Problem name: Generic
All variables: ████████████████████ 10 All constraints: ████████████████████ 8
free: ████████████████████ 10 free: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0
lower: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0 lower: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0
upper: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0 upper: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0
low/upp: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0 low/upp: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0
fixed: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0 fixed: ████████████████████ 8
infeas: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0 infeas: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0
nnzh: (-81.82% sparsity) 100 linear: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0
nonlinear: ████████████████████ 8
nnzj: ( 30.00% sparsity) 56
lin_nnzj: (------% sparsity)
nln_nnzj: ( 30.00% sparsity) 56
Backends
Pass a backend as the second argument of ExaModels.Optimizer to change the backend. For example, to create use a multi-threaded CPU routine, do:
import KernelAbstractions
set_optimizer(
model,
() -> ExaModels.Optimizer(NLPModelsIpopt.ipopt, KernelAbstractions.CPU()),
)
optimize!(model)This is Ipopt version 3.14.19, running with linear solver MUMPS 5.9.0.
Number of nonzeros in equality constraint Jacobian...: 56
Number of nonzeros in inequality constraint Jacobian.: 0
Number of nonzeros in Lagrangian Hessian.............: 100
Total number of variables............................: 10
variables with only lower bounds: 0
variables with lower and upper bounds: 0
variables with only upper bounds: 0
Total number of equality constraints.................: 8
Total number of inequality constraints...............: 0
inequality constraints with only lower bounds: 0
inequality constraints with lower and upper bounds: 0
inequality constraints with only upper bounds: 0
iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls
0 2.0570000e+03 2.48e+01 2.73e+01 -1.0 0.00e+00 - 0.00e+00 0.00e+00 0
1 1.0953147e+03 1.49e+01 8.27e+01 -1.0 2.20e+00 - 1.00e+00 1.00e+00f 1
2 3.2865521e+02 4.28e+00 1.36e+02 -1.0 1.43e+00 - 1.00e+00 1.00e+00f 1
3 1.3995370e+01 3.09e-01 2.18e+01 -1.0 5.63e-01 - 1.00e+00 1.00e+00f 1
4 6.2325715e+00 1.73e-02 8.47e-01 -1.0 2.10e-01 - 1.00e+00 1.00e+00f 1
5 6.2324586e+00 1.15e-05 8.16e-04 -1.7 3.35e-03 - 1.00e+00 1.00e+00h 1
6 6.2324586e+00 8.35e-12 7.97e-10 -5.7 2.00e-06 - 1.00e+00 1.00e+00h 1
Number of Iterations....: 6
(scaled) (unscaled)
Objective...............: 7.8692659500473039e-01 6.2324586324374653e+00
Dual infeasibility......: 7.9746024776619455e-10 6.3158851623082613e-09
Constraint violation....: 8.3533180372796778e-12 8.3533180372796778e-12
Variable bound violation: 0.0000000000000000e+00 0.0000000000000000e+00
Complementarity.........: 0.0000000000000000e+00 0.0000000000000000e+00
Overall NLP error.......: 7.9746024776619455e-10 6.3158851623082613e-09
Number of objective function evaluations = 7
Number of objective gradient evaluations = 7
Number of equality constraint evaluations = 7
Number of inequality constraint evaluations = 0
Number of equality constraint Jacobian evaluations = 7
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations = 6
Total seconds in IPOPT = 22.236
EXIT: Optimal Solution Found.
GPUs
To use MadNLP's GPU support, do:
import CUDA
import CUDSS
import MadNLP
import MadNLPGPU
set_optimizer(
model,
() -> ExaModels.Optimizer(MadNLP.madnlp, CUDA.CUDABackend()),
)
optimize!(model)This is [34mMad[31mN[32mL[35mP[0m version v0.11.0, running with cuDSS v1.0.0
Number of nonzeros in constraint Jacobian............: 56
Number of nonzeros in Lagrangian Hessian.............: 100
Total number of variables............................: 10
variables with only lower bounds: 0
variables with lower and upper bounds: 0
variables with only upper bounds: 0
Total number of equality constraints.................: 0
Total number of inequality constraints...............: 8
inequality constraints with only lower bounds: 0
inequality constraints with lower and upper bounds: 8
inequality constraints with only upper bounds: 0
iter objective inf_pr inf_du inf_compl lg(mu) lg(rg) alpha_pr ir ls
0 2.0570000e+03 2.48e+01 1.00e+02 1.98e-08 -1.0 - 0.00e+00 0 0
1 2.0569998e+03 2.48e+01 1.50e-03 1.00e-06 -1.0 - 1.00e-07 1 2h
2 1.1078128e+03 1.50e+01 1.13e-03 1.00e-06 -1.0 - 1.00e+00 6 1h
3 1.1173896e+02 2.13e+00 2.67e-03 1.00e-06 -1.0 - 1.00e+00 7 1h
4 6.4942270e+00 1.16e-01 2.31e-04 1.00e-06 -1.0 - 1.00e+00 5 1h
5 6.2325592e+00 1.50e-03 3.93e-05 1.00e-06 -2.5 - 1.00e+00 3 1h
6 6.2324586e+00 1.03e-06 4.73e-06 1.00e-06 -5.7 - 1.00e+00 2 1h
7 6.2324586e+00 5.42e-14 1.02e-10 5.21e-09 -8.6 - 1.00e+00 1 1h
Number of Iterations....: 7
(scaled) (unscaled)
Objective...............: 7.8692659499580975e-01 6.2324586323668143e+00
Dual infeasibility......: 1.0244502700646374e-10 8.1136461389119299e-10
Constraint violation....: 5.4179717415505297e-14 5.4179717415505297e-14
Complementarity.........: 6.5841546538672229e-10 5.2146504858628416e-09
Overall NLP error.......: 5.2146504858628416e-09 5.2146504858628416e-09
Number of objective function evaluations = 9
Number of objective gradient evaluations = 8
Number of constraint evaluations = 9
Number of constraint Jacobian evaluations = 8
Number of Lagrangian Hessian evaluations = 7
Number of KKT factorizations = 7
Number of KKT backsolves = 25
Total wall secs in initialization = 68.739 s
Total wall secs in linear solver = unavailable
Total wall secs in NLP function evaluations = 5.218 s
Total wall secs in solver (w/o init./fun./lin. alg.) = unavailable
Total wall secs = 176.016 s
[32mEXIT: Optimal Solution Found (tol = 1.0e-08).[0m
This page was generated using Literate.jl.