function [z, varargout] = gather(z,varargin)
%GATHER Collect values into current workspace
%   X = GATHER(D) returns a DLARRAY containing regular numeric or logical
%   data. This method applies GATHER to the underlying data in dlarray D.
%   If D is on the GPU, X is in the local workspace. The dlarray X is not stored on the GPU.
%   If D is in the local workspace (not on the GPU), X is equal to D.
%
%   [X,Y,Z,...] = GATHER(A,B,C,...) gathers multiple arrays.
%
%   See also GATHER, GPUARRAY/GATHER.

%   Copyright 2019-2023 The MathWorks, Inc.

if nargin==1
    % Gather the first input argument
    
    % If this file is modified to not be a standard unary element-wise
    % function, please also remove the following template tags.
    %!define $UnaryFunction gather($zdata)
    %!template $UnaryElementWiseTemplate
    % Code generated and inserted by deep.dev.generateDlarrayMethods.
    
    % Extract data to work on and create a template output object.
    [z, fdz, zdata] = extractUnaryElwiseData(z);
    
    zdata = gather(matlab.lang.internal.move(zdata));
    
    z = restoreUnaryElwiseData(z, ...
        matlab.lang.internal.move(fdz), ...
        matlab.lang.internal.move(zdata));
    %!template
else
    % We only know that one of the inputs is a dlarray but not which one.  Each
    % input must have gather called separately on it.
    z = gather(z);
    varargout = cell(size(varargin));
    for i=1:numel(varargin)
        varargout{i} = gather(varargin{i});
    end
end
end
