Problem with running code on gpu

3 次查看(过去 30 天)
Hello guys
I have a MATLAB code and want to use GPU to improve the running time, so I changed the arrays to gpuArray, but my code got worse
my laptop fan started working extremely and the temperature got high.
also I monitored gpu usage in Task manager/Performance and it was always zero
I attach my code if anyone can help me with its problem
thanks in advance.

回答(1 个)

Edric Ellis
Edric Ellis 2020-6-3
Your code is not standalone, so I couldn't try it. But the main point is that you need to vectorise your code. This will speed things up on the CPU, and will speed them up even more on the GPU. You will also end up with simpler code. To keep the GPU busy, you need to give it big chunks of work to do all at once. Your code was operating on arrays one element at a time - that sometimes works out OK on the CPU, but is always terrible on the GPU.
There's some general guidance on vectorisation in the documentation here: https://www.mathworks.com/help/matlab/matlab_prog/vectorization.html .
To take a tiny example from your code:
Difference=zeros(videoObject.Height,videoObject.Width,videoObject.NumFrames,'gpuArray');
for ii=1:videoObject.Height
for jj=1:videoObject.Width
for kk=1:videoObject.NumFrames
Difference(ii,jj,kk)=abs(double(blackAndWhiteVideo(ii,jj,kk))-double(BackGround(ii,jj)));
end
end
end
This is a perfect example where vectorisation can help. In fact, in recent releases of MATLAB, you can use MATLAB's implicit dimension expansion to make this whole block extremely concise:
% Step 1: transfer the two input arrays to the GPU, and
% convert them to 'double'
bwGpu = double(gpuArray(blackAndWhiteVideo));
bgGpu = double(gpuArray(BackGround));
% Step 2: compute the difference, and then call 'abs' (on the GPU).
Difference = abs(bwGpu - bgGpu);
The crucial point here is that the subtraction implicitly expands your "background" array in the third dimension to match the size of blackAndWhiteVideo.
  2 个评论
Nozhan Ghoreishi
Nozhan Ghoreishi 2020-6-3
I tried to fully vectorize my code but got wrong outputs therefore I implemented for-loop style,
by the way I didnt fully understand which part of my code made the problem I mentioned
maybe there is some wrong using of gpuArray syntax o something like that missing
Thanks alot.
Edric Ellis
Edric Ellis 2020-6-4
In that case, I would carefully go over your vectorisation. If you can narrow down places where you're not getting the answer you expect, post another question with a simple example showing what you tried and what the problem is.
Explicit loops over scalar values will never work well on the GPU. (The counter-example is that if you can arrange your code into an arrayfun form, that can work well, because the arrayfun function can be converted to an efficient form for the GPU).

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 GPU Computing in MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by