MATLAB Hilfe-Center
Transfer distributed array, Composite object, or gpuArray object to local workspace
Composite
gpuArray
X = gather(A)
[X1,X2,...,Xn] = gather(A1,A2,...,An)
X = gather(Cd,destination)
[X1,X2,...,Xn] = gather(Cd1,Cd2,...,Cdn,destination)
X = gather(Cp)
[X1,X2,...,Xn] = gather(Cp1,Cp2,...,Cpn)
X = gather(A) can operate on the following array data:
A
On a gpuArray: transfers the elements of A from the GPU to the local workspace and assigns them to X.
X
On a distributed array, outside an spmd statement: gathers together the elements of A from the multiple workers to the local workspace and assigns them to X.
spmd
On a codistributed array, inside an spmd statement or communicating job: gathers together the elements of A and replicates them into X on every worker.
You can call gather on other data types, such as tall arrays (See gather (tall)). If the data type does not support gathering, then gather has no effect.
gather
gather (tall)
Gathering GPU arrays or distributed arrays can be costly and is generally not necessary unless you need to use your result with functions that do not support these types of arrays. For more information on function support, see Run MATLAB Functions on a GPU or Run MATLAB Functions with Distributed Arrays.
X = gather(gpuArray(X)), X = gather(distributed(X)), or X = gather(codistributed(X)) return the original array X.
X = gather(gpuArray(X))
X = gather(distributed(X))
X = gather(codistributed(X))
example
[X1,X2,...,Xn] = gather(A1,A2,...,An) gathers multiple arrays A1,A2,...,An into the corresponding outputs X1,X2,...,Xn. The number of input arguments and output arguments must match.
A1,A2,...,An
X1,X2,...,Xn
X = gather(Cd,destination) converts a codistributed array Cd to a variant array X, such that all of the elements are contained on the worker specified by destination, and X is a 0-by-0 empty double on all other workers.
Cd
destination
[X1,X2,...,Xn] = gather(Cd1,Cd2,...,Cdn,destination) gathers codistributed arrays Cd1,Cd2,...,Cdn into corresponding outputs X1,X2,...,Xn, with all elements on the worker specified by destination. The number of input arguments and output arguments must match.
Cd1,Cd2,...,Cdn
X = gather(Cp) gathers all the elements of Composite array Cp from the multiple workers to the local workspace and assigns them to cell array X.The number of cells in X is dependent on number of workers referenced by Cp. (since R2023a)
Cp
[X1,X2,...,Xn] = gather(Cp1,Cp2,...,Cpn) gathers Composite arrays Cp1,Cp2,...,Cpn into corresponding cell arrays X1,X2,...,Xn. The number of input arguments and output arguments must match. (since R2023a)
Cp1,Cp2,...,Cpn
collapse all
Gather the results of a GPU operation to the MATLAB® workspace.
G = gpuArray(rand(1024,1)); F = sqrt(G); % Input and output are both gpuArray W = gather(G); % Return array to workspace whos
Name Size Bytes Class Attributes F 1024x1 8192 gpuArray G 1024x1 8192 gpuArray W 1024x1 8192 double
Gather all of the elements from a distributed array D onto the client.
D
n = 10; parpool('Processes',4); D = distributed(magic(n)); % Distribute array to workers M = gather(D) % Return array to client
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 of M = magic(n) on all workers and the client.
M = magic(n)
n = 10; parpool('Processes',4); spmd Cd = codistributed(magic(n)); M = gather(Cd); % Gather all elements to all workers end S = gather(Cd) % Gather elements to client
Gather all of the elements of Cd onto worker 1, for operations that cannot be performed across distributed arrays.
n = 10; spmd Cd = codistributed(magic(n)); out = gather(Cd,1); if spmdIndex == 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
Worker 1: areDiagonalsEqual = logical 1
Since R2023a
Gather all of the elements of the composite array Cp stored on parallel workers onto the client.
Use an spmd statement to create a magic square on your workers and a Composite array Cp on the client.
n = 10 p = parpool('Processes',4); spmd Cp = magic(n); end
Gather the results stored on your workers onto the client.
results = gather(Cp)
results = 1×4 cell array {10×10 double} {10×10 double} {10×10 double} {10×10 double}
Array to gather, specified as a gpuArray, distributed array, or codistributed array.
distributed
codistributed
Codistributed array to gather, specified as a codistributed array.
Composite array to gather, specified as a Composite array.
Note that gather 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 using gather within an spmd statement, the gathered array is accessible on the client via its corresponding Composite object; see Access Worker Variables with Composites. If you are running gather in a communicating job, you can return the gathered array to the client as an output argument from the task.
As the gather function on a distributed or codistributed array 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 as if spmdIndex == 1.
if spmdIndex == 1
You can gather gpuArray objects by right-clicking the variable in the workspace, and then selecting Gather from GPU. (since R2025a)
expand all
You can now gather gpuArray objects by right-clicking the variable in the workspace, and then selecting Gather from GPU. This is equivalent to calling the gather function on a gpuArray.
You can now use the gather function to gather elements of a Composite array on parallel workers to the local workspace.
Previously, when you use gather on a Composite array, MATLAB returns the same Composite array as the output.
arrayfun | codistributed | distributed | gpuArray | Composite | pagefun
arrayfun
pagefun
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Website auswählen
Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .
Sie können auch eine Website aus der folgenden Liste auswählen:
So erhalten Sie die bestmögliche Leistung auf der Website
Wählen Sie für die bestmögliche Website-Leistung die Website für China (auf Chinesisch oder Englisch). Andere landesspezifische Websites von MathWorks sind für Besuche von Ihrem Standort aus nicht optimiert.
Amerika
Europa
Asien-Pazifik
Kontakt zu Ihrer lokalen Niederlassung