Transfer distributed array or gpuArray to local workspace
X = gather(A)
X = gather(C,lab)
X = gather(A)
can operate inside anspmd
statement, pmode, or communicating job to gather together the elements of a codistributed array, or outside anspmd
statement to gather the elements of a distributed array. If you execute this inside anspmd
statement, pmode, or communicating job,X
is a replicated array with all the elements of the array on every worker. If you execute this outside anspmd
statement,X
is an array in the local workspace, with the elements transferred from the multiple workers.
X = gather(distributed(X))
orX = gather(codistributed(X))
returns the original arrayX
.
X = gather(C,lab)
converts a codistributed arrayC
to a variant arrayX
, such that all of the elements are contained on workerlab
, andX
is a 0-by-0 empty double on all other workers.
For a gpuArray input,X = gather(A)
transfers the array elements from the GPU to the local workspace.
If the input argument togather
is not a distributed, a codistributed, or a gpuArray, the output is the same as the input.
Distribute a magic square across your workers, then gather the whole matrix onto every worker and then onto the client. This code results in the equivalent ofM = magic(n)
on all workers and the client.
n = 10;spmdC = codistributed(magic(n)); M = gather(C)% Gather all elements to all workersendS = gather(C)% Gather elements to client
Gather all of the elements ofC
onto worker 1, for operations that cannot be performed across distributed arrays.
n = 10;spmdC = codistributed(magic(n)); out = gather(C,1);iflabindex == 1% Characteristic sum for this magic square:characteristicSum = sum(1:n^2)/n;% Ensure that the diagonal sums are equal to the% characteristic sum:areDiagonalsEqual = isequal...(trace(out),trace(flipud(out)),characteristicSum)endend
Lab 1: areDiagonalsEqual = 1
Gather all of the elements from a distributed arrayD
onto the client.
n = 10; D = distributed(magic(n));% Distribute array to workersM = gather(D)% Return array to client
Gather the results of a GPU operation to the MATLAB workspace.
G = gpuArray(rand(1024,1)); F = sqrt(G);% Input and output are both gpuArrayW = gather(G);% Return array to workspacewhos
Name Size Bytes Class F 1024x1 108 gpuArray G 1024x1 108 gpuArray W 1024x1 8192 double
Note thatgather
assembles the codistributed or distributed array in the workspaces of all the workers on which it executes, or on the MATLAB®client, respectively, but not both. If you are usinggather
within anspmd
声明中聚集数组的访问client via its correspondingComposite
object; seeAccess Worker Variables with Composites. If you are runninggather
in a communicating job, you can return the gathered array to the client as an output argument from the task.
As thegather
function requires communication between all the workers, you cannot gather data from all the workers onto a single worker by placing the function inside a conditional statement such asif labindex == 1
.
arrayfun
|bsxfun
|codistributed
|distributed
|gpuArray
|pagefun
|pmode