User`s guide

Working with Codistributed Arrays
5-9
(local part) on each worker first, and then combine them into a single array that is
distributed across the workers.
This example creates a 4-by-250 variant array A on each of four workers and then uses
codistributor to distribute these segments across four workers, creating a 4-by-1000
codistributed array. Here is the variant array, A:
spmd
A = [1:250; 251:500; 501:750; 751:1000] + 250 * (labindex - 1);
end
WORKER 1 WORKER 2 WORKER 3
1 2 ... 250 | 251 252 ... 500 | 501 502 ... 750 | etc.
251 252 ... 500 | 501 502 ... 750 | 751 752 ...1000 | etc.
501 502 ... 750 | 751 752 ...1000 | 1001 1002 ...1250 | etc.
751 752 ...1000 | 1001 1002 ...1250 | 1251 1252 ...1500 | etc.
| | |
Now combine these segments into an array that is distributed by the first dimension
(rows). The array is now 16-by-250, with a 4-by-250 segment residing on each worker:
spmd
D = codistributed.build(A, codistributor1d(1,[4 4 4 4],[16 250]))
end
Lab 1:
This lab stores D(1:4,:).
LocalPart: [4x250 double]
Codistributor: [1x1 codistributor1d]
whos
Name Size Bytes Class
A 1x4 613 Composite
D 16x250 649 distributed
You could also use replicated arrays in the same fashion, if you wanted to create
a codistributed array whose segments were all identical to start with. See the
codistributed function reference page for syntax and usage information.
Using MATLAB Constructor Functions
MATLAB provides several array constructor functions that you can use to build
codistributed arrays of specific values, sizes, and classes. These functions operate in the
same way as their nondistributed counterparts in the MATLAB language, except that
they distribute the resultant array across the workers using the specified codistributor
object, codist.