Loading...
Searching...
No Matches
OzakiScheme.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3
4#include "OzakiScheme.h"
6#include <algorithm>
7#include <cstdlib>
8#include <limits>
9
10#include "OzakiSchemeII.h"
11
12namespace gpr::linalg {
13namespace {
15int ceilLog2(Eigen::Index length)
16{
17 if (length <= 1) return 0;
18 int bits = 0;
19 auto rest = static_cast<unsigned long long>(length);
20 while (rest > 1) {
21 rest >>= 1;
22 ++bits;
23 }
24 if ((static_cast<unsigned long long>(length) &
25 (static_cast<unsigned long long>(length) - 1ULL)) != 0) {
26 ++bits;
27 }
28 return bits;
29}
31} // namespace
32
33int schemeISliceBits(Eigen::Index length)
34{
35 // 2*tau + ceil(log2(length)) <= 52 leaves the sum of `length` slice
36 // products inside the binary64 significand.
37 const int room = 52 - ceilLog2(length < 1 ? 1 : length);
38 const int width = room / 2;
39 return width < 1 ? 1 : width;
40}
41
42SchemeISlices splitSchemeI(const Eigen::Ref<const Eigen::VectorXd>& values)
43{
45 out.bits = schemeISliceBits(values.size());
46 const Eigen::Index length = values.size();
47 double max_abs = 0.0;
48 bool finite = true;
49 for (Eigen::Index i = 0; i < length; ++i) {
50 const double value = values(i);
51 if (!std::isfinite(value)) finite = false;
52 max_abs = std::max(max_abs, std::fabs(value));
53 }
54 if (!finite || length == 0) {
55 out.bits = 0;
56 return out;
57 }
58 if (max_abs == 0.0) {
59 out.slices.push_back(Eigen::VectorXd::Zero(length));
60 return out;
61 }
62
63 Eigen::VectorXd residual = values;
64 while (true) {
65 double residual_max = 0.0;
66 for (Eigen::Index i = 0; i < length; ++i)
67 residual_max = std::max(residual_max, std::fabs(residual(i)));
68 if (residual_max == 0.0) break;
69 int exponent = 0;
70 std::frexp(residual_max, &exponent);
71 const int shift = exponent - out.bits;
72 Eigen::VectorXd slice(length);
73 bool any = false;
74 for (Eigen::Index i = 0; i < length; ++i) {
75 const double value = residual(i);
76 if (value == 0.0) {
77 slice(i) = 0.0;
78 continue;
79 }
80 int value_exponent = 0;
81 std::frexp(std::fabs(value), &value_exponent);
82 // A term below this slice's unit is left for a later slice.
83 // Scaling it here would overflow the exponent range.
84 if (value_exponent <= shift) {
85 slice(i) = 0.0;
86 continue;
87 }
88 const double scaled = std::ldexp(value, -shift);
89 const double digits = std::trunc(scaled);
90 const double piece = std::ldexp(digits, shift);
91 slice(i) = piece;
92 residual(i) = value - piece;
93 if (piece != 0.0) any = true;
94 }
95 if (!any) break;
96 out.slices.push_back(std::move(slice));
97 }
98 if (out.slices.empty()) out.slices.push_back(Eigen::VectorXd::Zero(length));
99 return out;
100}
102double accumulateSchemeI(const SchemeISlices& left, const SchemeISlices& right)
103{
104 if (left.bits == 0 || right.bits == 0 || left.slices.empty() ||
105 right.slices.empty()) {
106 return std::numeric_limits<double>::quiet_NaN();
107 }
108 struct NormalizedSlice {
109 Eigen::VectorXd values;
110 int exponent;
111 };
112 const auto normalize = [](const SchemeISlices& input) {
113 std::vector<NormalizedSlice> result;
114 result.reserve(input.slices.size());
115 for (const auto& slice: input.slices) {
116 int exponent = 0;
117 if (slice.size() > 0)
118 std::frexp(slice.cwiseAbs().maxCoeff(), &exponent);
119 const int unit = exponent - input.bits;
120 Eigen::VectorXd values(slice.size());
121 for (Eigen::Index i = 0; i < slice.size(); ++i)
122 values(i) = std::ldexp(slice(i), -unit);
123 result.push_back({std::move(values), unit});
124 }
125 return result;
126 };
127 const auto left_normalized = normalize(left);
128 const auto right_normalized = normalize(right);
130 for (const auto& a: left_normalized) {
131 for (const auto& b: right_normalized) {
132 if (a.values.size() != b.values.size())
133 return std::numeric_limits<double>::quiet_NaN();
134 // Slice integers and their partial sums fit in 52 bits. Keeping
135 // the scale separate prevents floating overflow and underflow.
136 double dot = 0.0;
137 for (Eigen::Index i = 0; i < a.values.size(); ++i)
138 dot += a.values(i) * b.values(i);
139 SchemeIIWide coefficient{};
140 coefficient.negative = dot < 0;
141 coefficient.limb[0] = static_cast<uint64_t>(std::fabs(dot));
142 total.add(coefficient, a.exponent + b.exponent);
143 }
144 }
145 return total.value();
146}
147
148#define SCHEME_I_HD
149#include "SchemeIProduct.inl"
150#undef SCHEME_I_HD
151
152} // namespace gpr::linalg
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
int schemeISliceBits(Eigen::Index length)
double accumulateSchemeI(const SchemeISlices &left, const SchemeISlices &right)
Sum the exact slice products. The split and this accumulation are separate.
SchemeISlices splitSchemeI(const Eigen::Ref< const Eigen::VectorXd > &values)
std::vector< Eigen::VectorXd > slices
Definition OzakiScheme.h:22