function D = pDoBuildFromReplicatedScalar(s, dim)
%Private method to construct a codistributed scalar from replicated
%   scalar local parts using a 1d codistributor with dimension==dim.
%   The scalar is stored on worker 1.
%   
%   


%   Copyright 2020-2022 The MathWorks, Inc.

% Build a 1d codistributor for a scalar - distributed using dim
codistr = codistributor1d(dim, [1, zeros(1, spmdSize - 1)], ...
    [1, 1]);
% Set local part to empty with correct size if not on worker 1
if spmdIndex ~= 1
    dimVec = ones(1, max(2, dim));
    dimVec(dim) = 0;
    s = zeros(dimVec, 'like', s);
end
% Build codistributed variable
D = codistributed.build(s, codistr);
end