Parfor help

8 visualizaciones (últimos 30 días)
David
David el 21 de Mayo de 2011
Hi everyone,
I've been playing around with matlab recently and learning how to use it and a part of this has been experimenting with parallel computing. I've been running some tests on the following piece of code and am having trouble understanding the results.
I know this code is probably by no means optimal, but it's the behavior I'm getting that's confusing me. Here it is:
parfor index = 1:64000000
if (mod(index,8000) == 0)
eqn = fix(index/8000);
phi = 8000;
else
eqn = 1 + fix(index/8000);
phi = mod(index,8000);
end
if (eqn == phi)
Q(index) = 4;
else
if (mod(eqn,100) ~= 0 && phi == (eqn+1))
Q(index) = -1;
end
if (eqn <= 7900 && phi == (eqn+100))
Q(index) = -1;
end
if (mod(eqn,100) ~= 1 && phi == (eqn-1))
Q(index) = -1;
end
if (eqn >= 101 && phi == (eqn-100))
Q(index) = -1;
end
end
end
When I replace the "parfor" with a simple "for" command this piece of code executes just fine. But when I type "matlabpool local 4" and run the code with the parfor command, it takes up to 10-15 times longer to compute.
I've been trying to figure out what's causing the extreme slow down, seeing as how I thought a large number of iterations was a good candidate for parallelization. As far as I can tell, there are no huge variables continuously being passed to slow it down(Q should be sliced?) and I feel the overhead should be less than the gain or at the minimum produce a result comparable to the original..
It's not only this code, I've had similar code to this one with comparable iterations and commands experience the same issue.
Any tips to help me out as I learn this cool feature would be much appreciated!

Respuestas (1)

Walter Roberson
Walter Roberson el 21 de Mayo de 2011
You should consider using distributed arrays instead of parfor() here.
Also, you might as well combine all of those if/end statements into a single if statement, since they all have the same outcome of setting Q(index) = -1

Categorías

Más información sobre Parallel Computing Toolbox en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by