function out = hpccDataSizes(nWorkers, gbPerWorker)
%hpccDataSizes return suitable problem size for HPCC benchmarks

%   Copyright 2008-2018 The MathWorks, Inc.

if nargin < 1 || isempty(nWorkers)
    pool = gcp('nocreate');
    if isempty(pool)
        % If no there is no pool open, then set nWorkers = 1 and run locally
        nWorkers = 1;
    else
        nWorkers = pool.NumWorkers;
    end
end

if nargin < 2 || isempty(gbPerWorker)
    gbPerWorker = 1;
end

% Compute the total available memory on the system
totalMem = nWorkers * (1024^3) * gbPerWorker ;

% Use a little less than half the system memory. Ideally, it should be half but
% we need to keep a copy of the array in MATLAB as well as the
% ScaLAPACK data
%
% Matrix size: (8*N^2) > 1/2 system memory
out.HPL = fix(sqrt(totalMem / 2.3 / 8));

% For the DGEMM, we need 3 matrices in memory at once, so we use 1/3 of the HPL size
out.DGEMM = fix(sqrt(out.HPL .^ 2 / 3));

% Two vectors in and a vector out: (24*M bytes) > 1/2 of system memory
out.STREAM = fix(totalMem / 24 / 2);

% For the PTRANS case, use the same size as DGEMM as we have two input matrices
% and a single output matrix
out.PTRANS = out.DGEMM;

% Table size (8*M bytes) - 1/4 of system memory
out.RandomAccess = fix(totalMem / 4 / 8);

% A vector in and a vector out: (32*M bytes) > 1/4 of system memory
out.FFT = fix(totalMem / 4 / 32);
% Vector length must be a power of 2
out.FFT = 2^(floor(log2(out.FFT)));
end

