Programming Tips


All workers executing an spmd statement must have the same MATLAB® search path as the client, so that they can execute any functions called in their common block of code. Therefore, whenever you use cd, addpath, or rmpath on the client, it also executes on all the workers, if possible. For more information, see the parpool reference page. When the workers are running on a different platform than the client, use the function pctRunOnAll to properly set the MATLAB path on all workers.

Error Handling

When an error occurs on a worker during the execution of an spmd statement, the error is reported to the client. The client tries to interrupt execution on all workers, and throws an error to the user.

Errors and warnings produced on workers are annotated with the worker ID (labindex) and displayed in the client's Command Window in the order in which they are received by the MATLAB client.

The behavior of lastwarn is unspecified at the end of an spmd if used within its body.

spmd Limitations

Nested Functions

Inside a function, the body of an spmd statement cannot make any direct reference to a nested function. However, it can call a nested function by means of a variable defined as a function handle to the nested function.

Because the spmd body executes on workers, variables that are updated by nested functions called inside an spmd statement do not get updated in the workspace of the outer function.

Anonymous Functions

The body of an spmd statement cannot define an anonymous function. However, it can reference an anonymous function by means of a function handle.

Nested spmd Statements

The body of an spmd statement cannot directly contain another spmd. However, it can call a function that contains another spmd statement. The inner spmd statement does not run in parallel in another parallel pool, but runs serially in a single thread on the worker running its containing function.

Nested parfor-Loops

The body of a parfor-loop cannot contain an spmd statement, and an spmd statement cannot contain a parfor-loop.

Break and Return Statements

The body of an spmd statement cannot contain break or return statements.

Global and Persistent Variables

The body of an spmd statement cannot contain global or persistent variable declarations.

More About

Was this topic helpful?