Loading...
Searching...
No Matches
CompensatedSum.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3#pragma once
4
5#include <cmath>
6#include <cstdlib>
7#include <cstring>
8#include <memory>
9#include <vector>
10
11#include "ExactSum.h"
12
13namespace gpr::linalg {
14
16
17// GPR_PREDICT_SUM selects prediction contractions. Training and distributed
18// compensated accumulators keep their sum/correction representation.
20{
21 const char* value = std::getenv("GPR_PREDICT_SUM");
22 if (value == nullptr || value[0] == '\0' ||
23 std::strcmp(value, "compensated") == 0) {
25 }
26 if (std::strcmp(value, "scheme1") == 0) return ProductMode::SchemeI;
27 if (std::strcmp(value, "scheme2") == 0) return ProductMode::SchemeII;
28 if (std::strcmp(value, "kernel_float") == 0)
31}
32
33// True when the kernel_float device library (GPR_KERNEL_FLOAT_LIB, else
34// libscheme_ii_product.so) loads on this host.
36
37// User-facing choice for --predict-sum. Null, empty, and compensated clear the
38// variable so the product stays the TwoProductFMA sum. Unknown names are
39// refused, and so is kernel_float on a host without its device library.
40inline bool selectPredictProduct(const char* value)
41{
42 if (value == nullptr || value[0] == '\0' ||
43 std::strcmp(value, "compensated") == 0) {
44 return unsetenv("GPR_PREDICT_SUM") == 0;
45 }
46 if (std::strcmp(value, "kernel_float") == 0 &&
48 return false;
49 if (std::strcmp(value, "scheme1") == 0 ||
50 std::strcmp(value, "scheme2") == 0 ||
51 std::strcmp(value, "kernel_float") == 0) {
52 return setenv("GPR_PREDICT_SUM", value, 1) == 0;
53 }
54 return false;
55}
56
57inline constexpr const char* kPredictSumOption =
58 "--predict-sum <compensated|scheme1|scheme2|kernel_float>";
59inline constexpr const char* kPredictSumHelp =
60 "Predict product. compensated sums each TwoProductFMA expansion and "
61 "rounds once, and is the default. scheme1 is Ozaki Scheme I, scheme2 is "
62 "Ozaki Scheme II, and kernel_float is the Scheme II product formed with "
63 "kernel_float vectors.";
64
65double evaluateStoredProduct(const double* left, const double* right,
66 int length, ProductMode mode);
67
68// Column-major matrix-vector product. Every mode, including compensated,
69// evaluates each column with evaluateStoredProduct.
70void contractPredictionMatrix(const double* column_major, int rows, int cols,
71 const double* coefficients, double* output,
72 ProductMode mode);
73
75 const double* left;
76 const double* right;
77 int length;
78};
79
80void evaluateStoredProducts(const StoredProduct* products, int count,
81 ProductMode mode, double* values);
82
86 double sum = 0.0;
87 double corr = 0.0;
88
89 void add(double value)
90 {
91 const double next = sum + value;
92 corr += std::fabs(sum) >= std::fabs(value) ? (sum - next) + value
93 : (value - next) + sum;
94 sum = next;
95 }
96
100 void addProduct(double left, double right)
101 {
102 const double product = left * right;
103 add(product);
104 add(std::fma(left, right, -product));
105 }
106
107 double value() const
108 {
109 return sum + corr;
110 }
111};
112
113// Compensated mode sums the TwoProductFMA expansion (rounded product and
114// FMA residual, the same split as CompensatedSum::addProduct) in ExactSum
115// and rounds once. A Neumaier sum of that expansion keeps -2^-104 and drops
116// a residual that sits far below the running correction; it also moves the
117// alternating-pi dots off the certified cell. Other modes retain the
118// operands until value(). The mode is fixed for the accumulator's lifetime;
119// no environment lookup occurs inside the sum.
121 struct StoredTerms {
122 std::vector<double> left;
123 std::vector<double> right;
124 };
125 ProductMode mode_;
126 ExactSum exact_;
127 std::unique_ptr<StoredTerms> terms_;
128
129public:
130 // Independent products share a device launch without sharing an
131 // accumulator.
132 static void evaluateTogether(
133 const std::vector<const PredictProduct*>& products,
134 std::vector<double>& values);
135
136 explicit PredictProduct(ProductMode mode = productMode()) : mode_(mode) { }
138 : mode_(other.mode_),
139 exact_(other.exact_),
140 terms_(other.terms_ ? std::make_unique<StoredTerms>(*other.terms_)
141 : nullptr)
142 {
143 }
145 {
146 if (this != &other) {
147 mode_ = other.mode_;
148 exact_ = other.exact_;
149 terms_ = other.terms_ ? std::make_unique<StoredTerms>(*other.terms_)
150 : nullptr;
151 }
152 return *this;
153 }
154 PredictProduct(PredictProduct&&) noexcept = default;
155 PredictProduct& operator=(PredictProduct&&) noexcept = default;
156
157 void addProduct(double left, double right)
158 {
159 if (mode_ == ProductMode::Compensated) {
160 exact_.addProduct(left, right);
161 } else {
162 if (!terms_) terms_ = std::make_unique<StoredTerms>();
163 terms_->left.push_back(left);
164 terms_->right.push_back(right);
165 }
166 }
167
168 void clear()
169 {
170 exact_ = {};
171 if (terms_) {
172 terms_->left.clear();
173 terms_->right.clear();
174 }
175 }
176
177 double value() const
178 {
179 if (!terms_ || terms_->left.empty()) return exact_.value();
180 return evaluateStoredProduct(terms_->left.data(), terms_->right.data(),
181 static_cast<int>(terms_->left.size()),
182 mode_);
183 }
184};
185
186} // namespace gpr::linalg
Exact floating-point accumulator (Kulisch long accumulator).
static void evaluateTogether(const std::vector< const PredictProduct * > &products, std::vector< double > &values)
void addProduct(double left, double right)
PredictProduct & operator=(const PredictProduct &other)
PredictProduct(const PredictProduct &other)
PredictProduct(PredictProduct &&) noexcept=default
PredictProduct(ProductMode mode=productMode())
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
void contractPredictionMatrix(const double *column_major, int rows, int cols, const double *coefficients, double *output, ProductMode mode)
bool kernelFloatProductAvailable()
double evaluateStoredProduct(const double *left, const double *right, int length, ProductMode mode)
void evaluateStoredProducts(const StoredProduct *products, int count, ProductMode mode, double *values)
constexpr const char * kPredictSumHelp
ProductMode productMode()
bool selectPredictProduct(const char *value)
constexpr const char * kPredictSumOption
STL namespace.
void addProduct(double left, double right)