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
16using uint32_t = unsigned int;
17using uint64_t = unsigned long long;
18using 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
32namespace gpr::linalg {
33
34inline 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().
38inline constexpr int kSchemeIIMaxShift = 120;
39
40// Pairwise-coprime moduli. Returned by value so device code does not load a
41// function-local static.
42SCHEME_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__)
67inline const uint32_t* schemeIIModuli()
68{
69 static const uint32_t moduli[kSchemeIIModulusCount] = {
73 return moduli;
74}
75#endif
76
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]{};
85};
86
87// Scale both operands. The integer term is left_sig * right_sig * 2^(exp sum -
88// align).
89SCHEME_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.
94SCHEME_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.
99SCHEME_II_HD bool takeSchemeIIWindow(const SchemeIIIntegers& scaled,
100 char* consumed, SchemeIIIntegers* window);
101
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().
109SCHEME_II_HD SchemeIIWide crtSchemeII(const uint32_t* residues);
110
111// coefficient * 2^exponent, rounded to binary64.
112SCHEME_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.
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.
133SCHEME_II_HD double schemeIIProductRaw(const double* left, const double* right,
134 int length);
135
136} // namespace gpr::linalg
137
138#include "OzakiSchemeII.inl"
#define SCHEME_II_HD
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
constexpr int kSchemeIIModulusCount
bool takeSchemeIIWindow(const SchemeIIIntegers &scaled, char *consumed, SchemeIIIntegers *window)
constexpr int kSchemeIIMaxShift
SchemeIIWide crtSchemeII(const uint32_t *residues)
const uint32_t * schemeIIModuli()
double unscaleSchemeII(const SchemeIIWide &coefficient, int exponent)
bool scaleSchemeII(const double *left, const double *right, int length, SchemeIIIntegers *scaled)
constexpr uint32_t schemeIIModulus(int index)
double schemeIIProductRaw(const double *left, const double *right, int length)
uint32_t modularProductSchemeII(const SchemeIIIntegers &scaled, uint32_t modulus)
void add(const SchemeIIWide &coefficient, int exponent)
struct gpr::linalg::ExactProductAccumulator::Wide total
static constexpr int kMaxTerms
int64_t right_sig[kMaxTerms]
int64_t left_sig[kMaxTerms]
static constexpr int kLimbs