How to use parfor in explicit method?

조회 수: 7 (최근 30일)
Guanyang Xue
Guanyang Xue 2016년 5월 10일
답변: Edric Ellis 2016년 5월 11일
When I'm trying to use explicit method to solve a heat transfer problem, I tried to use parfor like
parfor i=2:n+2
for j=2:n+2
Tnew(i,j)=Told(i,j)+k*(Told(i-1,j)+Told(i+1,j)+Told(i,j-1)+Told(i,j+1)-4*To(i,j));
end
end
However, this is extremely slow, about 10 times slower than a regular loop. So how to make it run faster? Or is it possible to do this on GPU?

답변 (2개)

Walter Roberson
Walter Roberson 2016년 5월 10일
I am surprised it allowed it. You should have had to write something closer to
Tnew = zeros(n+2, n+2);
parfor i=2:n+2
ToldiPrev = Told(i-1,:);
Toldi = Told(i,:);
ToldiNext = Told(i+1,:);
Tnewi = zeros(1,n+2);
Toi = To(i,:);
for j=2:n+2
Tnewi(j) = Toldi(j) + k * (ToldiPrev(j) + ToldiNext(j) + Toldi(j-1) + Toldi(j+1) - 4 * Toi(j));
end
Tnew(i,:) = Tnewi;
end

Edric Ellis
Edric Ellis 2016년 5월 11일
parfor generally doesn't offer much speedup when the body of the loop is a small amount of simple arithmetic like this. In this case, I think you'd be much better off trying to recast the update as a 2D convolution - using conv2. That will almost certainly be much faster (and can even run on the GPU).

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by