How to use parfor in explicit method?

8 visualizzazioni (ultimi 30 giorni)
Guanyang Xue
Guanyang Xue il 10 Mag 2016
Risposto: Edric Ellis il 11 Mag 2016
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?

Risposte (2)

Walter Roberson
Walter Roberson il 10 Mag 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

Edric Ellis
Edric Ellis il 11 Mag 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).

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by