Skip to content

Commit

Permalink
Add ability to specify random number distribution in stochastic rounding
Browse files Browse the repository at this point in the history
  • Loading branch information
imciner2 committed May 11, 2022
1 parent f9033a0 commit 1d37238
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
12 changes: 12 additions & 0 deletions chop.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
% underflow, or subnormal numbers will be produced only if necessary
% for the data type of X. This option is useful for exploring
% low precisions independent of range limitations.
% 6. If options.randfunc is supplied, then in stochastic rounding (modes
% 5 and 6) the random numbers used for rounding will be generated
% using that function. It should be a function that has a single argument
% for the number of random numbers to generate and returns a vector of
% the random numbers. If this option is not specified, the default
% function using MATLAB's rand() is used.
%
% On the first call: if options is omitted or only partially specified
% the defaults stated above are used.
Expand Down Expand Up @@ -79,6 +85,7 @@
fpopts.format = 'h'; fpopts.subnormal = 1;
fpopts.round = 1; fpopts.flip = 0; fpopts.p = 0.5;
fpopts.explim = 1;
fpopts.randfunc = @(n) rand(n, 1);
reset_format_settings = 1;
elseif nargin == 2 && ~isempty(options)
% This is not the first call, but fpopts might have all empty fields.
Expand Down Expand Up @@ -116,6 +123,11 @@
else
fpopts.explim = 1;
end
if isfield(options,'randfunc') && ~isempty(options.randfunc)
fpopts.randfunc = options.randfunc;
else
fpopts.randfunc = @(n) rand(n, 1);
end
reset_format_settings = 1;
end

Expand Down
8 changes: 7 additions & 1 deletion roundit.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@
% If options.flip = 1 (default 0) then each element of the rounded result
% has, with probability options.p (default 0.5), a randomly chosen bit
% (in its binary representation) flipped.
% The random number generator used for stochastic rounding can be
% specified by setting options.randfunc to an anonymous function that
% takes as an argument the number of random numbers to generate and
% returns a vector of the random numbers. By default, this is the MATLAB
% rand function.

if nargin < 2 || isempty(options)
options.round = 1; options.flip = 0; options.p = 0.5;
end
if ~isfield(options,'round'), options.round = 1; end
if ~isfield(options,'flip'), options.flip = 0; end
if ~isfield(options,'p'), options.p = 0.5; end
if ~isfield(options,'randfunc'), options.randfunc = @(n) rand(n, 1); end

mysign = @(x) sign(x) + (x==0); % mysign(0) = 1;

Expand Down Expand Up @@ -57,7 +63,7 @@
if isempty(k)
y = x;
else
rnd = rand(length(k),1);
rnd = options.randfunc(length(k));
vals = frac(k); vals = vals(:);

switch options.round
Expand Down
10 changes: 10 additions & 0 deletions test_roundit.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@
end
assert_eq(true, abs(j / nsamp - options.p) <= 1e-2)

% Test custom random number generator function
% Force round up behavior by biasing generator
options = rmfield(options,'p');
options = rmfield(options,'flip');
options.round = 6;
options.randfunc = @(n) rand(n,1) + 0.6;
A = [0 -1.1 -1.5; -1.9 -2.4 -0.5];
y = roundit(A,options);
assert_eq(y,[0 -1 -1; -1 -2 0])

fprintf('All tests successful!\n')

%%%%%%%%%%%%%%%%%%%%%%%
Expand Down

0 comments on commit 1d37238

Please sign in to comment.