Nondistributed Versus Distributed Arrays
Introduction
Many built-in data types and data structures supported by MATLAB® software are also supported in the MATLAB parallel computing environment. This includes arrays of any number of dimensions containing numeric, character, logical values, cells, or structures. In addition to these basic building blocks, the MATLAB parallel computing environment also offers different types of arrays.
Nondistributed Arrays
When you create a nondistributed array, MATLAB constructs a separate array in the workspace of each worker, using the same variable name on all workers. Any operation performed on that variable affects all individual arrays assigned to it. If you display from worker 1 the value assigned to this variable, all workers respond by showing the array of that name that resides in their workspace.
The state of a nondistributed array depends on the value of that array in the workspace of each worker:
Replicated Arrays
A replicated array resides in the workspaces of all workers, and its size and content are identical on all workers. When you create the array, MATLAB assigns it to the same variable on all workers. If you display in spmd the value assigned to this variable, all workers respond by showing the same array.
spmd, A = magic(3), end WORKER 1 WORKER 2 WORKER 3 WORKER 4 | | | 8 1 6 | 8 1 6 | 8 1 6 | 8 1 6 3 5 7 | 3 5 7 | 3 5 7 | 3 5 7 4 9 2 | 4 9 2 | 4 9 2 | 4 9 2
Variant Arrays
A variant array also resides in the workspaces of all workers, but its content differs on one or more workers. When you create the array, MATLAB assigns a different value to the same variable on all workers. If you display the value assigned to this variable, all workers respond by showing their version of the array.
spmd, A = magic(3) + spmdIndex - 1, end WORKER 1 WORKER 2 WORKER 3 WORKER 4 | | | 8 1 6 | 9 2 7 | 10 3 8 | 11 4 9 3 5 7 | 4 6 9 | 5 7 9 | 6 8 10 4 9 2 | 5 10 3 | 6 11 4 | 7 12 5
A replicated array can become a variant array when its value becomes unique on each worker.
spmd B = magic(3); %replicated on all workers B = B + spmdIndex; %now a variant array, different on each worker end
Private Arrays
A private array is defined on one or more, but not all
workers. You could create this array by using spmdIndex
in a
conditional statement, as shown here:
spmd if spmdIndex >= 3, A = magic(3) + spmdIndex - 1, end end WORKER 1 WORKER 2 WORKER 3 WORKER 4 | | | A is | A is | 10 3 8 | 11 4 9 undefined | undefined | 5 7 9 | 6 8 10 | 6 11 4 | 7 12 5
Codistributed Arrays
With replicated and variant arrays, the full content of the array is stored in the workspace of each worker. Codistributed arrays, on the other hand, are partitioned into segments, with each segment residing in the workspace of a different worker. Each worker has its own array segment to work with. Reducing the size of the array that each worker has to store and process means a more efficient use of memory and faster processing, especially for large data sets.
This example distributes a 3-by-10 replicated array A
across
four workers. The resulting array D is also 3-by-10 in size, but only a segment of
the full array resides on each worker.
spmd A = [11:20; 21:30; 31:40]; D = codistributed(A); getLocalPart(D) end WORKER 1 WORKER 2 WORKER 3 WORKER 4 | | | 11 12 13 | 14 15 16 | 17 18 | 19 20 21 22 23 | 24 25 26 | 27 28 | 29 30 31 32 33 | 34 35 36 | 37 38 | 39 40
For more details on using codistributed arrays, see Working with Codistributed Arrays.