How can I make my code faster by vectorizing it?

My code calculating equilibrium distribution works fine. But I was wondering that can I make my code below faster by vectorizing it?
function[tasaPainoJakauma_PS] = tPj_PS(lambda_R,lambda_A,mu_1,mu_2,n)
Q = zeros((n + 1).^2);
indeksi = @(x,y) y*(n + 1) + x + 1;
for x = 0:n
for y = 0:n
if x < n
Q(indeksi(x,y),indeksi(x+1,y)) = lambda_R;
end
if y < n
Q(indeksi(x,y),indeksi(x,y+1)) = lambda_A;
end
if x > 0
Q(indeksi(x,y),indeksi(x-1,y)) = mu_1 + x*mu_2/(x + y);
end
if y > 0
Q(indeksi(x,y),indeksi(x,y-1)) = y*mu_2/(x + y);
end
end
end
riviSummat = sum(Q,2);
for a = 1:(n + 1)^2
Q(a,a) = -riviSummat(a);
end
E = ones((n + 1)^2);
e = ones(1,(n + 1)^2);
tasaPainoJakauma_PS = e/(Q+E);
end
If so how?

4 个评论

Almost certainly it can depending on how large 'n' gets. Can you provide some values for your inputs with which to test your function?
It seems to me that your two 'for' loops could be replaced by a direct assignment and the four 'if' statements could be replaced with a logical index.
The way you're indexing Q using indeksi is a bit non-traditional. I need some "real" numbers to make sure my vectorized code creates the same output array as yours.
Thank You Chad for Your interest in my problem!
I'm using n=100 in my simulation of N-model using redundant requests. It takes about 1050 seconds to calculate the latency of the system. And about 1000 seconds of it is the tPj_PS-function. I'd really like to use bigger n, but it just takes too long!
But here are some 'real' numbers you asked for the tPj_PS-function:
>> tPj_PS(1.2,0.6,1,1,2)
ans =
0.2033 0.1126 0.0628 0.1409 0.1164 0.0844 0.0977 0.0979 0.0840
It takes your computer 1000 seconds to run n=100 - or 100 runs takes 1000 seconds? If it's the former, I suspect you're running out of memory and swapping to the disk. That case took 7.4 seconds on my computer. It was using about 4 GB of RAM though.
Looking at your code, you potentially could speed up the 'for' loops and the 'if' statements. However, of my 7.4 seconds run, only 0.9 seconds was spent doing those steps. If you are actually running this 100 times then you could save, at best, 12% of your run time. It's the matrix solution that chews up the majority of the time and that's already vectorized.
Your matrix, 'Q' is mostly empty. Is there a way for you to solve the exact linear algebra system and code the solution directly? That might be faster - like solving a tridiagonal matrix doesn't require inverting the entire matrix. However, short of that, the run time isn't going to change substantially, I'm sorry.
Just the tPj_PS function takes 9.8 seconds to run on my system if n=100, but I use it 100 times plotting over l_R = linspace(0,lambda_R). Maybe I'll plot with fewer points...
So the loops aren't the ones taking the time. That I was afraid of. Thaks for clearing that out for me. Can't really get rid of the Q-matrix because I need it elsewhere.
Thank you Chad for Your help. And don't worry, basically n=100 is enough for my needs. I was just wondering if the vectorization of the loops could have been a miracle solution...

请先登录,再进行评论。

回答(1 个)

Here is a version with a sparse matrix - without a remarkable speedup:
function PS = tPj_PS(lambda_R,lambda_A,mu_1,mu_2,n)
m = (n + 1) .^ 2;
nQ = 1 + 6 * n + 5 * n^2;
Q = spalloc(m, m, nQ); % Not useful here!
% Q = zeros(m, m);
for x = 0:n
for y = 0:n
c = y * (n + 1) + x + 1;
if x < n
Q(c, y * (n + 1) + x + 2) = lambda_R;
end
if y < n
Q(c, (y+1) * (n + 1) + x + 1) = lambda_A;
end
if x > 0
Q(c, y * (n + 1) + x) = mu_1 + x*mu_2 / (x + y);
end
if y > 0
Q(c, (y-1) * (n + 1) + x + 1) = y*mu_2 / (x + y);
end
end
end
% Set the diagonal elements:
Q(1:(m+1):m*m) = -sum(Q, 2);
PS = ones(1, m) / (Q + 1);
end
The code for setting the diagonal was vectorized, but it was more important to replace Q+ones() by Q+1. Unfortunately most of the time is spent in ones/(Q+1), such that smart tricks to improve the loops will not help much. Even the sparse method does not help, because Q+1 is full. I admit, I posted it only, because I found out the formula of the number of non-zero elements...
The best speedup would be obtained by writing a solver for ones / (Q+1) which considers the known structure already - preferrably in a C-mex function.

类别

在 帮助中心 和 File Exchange 中查找有关 Link-Level Simulation 的更多信息

标签

提问:

2018-5-5

编辑:

Jan
2018-5-7

Community Treasure Hunt

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

Start Hunting!

Translated by