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)".

回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 12 月 25 日

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 件のコメント

Daichi Daichi
Daichi Daichi 2020 年 12 月 26 日
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?
Walter Roberson
Walter Roberson 2020 年 12 月 26 日
Yes. MATLAB automatically calls LAPACK and MKL when appropriate, when it eestimates that the matrix sizes are large enough to make it worth doing.
Daichi Daichi
Daichi Daichi 2020 年 12 月 26 日
Thanks, all of mysteries is solved!

サインインしてコメントする。

カテゴリ

製品

リリース

R2020b

質問済み:

2020 年 12 月 25 日

コメント済み:

2020 年 12 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by