User`s guide

1 Getting Started
1-12
Distribute Arrays and Run SPMD
Distributed Arrays
The workers in a parallel pool communicate with each other, so you can distribute an
array among the workers. Each worker contains part of the array, and all the workers
are aware of which portion of the array each worker has.
Use the distributed function to distribute an array among the workers:
M = magic(4) % a 4-by-4 magic square in the client workspace
MM = distributed(M)
Now MM is a distributed array, equivalent to M, and you can manipulate or access its
elements in the same way as any other array.
M2 = 2*MM; % M2 is also distributed, calculation performed on workers
x = M2(1,1) % x on the client is set to first element of M2
Single Program Multiple Data (spmd)
The single program multiple data (spmd) construct lets you define a block of code that
runs in parallel on all the workers in a parallel pool. The spmd block can run on some or
all the workers in the pool.
spmd % By default creates pool and uses all workers
R = rand(4);
end
This code creates an individual 4-by-4 matrix, R, of random numbers on each worker in
the pool.
Composites
Following an spmd statement, in the client context, the values from the block are
accessible, even though the data is actually stored on the workers. On the client,
these variables are called Composite objects. Each element of a composite is a symbol
referencing the value (data) on a worker in the pool. Note that because a variable might
not be defined on every worker, a Composite might have undefined elements.
Continuing with the example from above, on the client, the Composite R has one element
for each worker:
X = R{3}; % Set X to the value of R from worker 3.