Giter Site home page Giter Site logo

JIT Compiliation time about casadi HOT 5 CLOSED

sshin23 avatar sshin23 commented on July 18, 2024 1
JIT Compiliation time

from casadi.

Comments (5)

victorfors avatar victorfors commented on July 18, 2024 1

For compilation time I find that MX is often faster. A good compromise is to write the inner functions using SX and then write the outer problem in MX using vectorized operations (or map/fold functions if that is not straightforward).

For this specific case this will be fast for large N

import sys
import casadi as ca

N = int(sys.argv[1])

x = ca.MX.sym("x",N)
x0 = [1.0 if i % 2 == 1 else -1.2 for i in range(N)]
f = ca.sum1(100*(x[:-1]**2-x[1:])**2+(x[:-1]-1)**2)
g = 3*x[1:-1]**3 + 2*x[2:] - 5 + ca.sin(x[1:-1]-x[2:])*ca.sin(x[1:-1]+x[2:]) + 4*x[1:-1] - x[:-2]*ca.exp(x[:-2]-x[1:-1]) - 3

nlp = {'x':x, 'f':f, 'g':g}

options = {
    "jit": True, 
    "compiler": "shell", 
    "jit_options": {
        "flags": ["-O3"], 
        "verbose": True
    }, 
    "ipopt.linear_solver": "ma27", 
}

solver = ca.nlpsol("solver", "ipopt", nlp, options)
solver(x0 = x0, lbg=0, ubg=0)

from casadi.

OlivieroAgnelli avatar OlivieroAgnelli commented on July 18, 2024

I have the same issue unfortunately... I was also wondering if there was an option or an alternative method to speed-up the compiling process, so I'll also be grateful for any advice!

from casadi.

sshin23 avatar sshin23 commented on July 18, 2024

Vectorizing the operation solved my issue. Thanks @victorfors! Closing the issue.

from casadi.

jgillis avatar jgillis commented on July 18, 2024

If g_i were a bit more involved, the winner combination would typically be an inner SX Function with a map on top ( https://web.casadi.org/docs/#for-loop-equivalents ):

import sys
import casadi as ca

N = int(sys.argv[1])

x = ca.MX.sym("x",N)
x0 = [1.0 if i % 2 == 1 else -1.2 for i in range(N)]
f = sum(100*(x[i-1]**2-x[i])**2+(x[i-1]-1)**2 for i in range(1,N))

xi = ca.SX.sym("x[i]")
xip = ca.SX.sym("x[i+1]")
xipp = ca.SX.sym("x[i+2]")

gf = Function('g',[xi,xip,xipp],[3*xip**3 + 2*xipp - 5 + ca.sin(xip-xipp)*ca.sin(xip+xipp) + 4*xip - xi*ca.exp(xi-xip) - 3])

g = gf.map(N-2)(x[:-2],x[1:-1],x[2:])

nlp = {'x':x, 'f':f, 'g':g}

options = {
    "jit": True, 
    "compiler": "shell",
    "jit_cleanup": False, 
    "jit_options": {
        "flags": ["-O3"],
        "verbose": True,
    }, 
    "ipopt.linear_solver": "ma27", 
}

solver = ca.nlpsol("solver", "ipopt", nlp, options)
solver(x0 = x0, lbg=0, ubg=0)

(The example has a lot of potential for further speedup of vectorized instructions, which has not been merged yet with main CasADi branch.)

from casadi.

sshin23 avatar sshin23 commented on July 18, 2024

Thanks for the comment @jgillis. I think you meant better runtime performance? I checked the SX approach you suggested, and it seems that the compilation time tends to be longer than the pure MX approach.

This is purely MX

time python luksan.py 400

real    0m1.107s
user    0m1.045s
sys     0m0.061s
time python luksan.py 800

real    0m1.121s
user    0m1.046s
sys     0m0.073s

luksan-2.py is SX approach you suggested

time python luksan-2.py 400

real    0m12.617s
user    0m12.485s
sys     0m0.122s
time python luksan-2.py 800

real    0m38.007s
user    0m37.743s
sys     0m0.236s

SX approach seems to be similar to what we are doing at ExaModels.jl---our experimental algebraic modeling/AD implementation in Julia running on GPUs. Would you be able to explain why the SX approach is expected to have better performance than MX? And how is the new vectorized instruction under development different from MX? Sorry for lots of questions :)

from casadi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.