About the overhead of 'parallel.pool.Constant' in sub-function arugment transfer.

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-functionC
the cost time is 1000 secs, but when I transfer C to the sub-function like this:
out=sub-functionC.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.

2 Comments

We need to see what subfunction() is doing in each case.
C=parallel.pool.Constant(c), and c is an inerpolation funtion created by 'griddedInterpolant', C is transferrd to the subfuntion having the form:
function out=subfunction(C_interp)
while condition is true
vq=C(position) or vq=C.Value(position)
......
end
end
'...... ' above are some ordinary assignment statements. When C is transferred to C_interp, the cost time is 1000 secs, and when C.Value is transferred to C_interp, the cost time is 6 secs.

Sign in to comment.

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
    end

1 Comment

I have tested this method, it works partly. The cost time is reduced from 1000s to about 300 secs, but still longer than the form of transferring C.value to the subfunction. So I guess transferring parallel.pool.Constant to subfuntion has more overhead than transferring the value of parallel.pool.Constant .

Sign in to comment.

Categories

Asked:

on 17 Apr 2018

Edited:

on 18 Apr 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!