How to use parfor in explicit method?

8 ビュー (過去 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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by