Why mex function is slower than m file?

I generated a mex function from the m file (function) that is called in ODE45. However, the execution speed became 10 times slower.
Please explain to me why it became slower?
Below, I show the function and m files
  1. ode45 call "ODEABC" (I converted this "ODEABC" function into mex file by using Matlab coder.
[tf,qf,tfe,yfe,ife]=ode45(@(tf,qf) ODEABC(qf,tf,tends,Angle,A,B,C,D,obj),tspan,q0,Opt);
2. The following is ODEABC
function dy=ODEABC(q,t,tend,Angle,A,B,C,D,obj)
if strcmp(obj.Phase,'AX')
[Fy,d]=CompositionCalcAX(q,obj);
q_ddot=ResultCalcAX(t,tend,q,angle,Fy,obj);
elseif strcmp(obj.Phase,'BX')
[Fy,d]=CompositionCalcBX(q,obj);
q_ddot=ResultCalcBX(t,tend,q,angle,Fy,obj);
end
ty=q_ddot;
dy(1,1)=q(7);
dy(2,1)=q(8);
dy(3,1)=q(9);
dy(4,1)=q(10);
dy(5,1)=q(11);
dy(6,1)=q(12);
dy(7,1)=ty(1,1);
dy(8,1)=ty(2,1);
dy(9,1)=ty(3,1);
dy(10,1)=ty(4,1);
dy(11,1)=ty(5,1);
dy(12,1)=ty(6,1);
end
3. ResultCalcAX and ResultCalcBX are similar and it perform matrix calculations including inverse matrix with no "for sentens (loop)".

Answers (1)

When you generate C code for matrix calculations, it generates straight forward C code, instead of inserting calls to the high performance math libraries that know about cache behaviour and know how to generate threads to use multiple cores.

3 Comments

Thank you for your quick responce.
Do you mean that the m file calls math libraries that can process matrix calculation efficientrly using multiple cores even if the user does not specify palallel computing as options?
Yes. MATLAB automatically calls LAPACK and MKL when appropriate, when it eestimates that the matrix sizes are large enough to make it worth doing.
Thanks, all of mysteries is solved!

Sign in to comment.

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Products

Release

R2020b

Asked:

on 25 Dec 2020

Commented:

on 26 Dec 2020

Community Treasure Hunt

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

Start Hunting!