MATLAB parfor and classification/use of variables

2 views (last 30 days)
I do not have much experience with parallelization in MATLAB (<2 weeks of do-it-yourself), still I got a parfor loop working, similar to this:
N = 11;
var1 = zeros(N, 2);
parfor n=1:N
a = zeros(3, 3);
b = zeros(3, 3);
[a, b] = myfun(<arguments>);
var1(n, :) = [sum(sum(a,1),2) sum(sum(b,1),2);]
end
<portion of non parallelized code that makes use of var1 contents>
If I understood the documentation/examples so far, 'var1' is classified as a sliced variable and 'a' and 'b' as temporary variables. I'm using 'var1' this way because I need to use its content after the execution of the parfor loop.
I also wanted to use 'a' and 'b' variables as sliced variables to avoid reinitialization/unnecessary memory reallocation(?)/increase parallel performance.
I tried something like this for variable 'a':
N = 11;
var1 = zeros(N, 2);
a = zeros(3, 3, N);
parfor n=1:N
b = zeros(3, 3);
[a(:,:,n), b] = myfun(<arguments>);
var1(n, :) = [sum(sum(a(:,:,n) ,1),2) sum(sum(b,1),2);]
end
<another portion of non parallelized code that makes use of var1 contents>
with the intent of making workers (re)use pieces of the same region in memory, but I get the error "The variable a in a parfor cannot be classified."
So... why can't it be classified and used as a sliced variable, like var1?
Thank you.
  2 Comments
Matt J
Matt J on 16 Mar 2015
Edited: Matt J on 16 Mar 2015
A more efficient alternative to this
var1(n, :) = [sum(sum(a,1),2) sum(sum(b,1),2);]
is this,
var1(n, :) = [sum(a(:)) sum(b(:))]
Notice that you have half the number of calls to the sum() function, this way.

Sign in to comment.

Answers (2)

Matt J
Matt J on 16 Mar 2015
Edited: Matt J on 16 Mar 2015
Your code gives me no classification errors, so your example may not be sufficient to capture what you are seeing in your actual code.
Regardless, though, the only reason you should try to slice a and b in the code you've shown is if you plan to use them later after the parfor loop. There is no other benefit, e.g., in memory allocation, that the second version of your code could achieve, as far as I can see. The call to a function like myfun() in the line
[a(:,:,n), b] = myfun(<arguments>);
always creates temporary variables when it returns output arguments. It has to do so to determine what to assign to a(:,:,n) and b. Therefore, no memory allocation is avoided in this step.
Similarly also, you are not accomplishing anything by having pre-allocation statements in your first loop like
a = zeros(3, 3);
b = zeros(3, 3);
since a and b get completely overwritten later. Pre-allocation of an array is only valuable in situations when your code would otherwise cause the array to grow or shrink in size throughout a loop.

Alexandre Dias
Alexandre Dias on 16 Mar 2015
There was something important missing in my examples.
Function 'myfun' has a (really) long list of arguments, and variable 'a' was also one of such arguments. So, when I tried to slice 'a', the code was something like:
N = 11;
var1 = zeros(N, 2);
a = zeros(3, 3, N);
parfor n=1:N
b = zeros(3, 3);
[a(:,:,n), b] = myfun(a,<rest of the arguments>);
var1(n, :) = [sum(sum(a(:,:,n) ,1),2) sum(sum(b,1),2);]
end
Of course, variable 'a' could not be classified, as it is indexed inside parfor in two different ways (a or a(:,:,:) and a(:,:,n)).
Having variable 'a' as an argument was a legacy piece of code. I originally learned how to program in C and I was secretly hoping that by passing 'a' as argument MATLAB would re-use the corresponding memory region inside function 'myfun' and return it as output. I'm just not very knowledgeable in Java/Objected Oriented paradigm...
In the first example, after removing 'a' from the arguments list of 'myfun', I got the message "The variable 'a' appears to be pre-allocated, but pre-allocation is not recommended here." in line 'a = zeros(3, 3);', which follows what you said.
Actually, I had this problem for 4 variables in my code that behave like variable 'a' in this sense (parallelization), so this is supposed to save me some more time.
I saw in your profile that you're into Optimization Algorithms. I can tell you that the purpose of this parfor loop is to make each worker calculate objective function values for different stages (N=11 iterations/stages, 'a' and 'b' represent objectives that are calculated inside 'myfun' and variable 'var1' stores the objective function values for the N stages of optimization).
I'm in hands with a multi-objective and multi-stage optimization problem. After parallelization, I only need to wait about 1-2 days instead of 4 days for final results :) (quadcore CPU)
My problem is solved for now. I'll keep working on further optimizing my code.

Categories

Find more on Parallel Computing Fundamentals 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!