How to clear gpuArray, which is a property of a class, from memory when deleting an object class instance

20 ビュー (過去 30 日間)
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 件のコメント
Joss Knight
Joss Knight 2017 年 10 月 16 日
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?

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

回答 (1 件)

Jacob Lynch August
Jacob Lynch August 2018 年 11 月 6 日
In the delete method you created for the handle, instead of clear, did you try something F.Z_d(:)=[] or Z_id(:)=[]?

カテゴリ

Help Center および File ExchangeGPU Computing in MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by