Algorithm Search⌘K

Proofs: Dot2 compensated dot product

These are the human proofs of Dot2 compensated dot product, at full length.

The error bound of Dot2 full

This proof shows: correctness.

Setting. A binary floating-point system with precision pp, unit roundoff u=2−pu = 2^{-p}, round to nearest (any tie rule), and the standard model fl(a∘b)=(a∘b)(1+δ)\mathrm{fl}(a \circ b) = (a \circ b)(1 + \delta) with ∣δ∣≤u|\delta| \le u. We assume that no overflow occurs, that no product xiyix_i y_i underflows in the sense that TwoProduct is exact (for TwoProduct with an FMA it suffices that exi+eyi≥emin⁡+p−1e_{x_i} + e_{y_i} \ge e_{\min} + p - 1), and that nu<1nu < 1. Write γk=ku/(1−ku)\gamma_k = ku/(1 - ku), P=∑i∣xiyi∣P = \sum_{i} |x_i y_i| and xTy=∑ixiyix^{T}y = \sum_i x_i y_i. Two facts are used.

  • (EFT) TwoSum returns (s,q)(s, q) with a+b=s+qa + b = s + q exactly and ∣q∣≤u∣s∣|q| \le u|s|; TwoProduct returns (h,r)(h, r) with ab=h+rab = h + r exactly and ∣r∣≤u∣h∣|r| \le u|h|. The exactness is proved in the encyclopedia pages on TwoSum and TwoProduct; the bounds hold because the error of a rounding to nearest is at most half an ulp of the result, which is at most uu times its magnitude (Muller et al., Section 2.3.2, ulp as defined there), and because a sum whose result is subnormal is exact.
  • (Gamma) (1+u)(1+γk)≤1+γk+1(1 + u)(1 + \gamma_k) \le 1 + \gamma_{k+1}, and if ∣δj∣≤u|\delta_j| \le u for j≤kj \le k then ∏j(1+δj)=1+θk\prod_j (1 + \delta_j) = 1 + \theta_k with ∣θk∣≤γk|\theta_k| \le \gamma_k, for ku<1ku < 1 (Higham, Lemma 3.1 and Lemma 3.3).

Notation for the run. Let (p1,s1)=(h1,r1)(p_1, s_1) = (h_1, r_1) be the output of L1, and for i≥2i \ge 2 let (hi,ri)(h_i, r_i), (pi,qi)(p_i, q_i) and sis_i be the values computed in L3, L4 and L5 at step ii. Put q1=0q_1 = 0 and ei=qi+rie_i = q_i + r_i (exact, a real number), and let ti=fl(qi+ri)t_i = \mathrm{fl}(q_i + r_i) be its computed value for i≥2i \ge 2, with t1=r1=e1t_1 = r_1 = e_1.

Lemma 1 (an exact identity). xTy=pn+∑i=1neix^{T}y = p_n + \sum_{i = 1}^{n} e_i.

Proof. By (EFT), x1y1=p1+r1x_1 y_1 = p_1 + r_1 and, for i≥2i \ge 2, xiyi=hi+rix_i y_i = h_i + r_i and pi−1+hi=pi+qip_{i - 1} + h_i = p_i + q_i. Adding, ∑i≤kxiyi=pk+∑i≤k(qi+ri)\sum_{i \le k} x_i y_i = p_k + \sum_{i \le k}(q_i + r_i) by induction on kk. □\square

Lemma 2 (the small parts are small). ∑i=1n∣ei∣≤γnP\sum_{i = 1}^{n} |e_i| \le \gamma_n P.

Proof. Let Sk=∑i≤k∣hi∣S_k = \sum_{i \le k} |h_i|. We show by induction that ∣pk∣≤(1+γk−1)Sk|p_k| \le (1 + \gamma_{k-1}) S_k and ∑i=2k∣qi∣≤γk−1Sk\sum_{i = 2}^{k} |q_i| \le \gamma_{k - 1} S_k. For k=1k = 1, p1=h1p_1 = h_1 and the sum is empty. For k≥2k \ge 2, pk=fl(pk−1+hk)p_k = \mathrm{fl}(p_{k-1} + h_k), so ∣pk∣≤(1+u)(∣pk−1∣+∣hk∣)≤(1+u)(1+γk−2)Sk≤(1+γk−1)Sk|p_k| \le (1 + u)(|p_{k-1}| + |h_k|) \le (1 + u)(1 + \gamma_{k - 2}) S_k \le (1 + \gamma_{k - 1}) S_k by (Gamma). Then ∣qk∣≤u∣pk∣≤u(1+γk−1)Sk=u1−(k−1)uSk|q_k| \le u|p_k| \le u(1 + \gamma_{k-1}) S_k = \frac{u}{1 - (k-1)u} S_k, and

∑i=2k∣qi∣≤γk−2Sk−1+u1−(k−1)uSk≤(γk−2+u1−(k−1)u)Sk≤γk−1Sk,\sum_{i=2}^{k} |q_i| \le \gamma_{k-2} S_{k-1} + \frac{u}{1 - (k-1)u} S_k \le \Big(\gamma_{k-2} + \frac{u}{1 - (k-1)u}\Big) S_k \le \gamma_{k - 1} S_k,

since γk−1−γk−2=u(1−(k−1)u)(1−(k−2)u)≥u1−(k−1)u\gamma_{k-1} - \gamma_{k-2} = \frac{u}{(1 - (k-1)u)(1 - (k-2)u)} \ge \frac{u}{1 - (k-1)u}. Next, ∣ri∣≤u∣hi∣|r_i| \le u|h_i| gives ∑i∣ri∣≤uSn\sum_i |r_i| \le u S_n, and ∣hi∣≤(1+u)∣xiyi∣|h_i| \le (1 + u)|x_i y_i| gives Sn≤(1+u)PS_n \le (1 + u) P. So ∑i∣ei∣≤(γn−1+u)(1+u)P\sum_i |e_i| \le (\gamma_{n-1} + u)(1 + u) P. Finally (γn−1+u)(1+u)≤γn(\gamma_{n-1} + u)(1 + u) \le \gamma_n: the left side is γn−1+u(11−(n−1)u+u)\gamma_{n-1} + u\big(\tfrac{1}{1 - (n-1)u} + u\big), while γn−γn−1=u1−(n−1)u⋅11−nu=u1−(n−1)u(1+nu1−nu)≥u1−(n−1)u+u⋅nu≥u1−(n−1)u+u2\gamma_n - \gamma_{n-1} = \tfrac{u}{1 - (n-1)u}\cdot\tfrac{1}{1 - nu} = \tfrac{u}{1 - (n-1)u}\big(1 + \tfrac{nu}{1 - nu}\big) \ge \tfrac{u}{1 - (n-1)u} + u \cdot nu \ge \tfrac{u}{1-(n-1)u} + u^2. □\square

Lemma 3 (the sum of the small parts). ∣sn−∑iei∣≤γn∑i∣ei∣|s_n - \sum_{i} e_i| \le \gamma_n \sum_i |e_i|.

Proof. s1=e1s_1 = e_1, and for i≥2i \ge 2, ti=ei(1+δi)t_i = e_i(1 + \delta_i) and si=(si−1+ti)(1+εi)s_i = (s_{i-1} + t_i)(1 + \varepsilon_i) with ∣δi∣,∣εi∣≤u|\delta_i|, |\varepsilon_i| \le u. Unrolling, sn=e1∏j=2n(1+εj)+∑i=2nei(1+δi)∏j=in(1+εj)s_n = e_1 \prod_{j=2}^{n}(1 + \varepsilon_j) + \sum_{i=2}^{n} e_i (1 + \delta_i) \prod_{j = i}^{n} (1 + \varepsilon_j). Each eie_i is multiplied by at most nn factors (1+δ)(1 + \delta) (n−1n - 1 for i=1i = 1, n−i+2≤nn - i + 2 \le n for i≥2i \ge 2), so by (Gamma) sn=∑iei(1+θ(i))s_n = \sum_i e_i (1 + \theta^{(i)}) with ∣θ(i)∣≤γn|\theta^{(i)}| \le \gamma_n. □\square

Theorem. Under the assumptions of the setting, the result res\mathrm{res} of Dot2 satisfies

∣res−xTy∣≤u ∣xTy∣+(1+u) γn2 P.|\mathrm{res} - x^{T}y| \le u\,|x^{T}y| + (1 + u)\,\gamma_n^{2}\, P .

If xTy≠0x^{T}y \ne 0 and cond=2P/∣xTy∣\mathrm{cond} = 2P/|x^{T}y| is the condition number of the dot product, the relative error is at most u+12(1+u)γn2 condu + \tfrac12(1 + u)\gamma_n^{2}\,\mathrm{cond}.

Proof. Let E=∑ieiE = \sum_i e_i, so xTy=pn+Ex^{T}y = p_n + E by Lemma 1, and ∣sn−E∣≤γn2P|s_n - E| \le \gamma_n^{2} P by Lemmas 2 and 3. L6 computes res=(pn+sn)(1+δ)\mathrm{res} = (p_n + s_n)(1 + \delta) with ∣δ∣≤u|\delta| \le u. Then

res−xTy=δ (pn+sn)+(sn−E)=δ xTy+(1+δ)(sn−E),\mathrm{res} - x^{T}y = \delta\,(p_n + s_n) + (s_n - E) = \delta\, x^{T}y + (1 + \delta)(s_n - E),

so ∣res−xTy∣≤u∣xTy∣+(1+u)γn2P|\mathrm{res} - x^{T}y| \le u|x^{T}y| + (1 + u)\gamma_n^{2} P. Dividing by ∣xTy∣=2P/cond|x^{T}y| = 2P/\mathrm{cond} gives the relative form. ■\blacksquare

Remarks. Ogita, Rump and Oishi state the bound with γn2\gamma_n^{2} in place of (1+u)γn2(1 + u)\gamma_n^{2}; the extra factor here, which changes the bound by a relative 2−532^{-53}, comes from the cruder treatment of the last addition. The bound says that Dot2 behaves like a dot product computed with unit roundoff about u2u^{2} and then rounded to working precision: the first term is the final rounding, and the second is negligible unless cond≳1/(nu)2\mathrm{cond} \gtrsim 1/(nu)^2, roughly u−2/n2u^{-2}/n^2. With underflow, TwoProduct can lose the error term, and an additive term proportional to nn times the underflow unit must be added (Ogita, Rump and Oishi treat this case); the tests show a case where the bound above fails without it.

Back to Dot2 compensated dot product