why vectorization becomes slower?

조회 수: 6 (최근 30일)
Nadeem Ahmed
Nadeem Ahmed 2021년 11월 4일
댓글: Nadeem Ahmed 2021년 11월 7일
I was trying to compute a simple function by 'for' loop but when I vectorized the same function as shown below in 'VECTORISED FORM' instead of improvement in CPU time it becomes slower than 'for' loop, I don't exactly know the reason for this because I am new in vectorization therefore don't even know whether I did vectorization right or wrong
SIMPLE FORM
for i=2:n+1
for j=2:m+1
f1(i,j)= -((+ rho*(((((uyt(i,j) - uyt(i-1,j))/(2*a))+((vxt(i,j) - vxt(i,j-1))/(2*a))))/(2*tao))) + ...
(rho*(((uytold(i,j)-uytold(i-1,j))/(2*a))*((uyt(i,j)-uyt(i-1,j))/(2*a)))) + ...
(rho*(((vyt(i,j)-vyt(i-1,j))/(2*a))*((uxtold(i,j)-uxtold(i,j-1))/(2*b)))) + (rho*(((vytold(i,j)-vytold(i-1,j))/(2*a))*((uxt(i,j)-uxt(i,j-1))/(2*b)))) + ...
(rho*(((vxtold(i,j)-vxtold(i,j-1))/(2*b))*((vxt(i,j)-vxt(i,j-1))/(2*b)))));
end
end
VECTORISED FORM
f1(2:n+1,2:m+1)= -((+ rho.*(((((uyt(2:n+1,2:m+1) - uyt(1:n,2:m+1))./(2.*a))+((vxt(2:n+1,2:m+1) - vxt(2:n+1,1:m))./(2.*a))))./(2.*tao))) + ...
(rho.*(((uytold(2:n+1,2:m+1)-uytold(1:n,2:m+1))./(2.*a)).*((uyt(2:n+1,2:m+1)-uyt(1:n,2:m+1))./(2.*a)))) + ...
(rho.*(((vyt(2:n+1,2:m+1)-vyt(1:n,2:m+1))./(2.*a)).*((uxtold(2:n+1,2:m+1)-uxtold(2:n+1,1:m))./(2.*b)))) + (rho.*(((vytold(2:n+1,2:m+1)-vytold(1:n,2:m+1))./(2.*a)).*((uxt(2:n+1,2:m+1)-uxt(2:n+1,1:m))./(2.*b)))) + ...
(rho.*(((vxtold(2:n+1,2:m+1)-vxtold(2:n+1,1:m))./(2.*b)).*((vxt(2:n+1,2:m+1)-vxt(2:n+1,1:m))./(2.*b)))));

채택된 답변

Matt J
Matt J 2021년 11월 4일
편집: Matt J 2021년 11월 4일
One reason is that you are extracting the same submatrices multiple times. For example, the expression uyt(2:n+1,2:m+1) appears multiple times in your vectorized code. This operation allocates memory for a new matrix every time you do it. You should really go through your expression and pull out repeated instances of sub-expressions and precompute them.
Also, it looks like you can replace certain expressions like uyt(2:n+1,2:m+1) - uyt(1:n,2:m+1) with diff(). If uyt is of size (n+1)x(m+1), then the latter will be faster, e.g.,
m=2000;
n=2000;
uyt=rand(n+1,m+1);
tic
d_uyt = uyt(2:n+1,2:m+1) - uyt(1:n,2:m+1);
toc
Elapsed time is 0.053775 seconds.
tic
d_uyt = diff(uyt,1,1);
toc
Elapsed time is 0.027445 seconds.
  댓글 수: 9
Matt J
Matt J 2021년 11월 5일
편집: Matt J 2021년 11월 5일
So, you have a few options.
(1) Accept the performance of the original loop. Are you sure it is the bottleneck in your code?
(2) Implement the loop in a MEX file. In a MEX, you are not forced to extract data copies of sub-matrices. This can be worthwhile if the code is truly a critical bottleneck (see also option 1).
(3) When you create your matrices, try to avoid embedding the data blocks you'll need later inside larger matrices. In the code you've shown, for example, the first and last row of F53 are never used. Do they need to be there? If you need them, could they be kept in their own separate variables?
Nadeem Ahmed
Nadeem Ahmed 2021년 11월 7일
Thank you for your suggestions.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by