Does gather() clear memory

18 ビュー (過去 30 日間)
James
James 2025 年 1 月 18 日
コメント済み: James 2025 年 1 月 21 日
I am running in to memory limits on my GPUs. I know I can reset(gpuDevice) to clear all memory on the device, however, I would like to move arrays one at a time from GPU memory to memory and then clear the orginal GPU version. Does gather() also clear the GPU memory after copying/moving to memory?
If not, what would be the best way to achieve this?

採用された回答

Joss Knight
Joss Knight 2025 年 1 月 18 日

gather creates a copy of the array in main memory. Clearing a gpuArray variable will release its memory. So if you replace a gpuArray variable x with gather(x) then that will clear the gpuArray and release the memory. In other words

x = gather(x);

releases memory, but

y = gather(x);

does not, because x is still a gpuArray.

Hope that helps.

Note that MATLAB pools GPU memory, so memory available to MATLAB may continue to appear used in the Task Manager. You can get rid of this behaviour by setting the gpuDevice CachePolicy property to "minimum".

  4 件のコメント
Joss Knight
Joss Knight 2025 年 1 月 21 日
編集済み: Joss Knight 2025 年 1 月 21 日
Yes, clear will release GPU memory used by the cleared variable.
tmp = rand(1000, 1, "gpuArray") <= 0.01 creates a temporary 1000-by-1 array of double on the GPU, which will be cleared at the end of the operation, but tmp will now contain a 1000-by-1 array of logical on the GPU. Also, this comparison will be performed lazily so the temporary variable will not be released unless you display tmp or do something else to cause GPU synchronization.
James
James 2025 年 1 月 21 日
Perfect, thank you!

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

その他の回答 (1 件)

Matt J
Matt J 2025 年 1 月 18 日
It seems to for me:
>> A=gpuArray.rand(300,300,300);
>> gpuDevice().AvailableMemory
ans =
3.1956e+09
>> A=gather(A);
>> gpuDevice().AvailableMemory
ans =
3.4116e+09
  1 件のコメント
James
James 2025 年 1 月 18 日
Great, thanks, I have the same

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

カテゴリ

Help Center および File ExchangeParallel and Cloud についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by