MATLAB parfor and classification/use of variables
2 views (last 30 days)
Show older comments
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
Answers (2)
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.
0 Comments
See Also
Categories
Find more on Parallel Computing Fundamentals in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!