Calculation speed/time changes for same operation

Hi all. I have come across a very weird issue lately. I have a long piece of code setup where iteratively the same code snippets get called. There is this one moment in the code where a large equation/matrix solve has to be done:
U(freedof,:) = K(freedof, freedof)\F(freedof,:);
Where:
U = zeros(verylargenumber, 24);
F = (verylargenumber by 24);
K = (verylargenumber by verylargenumber);
The strange thing is, i.e. that when the first time this equation is solved it takes 130 seconds and every second time (even with different input variables) this equation keeps going. It seems that MATLAB can't do it again. If I do the equation with the same input from iteration 2 but then in iteration 1, it also takes 130 seconds.
Does anyone know what can cause this problem and what is a possible solution. Personally, I was thinking it had something to do with a memory issue...
Greetings
Daan

 Accepted Answer

It definitely is a memory issue. Simplest is to get more memory, which is relatively cheap.
If the array is always the same size, you might be able to resolve the problem by not creating a new array each time on the fly. You are now forcing MATLAB to allocate a large chunk of memory. So, if instead, you just stuff the new array into the old one, like this
A(:) = K(freedof, freedof);
U(freedof,:) = A\F(freedof,:);
you might be able to avoid that allocation step. Again, it requires a fixed size array.

4 Comments

Every iteration it just overwrites itself, so isn't that what you suggest? All the variables are calculated again in every iteration but are allocated with the same name, so doesn't this mean that always the same amount of memory is used?
When you do this operation:
K(freedof, freedof)
MATLAB allocates memory to store it, as a temporary variable, each time anew. There is no assurance that MATLAB is able to find a good place for it, so there may be some memory reshuffling as your computer farms some stuff out to disk. For example, suppose the spot where MATLAB put that array now is no longer available as contiguous memory, because some other variable got created there? An array needs to live in contiguous memory.
A quick check on my part seems to show no gain on my computer on a moderate problem, thus 15000x15000. In fact, it is slightly slower in my tests. But I don't know what size problem you are solving, nor how much free memory you have. My conjecture was it is possible this might help you. While it probably won't help, it seemed worth a try.
OK, and can this problem be fixed by using a clearvars or clear command at the end of the iteration so that it has plenty of memory to work with afterworths?
It seems that this does the job! Thanks for the help!

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!