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!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 MATLAB Coder에 대해 자세히 알아보기

제품

릴리스

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