How to improve the speed of large matrix addition?

조회 수: 11 (최근 30일)
Christian Stetco
Christian Stetco 2021년 4월 12일
편집: James Tursa 2021년 4월 12일
Dear all,
I am currently working on a real-time implementation of a finite-element method solver which requires large matrix operations.
I encountered the following issue:
In my code, I need to perform addition of two matrices which are both M x M where M has minimum value of 5000. I know that the second matrix is a diagonal matrix.
So, for example code snippet like this
M = 5000;
A = randn(M, M);
B = eye(M);
C = A + B;
Unfortunately, this operation costs between 50 ms and 70 ms of speed, which is a lot in my application.
I also tried to only manipulate only the diagonal indices but in terms of timing there was no real improvement.
Is there any way of improving this in terms of speed?
Thanks and best regards,
Christian

답변 (1개)

James Tursa
James Tursa 2021년 4월 12일
편집: James Tursa 2021년 4월 12일
If you are generating a new matrix C each time, then you are essentially doing two things:
(1) Copying all the elements of A into a new matrix
(2) Adding the diagonals of B into this new matrix
So speeding up (2) isn't going to do anything to speed up (1). I.e., just indexing the diagonals may speed up (2) but all the data copying in step (1) is still going to cost you. If you could use just one matrix and avoid the data copying then maybe you might get a speed improvement:
A(1:M+1:end) = A(1:M+1:end) + B(1:M+1:end)
Or if you just held B as an M-element vector, then
A(1:M+1:end) = A(1:M+1:end) + B
The actual addition operation itself will not take much time at all. Just the normal MATLAB m-code overhead on this is going to cost you some time that you can't avoid, unless you do unapproved inplace operations via a mex routine.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by