speed up run time of mldivide function.

6 views (last 30 days)
pb
pb on 23 Nov 2018
Commented: pb on 28 Nov 2018
Hi,
I am calling mldivide function about 10^7 times:
my syntax looks this way:
C=mldivide(A,B);
where both A,B are sparse matrices. size(A)=2^10*2^10 and size(B)=(1x2^10) (Assume that my matrix A has ~2^12 non zero data points ~95% of them close to center diagonal.A is a lower traingular matrix)
After using the profile command ,when i run the code, i found it takes 60-70% of total time(~2000 secs) in that particular line.(My computer specs are i5, 8gb ram, matlab 2018b)
can i make mldivide compute any faster? (assume that I can only use sparse matrices, and assume that i want to run it on my computer.).
Unfortunately, GPU computing of mldivide using sparse matrice does not seem to work so well. If you can, please comment on using 'GPU computing with (mldivide + sparse matrices)' as well..

Accepted Answer

Steven Lord
Steven Lord on 23 Nov 2018
If you're solving the system A*x = b with the same A every time (or a small number of different A matrices many times each) try creating a decomposition and reusing that decomposition each time.
  8 Comments
Bruno Luong
Bruno Luong on 28 Nov 2018
Edited: Bruno Luong on 28 Nov 2018
Matrix upper/lower triangular does not need to de factorized since back-substution can work directly with original matrix. So it's normal decomposition, lu, chol or friends won't help (they actually do nothing more than factorizing a general matrix of two or more triangulars/permutation matrices, which in your case reduce nothing).
You might want to program your own back-substitution in MEX if the structure of your matrix is special. But I doubt you'll beat MATLAB.
I think what you ask is impossible,just buy a faster machine.
pb
pb on 28 Nov 2018
Thank you Bruno for the comment.
I was thinking about MEX too.but it might comsume asignificant amount of time.So i am avoiding that.
I dont want to spend money. So faster machine is not possible either.

Sign in to comment.

More Answers (2)

Bruno Luong
Bruno Luong on 23 Nov 2018
You might want to use iterative method.
  7 Comments
pb
pb on 27 Nov 2018
Edited: pb on 27 Nov 2018
Hi Bruno, I have taken your code and made minor changes: the results are below:
A = sparse(eye(10000));
b = randn(length(A),1);
A(:,1)=A(:,1)+b;A(1,1)=1;
A(:,2)=A(:,2)+b;A(2,2)=1;A(1,2)=0;
A(:,3)=A(:,3)+b;A(3,3)=1;A(1,3)=0;A(2,3)=0;
ntest = 10000;
x1=b;
tic
for ii=1:ntest
x11 = A \ x1;
end
toc
Elapsed time is 0.889389 seconds.
x2=b;
tic % iteratibe method
for ii=1:ntest
[x22,flag,relres,iter,resvec] = cgs(A,x2);
end
toc
Elapsed time is 11.631365 seconds
x3=b;
dA = decomposition(A);
tic
for ii=1:ntest
x33 = dA \ x3;
end
toc
Elapsed time is 0.888435 seconds.
It seems that amount of time reduced by decomposition()-function when A is already lower traingular (& probably even when A is upper traiangular) seems very minor when compared with '\'.
And when we solve a slightly different problem ( a loop of x2=A\x2 instead of x22=A\x2 (code and example below)),decomposition and cgs interative method seem even less effective than '\'.
A = sparse(eye(10000));
b = randn(length(A),1);
A(:,1)=A(:,1)+b;A(1,1)=1;
A(:,2)=A(:,2)+b;A(2,2)=1;A(1,2)=0;
A(:,3)=A(:,3)+b;A(3,3)=1;A(1,3)=0;A(2,3)=0;
ntest = 10000;
x1=b;
tic
for ii=1:ntest
x1 = A \ x1;
end
toc
Elapsed time is 0.876455 seconds.
x2=b;
tic % iteratibe method
for ii=1:ntest
[x2,flag,relres,iter,resvec] = cgs(A,x2);
end
toc
Elapsed time is 5.783371 seconds.
x3=b;
dA = decomposition(A);
tic
for ii=1:ntest
x3 = dA \ x3;
end
toc
Elapsed time is 0.884621 seconds.
It seems Matlab '\' operator more powerful in itself than with any additional commands in the above examples.
Bruno Luong
Bruno Luong on 27 Nov 2018
A = sparse(eye(10000));
b = randn(length(A),1);
A(:,1)=A(:,1)+b;A(1,1)=1;
A(:,2)=A(:,2)+b;A(2,2)=1;A(1,2)=0;
A(:,3)=A(:,3)+b;A(3,3)=1;A(1,3)=0;A(2,3)=0;
Such matrix is special, you can just the solution by splitting in blocks of
A(1:3,1:3), A(4:end,1:3) and A(4:end,4:end)
Solutions are reduce to 3x3 inversion.
Again if you are not willing to tell us more about your matrix, we can only guess the best way to make the resolution. This can take forever.

Sign in to comment.


James Tursa
James Tursa on 26 Nov 2018
Edited: James Tursa on 26 Nov 2018
  1 Comment
pb
pb on 27 Nov 2018
Edited: pb on 27 Nov 2018
Thank you for the answer James. I will try it.

Sign in to comment.

Categories

Find more on Performance and Memory in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!