I have to traverse a 2D-array and perform some operations on them, depending on the results of an if-statement. I have made a small example of the loop here:
N=128;
A = rand(N,N);
B = rand(N,N);
sqr = @(x) x.^2;
for xi=1:N
for yi=1:N
a = A(xi,yi);
b = B(xi,yi);
if( abs(a-b)<1 )
result=2.0;
else
result = sqr(a-b);
end
res_matrix(xi,yi) = result;
end
end
I want to parallelize this for-loop. I have read the MathWorks-page on parfor, and the way I would parallelize it is just by making the outer-loop into a parfor.
Is this the best speed-up I can get, or should I structure my loop differently?
Here is a more detailed version of my loop:
for xi= 1:N
for yi= 1:N
a = A(:,xi,yi); %a is a vector
b = B(:,xi,yi); %b is a vector
D = a-b;
if( max(D./a)<1e-3 )
test_var=2.0;
else
F_min = F(a, b, 0); %F is some function, such as a Newton-Raphson solver etc...
F_max = F(a, b, 1); %F is some function, such as a Newton-Raphson solver etc...
if( F_min*F_max>0.0 )
test_var=2.0;
else
test_var = F(a, b, 2);
end
end
var(1, xi,yi) = test_var;
end
end
forloops intopar-forloops is good when, you want to repeat the same process again and again (for instance when gathering statistical information). Otherwise you need to manage the resource assignment to each different worker.