why vectorization becomes slower?

7 views (last 30 days)
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)))));

Accepted Answer

Matt J
Matt J on 4 Nov 2021
Edited: Matt J on 4 Nov 2021
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 Comments
Matt J
Matt J on 5 Nov 2021
Edited: Matt J on 5 Nov 2021
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 on 7 Nov 2021
Thank you for your suggestions.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!