code generation

1,076 views
Skip to first unread message

Raoul Herzog

unread,
Sep 14, 2016, 12:57:48 PM9/14/16
to CasADi
dear Joris,

I have two questions about code generation :

1) If I try to do code generation of a function including integrators (either CVODES or RK), I get an error message : 

Error using casadiMEX
 on line 2166 of file
 "/home/travis/build/casadi/binaries/casadi/casadi/core/function/function_internal.cpp"
 'generateBody' not defined for N6casadi12RkIntegratorE


Error in casadi.Function/generate (line 1260)
      [varargout{1:nargout}] = casadiMEX(792, self, varargin{:});

Error in SwigRef/subsref (line 29)
        [varargout{1:nargout}] = builtin('subsref',self,s);

Is this a missing feature or do I something wrong ?

2) Maybe a silly question : for a function not including integrators, code generation works, but afterwards, do I have to write a "wrapper" function in Matlab, using 

void mexFunction( int nlhs, mxArray *plhs[], 
 int nrhs, const mxArray*prhs[] )

and compile it using the "mex" command, or is there a simpler way ?

Many thanks in advance,

--raoul

Raoul Herzog

unread,
Sep 14, 2016, 1:06:45 PM9/14/16
to CasADi

Joris Gillis

unread,
Sep 14, 2016, 1:08:03 PM9/14/16
to CasADi
Dear Raoul,

1)
At the present moment, we can only code-export 'pure' algebraic functions, ie no integrator, linear solver, rootfinder etc.
Adding that feature is medium-priority I'd say.

You may work around by writing rk4 explictly in algebraic form like here: https://github.com/casadi/casadi/blob/master/docs/examples/matlab/direct_multiple_shooting.m#L54-L70

2)
Yes, there is no need for wrapping. Simply pass mex: True option:
f.generate('f',struct('mex',true)).
Afterwards, compile with 'mex -largeArrayDims'.
This info can be found in chapter 5 of the user guide.

Best regards,
  Joris
> --
> Sent from CasADi's user forum at http://forum.casadi.org.
> ---
> You received this message because you are subscribed to the Google Groups
> "CasADi" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to casadi-users...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/casadi-users/CAORqfTZOmc5xhwvGQ72JLsuAAvDDMbqDipbCz_k_cCNVOf4B9A%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

Joris Gillis

unread,
Sep 14, 2016, 1:49:12 PM9/14/16
to Raoul Herzog, CasADi
Dear Raoul,

This issue should be fixed ( https://github.com/casadi/casadi/issues/1797 ).
Could you try out the 3.1.0-rc1 version which includes the patch?

Best regards,
Joris

On 14 September 2016 at 19:44, Raoul Herzog <raoul1....@gmail.com> wrote:
> thanks for your fast reply, Joris.
>
> I tried, but my code snippet
>
> x = SX.sym('x', 1);
>
> y = x^2;
>
> myf = Function('myf', {x}, {y});
>
> myf.generate('myf',struct('mex',true));
>
> mex myf.c -largeArrayDims
>
> gives the compiler error message :
>
>
> Building with 'Microsoft Visual C++ 2013 Professional (C)'.
> Error using mex
> myf.c
> D:\EIVD\Projets\TrajOpt\Matlab\Casadi\myf.c(80) : warning C4267: '=' :
> conversion
> from 'size_t' to 'int', possible loss of data
> D:\EIVD\Projets\TrajOpt\Matlab\Casadi\myf.c(86) : warning C4267: '=' :
> conversion
> from 'size_t' to 'int', possible loss of data
> D:\EIVD\Projets\TrajOpt\Matlab\Casadi\myf.c(74) : warning C4101: 'r' :
> unreferenced
> local variable
> D:\EIVD\Projets\TrajOpt\Matlab\Casadi\myf.c(181) : error C2466: cannot
> allocate an
> array of constant size 0
> D:\EIVD\Projets\TrajOpt\Matlab\Casadi\myf.c(181) : error C2133: 'iw' :
> unknown size
> D:\EIVD\Projets\TrajOpt\Matlab\Casadi\myf.c(202) : warning C4098:
> 'mexFunction' :
> 'void' function returning a value
>
> Do you have any idea what is going wrong here ?
>> https://groups.google.com/d/msgid/casadi-users/d700e7b0-9ae2-4204-b315-c5e77b6f34f2%40googlegroups.com.

Raoul Herzog

unread,
Sep 14, 2016, 5:24:29 PM9/14/16
to Joris Gillis, CasADi
dear Joris,

a code working with v3.0 is no longer working with 3.1.0-rc1  !?

The code snippet :

t = SX.sym('t', 1);

x = SX.sym('x', ns);

q1 = x(1);

v1 = x(2);

qr = x(3);

vr = x(4);

p = SX.sym('p', 1+nu);

T = p(1);

u = p(2);


xdot = T*[v1;

u;

vr;

-om0^2*qr + u];

opts = struct('tf', 1/nk);

ode = struct('t', t, 'x', x, 'ode', xdot, 'p', [T, u]);

I = integrator('I', 'rk', ode, opts);

=====================================================

and the error message :

Error using casadiMEX
The assertion "e.numel()==nr*nrhs" on line 1310 of file
"/home/travis/build/casadi/binaries/casadi/casadi/core/function/integrator.cpp"
failed.
Inconsistent number of rhs


Error in casadi.integrator (line 489)
  [varargout{1:nargout}] = casadiMEX(843, varargin{:});


Any idea ?


best regards,


--raoul


2016-09-14 19:57 GMT+02:00 Raoul Herzog <raoul1....@gmail.com>:
dear Joris,
confirmed, it works with 3.1.0-rc1  !

Many thanks (I'm waiting in the airport of Bruxelles  :-)

--raoul

Raoul Herzog

unread,
Sep 14, 2016, 5:24:29 PM9/14/16
to Joris Gillis, CasADi
dear Joris,
confirmed, it works with 3.1.0-rc1  !

Many thanks (I'm waiting in the airport of Bruxelles  :-)

--raoul

Raoul Herzog

unread,
Sep 14, 2016, 5:24:29 PM9/14/16
to Joris Gillis, CasADi
thanks for your fast reply, Joris.

I tried, but my code snippet

x = SX.sym('x', 1);

y = x^2;

myf = Function('myf', {x}, {y});

myf.generate('myf',struct('mex',true));

mex myf.c -largeArrayDims

gives the compiler error message :


Building with 'Microsoft Visual C++ 2013 Professional (C)'.
Error using mex
myf.c
D:\EIVD\Projets\TrajOpt\Matlab\Casadi\myf.c(80) : warning C4267: '=' : conversion
from 'size_t' to 'int', possible loss of data
D:\EIVD\Projets\TrajOpt\Matlab\Casadi\myf.c(86) : warning C4267: '=' : conversion
from 'size_t' to 'int', possible loss of data
D:\EIVD\Projets\TrajOpt\Matlab\Casadi\myf.c(74) : warning C4101: 'r' : unreferenced
local variable
D:\EIVD\Projets\TrajOpt\Matlab\Casadi\myf.c(181) : error C2466: cannot allocate an
array of constant size 0
D:\EIVD\Projets\TrajOpt\Matlab\Casadi\myf.c(181) : error C2133: 'iw' : unknown size
D:\EIVD\Projets\TrajOpt\Matlab\Casadi\myf.c(202) : warning C4098: 'mexFunction' :
'void' function returning a value

Do you have any idea what is going wrong here ?


Many thanks in advance,


--raoul


2016-09-14 19:08 GMT+02:00 Joris Gillis <joris.g...@gmail.com>:

> To view this discussion on the web visit
> https://groups.google.com/d/msgid/casadi-users/CAORqfTZOmc5xhwvGQ72JLsuAAvDDMbqDipbCz_k_cCNVOf4B9A%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.




On Wednesday, 14 September 2016 18:57:48 UTC+2, Raoul Herzog wrote:
dear Joris,

I have two questions about code generation :

1) If I try to do code generation of a function including integrators (either CVODES or RK), I get an error message : 

Error using casadiMEX
 on line 2166 of file
 "/home/travis/build/casadi/binaries/casadi/casadi/core/function/function_internal.cpp"
 'generateBody' not defined for N6casadi12RkIntegratorE


Error in casadi.Function/generate (line 1260)
      [varargout{1:nargout}] = casadiMEX(792, self, varargin{:});

Error in SwigRef/subsref (line 29)
        [varargout{1:nargout}] = builtin('subsref',self,s);

Is this a missing feature or do I something wrong ?

2) Maybe a silly question : for a function not including integrators, code generation works, but afterwards, do I have to write a "wrapper" function in Matlab, using 

void mexFunction( int nlhs, mxArray *plhs[], 
 int nrhs, const mxArray*prhs[] )

and compile it using the "mex" command, or is there a simpler way ?

Many thanks in advance,

--raoul

--
Sent from CasADi's user forum at http://forum.casadi.org.
---
You received this message because you are subscribed to the Google Groups "CasADi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to casadi-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/casadi-users/d700e7b0-9ae2-4204-b315-c5e77b6f34f2%40googlegroups.com.
myf.c

Joris Gillis

unread,
Sep 15, 2016, 3:22:26 AM9/15/16
to CasADi, joris.g...@gmail.com
The parameter vector should be a column vector ([T; u]).
For sure we must improve the error message in this case..

Best,
  Joris

Raoul Herzog

unread,
Sep 16, 2016, 1:58:16 AM9/16/16
to Joris Gillis, CasADi
dear Joris,

I replaced the integrator by RK4 programmed within Casadi as you recommended.
I'm now able to generate code for my objective function f and the constraints g !  Great !
Mex compilation works although it is extremely slow.

How can I call IPOPT within Casadi by giving mex functions for f, g, and also mex functions for the derivatives of f and g ?

best regards,

--raoul


Joel Andersson

unread,
Sep 16, 2016, 9:32:11 AM9/16/16
to CasADi
Hi Raoul,

If the mex compilation is slow it's probably because the generated file is very large. You can change this by breaking up the problem into functions and embed them in an MX-expression.

If you want to generate C code corresponding to an NLP that can then be used with IPOPT, please have a look at the example:

The C code in that example won't include MEX, but if you want the generated code to be valid MEX, you can just pass "struct('mex', true)" as an additional option to "solver.generate_dependencies".

Also note that if your purpose is simply to speed up the evaluation, you can just use CasADi's just-in-time compiler, no need to manually generate and compile code.

Best regards,
Joel

Joris Gillis

unread,
Sep 16, 2016, 9:42:23 AM9/16/16
to CasADi
One can add arbitrary matlab code (icnluding mex calls) into CasADi graphs, but this is far from what you need.

Supplementing Joel's answer, you most likely can profit from jit: add struct('jit',true) to nlpsol constructor.

Best,
   Joris

Raoul Herzog

unread,
Sep 17, 2016, 6:28:11 AM9/17/16
to Joris Gillis, CasADi
dear Joris, dear Joel,

I implemented RK4 using a function embedded into an MX graph.
Indeed the size of the generated C function of my NLP problem decreased from 600kB to 260kB.

But mex compilation still took 15 minutes.
The solution was to add the -g option which disables optimization.
With this option, mex compilation time went down to 2 seconds !

Thanks a lot for your help !

best regards,

--raoul 

--
Sent from CasADi's user forum at http://forum.casadi.org.
---
You received this message because you are subscribed to the Google Groups "CasADi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to casadi-users+unsubscribe@googlegroups.com.

Joris Gillis

unread,
Sep 17, 2016, 11:28:13 AM9/17/16
to CasADi, joris.g...@gmail.com
If you have a long horizon, you may find that the use of mapaccum/map reduces the compilation time drastically with optimization flags.

   Joris

Joel Andersson

unread,
Sep 22, 2016, 2:39:14 PM9/22/16
to CasADi, joris.g...@gmail.com
Great! You should also be able to reduce it further by using multiple levels of MX. If you have one function object for the ODE/DAE right-hand-side, you can have one MX function for say 20 steps of the integrator. Then call this function multiple times in another graph. This is how you do "checkpointing" in CasADi.

Joel

gargso...@gmail.com

unread,
Mar 15, 2019, 8:44:09 AM3/15/19
to CasADi
Dear Joel, Joris and Raoul,

I am using CasADi with Bonmin solver. I have MATLAB code running on ubuntu machine and now I want to convert my code to c++. I have setup CasADi with Bonmin and ma27 from source. My optimization variables are velocity and omega which are integrated using RK4  integrator to obtain x,y position and heading of my robot. Can you tell me how should I proceed?

Thanks
Reply all
Reply to author
Forward
0 new messages