parallelizing calculations with nested for loops?

2 views (last 30 days)
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 Comment
Walter Roberson
Walter Roberson on 2 Feb 2014
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 ?

Sign in to comment.

Answers (1)

Jan
Jan on 2 Feb 2014
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.

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!