Why is MATLAB running slow? 2013a vs 2011a

1 回表示 (過去 30 日間)
Dominic
Dominic 2014 年 10 月 6 日
編集済み: Dominic 2014 年 10 月 7 日
Hi everyone,
I am running an m-file in Matlab 2013a, which takes roughly 20sec to complete. However, when a friend runs the same code in 2011a it takes a fraction of a second to complete. What could be causing this in my 2013a installation?
Our PCs have the same components so that is not the culprit.
Just a note; the code I am testing is very simple, for the most part just for loops.
Thanks
clc; clear all; close all;
format compact
% Define number of cells in x and y
nx = 20;
ny = 20;
uw = 1;
% Define cell length in x and y
dx = 1.0/nx;
dy = 1.0/ny;
% Relaxation Parametres
relax1 = 0.2;
relax2 = 1.2;
relax3 = 0.2;
% Reynolds Number
re = 100;
nu = 1/re;
kappa = 1/re;
rex = 2.0/dx/dx + 2.0/dy/dy;
% Initialise the streamfunction and vorticity everywhere.
for i = 1:nx+1
for j = 1:ny+1
psi(i,j) = 0.0; % Streamfunction
w(i,j) = 0.0;
x(i,j) = (i-1)*dx;
y(i,j) = (j-1)*dy;
T(i,j) = 0.0;
if j == ny+1
T(i,j) = 1.0;
end
end;
end;
for k = 1:20000 %main loop - iterate until convergence.
adu = 0.0;
adv = 0.0;
psimin = 1000000.0;
psimax = -1000000.0;
for i = 2:nx %sweep through all internal points on the grid
for j = 2:ny %updating until convergence.
wimj0 = w(i-1,j); %for the vorticity equations these values
wipj0 = w(i+1,j); %may have to be modified at the points
wi0jm = w(i,j-1); %adjacent to the walls
wi0jp = w(i,j+1);
% Boundary Adjacent Conditions
if(i == 2)
wimj0 = -2.0*psi(i,j)/dx/dx;
end;
if(i == nx)
wipj0 = -2.0*psi(i,j)/dx/dx;
end;
if(j == 2)
wi0jm = -2.0*psi(i,j)/dy/dy;
end;
if(j == ny)
wi0jp = -2.0*(psi(i,j)+dy*uw)/dy/dy;
end;
%this is the update for w(i,j) from the FDE.
wij = (nu*((wipj0+wimj0)/dx/dx + (wi0jp+wi0jm)/dy/dy)...
+ (((psi(i+1,j)-psi(i-1,j))*(wi0jp-wi0jm)-((psi(i,j+1)...
-psi(i,j-1))*(wipj0-wimj0)))/(4*dx*dy)))/(nu*rex);
dw = wij - w(i,j);
%compute the difference between new and old value.
w(i,j) = w(i,j) + relax1*dw;
%update using relaxation
%update equation for the streamfunction
psiij = ((psi(i+1,j)+psi(i-1,j))/dx/dx + ...
(psi(i,j+1)+psi(i,j-1))/dy/dy + w(i,j))/rex;
dpsi = psiij - psi(i,j);
psi(i,j) = psi(i,j) + relax2*dpsi;
if(psi(i,j)>psimax) %determine maximum and minimum streamfunction
psimax = psi(i,j);
end;
if(psi(i,j)<psimin)
psimin = psi(i,j);
end;
ddu = abs(dpsi); %compute maximum change in solution over full sweep.
ddv = abs(dw);
if(ddu>adu)
adu = ddu;
end;
if(ddv>adv)
adv = ddv;
end;
end;
end;
% k
% psimax
% psimin
% adu
% adv
% Exit loop if converged
if(adu<1.0e-4 && adv<1.0e-4)
break;
end;
end;
% Compute the temperature distribution
for l = 1:10000
adT = 0.0;
for i = 1:nx+1
for j = 1:ny+1
if j ~= 1 && j ~= ny+1
if i == 1 % Using a forward difference scheme on the psi differential
Tij = (kappa*((T(i,j+1)+T(i,j-1))/dy/dy) + ...
(psi(i+1,j)-psi(i,j))*(T(i,j+1)-T(i,j-1))/(2*dx*dy))/(kappa*2/dy/dy);
elseif i == nx+1 % Using a backward difference scheme on the psi differential
Tij = (kappa*((T(i,j+1)+T(i,j-1))/dy/dy) + ...
(psi(i,j)-psi(i-1,j))*(T(i,j+1)-T(i,j-1))/(2*dx*dy))/(kappa*2/dy/dy);
else
Tij = (kappa*((T(i+1,j)+T(i-1,j))/dx/dx + ...
(T(i,j+1)+T(i,j-1))/dy/dy) + ...
(psi(i+1,j)-psi(i-1,j))*(T(i,j+1)-T(i,j-1))/(4*dx*dy)...
- (psi(i,j+1)-psi(i,j-1))*(T(i+1,j)-T(i-1,j))/(4*dx*dy))/(kappa*rex);
end;
dT = Tij - T(i,j);
T(i,j) = T(i,j) + dT*relax3;
ddT = abs(dT);
if(ddT>adT)
adT = ddT;
end;
end;
end;
end;
if adT < 1e-6
break
end
% l
% adT
end;
k
psimin
adu
adv
l
adT
% Plot solution.
for i = 1:10
v(i) = -(i-1)*0.01;
end;
for i = 11:20
v(i) = (i-10)*psimax/10;
end;
figure(1)
contourf(x,y,psi,v);
xlabel('x')
ylabel('y')
title('Streamfunction')
colorbar
figure(2)
contourf(x,y,T)
xlabel('x')
ylabel('y')
title('Temperature Distribution (\circC)')
colorbar
  2 件のコメント
Oleg Komarov
Oleg Komarov 2014 年 10 月 6 日
Paste the code so we can test it. Without it's impossible to say.
Oleg Komarov
Oleg Komarov 2014 年 10 月 7 日
I confirm it takes ~20 sec on R2014a.

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

採用された回答

Edric Ellis
Edric Ellis 2014 年 10 月 7 日
I tried your code on my machine in both R2011a and R2014a, and as posted it is indeed quite a bit slower in R2014a. You can get nearly all the speed back if you make your code a function rather than a script. See this page for more about functions if you're not already used to writing them.
  4 件のコメント
Edric Ellis
Edric Ellis 2014 年 10 月 7 日
I'm afraid I'm not an expert on the interpretation of the MATLAB language, but I believe basically you're on the right lines that there are a bunch of optimisations that can be applied inside a function because of things like the reduced variable scope.
Dominic
Dominic 2014 年 10 月 7 日
Thanks for your reply Edric,
I will try making it a function instead

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

その他の回答 (2 件)

Andreas Goser
Andreas Goser 2014 年 10 月 7 日
When I copy / paste the code, I need to correct too many smaller issues as of unintended line breaks that I can be sure I run the code you are using. A couple of comments:
I understand you say your machines have "the same components". I can however share that the majority of such effects is happening not from release to relase, but when changing hardware. I suggest you do an R2011a/b installation on your machine in parallel to narrow down the issue. Even then, there can be issues with stuff like the math kernel library.
Second, I wonder if your question is really finding out the why or if you need to reduce runtime. There might be opportunities to get rid of loops.
  2 件のコメント
Andreas Goser
Andreas Goser 2014 年 10 月 7 日
Memory should not be an issue - all matrices involed appear to be small.
What I wonder if - as you do iterations - the code breaks out of the loop at different times?
Dominic
Dominic 2014 年 10 月 7 日
編集済み: Dominic 2014 年 10 月 7 日
Thanks for your response Andreas,
I did a 2011a install and it did reduce the time by roughly x4, still not anywhere near as fast as it is for my friend. This speed will suffice for now and I will try to make it into a function instead, as suggested below.
The reason I would like an increase in the speed is for when I increase some of the parameters (re) and hence must increase the grid number (nx,ny) and in turn the matrix size, after increasing this it took roughly 2-3 hours to run on my pc and on my friend's roughly 15-20min. <-- This is why i dont think it is hardware related

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


yonatan gerufi
yonatan gerufi 2014 年 10 月 6 日
there are many reason this could happen.
any other programs are running?
try
memory
to see how much matlab use, and also do
clear all
perhaps it will help.
if it doesn't help you can use "profiler" to see your time for each line.
you can upload the code, so other people will compare there time to yours.
  1 件のコメント
Dominic
Dominic 2014 年 10 月 6 日
編集済み: Dominic 2014 年 10 月 7 日
Thanks, I have checked the memory, it uses about 1GB out of eight and whilst MATLAB is running there is about 4GB free. I also tried increasing the Java heap memory to no avail.
Using profiler, nothing in particular takes up a lot of time, total time ~20sec, and the rest of the lines take a fraction of a second.

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

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by