Huge overhead when passing a mex file to ode solver
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hey!
I noticed there seems to be a significant overhead when a mex file, rather than a "normal" MATLAB function, is passed to a MATLAB solver (like ode45, ode23s etc).
In my case, i'm computing the time evolution of a system of stiff ordinary differential equations derived from chemical kinetics.
I compared the computing times for three types of ode functions:
- A normal matlab ode function (a)
- A normal matlab ode function that includes some part written in mex (called inside the function) (b)
- A complete mex implementation of said function (c)
I compared the computing times for the functions themselves as well as the solvers with the functions passed to them. The results were like this:
ode times:
0.0250 (a)
0.0116 (b)
0.0020 (c)
solver times:
0.5098 (a)
0.4384 (b)
0.6482 (c)
We can see that the mex file is much faster than the others, but there seems to be a huge overhead when it's passed to the ode solver.
So i tried to write a thin .m function wrapper around the mex file (d). The calling before:
[~,~] = ode23s(@(a,b) mex_fun(a,b,c,d), ...);
The calling after:
[~,~] = ode23s(@(a,b) mex_fun_wrapper(a,b,c,d), ...);
where mex_fun_wrapper is defined as:
function output = mex_fun_wrapper(a,b,c,d)
output = mex_fun(a,b,c,d);
end
The times were now like this:
solver times:
0.5098 (a)
0.4384 (b)
0.6482 (c)
0.3673 (d)
We can see that the difference between (c) and (d) is huge, even though the expected added overhead should slightly slow down the solver.
So now i'm wondering what is going on under the hood, and if it's possible to avoid the wrapper to get higher speeds. Thanks for all the answers!
0 Commenti
Risposte (0)
Vedere anche
Categorie
Scopri di più su Ordinary Differential Equations in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!