parallelizing calculations with nested for loops?

1 次查看(过去 30 天)
I have the following (simplified) code that I want to parallelize. It is a sort of 'embarrassingly simple' parallelizable task. Basically, there is a large 2D matrix 'agegrid' which represents a 2D space. Another 2D matrix of the same size 'netw' needs to be constructed that calculates a result depending on the properties of the matrix 'agegrid'. Currently, it is trivial to parallelize this problem with near 100% efficiency simply by running several instances of matlab and running a code that only evaluates a particular region of netw(yy,xx). But this adds a postprocessing problem to stitch the results together, and matlab appears to slow down windows dramatically when many instances are open (>10), even if the task manager says no work is being done. It seems matlabs parallelization isn't intelligent enough to parallelize loops like this?
I wish I could just assign work to processors in a more straightforward way...
What should my strategy be?
for yy=ymin:ymax
for xx=xmin:xmax
netw=0;
for i=ymin:ymax
for j=xmin:xmax
age=agegrid(i,j);
y=(i-yy)*dy;
x=(j-xx)*dx;
netw=netw+depth(age)/(x^2+y^2)^0.5
end
end
steps=steps+1;
progress=steps/totsteps
end
end
  1 个评论
Walter Roberson
Walter Roberson 2014-2-2
I would imagine part of your strategy would involve using the value of netw after the end of the "for i" loop. ;-)
Is there a reason you have not vectorized your "for i" "for j" calculation of netw ?

请先登录,再进行评论。

回答(1 个)

Jan
Jan 2014-2-2
Do not start a parallelization before cleaning the code. E.g.
y=(i-yy)*dy;
and
y^2
is calculated in each iteration of the for j loop, although its result does not depend on j.
The power operation is very expensive, so prefer sqrt() insead of ^0.5.
When the loops over i and j are not large (the exact limit depends on many factors), a vectorization of the inner loops can result in a substantial speedup. But above a certain problem size, reserving memory for the intermediate matrices can rule the processing time. Then one parallelized loop by parfor might be better. You can simply try if the innermost or outermost loop increases the speed more.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by