How to use parfor in explicit method?
8 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Walter Roberson
on 10 May 2016
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 Comments
Edric Ellis
on 11 May 2016
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 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!