How to use parfor in explicit method?
조회 수: 7 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
답변 (2개)
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
댓글 수: 0
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).
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!