Worker Object Wrapper

Simplifies managing resources such as large data within PARFOR loops and SPMD blocks
다운로드 수: 3K
업데이트 날짜: 2016/10/31

라이선스 보기

편집자 메모: 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 );
parfor ii = 1:20
y(ii) = someFcn( largeData, x(ii) );

This could be simplified like so:

wrapper = WorkerObjWrapper( @generateLargeData, 5000 );
parfor ii = 1:20
x(ii) = someFcn( wrapper.Value );
parfor ii = 1:20
y(ii) = someFcn( wrapper.Value, x(ii) );

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 );

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 (, MATLAB Central File Exchange. 검색됨 .

MATLAB 릴리스 호환 정보
개발 환경: R2013b
모든 릴리스와 호환
플랫폼 호환성
Windows macOS Linux
Help CenterMATLAB Answers에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!
버전 게시됨 릴리스 정보

Added note that parallel.pool.Constant supersedes WorkerObjWrapper
Updated license

Change worker cleanup to retrieve memory sooner.

Fix first-time initialization problems in R2013a and R2013b.

Minor performance improvements; ability to construct wrapper from Composite.