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

 採用された回答

John D'Errico
John D'Errico 2018 年 3 月 7 日

0 投票

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 件のコメント

Daan Van Cauteren
Daan Van Cauteren 2018 年 3 月 7 日
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?
John D'Errico
John D'Errico 2018 年 3 月 8 日
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.
Daan Van Cauteren
Daan Van Cauteren 2018 年 3 月 8 日
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?
Daan Van Cauteren
Daan Van Cauteren 2018 年 3 月 8 日
It seems that this does the job! Thanks for the help!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by