Build parallel.pool.Constant from data or function handle
C = parallel.pool.Constant(X)
C = parallel.pool.Constant(FH)
C = parallel.pool.Constant(FH,CLEANUP)
C = parallel.pool.Constant(COMP)
C = parallel.pool.Constant(X)
copies the value
X
to each worker and returns a parallel.pool.Constant object,
C
, which allows each worker to access the value
X
within a parallel language construct
(parfor
, spmd
,
parfeval
) using the property C.Value
. This can
improve performance when you have multiple parfor-loops accessing the same constant
set of data, because X
is transferred only once to the
workers.
C = parallel.pool.Constant(FH)
evaluates function handle
FH
on each worker and stores the result in
C.Value
. This is also useful for creating and using any
handle-type resources on the workers, such as file handles and database
connections.
C = parallel.pool.Constant(FH,CLEANUP)
evaluates function
handle FH
on each worker and stores the result in
C.Value
. When C
is cleared, the function
handle CLEANUP
is evaluated with a single argument
C.Value
on each worker.
C = parallel.pool.Constant(COMP)
uses the values stored in
the Composite COMP
, and stores them in C.Value
on each worker. This is especially useful when the data that you need to use inside
a parfor-loop can be constructed only on the workers, such as when the data is too
large to conveniently fit in the client, or when it is being loaded from a file that
only the workers can access. If COMP
does not have a defined
value on every worker, an error results.
parallel.pool.Constant
must be called in the MATLAB client
session.
You can use a parallel.pool.Constant
with an already running
parallel pool or subsequent parallel pools.
This example shows how to create a numeric parallel.pool.Constant, and use it
in multiple parfor
-loops on the same pool.
First, create some large data on the client, then build a parallel.pool.Constant, transferring the data to the pool only once.
data = rand(1000); c = parallel.pool.Constant(data); for ii = 1:10 % Run multiple PARFOR loops accessing the data. parfor jj = 1:10 x(ii,jj) = c.Value(ii,jj); end end
This example shows how to create a parallel.pool.Constant with a function handle and a cleanup function.
Create a temporary file on each worker. By passing @fclose
as the second argument, the file is automatically closed when
c
goes out of scope.
c = parallel.pool.Constant(@() fopen(tempname(pwd),'wt'),@fclose); spmd disp(fopen(c.Value)); % Displays the temporary filenames. end parfor idx = 1:1000 fprintf(c.Value,'Iteration: %d\n',idx); end clear c; % Closes the temporary files.
This example shows how to build large data sets as a Composite on pool workers
inside an spmd
block, and then use that data as a
parallel.pool.Constant inside a parfor
-loop.
spmd if labindex == 1 x = labBroadcast(1,rand(5000)); else x = labBroadcast(1); end end xc = parallel.pool.Constant(x); parfor idx = 1:10 s(idx) = sum(xc.Value(:,idx)); end s
s = 1.0e+03 * 2.5108 2.5031 2.5123 2.4909 2.4957 2.5462 2.4859 2.5320 2.5076 2.5432
parcluster
| parfeval
| parfor
| parpool
| spmd