How to clear gpuArray, which is a property of a class, from memory when deleting an object class instance
13 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I need to interpolate the same matrix many different times, and doing so with mex/C is still too slow. Thus, I am trying to create a class that will hold the matrix as a gpuArray and then have a method that allows me to interpolate it, without having to send it to the gpu device each time I interpolate it.
The problem I am having is clearing the array from the gpu memory when I clear the class object. The gpuArray does not leave memory and my memory constantly increases upon creating more instances of this class. Below is a minimal working class, and below that is an example of calling it and viewing the available device memory.
classdef gpuInterp
properties
Z
Z_d
end
methods
function F = gpuInterp(Z)
F.Z = Z;
F.Z_d = gpuArray(Z);
end
function Zi = interp(obj,xi,yi)
xi_d = gpuArray(xi);
yi_d = gpuArray(yi);
Zi_d = interp2(obj.Z_d, xi_d, yi_d); clear xi_d yi_d
Zi = gather(Zi_d); clear Zi_d
end
end
end
Example calling the class. First test : create the class, then destroy the class without calling the interp() method. In this test, it appears that the gpuArray in the class is removed as the available memory after clearing the instance is the same as before creating instance.
>> gpuDevice([]);
>> dev = gpuDevice(); % initialize device
>> dev.AvailableMemory % display initial memory
ans =
1563811840
>> F = gpuInterp(rand(5000)); % create instance of class
>> dev.AvailableMemory % display available memory
ans =
1363795968
>> clear F % destroy instance
>> dev.AvailableMemory % display available memory
ans =
1563811840
Second test : create the class, call the interp() method, then destroy the class. In this test, it appears that the gpuArray in the class is not removed as the available memory after clearing the instance is the same as after first creating it.
>> gpuDevice([]);
>> dev = gpuDevice(); % initialize device
>> dev.AvailableMemory % display initial memory
ans =
1526210560
>> F = gpuInterp(rand(5000)); % create instance of class
>> dev.AvailableMemory % display available memory
ans =
1326194688
>> Zi = F.interp(1.5:500.5, (1.5:500.5)'); % call the interp method
>> dev.AvailableMemory % display available memory
ans =
1326097536
>> clear F % destroy instance
>> dev.AvailableMemory % display available memory
ans =
1326097536
Note, I have also tried making a cleanup property as suggested here, that will call a cleanUp function that tries to clear the gpuArray (clear obj.Z_d). I have also tried making it a handle class with a delete method that tried to clear the gpuArray. Neither of these worked.
Is there a way to do what I am trying to do (store a matrix on a gpu to interpolate it many times without having to send it to the gpu each time)?
1 Kommentar
Joss Knight
am 16 Okt. 2017
Can you confirm that every time you create a new gpuInterp object the GPU AvailableMemory goes down, even if you call delete(F) each time? So that eventually you can exhaust GPU memory?
Antworten (1)
Jacob Lynch August
am 6 Nov. 2018
In the delete method you created for the handle, instead of clear, did you try something F.Z_d(:)=[] or Z_id(:)=[]?
0 Kommentare
Siehe auch
Kategorien
Mehr zu GPU Computing finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!