GPRidge v1.0.0
GPR-accelerated saddle point searches on potential energy surfaces
☾
Toggle main menu visibility
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
13
namespace
gpr::linalg
{
14
15
enum class
ProductMode
{
Compensated
,
SchemeI
,
SchemeII
,
KernelFloat
};
16
17
// GPR_PREDICT_SUM selects prediction contractions. Training and distributed
18
// compensated accumulators keep their sum/correction representation.
19
inline
ProductMode
productMode
()
20
{
21
const
char
* value = std::getenv(
"GPR_PREDICT_SUM"
);
22
if
(value ==
nullptr
|| value[0] ==
'\0'
||
23
std::strcmp(value,
"compensated"
) == 0) {
24
return
ProductMode::Compensated
;
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)
29
return
ProductMode::KernelFloat
;
30
return
ProductMode::Compensated
;
31
}
32
33
// True when the kernel_float device library (GPR_KERNEL_FLOAT_LIB, else
34
// libscheme_ii_product.so) loads on this host.
35
bool
kernelFloatProductAvailable
();
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.
40
inline
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 &&
47
!
kernelFloatProductAvailable
())
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
57
inline
constexpr
const
char
*
kPredictSumOption
=
58
"--predict-sum <compensated|scheme1|scheme2|kernel_float>"
;
59
inline
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
65
double
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.
70
void
contractPredictionMatrix
(
const
double
* column_major,
int
rows,
int
cols,
71
const
double
* coefficients,
double
* output,
72
ProductMode
mode);
73
74
struct
StoredProduct
{
75
const
double
*
left
;
76
const
double
*
right
;
77
int
length
;
78
};
79
80
void
evaluateStoredProducts
(
const
StoredProduct
* products,
int
count,
81
ProductMode
mode,
double
* values);
82
85
struct
CompensatedSum
{
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.
120
class
PredictProduct
{
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
129
public
:
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) { }
137
PredictProduct
(
const
PredictProduct
& other)
138
: mode_(other.mode_),
139
exact_(other.exact_),
140
terms_(other.terms_ ?
std
::make_unique<StoredTerms>(*other.terms_)
141
: nullptr)
142
{
143
}
144
PredictProduct
&
operator=
(
const
PredictProduct
& other)
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
ExactSum.h
Exact floating-point accumulator (Kulisch long accumulator).
gpr::linalg::ExactSum
Definition
ExactSum.h:34
gpr::linalg::PredictProduct::evaluateTogether
static void evaluateTogether(const std::vector< const PredictProduct * > &products, std::vector< double > &values)
Definition
PredictProduct.cpp:214
gpr::linalg::PredictProduct::addProduct
void addProduct(double left, double right)
Definition
CompensatedSum.h:157
gpr::linalg::PredictProduct::operator=
PredictProduct & operator=(const PredictProduct &other)
Definition
CompensatedSum.h:144
gpr::linalg::PredictProduct::PredictProduct
PredictProduct(const PredictProduct &other)
Definition
CompensatedSum.h:137
gpr::linalg::PredictProduct::value
double value() const
Definition
CompensatedSum.h:177
gpr::linalg::PredictProduct::PredictProduct
PredictProduct(PredictProduct &&) noexcept=default
gpr::linalg::PredictProduct::PredictProduct
PredictProduct(ProductMode mode=productMode())
Definition
CompensatedSum.h:136
gpr::linalg::PredictProduct::clear
void clear()
Definition
CompensatedSum.h:168
gpr::linalg
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
Definition
ConditionedKernel.h:14
gpr::linalg::ProductMode
ProductMode
Definition
CompensatedSum.h:15
gpr::linalg::ProductMode::SchemeII
@ SchemeII
Definition
CompensatedSum.h:15
gpr::linalg::ProductMode::KernelFloat
@ KernelFloat
Definition
CompensatedSum.h:15
gpr::linalg::ProductMode::Compensated
@ Compensated
Definition
CompensatedSum.h:15
gpr::linalg::ProductMode::SchemeI
@ SchemeI
Definition
CompensatedSum.h:15
gpr::linalg::contractPredictionMatrix
void contractPredictionMatrix(const double *column_major, int rows, int cols, const double *coefficients, double *output, ProductMode mode)
Definition
PredictProduct.cpp:251
gpr::linalg::kernelFloatProductAvailable
bool kernelFloatProductAvailable()
Definition
PredictProduct.cpp:165
gpr::linalg::evaluateStoredProduct
double evaluateStoredProduct(const double *left, const double *right, int length, ProductMode mode)
Definition
PredictProduct.cpp:277
gpr::linalg::evaluateStoredProducts
void evaluateStoredProducts(const StoredProduct *products, int count, ProductMode mode, double *values)
Definition
PredictProduct.cpp:170
gpr::linalg::kPredictSumHelp
constexpr const char * kPredictSumHelp
Definition
CompensatedSum.h:59
gpr::linalg::productMode
ProductMode productMode()
Definition
CompensatedSum.h:19
gpr::linalg::selectPredictProduct
bool selectPredictProduct(const char *value)
Definition
CompensatedSum.h:40
gpr::linalg::kPredictSumOption
constexpr const char * kPredictSumOption
Definition
CompensatedSum.h:57
std
STL namespace.
Compensated
Definition
compensated_product_sum.c:9
gpr::linalg::CompensatedSum
Definition
CompensatedSum.h:85
gpr::linalg::CompensatedSum::add
void add(double value)
Definition
CompensatedSum.h:89
gpr::linalg::CompensatedSum::addProduct
void addProduct(double left, double right)
Definition
CompensatedSum.h:100
gpr::linalg::CompensatedSum::corr
double corr
Definition
CompensatedSum.h:87
gpr::linalg::CompensatedSum::sum
double sum
Definition
CompensatedSum.h:86
gpr::linalg::CompensatedSum::value
double value() const
Definition
CompensatedSum.h:107
gpr::linalg::StoredProduct
Definition
CompensatedSum.h:74
gpr::linalg::StoredProduct::right
const double * right
Definition
CompensatedSum.h:76
gpr::linalg::StoredProduct::length
int length
Definition
CompensatedSum.h:77
gpr::linalg::StoredProduct::left
const double * left
Definition
CompensatedSum.h:75
gpr
linalg
CompensatedSum.h
Generated by
1.17.0
Generated by
Doxygen 1.17.0
Analytics by
Antics
provided by
TurtleTech ehf