About the overhead of 'parallel.pool.Constant' in sub-function arugment transfer.
Show older comments
I write a nested parfor loops like this:
parfor i1=1:n1
.......
for i2=1:n2
......
out=sub-function( )
....
end
end
it costs about 1000 secs, munch longer than ordinary for loops (8 secs). After some tries, I find that the reason for the big differences of cost time migtht rely on the arguments transfer of sub-funtion in the nested parfor loops. I define a 'C=parallel.pool.Constant(c) ' before the parfor loop, then when I directly transfer C to the sub-function like this:
out=sub-function(C)
the cost time is 1000 secs, but when I transfer C to the sub-function like this:
out=sub-function(C.Value)
the cost time is 6 secs. So I wanna know which reason or mechanism of 'parallel.pool.Constant' results in the big difference of cost time. I have read the help doc of 'parallel.pool.Constant', but didn't find answer.
Answers (1)
Well, this is just a guess, but a class property access statement like C.Value always calls a property get method get.Value(). Depending on what the get-method does for a given class, there can be significant overhead. In the case when you pass the whole parallel.pool.Constant, you are incurring this overhead in every pass through the while loop in your subfunction.
One thing you could test is to write the sub-function as follows, and see if the overhead changes when you pass the whole C object.
function out=subfunction(C)
val=C.Value; %extract Value once, outside loop.
while condition is true
vq=val(position)
......
end
end1 Comment
Categories
Find more on Parallel for-Loops (parfor) 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!