Skip to Main Content Skip to Search
Product Documentation

gather - Transfer distributed array data or GPUArray to local workspace

Syntax

X = gather(A)
X = gather(C, lab)

Description

X = gather(A) can operate inside an spmd statement, pmode, or parallel job to gather together the data of a codistributed array, or outside an spmd statement to gather the data of a distributed array. If you execute this inside an spmd statement, pmode, or parallel job, X is replicated array with all the data of the array on every lab. If you execute this outside an spmd statement, X is an array in the local workspace, with the data transferred from the multiple labs.

X = gather(distributed(X)) or X = gather(codistributed(X)) returns the original array X.

X = gather(C, lab) converts a codistributed array C to a variant array X, such that all of the data is contained on lab lab, and X is a 0-by-0 empty double on all other labs.

For a GPUArray input, X = gather(A) transfers the data from the GPU to the local workspace.

If the input argument to gather is not a distributed, a codistributed, or a GPUArray, the output is the same as the input.

Tips

Note that gather assembles the codistributed or distributed array in the workspaces of all the labs on which it executes, or on the MATLAB client, respectively, but not both. If you are using gather within an spmd statement, the gathered array is accessible on the client via its corresponding Composite object; see Accessing Data with Composites. If you are running gather in a parallel job, you can return the gathered array to the client as an output argument from the task.

As the gather function requires communication between all the labs, you cannot gather data from all the labs onto a single lab by placing the function inside a conditional statement such as if labindex == 1.

Examples

Distribute a magic square across your labs, then gather the whole matrix onto every lab and then onto the client. This code results in the equivalent of M = magic(n) on all labs and the client.

n = 10;
spmd
  C = codistributed(magic(n));
  M = gather(C) % Gather data on all labs
end
S = gather(C) % Gather data on client

Gather all of the data in C onto lab 1, for operations that cannot be performed across distributed arrays.

n = 10;
spmd
  C = codistributed(magic(n));
  out = gather(C, 1);
  if labindex == 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)
  end
end
Lab 1:
  areDiagonalsEqual =
    1

Gather all of the data from a distributed array into D on the client.

n = 10;
D = distributed(magic(n)); % Distribute data to labs
M = gather(D) % Return data to client

Gather the results of a GPU operation to the local workspace.

G = gpuArray(rand(1024,1));
F = sqrt(G); %input and output both GPUArray
W = gather(G); % Return data to client
whos
Name         Size            Bytes  Class

F         1024x1               108  parallel.gpu.GPUArray
G         1024x1               108  parallel.gpu.GPUArray
W         1024x1              8192  double

See Also

arrayfun | codistributed | distributed | gpuArray | pmode

  


Free Parallel Computing Interactive Kit

See how to solve large problems with minimal effort and reduce simulation time.

Get free kit

Trials Available

Try the latest versions of parallel computing products.

Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS