I have problem with clear GPU memory

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

Hi Vitaly,
I don't have a GPU at my disposal. Your code, A..D, consumes 2.37 GB. D alone is 763 MB. So I think that aligns with what you're seeing, except I'm not sure why you think only D is in the GPU memory.
Have you called reset to clear the GPU?
reset(gpuDevice)
Are you using nvidia-smi to check if the memory goes up and down?
Raymond
Vitaly Bur
Vitaly Bur 2020년 10월 30일
편집: Vitaly Bur 2020년 10월 30일
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?

댓글을 달려면 로그인하십시오.

답변 (1개)

Joss Knight
Joss Knight 2020년 11월 2일

2 개 추천

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

Vitaly Bur
Vitaly Bur 2020년 11월 2일
Thanks for the answer.
The problem is that I don't need to clear ALL my GPU memory. I need to clear up memory from data that will not be used, but that takes up expensive memory space.
Joss Knight
Joss Knight 2020년 11월 2일
편집: Joss Knight 2020년 11월 2일
You can force MATLAB to stop pooling memory with this feature command:
feature('GpuAllocPoolSizeKb',0);
Vitaly Bur
Vitaly Bur 2020년 11월 2일
편집: Vitaly Bur 2020년 11월 2일
The effect appeared, but the memory is not cleared. something is happening (look for the red arrow), but not that.
You can see on CPU is all ok.
Joss Knight
Joss Knight 2020년 11월 2일
What does the red arrow represent? You calling clear E F C A B?
Yes. The red arrow is the moment when everything is cleared except D. (clear E F C A B).
To the Right of this arrow is the result of cleaning.
The Blue arrow is when D is cleared.
The Yellow arrow is when an array of D is created again (for to see the size of the array D).
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;
pause
clear E F C A B
pause
clear D
pause
D=gpuArray(rand(100000,1000));
pause
Joss Knight
Joss Knight 2020년 11월 2일
Oh I see now. This is just because D hasn't been evaluated yet, which means E and F are still being held in memory, as is C because E and F are just slices of C which means it's being held in memory until we attempt to modify them. We use a lazy evaluation optimization and you haven't actually required D to be used or displayed. Insert gather(D) or wait(gpu) to force evaluation, and the memory will be freed.
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

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2020년 10월 29일

댓글:

2020년 11월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by