function out = hpccSTREAM(m)
%hpccSTREAM HPCC STREAM Benchmark computation
%   hpccSTREAM(m) computes a = b + c*k for random vectors b & c of length m
%   and a random constant k. The minimum of 10 timed runs is used to deduce
%   the overall memory access bandwidth of the system in GB/s.

if nargin < 1
    sz = hpccDataSizes();
    m = sz.STREAM;
end

dataSizeBytes = 8 * m;
hpccBenchmarkStarting("STREAM", dataSizeBytes);

% Repeat the test n times
n = 10;
spmd
    % How big should we make the individual vectors 
    N = fix(m/numlabs);

    % Create vectors b and c of normally distributed random numbers
    b = randn(N, 1);
    c = randn(N, 1);
    alpha = randn;
    t = inf;
    
    for k = 1:n
        tic
        a = b + alpha*c; 
        % Performance on one process uses the best time achieved out of 10
        t = min(t, toc);
    end
    
    if a(1) ~= b(1) + alpha*c(1)
        error('Failed the HPC Stream Benchmark');
    end
end

% Performance across all the processes uses the mean time.
% 24 bytes the data transfer for each element in the vector, made from
% the reading of b and c and assignment to a, alpha is assumed to already
% be in some cache.
tmean = mean([t{:}]);
perf  = 24 * m / tmean / 1.e9;
out   = hpccBenchmarkEnd("STREAM", dataSizeBytes, tmean, perf, "GBperSec");
end
