Speed up sparse matrix calculations

조회 수: 11 (최근 30일)
John Doe
John Doe 2013년 5월 12일
Is it possible to speed up large sparse matrix calculations by e.g. placing parantheses optimally?
What I'm asking is: Can I speed up the following code by forcing Matlab to do the operations in a specified order (for instance "from right to left" or something similar)?
I have a sparse square symmetric matrix H (4000x4000), that previously has been factorized, and a sparse vector M with length equal to the dimension of H. What I want to do is the following:
k = 0;
cList = 1:3500 % List of contingecies to study
nc = length(cList);
nb = size(bus,1); % bus is a matrix with information about buses in the system
resVa = zeros(nb,nc);
[L U P] = lu(H); % H is sparse (thus also L, U and P). Dimension = nbxnb
while k < nc
k = k + 1;
cont = cList(k);
bf = branch(cont,1); % branch is a matrix with information about branches
bt = branch(cont,2); % Col 1 = from-bus, col 2 = to-bus
dy = -H(bf,bt); %
M = sparse([bf,bt],1,[1,-1],nb,1);
z = -M'*(U \ (L \ (P * M)));
c = (1/dy + z)^-1;
% V = Vm*exp(j*Va); - A complex vector of dimension nbx1
% Sbus is a complex vector of dimension nbx1
% Ybus is a complec matrix of dimension nbxnb
mis = (V .*conj((Ybus)*V) - Sbus) ./ Vm;
P_mis = real(mis);
converged = 0;
i = 0;
while (~converged && i < max_it)
i = i + 1;
%%The lines I hope to optimize:
dVa = - (U \ (L \ (P * P_mis)));
dVaComp = (U \ (L \ (P * M * c * M' * dVa)));
Va = Va + dVa + dVaComp;
V = Vm .* exp(1j * Va);
mis = (V .*conj((Ybus)*V) - Sbus) ./ Vm;
P_mis = real(mis);
normP = norm(P, inf);
if normP < tol
converged = 1;
break;
end
end
resVa(:,k) = Va * 180 / pi;
end
Some additional information regarding the matrices:
All diagonal elements of H are non-zeros (it's still square, sparse and symmetrical).
Ybus and H have the same structure, but Ybus is complex and H is real.
Vm is updated using the imaginary part of mis, and some other matrices, but I've left it out for simplicity.
EDIT:
I achieved almost 75% reduction in computation time by using the extended syntax for lu when factorizing H.
[L U P Q] = lu(H);
Please let me know if any additional savings are possible to achieve.
Thanks!
  댓글 수: 4
Jan
Jan 2013년 5월 13일
편집: Jan 2013년 5월 13일
@Robert P: It would be much more convenient and useful to post the Matlab code instead of a mathematical notation. Instead of optimizing your code, we have to write it from scratch. On one hand this is more work for us, on the other hand the created code will not necessarily match your one sufficiently. So please post the code, which you want to get optimized.
You mathematical notation is even not clear:
start loop # 2 (10 iterations)
dVa = - (U \ (L \ (P * P)));
dVaComp = (U \ (L \ (P * M * c * M' * dVa)));
end loop # 2
It is not specified, what is changed in the iterations. Then it is not meaningful to run the loop 10 times at all.
Calculating the inverse explicitly is not useful in general: it is slower and less accurate compared to solving the corresponding linear equation system.
John Doe
John Doe 2013년 5월 13일
편집: John Doe 2013년 5월 13일
Thanks for the hints Jan! I have updated the question, please let me know if I should do further changes, or provide additional information.

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

답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by