Problem with running code on gpu

6 views (last 30 days)
Nozhan Ghoreishi
Nozhan Ghoreishi on 3 Jun 2020
Commented: Edric Ellis on 4 Jun 2020
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.

Answers (1)

Edric Ellis
Edric Ellis on 3 Jun 2020
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: .
To take a tiny example from your code:
for ii=1:videoObject.Height
for jj=1:videoObject.Width
for kk=1:videoObject.NumFrames
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.
Edric Ellis
Edric Ellis on 4 Jun 2020
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).

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!