Checking if persistent variable exists on GPU: check if 'data no longer exists on the GPU' for a variable.

조회 수: 5 (최근 30일)
Hello,
I am running into an issue if I have persistent variables that I am storing as GPU arrays. This is especially useful if they are somewhat large, static variables, since I don't need to move them on/off the device or recalculate them on every function call. However, if I have foolishly reset my gpuDevice, I run into issues. Typically I will have code of the form:
persistent x
if isempty(x)
x = calculateX(argin)
end
I then run into an issue on isempty, which unsurprisingly throws an error 'The data no longer exists on the device.' The isempty boolean never returns, the program returns an error, and the persistent variable is never assigned.
What I really need is a logical check as to whether a variable has been cleared on the device, but I cannot seem to find any obvious solutions. Everything else I have thought of seems too kludgy:
(A) use a try-catch statement around the isempty statement (yuck).
(B) have a custom reset(gpuDevice) function that also calls clear on all of the functions that use persistent gpuArrays (more yuck)
(C) have a try-catch statement at the header of each function that will reset all persistent variables if it throws an error.
Since the obvious solution is just a logical telling me whether a variable has been cleared, these do not seem like the best options. There, of course, may be good reasons to avoid storing persistent variables on the GPU, but I haven't seen them.
Thanks,
-Dan

채택된 답변

Edric Ellis
Edric Ellis 2018년 12월 6일
You need existsOnGPU, like so:
x = gpuArray(1);
assert(existsOnGPU(x));
reset(gpuDevice);
assert(~existsOnGPU(x));
  댓글 수: 1
D. Plotnick
D. Plotnick 2018년 12월 6일
편집: D. Plotnick 2018년 12월 6일
Thanks, that is exactly the command I needed.
Note (this may be intentional) but
doc existsOnGPU
sends you to the gpuArray help document. You then have to access existsOnGPU via the See Also links.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by