MATLAB Parallel Computing Toolbox (PCT)
How to use drange
Demonstrated below is an example on drange using matlabpool
interactively. You could also apply it in pmode or save the commands
in a file and run it in batch.
>> n = 40; A = 1:n;
>> matlabpool open 4 %
>> spmd
B = zeros(1,n,codistributor()); % initialize B and declare it codistributed
for i=drange(1:n)
B(i) = A(i);
end
end
>> B
B =
1: class = codistributed, size = [1 40]
2: class = codistributed, size = [1 40]
3: class = codistributed, size = [1 40]
4: class = codistributed, size = [1 40]
In the above, with 4 processors, the 40 elements of A are distributed, in chunks of 10, to B on
each lab. For example, on lab 2
>> spmd
if labindex == 2
b = localPart(B)
disp(['On Lab 2, the size of b is:' num2str(size(b))])
end
end
2
b =
11 12 13 14 15 16 17 18 19 20
On Lab 2, the size of b is: 1 10
Caution: if B is not codistributed, the following results
>> n = 40; A = 1:n;
>> matlabpool open 4 %
>> spmd
for i=drange(1:n)
B(i) = A(i);
end
end
>> B
B =
1: class = double, size = [1 10]
2: class = double, size = [1 20]
3: class = double, size = [1 30]
4: class = double, size = [1 40]
Also, you could not use localPart to print (or use) the local part of B
on lab 2 as was done in the first example. This is because B is not codistributed.
Another example:
n=100; % size of matrix
% initialize matrices to avoid memory framgmentation, performance, and distribute
% among workers
U = zeros(1, n, codistributor()); %
V = U; W = U; % V and W inherit the distributed properties of U
A = ones(1, n, codistributor()).*[1:n];
B = ones(1, n, codistributor()).*[1:n]*10;
C = ones(1, n, codistributor()).*[1:n]*100;
for i=drange(1:n)
[U(i), V(i), W(i)] = my_func(A(i), B(i), C(i));
end
U1 = gather(U,1) % collect U from all labs to lab 1
u = U1{1}; % u lives in the MATLAB workspace
function [u, v, w] = my_func(a, b, c)
u = a*2;
v = b*3;
w = c*4;
In the above example, the loop index i must be used to benefit from drange's functionality to distribute work to workers.
The output arrays U, V, and W, reside locally on the labs according to
the distribution specified through codistributor. On the client, these arrays
are designated as composite arrays -- although they are defined locally as
codistributed arrays. They are not accessible from the client.
To access U from the
MATLAB client for further (serial) processing, such as plotting or other serial
processing,
you need to use gather to either replicate the
entire array on all the workers or, as in this example, gather
the local contributions from all the labs onto lab 1. As a result of gather, U1 is
now accessible from the client. Note that, before the gather, U is a composite
array. After the gather, U1 is a double (cell) array. You can now access it
from the client. If U1 will be used on the client more than once, it would be
more efficient to copy it onto the client and then use the
client-based array u.
|
|