GPRidge v1.0.0
GPR-accelerated saddle point searches on potential energy surfaces
☾
Toggle main menu visibility
Loading...
Searching...
No Matches
OzakiSchemeII.h
Go to the documentation of this file.
1
// SPDX-License-Identifier: MIT
2
// Copyright (c) gpridge developers
3
#pragma once
4
5
// Ozaki Scheme II for the predict contraction: scale each term to an integer
6
// significand, multiply modulo pairwise-coprime moduli, reconstruct one
7
// integer with the Chinese Remainder Theorem, then unscale by the power of two.
8
// This is not a significand slice split and not a cast to a wider floating
9
// type.
10
//
11
// Each exponent window is reconstructed exactly. Window and chunk totals
12
// stay in an integer accumulator until the final binary64 rounding.
13
14
#if defined(__CUDACC_RTC__)
15
#ifndef UINT32_MAX
16
using
uint32_t =
unsigned
int;
17
using
uint64_t =
unsigned
long
long;
18
using
int64_t =
long
long;
19
#endif
20
#else
21
#include <cstdint>
22
#endif
23
24
#if defined(__CUDACC_RTC__)
25
#define SCHEME_II_HD __device__
26
#elif defined(__CUDACC__)
27
#define SCHEME_II_HD inline __host__ __device__
28
#else
29
#define SCHEME_II_HD inline
30
#endif
31
32
namespace
gpr::linalg
{
33
34
inline
constexpr
int
kSchemeIIModulusCount
= 8;
35
36
// 128 terms of 106-bit significand products, shifted by this many bits,
37
// stay strictly inside the 238-bit product of schemeIIModulus().
38
inline
constexpr
int
kSchemeIIMaxShift
= 120;
39
40
// Pairwise-coprime moduli. Returned by value so device code does not load a
41
// function-local static.
42
SCHEME_II_HD
constexpr
uint32_t
schemeIIModulus
(
int
index)
43
{
44
switch
(index) {
45
case
0:
46
return
998244353u;
47
case
1:
48
return
985661441u;
49
case
2:
50
return
975175681u;
51
case
3:
52
return
469762049u;
53
case
4:
54
return
167772161u;
55
case
5:
56
return
1004535809u;
57
case
6:
58
return
2013265921u;
59
case
7:
60
return
1811939329u;
61
default
:
62
return
0u;
63
}
64
}
65
66
#if !defined(__CUDACC_RTC__)
67
inline
const
uint32_t*
schemeIIModuli
()
68
{
69
static
const
uint32_t moduli[
kSchemeIIModulusCount
] = {
70
schemeIIModulus
(0),
schemeIIModulus
(1),
schemeIIModulus
(2),
71
schemeIIModulus
(3),
schemeIIModulus
(4),
schemeIIModulus
(5),
72
schemeIIModulus
(6),
schemeIIModulus
(7)};
73
return
moduli;
74
}
75
#endif
76
77
struct
SchemeIIIntegers
{
78
static
constexpr
int
kMaxTerms
= 128;
79
int
length
= 0;
80
int
align_exp
= 0;
81
int64_t
left_sig
[
kMaxTerms
]{};
82
int64_t
right_sig
[
kMaxTerms
]{};
83
int
left_exp
[
kMaxTerms
]{};
84
int
right_exp
[
kMaxTerms
]{};
85
};
86
87
// Scale both operands. The integer term is left_sig * right_sig * 2^(exp sum -
88
// align).
89
SCHEME_II_HD
bool
scaleSchemeII
(
const
double
* left,
const
double
* right,
90
int
length, SchemeIIIntegers* scaled);
91
92
// Sum of the scaled integer products modulo one modulus.
93
// A term below align_exp is left for a lower window and does not clear the sum.
94
SCHEME_II_HD
uint32_t
modularProductSchemeII
(
const
SchemeIIIntegers& scaled,
95
uint32_t modulus);
96
97
// Copy the leading unconsumed terms whose product exponents fit in one modulus
98
// window. `consumed` has one byte per scaled term.
99
SCHEME_II_HD
bool
takeSchemeIIWindow
(
const
SchemeIIIntegers& scaled,
100
char
* consumed, SchemeIIIntegers* window);
101
102
struct
SchemeIIWide
{
103
static
constexpr
int
kLimbs
= 4;
104
uint64_t
limb
[
kLimbs
]{};
105
bool
negative
=
false
;
106
};
107
108
// Reconstruct the signed integer from residues of schemeIIModulus().
109
SCHEME_II_HD
SchemeIIWide
crtSchemeII
(
const
uint32_t* residues);
110
111
// coefficient * 2^exponent, rounded to binary64.
112
SCHEME_II_HD
double
unscaleSchemeII
(
const
SchemeIIWide
& coefficient,
113
int
exponent);
114
115
// Finite binary64 factors have significand exponents in [-1126, 971].
116
// Windows start at least at 2*(-1126)-120 = -2372. A product is below
117
// 2^2048; INT_MAX terms sum to less than 2^2079. Seventy 64-bit limbs
118
// cover that whole interval, including cancellation between windows.
119
struct
ExactProductAccumulator
{
120
static
constexpr
int
kMinExponent
= -2372;
121
struct
Wide
{
122
static
constexpr
int
kLimbs
= 70;
123
uint64_t
limb
[
kLimbs
]{};
124
bool
negative
=
false
;
125
}
total
{};
126
127
// coefficient and exponent are the result of a bounded CRT window.
128
SCHEME_II_HD
void
add
(
const
SchemeIIWide& coefficient,
int
exponent);
129
SCHEME_II_HD
double
value
()
const
;
130
};
131
132
// Any nonnegative int length; storage is bounded independently of length.
133
SCHEME_II_HD
double
schemeIIProductRaw
(
const
double
* left,
const
double
* right,
134
int
length);
135
136
}
// namespace gpr::linalg
137
138
#include "
OzakiSchemeII.inl
"
SCHEME_II_HD
#define SCHEME_II_HD
Definition
OzakiSchemeII.h:29
OzakiSchemeII.inl
gpr::linalg
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
Definition
ConditionedKernel.h:14
gpr::linalg::kSchemeIIModulusCount
constexpr int kSchemeIIModulusCount
Definition
OzakiSchemeII.h:34
gpr::linalg::takeSchemeIIWindow
bool takeSchemeIIWindow(const SchemeIIIntegers &scaled, char *consumed, SchemeIIIntegers *window)
Definition
OzakiSchemeII.inl:376
gpr::linalg::kSchemeIIMaxShift
constexpr int kSchemeIIMaxShift
Definition
OzakiSchemeII.h:38
gpr::linalg::crtSchemeII
SchemeIIWide crtSchemeII(const uint32_t *residues)
Definition
OzakiSchemeII.inl:309
gpr::linalg::schemeIIModuli
const uint32_t * schemeIIModuli()
Definition
OzakiSchemeII.h:67
gpr::linalg::unscaleSchemeII
double unscaleSchemeII(const SchemeIIWide &coefficient, int exponent)
Definition
OzakiSchemeII.inl:342
gpr::linalg::scaleSchemeII
bool scaleSchemeII(const double *left, const double *right, int length, SchemeIIIntegers *scaled)
Definition
OzakiSchemeII.inl:267
gpr::linalg::schemeIIModulus
constexpr uint32_t schemeIIModulus(int index)
Definition
OzakiSchemeII.h:42
gpr::linalg::schemeIIProductRaw
double schemeIIProductRaw(const double *left, const double *right, int length)
Definition
OzakiSchemeII.inl:412
gpr::linalg::modularProductSchemeII
uint32_t modularProductSchemeII(const SchemeIIIntegers &scaled, uint32_t modulus)
Definition
OzakiSchemeII.inl:290
gpr::linalg::ExactProductAccumulator::Wide
Definition
OzakiSchemeII.h:121
gpr::linalg::ExactProductAccumulator::Wide::kLimbs
static constexpr int kLimbs
Definition
OzakiSchemeII.h:122
gpr::linalg::ExactProductAccumulator::Wide::negative
bool negative
Definition
OzakiSchemeII.h:124
gpr::linalg::ExactProductAccumulator::Wide::limb
uint64_t limb[kLimbs]
Definition
OzakiSchemeII.h:123
gpr::linalg::ExactProductAccumulator
Definition
OzakiSchemeII.h:119
gpr::linalg::ExactProductAccumulator::value
double value() const
Definition
OzakiSchemeII.inl:371
gpr::linalg::ExactProductAccumulator::add
void add(const SchemeIIWide &coefficient, int exponent)
Definition
OzakiSchemeII.inl:348
gpr::linalg::ExactProductAccumulator::total
struct gpr::linalg::ExactProductAccumulator::Wide total
gpr::linalg::ExactProductAccumulator::kMinExponent
static constexpr int kMinExponent
Definition
OzakiSchemeII.h:120
gpr::linalg::SchemeIIIntegers
Definition
OzakiSchemeII.h:77
gpr::linalg::SchemeIIIntegers::kMaxTerms
static constexpr int kMaxTerms
Definition
OzakiSchemeII.h:78
gpr::linalg::SchemeIIIntegers::align_exp
int align_exp
Definition
OzakiSchemeII.h:80
gpr::linalg::SchemeIIIntegers::right_exp
int right_exp[kMaxTerms]
Definition
OzakiSchemeII.h:84
gpr::linalg::SchemeIIIntegers::right_sig
int64_t right_sig[kMaxTerms]
Definition
OzakiSchemeII.h:82
gpr::linalg::SchemeIIIntegers::left_sig
int64_t left_sig[kMaxTerms]
Definition
OzakiSchemeII.h:81
gpr::linalg::SchemeIIIntegers::length
int length
Definition
OzakiSchemeII.h:79
gpr::linalg::SchemeIIIntegers::left_exp
int left_exp[kMaxTerms]
Definition
OzakiSchemeII.h:83
gpr::linalg::SchemeIIWide
Definition
OzakiSchemeII.h:102
gpr::linalg::SchemeIIWide::kLimbs
static constexpr int kLimbs
Definition
OzakiSchemeII.h:103
gpr::linalg::SchemeIIWide::negative
bool negative
Definition
OzakiSchemeII.h:105
gpr::linalg::SchemeIIWide::limb
uint64_t limb[kLimbs]
Definition
OzakiSchemeII.h:104
gpr
linalg
OzakiSchemeII.h
Generated by
1.17.0
Generated by
Doxygen 1.17.0
Analytics by
Antics
provided by
TurtleTech ehf