function out = hpccHPL(m)
%hpccHPL HPCC HPL Benchmark computation
%   hpccHPL(m) computes x = A\b for a real random matrix A of size m-by-m, and
%   real random vector b of size m-by-1.

% Copyright 2018 The MathWorks, Inc.

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

dataSizeBytes = 8 * m^2;
hpccBenchmarkStarting("HPL", dataSizeBytes);

spmd
    % Create a distributed matrix in the 2d block cyclic distribution and a
    % distributed column vector in 1d
    A = codistributed.randn(m, m, codistributor2dbc);
    b = codistributed.rand(m, 1);
    
    % Time the solution of the linear system   
    tic
    x = A\b;
    t = toc;    

    % Compute scaled residuals
    r1 = norm(A*x-b,inf)/(eps*norm(A,1)*m);
    r2 = norm(A*x-b,inf)/(eps*norm(A,1)*norm(x,1));
    r3 = norm(A*x-b,inf)/(eps*norm(A,inf)*norm(x,inf)*m);
    % This test is specified in the benchmark definition
    if max([r1 r2 r3]) > 16
        error('Failed the HPC HPL Benchmark');
    end
end

% Performance in gigaflops
tmax = max([t{:}]);
perf = (2/3*m^3 + 3/2*m^2)/tmax/1.e9;
out  = hpccBenchmarkEnd("HPL", dataSizeBytes, tmax, perf, "GFlops");
end
