-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathODE_tests.m
207 lines (185 loc) · 6.14 KB
/
ODE_tests.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
% Testbench for ODE solvers with different rounding routines.
% Reworked version of https://epubs.siam.org/doi/10.1137/19M1251308
% Clear chop options and reset PRNG seed
clear options
rng(1)
% Set up some ODE conditions
a = 0; b = 0.015625;
y0 = 1.0;
global precision
% Exact solution to the exponential decay ODE
yexact = exp(-0.015625/20)*y0;
% Range of timesteps
nrange = round(10.^linspace(1, 3, 16));
m = length(nrange);
% Standard double precision.
for j = 1:m
n = nrange(j);
x_dp = a;
h_dp = (b-a)/n;
y_dp = y0;
for i=1:n
y_dp = Euler(1, h_dp, x_dp, y_dp, 0, []);
%y_dp = Midpoint(1, h_dp, x_dp, y_dp, 0, []);
%y_dp = Heun(1, h_dp, x_dp, y_dp, 0, []);
x_dp = x_dp + h_dp;
end
efp(j, 1) = abs(y_dp - yexact);
end
options.round = 1; % RN
% All chop formats.
for k = 1:6
switch k
case 1, options.format = 'b'; precision = 7; ...
options.subnormal = 1; sr = 0;
case 2, options.format = 'b'; precision = 7; ...
options.subnormal = 1; sr = 1;
case 3, options.format = 'h'; precision = 10; ...
options.subnormal = 1; sr = 0;
case 4, options.format = 'h'; precision = 10; ...
options.subnormal = 1; sr = 1;
case 5, options.format = 's'; precision = 23; ...
options.subnormal = 1; sr = 0;
case 6, options.format = 's'; precision = 23; ...
options.subnormal = 1; sr = 1;
end
fprintf('k = %1.0f, prec = %s, subnormal = %1.0f\n', ...
k,options.format,options.subnormal)
chop([],options)
% NOTE: We do not want to chop these, or any other constants with SR.
a = chop(a); b = chop(b); y0 = chop(y0);
for j = 1:m
n = nrange(j);
x_fp = chop(a);
h_fp = chop((b-a)/n);
y_fp = chop(y0);
for i=1:n
y_fp = Euler(0, h_fp, x_fp, y_fp, sr, options);
%y_fp = Midpoint(0, h_fp, x_fp, y_fp, sr, options);
%y_fp = Heun(0, h_fp, x_fp, y_fp, sr, options);
%x_fp = chop(x_fp + h_fp, options);
end
efp(j,k+1) = abs(y_fp - yexact);
end
end
h = loglog(...
nrange,efp(:,1),'d-', ...
nrange,efp(:,2),'x--', ...
nrange,efp(:,3),'*--', ...
nrange,efp(:,4),'o--', ...
nrange,efp(:,5),'s-.', ...
nrange,efp(:,6),'o-', ...
nrange,efp(:,7),'-');
xlabel('$n$','Interpreter','latex')
ylabel('Error')
grid
legend('fp64','bfloat16 RN','bfloat16 SR', ...
'fp16 RN','fp16 SR', 'fp32 RN',...
'fp32 SR','Position',[0.69 0.6 0.1 0.2])
set(h,'LineWidth',1.2)
function f = decay_ODE(accurate, x, y, options);
if (accurate)
f = -y/20;
else
f = -chop(y/20, options);
end
end
% ODE solvers
function f = Euler(accurate, h, x, y, SR, options);
if (accurate)
f = y + h*decay_ODE(1, x, y, options);
elseif (SR)
f =srSum(y, srMultFMA(h, decay_ODE(0, x, y, options), ...
rand(), options), rand(), options);
else
f = chop(y + ...
chop(h*chop(decay_ODE(0, x,y, options), options), options), ...
options);
end
end
function f = Midpoint(accurate, h, x, y, SR, options);
if (accurate)
f = y + h*decay_ODE(1, x, y + h*1/2*decay_ODE(1, x, y, options), ...
options);
elseif (SR)
temp = srMultFMA(h, srMultFMA(1/2, decay_ODE(0, x, y, options), ...
rand(), options), rand(), options);
temp = srSum(y, temp, rand(), options);
f = srSum(y, srMultFMA(h, decay_ODE(0, x, temp, options), ...
rand(), options), rand(), options);
else
f = chop(y + ...
chop(h*decay_ODE(0, x, ...
chop(y + ...
chop(chop(1/2*h, options)*decay_ODE(0, x, y, options), ...
options)), options), options), options);
end
end
function f = Heun(accurate, h, x, y, SR, options);
if (accurate)
y_temp = y + h*decay_ODE(1, x, y, options);
f = y + h/2 * (decay_ODE(1, x, y, options) + ...
decay_ODE(1, x, y_temp, options));
elseif(SR)
y_temp = srSum(y, srMultFMA(h, decay_ODE(0, x, y, options), ...
rand(), options), rand(), options);
f = srSum(y, srMultFMA(h/2, srSum(decay_ODE(0, x, y, options), ...
decay_ODE(0, x, y_temp, options), rand(), options), rand(), ...
options), rand(), options);
else
y_temp = chop(y + chop(h*decay_ODE(0, x, y, options), options));
f = chop(y + chop(chop(h/2, options) * ...
chop(decay_ODE(0, x, y, options) + ...
decay_ODE(0, x, y_temp, options), options)));
end
end
% Augmented multiplication algorithm based on FMA
function [sigma, error] = TwoMultFMA(a, b, options);
sigma = chop(a*b, options);
error = chop(a*b-sigma, options); % NOTE: This is an FMA.
end
% Multiplication with stochastic rounding
function f = srMultFMA(a, b, R, options);
global precision
% Switch to RZ
options.round = 4;
chop([], options);
[sigma, error] = TwoMultFMA(a, b, options);
Es = floor(log2(abs(sigma)));
P = sign(error)*R*2^(Es-precision);
f = chop(chop(error+P)+sigma);
% Switch back to RTN
options.round = 1;
chop([], options);
end
% Augmented summation algorithm
function [sum, error] = TwoSum(a, b, options);
sum = chop(a + b, options);
a_trunc = chop(sum - b);
b_trunc = chop(sum - a_trunc);
a_error = chop(a - a_trunc);
b_error = chop(b - b_trunc);
error = chop(a_error + b_error);
end
% Addition with stochastic rounding
function f = srSum(a, b, R, options);
global precision
% Switch on RN
options.round = 1;
[sum, error] = TwoSum(a, b, options);
% Switch to RZ
options.round = 4;
Es = floor(log2(abs(chop(a+b, options))));
P = sign(error)*R*2^(Es-precision);
% Switch to RD or RU.
if (error >= 0)
options.round = 3;
else
options.round = 2;
end
chop([], options);
f = chop(chop(error+P)+sum);
% Switch back to RN
options.round = 1;
chop([], options);
end