function commonSubmitArgs = getCommonSubmitArgs(cluster, numWorkers)
% Get any additional submit arguments for the Slurm sbatch command
% that are common to both independent and communicating jobs.

% Copyright 2016-2017 The MathWorks, Inc.

% wiki: 

commonSubmitArgs = '';

ppn = validatedPropValue(cluster, 'ProcsPerNode', 'double');
if isempty(ppn) || ppn < 1
     emsg = sprintf(['\n\t\t>> %% Must set ProcsPerNode.  E.g.\n\n', ...
		     '\t\t>> c = parcluster;\n', ...
		     '\t\t>> c.AdditionalProperties.ProcsPerNode = 16;\n', ...
		     '\t\t>> c.saveProfile\n\n']);
     error(emsg) %#ok<SPERR>
end

if numWorkers < ppn
	% If numWorkers is less than ppn, the entire job can fit on one node
	commonSubmitArgs = sprintf('--ntasks=%d', numWorkers);
else
	% Need to split the job up accross multiple nodes
    numNodes = validatedPropValue(cluster, 'NumberOfNodes', 'double');
    if isempty(numNodes) || numNodes < 1
        % Number of nodes not specified or is invalid, automatically
        % determine the number of nodes required for the job.
        numNodes = ceil(numWorkers/ppn);
    end
    commonSubmitArgs = sprintf('--ntasks=%d --nodes=%d --ntasks-per-node=%d',numWorkers, numNodes, ppn);
end


%% REQUIRED

% Partition / Check for GPU
ngpus = validatedPropValue(cluster, 'GpusPerNode', 'double');
if ngpus>0
    qn = '';
else
    qn = validatedPropValue(cluster, 'QueueName', 'char');
end
if ~isempty(qn)
    commonSubmitArgs = [commonSubmitArgs ' -p ' qn];
else
    emsg = sprintf(['\n\t\t>> %% Must set QueueName to use.  E.g.\n\n', ...
                    '\t\t>> c = parcluster;\n', ...
                    '\t\t>> c.AdditionalProperties.QueueName = ''queue-name'';\n', ...
                    '\t\t>> c.saveProfile\n\n']);
    error(emsg) %#ok<SPERR>
end

% Account
an = validatedPropValue(cluster, 'AccountName', 'char');
if isempty(an)
	emsg = sprintf(['\n\t\t>> %% Must provide Account Name to use. E.g.:\n\n', ...
	    '\t\t>> c = parcluster;\n', ...
	    '\t\t>> c.AdditionalProperties.AccountName = ''account_name'';\n', ...
	    '\t\t>> c.saveProfile\n\n']);	
	error(emsg) %#ok<SPERR>
else
	commonSubmitArgs = [commonSubmitArgs ' -A ' an];
end


%% OPTIONAL

% Account Name
% an = validatedPropValue(cluster, 'Account', 'char');
% if ~isempty(an)
%     commonSubmitArgs = [commonSubmitArgs ' -A ' an];
% end

% Quality of Service (QoS)
qos = validatedPropValue(cluster, 'QualityOfService', 'char');
if ~isempty(qos)
    commonSubmitArgs = [commonSubmitArgs ' --qos=' an];
end

% Partition / Check for GPU
% ngpus = validatedPropValue(cluster, 'GpusPerNode', 'double');
% if ngpus>0
%     qn = '';
% else
%     qn = validatedPropValue(cluster, 'QueueName', 'char');
% end
% if ~isempty(qn)
%     commonSubmitArgs = [commonSubmitArgs ' -p ' qn];
% end

% Walltime
wt = validatedPropValue(cluster, 'WallTime', 'char');
if ~isempty(wt)
    commonSubmitArgs = [commonSubmitArgs ' -t ' wt];
end

% Physical Memory used by a single core
mu = validatedPropValue(cluster, 'MemUsage', 'char');
if ~isempty(mu)
    commonSubmitArgs = [commonSubmitArgs ' --mem-per-cpu=' mu];
end

% Email notification
ea = validatedPropValue(cluster, 'EmailAddress', 'char');
if ~isempty(ea)
    commonSubmitArgs = [commonSubmitArgs ' --mail-type=ALL --mail-user=' ea];
end

% Every job is going to require a certain number of MDCS licenses.
% Specification of MDCS licenses which must be allocated to this
% job.  The /etc/slurm/slurm.conf file must list
%
%   # MDCS licenses
%   Licenses=MATLAB_Distrib_Comp_Engine:32
%
% And then call
%
%   % scontrol reconfigure
%
%commonSubmitArgs = sprintf('%s --licenses=MATLAB_Distrib_Comp_Engine:%d',commonSubmitArgs,numWorkers);

% Catch-all
asa = validatedPropValue(cluster, 'AdditionalSubmitArgs', 'char');
if ~isempty(asa)
    commonSubmitArgs = [commonSubmitArgs ' ' asa];
end

commonSubmitArgs = strtrim(commonSubmitArgs);
