Trying to run my code in GPU

조회 수: 3 (최근 30일)
Jingqi Sun
Jingqi Sun 2022년 12월 9일
댓글: Jingqi Sun 2022년 12월 10일
Hello, I am trying to run my code in GPU. I just installed parallel toolbox. Here is a part of my code:
w=zeros(1000);
A=gpuArray(w);
for z=1:3000
for i = 2:length(A)-1
for n=2:width(A)-1
A(i,n)=(A(i+1,n)+A(i-1,n)+A(i,n+1))+A(i,n-1)/4;
end
z
end
end
I don't think my code is running in GPU because the Task Manager shows GPU is not working.
And the code is running much slower than not running it in GPU.
Can someone help me fix it?
Thanks
  댓글 수: 2
Rik
Rik 2022년 12월 9일
You probably intended to use height instead of length.
Other than that, you didn't define the array A and you're not using the array you created on the GPU, so why exactly did you expect this to work?
It also looks like your final result might be reproducible with a convolution or another array operation, although I'm not entirely certain which one exactly. The fastest loop is of course replacing the loop with array operations.
Jingqi Sun
Jingqi Sun 2022년 12월 9일
@Rik Hi, I defined 'A' outside the matrix , sorry I forgot to copy and paste that line. I will updated my question right now. I know built in array operations could operate much faster, but I don't think there have a built in function for what I am trying to do. Thanks.

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

채택된 답변

Matt J
Matt J 2022년 12월 9일
편집: Matt J 2022년 12월 9일
Your code isn't gettng the benefit of the GPU because you are not applying any parallelized functions to your gpuArray, A. In this case, conv2() would be appropriate.
w=zeros(1000);
A=gpuArray(w);
k=[0 1 0;
1 0 1;
0 1 0]/4;
for z=1:3000
A=conv2(A,k,'valid');
end
  댓글 수: 1
Jingqi Sun
Jingqi Sun 2022년 12월 10일
Hi thank you for your reply! I think you are right because iteration time even takes longer when I use gpuArray.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 GPU Computing에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by