MATLAB Answers

I have problem with clear GPU memory

79 views (last 30 days)
Vitaly Bur
Vitaly Bur on 29 Oct 2020
Commented: Vitaly Bur on 2 Nov 2020
After executing this code, the GPU memory is use by 2 GB. Only the D matrix in GPU memory...
A=fix(gpuArray(rand(1,1000))*99)+1;
B=fix(gpuArray(rand(1,1000))*99)+1;
C=gpuArray(rand(100000,100));
E=C(:,A);
F=C(:,B);
D=E.*F;
clear E F C A B
However, if I execute this code.
D=gpuArray(rand(100000,1000));
There we see D matrix (same size) in GPU memory, but now it only use 1 GB of GPU memory. Why is there a difference? and how to clear the memory in the first variant?
  2 Comments
Vitaly Bur
Vitaly Bur on 30 Oct 2020
Hi, Raymond!
If add in first code a clear D - the GPU memory is reset to 0.
I see the difference between the variants under the same conditions - before starting the program, of course, I reset memory to 0 and everything is cleared to 0.
When the program ends or when I interrupt its execution, everything is reset to 0. The problem is that along with D, a copy of it is stored in memory, or something else that I don't need.
I using the task Manager: GPU memory.
If I use CPU such problem is not present.
look at the charts. You can see that clear works on the CPU and clear memory. But the clear command doesn't work on the GPU. On the GPU, memory is cleared when all variables are cleared. So it shouldn't be?

Sign in to comment.

Answers (1)

Joss Knight
Joss Knight on 2 Nov 2020
MATLAB does not clear all GPU memory unless all variables are released because allocating memory is a performance bottleneck. So MATLAB instead pools memory for later allocations.
To force the GPU to release all memory, you can call reset(gpuDevice).
  7 Comments
Vitaly Bur
Vitaly Bur on 2 Nov 2020
Thanks.A miracle happened - the use of wait(gpu) solved the problem.
gpu=gpuDevice();
reset(gpu);
gpu=gpuDevice();
disp(gpu)
feature('GpuAllocPoolSizeKb',0);
A=fix(gpuArray(rand(1,1000))*99)+1;
B=fix(gpuArray(rand(1,1000))*99)+1;
C=gpuArray(rand(100000,100));
E=C(:,A);
F=C(:,B);
D=E.*F;
wait(gpu)
pause
clear E F C A B
pause
clear D
pause
D=gpuArray(rand(100000,1000));
pause

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by