function y = mean(x, varargin)
%MEAN Average or mean value of gpuArray
%   S = MEAN(X)
%   S = MEAN(X,DIM)
%   S = MEAN(...,'double')
%   S = MEAN(...,'default')
%   S = MEAN(...,'omitnan')
%   S = MEAN(...,'includenan')
%   
%   The 'native' option is not supported.
%   
%   Example:
%    
%       N = 10000;
%       X = gpuArray.colon(-N, N);
%       m = mean(X);
%       isequal(m, gpuArray(0)) % true
%   
%   See also MEAN, GPUARRAY, GPUARRAY/MIN, GPUARRAY/MAX,
%            gpuArray/VAR, gpuArray/COV, gpuArray/SUM

%   Copyright 2013-2014 The MathWorks, Inc.

% Gather all trailing args
varargin = cellfun(@gather, varargin, 'UniformOutput', false);

% If 'x' is not on the GPU, use the host version
if ~isa(x, 'gpuArray')
    y = mean(x, varargin{:});
    return;
end

% try...catch prevents error being reported by internal
try
    y = parallel.internal.flowthrough.mean(x, varargin{:});
catch ME
    throw(ME);
end
