MATLAB Answers


Efficient way to perform loops of matrix multiplications

Asked by AA
on 26 Jan 2018
Latest activity Commented on by David Goodmanson on 27 Jan 2018
I have an implementation that involves multiplying matrices, summing them up, and storing them. It goes like this,
A = 0;
for i=1:1225
% Load operator A_i of size NxN
A_i = load('A_i.mat');
% z_i is some variable of size Nx1 that I compute in this loop
% x is some variable of size Nx1 calculated above this loop
% I have to perform some operations like these two
y_i = A_i*(z_i + x);
A = A + A_i*A_i'
My problem is the large amount of simulation time being consumed by the above code. Even when the operations inside the loop are efficient (let's say ~0.01mins), the entire looped implementation still consumes about ~12-13mins. Can somebody please help me out and suggest an efficient way to do this? Thanks so much!


Potentially you could load them in batches (to avoid filling memory) using parfor (to load in parallel.) However, using parfor often turns out to be slower, especially when you take into account the time to start the pool.
Thanks for your response. Yes, in my case, parfor is not able to help. As you rightly pointed out, the time taken to start the pool overwhelms any time savings thereafter. Unfortunately, I faced the same problem while working on gpuArrays. The time taken for transferring the data is large, and shows no overall improvement. I am currently stuck and have no good way to get it done within some practical time.
Thanks for your help again!
Hi AA,
If N stays constant, when you compute them offline could you stack them into sets of 3d arrays, no one of which is large enough to overrun RAM? That way you would have to load stuff a lot less often.

Sign in to comment.

0 Answers