GPU processing slower that CPU
조회 수: 4 (최근 30일)
이전 댓글 표시
Hello to all!
This is my first question here! I run a code for image processing and I try to implement this on GPU arrays in order to reduce time. The main part of code is below. When I execute loaded all matrixes as gpuArrays, it takes much longer than using it with simple arrays on workspace. I'm very new on GPU processing and this is my first try. Can someone explain to me about this delay?
Thank you a lot
PS: I'm using Matlab R2013a on a Macbook pro
for i = 1:dim(1)
for j = 1:dim(2)
iMin = max(i-w,1);
iMax = min(i+w,dim(1));
jMin = max(j-w,1);
jMax = min(j+w,dim(2));
I = A(iMin:iMax,jMin:jMax);
H = exp(-(I-A(i,j)).^2/(2*sigma_r^2));
F = H.*G((iMin:iMax)-i+w+1,(jMin:jMax)-j+w+1);
B(i,j) = sum(F(:).*I(:))/sum(F(:));
end
end
댓글 수: 0
채택된 답변
추가 답변 (2개)
Matt J
2013년 6월 28일
편집: Matt J
2013년 6월 28일
You're not using any of gpuArray's accelerated functions as far as I can see, so no wonder that it is slow. The computations you're doing also don't look terribly appropriate for GPU acceleration. About the only thing that can be parallel-split are the iterations of the for-loop, which is best done on the CPU. You might try the version below, which uses more vectorization and is also re-organized to use PARFOR.
II=1:dim(1);
JJ=1:dim(2);
[III,JJJ]=ndgrid(II,JJ);
IMin = max( II - w,1);
IMax = min( II + w,dim(1));
JMin = max(JJ - w,1);
JMax = min(JJ + w,dim(2));
z=2*sigma_r^2;
wplus1=w+1;
parfor k=1:numel(JJJ)
i=III(k); j=JJJ(k);
irange=IMin(i):IMax(i);
jrange=JMin(j):JMax(j);
I = A(irange,jrange);
H = exp(-(I-A(i,j)).^2/z);
F = H.*G((irange)+(wplus1-i),(jrange)+(wplus1-j));
B(i,j) = sum(F(:).*I(:))/sum(F(:));
end
댓글 수: 2
Matt J
2013년 6월 29일
Evripidis Commented:
Thanks for the answer... :) I convert matrixes to GPU arrays previously to program. I just referred here the important point of code, so all A,I,G,H and are already gpuArrays.
Matt J
2013년 6월 29일
편집: Matt J
2013년 6월 29일
Hi Evripidis,
Yes, I understood that A,I,G,H and are already gpuArrays. But converting arrays to gpuArrays doesn't magically make everything you do with them faster. Only certain kinds of operations are accelerated for gpuArrays, like arrayfun() and the functions listed here
For-loops, in particular, are bad on the GPU. That's why I suggested that you convert all the gpuArrays back to normal arrays and try a parfor approach.
참고 항목
카테고리
Help Center 및 File Exchange에서 GPU Computing in MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!