Effacer les filtres
Effacer les filtres

Huge overhead when passing a mex file to ode solver

2 vues (au cours des 30 derniers jours)
Damjan Lasic
Damjan Lasic le 7 Nov 2017
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!

Réponses (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by