seams-core v2.14.0
libyodaLib, the C++ engine of d-SEAMS
Loading...
Searching...
No Matches
ozaki.hpp
Go to the documentation of this file.
1#ifndef SEAMS_OZAKI_H_
2#define SEAMS_OZAKI_H_
3
4// Emulated products for the short dots and the one tiny GEMM in d-SEAMS.
5//
6// Ozaki scheme I splits a floating-point vector into slices whose products
7// are exact in FP64 (Ozaki, Ogita and Oishi, Numer. Algorithms 59, 95, 2012).
8// Ozaki scheme II replaces that slicing by an integer matrix product modulo
9// several coprime moduli and a Chinese-remainder reconstruction (Ozaki,
10// Uchino and Imamura, arXiv:2504.08009, 2025). The INT8 products use
11// AVX-512 VNNI or AMX-INT8 when the shape fills a tile; shorter products
12// use an INT32 accumulator, which is exact for the lengths that occur here.
13//
14// TwoProd / TwoSum and pairwise summation are the error-free transformations
15// of Dekker, Knuth, and Ogita, Rump and Oishi (SIAM J. Sci. Comput. 26,
16// 2005). Pairwise summation is the reduction whose error Higham bounds by
17// gamma_{ceil(log2 n)} (Accuracy and Stability of Numerical Algorithms,
18// SIAM, 2002). It does not depend on a left-to-right fold.
19
20#include <complex>
21#include <cstddef>
22#include <vector>
23
24namespace seams::product {
25
26enum class Mode {
27 Fp64 = 0,
28 Dot2 = 1,
29 OzakiI = 2,
31};
32
33enum class Reduction {
34 Left = 0,
37};
38
39enum class Int8Backend {
40 Auto = 0,
41 Scalar = 1,
42 Vnni = 2,
43 Amx = 3,
44};
45
46struct RealDot {
47 double value;
48 double bound;
50 int work;
51 const char *backend;
52};
53
54struct ComplexDot {
55 double re;
56 double im;
57 double normA;
58 double normB;
59 double boundRe;
60 double boundIm;
61 double boundNormA;
62 double boundNormB;
64 const char *backend;
65};
66
68void setMode(Mode m);
69const char *modeName(Mode m);
70
73const char *reductionName(Reduction r);
74
77
78void twoSum(double a, double b, double &s, double &e);
79void twoProd(double a, double b, double &hi, double &lo);
80
81double pairwiseSum(const double *x, int n);
82double neumaierSum(const double *x, int n);
83double leftSum(const double *x, int n);
84double reduce(const double *x, int n, Reduction r);
85
86// Absolute error bound for pairwise summation of n finite terms whose
87// absolute values sum to absSum (Higham's gamma_ceil(log2 n) bound,
88// rounded upward).
89double pairwiseBound(int n, long double absSum);
90
91RealDot realDot(const double *x, const double *y, int n, Mode m);
92
93ComplexDot complexDot(const std::complex<double> *a,
94 const std::complex<double> *b, int n, Mode m);
95
96// c = Re(a ยท conj(b)) / (|a| |b|), with an absolute certificate.
97struct Cij {
98 double value;
99 double bound;
101 const char *backend;
102};
103
104Cij bondCorrelation(const std::complex<double> *a, const std::complex<double> *b,
105 int n, Mode m);
106
107// Mean of the finite entries, in input order. Left matches a plain
108// accumulator; pairwise and Neumaier do not depend on a chain of +=.
109double meanOfFinite(const double *x, int n, Reduction r);
110
111// Row-major C = A(m x k) * B(k x n). bounds may be null. On Ozaki-II each
112// bounds[i*n+j] is an absolute certificate. Fp64 leaves bounds at 0 when
113// the pointer is non-null (no certificate for the rounded GEMM).
114struct GemmInfo {
115 bool ok;
116 const char *backend;
119};
120
121GemmInfo gemm(int m, int n, int k, const double *A, int lda, const double *B,
122 int ldb, double *C, int ldc, Mode mode, double *bounds,
124
125// INT8 C += A * B, row-major, int32 accumulator. Used to check the tiles.
126// Returns false when the backend cannot run this shape.
127bool int8Gemm(int m, int n, int k, const signed char *A, int lda,
128 const signed char *B, int ldb, int *C, int ldc,
129 Int8Backend backend);
130
131} // namespace seams::product
132
133#endif
double meanOfFinite(const double *x, int n, Reduction r)
ComplexDot complexDot(const std::complex< double > *a, const std::complex< double > *b, int n, Mode m)
RealDot realDot(const double *x, const double *y, int n, Mode m)
double pairwiseSum(const double *x, int n)
void setReduction(Reduction r)
double pairwiseBound(int n, long double absSum)
double reduce(const double *x, int n, Reduction r)
double leftSum(const double *x, int n)
bool amxAvailable()
bool int8Gemm(int m, int n, int k, const signed char *A, int lda, const signed char *B, int ldb, int *C, int ldc, Int8Backend backend)
Reduction reduction()
double neumaierSum(const double *x, int n)
const char * reductionName(Reduction r)
void twoSum(double a, double b, double &s, double &e)
const char * modeName(Mode m)
void setMode(Mode m)
GemmInfo gemm(int m, int n, int k, const double *A, int lda, const double *B, int ldb, double *C, int ldc, Mode mode, double *bounds, Int8Backend backend=Int8Backend::Auto)
void twoProd(double a, double b, double &hi, double &lo)
Cij bondCorrelation(const std::complex< double > *a, const std::complex< double > *b, int n, Mode m)
bool vnniAvailable()
const char * backend
Definition ozaki.hpp:101
const char * backend
Definition ozaki.hpp:116
const char * backend
Definition ozaki.hpp:51