Worker Object Wrapper
编者注: This file was selected as MATLAB Central Pick of the Week
Note that since MATLAB release R2015b, parallel.pool.Constant supersedes
the WorkerObjWrapper.
The WorkerObjWrapper is designed for situations where a piece of
data is needed multiple times inside the body of a PARFOR loop or
an SPMD block, and this piece of data is both expensive to
create, and does not need to be re-created multiple
times. Examples might include: database connection handles, large
arrays, and so on.
Consider a situation where each worker needs access to a large
but constant set of data. While this data set can be passed in to
the body of a PARFOR block, it does not persist there, and will
be transferred to each worker for each PARFOR block. For example:
largeData = generateLargeData( 5000 );
parfor ii = 1:20
x(ii) = someFcn( largeData );
end
parfor ii = 1:20
y(ii) = someFcn( largeData, x(ii) );
end
This could be simplified like so:
wrapper = WorkerObjWrapper( @generateLargeData, 5000 );
parfor ii = 1:20
x(ii) = someFcn( wrapper.Value );
end
parfor ii = 1:20
y(ii) = someFcn( wrapper.Value, x(ii) );
end
In that case, the function "generateLargeData" is evaluated only
once on each worker, and no large data is transferred from the
client to the workers. The large data is cleared from the workers
when the variable "wrapper" goes out of scope or is cleared on
the client.
Another example might be constructing a worker-specific
log-file. This can be achieved like so:
% build a function handle to open a numbered text file:
fcn = @() fopen( sprintf( 'worker_%d.txt', labindex ), 'wt' );
% opens the file handle on each worker, specifying that fclose
% will be used later to "clean up" the file handle created.
w = WorkerObjWrapper( fcn, {}, @fclose );
% Run a parfor loop, logging to disk which worker operated on which
% loop iterates
parfor ii=1:10
fprintf( w.Value, '%d\n', ii );
end
clear w; % causes "fclose(w.Value)" to be invoked on the workers
type worker_1.txt % see which iterates worker 1 got
引用格式
MathWorks Parallel Computing Toolbox Team (2024). Worker Object Wrapper (https://www.mathworks.com/matlabcentral/fileexchange/31972-worker-object-wrapper), MATLAB Central File Exchange. 检索时间: .
MATLAB 版本兼容性
平台兼容性
Windows macOS Linux类别
标签
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!版本 | 已发布 | 发行说明 | |
---|---|---|---|
1.4.0.1 | Added note that parallel.pool.Constant supersedes WorkerObjWrapper
|
||
1.4.0.0 | Change worker cleanup to retrieve memory sooner. |
||
1.3.0.0 | Fix first-time initialization problems in R2013a and R2013b. |
||
1.2.0.0 | Minor performance improvements; ability to construct wrapper from Composite. |
||
1.0.0.0 |