Loading...
Searching...
No Matches
ScalarGebpKernel.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5#ifndef GPR_LINALG_SCALAR_GEBP_KERNEL_H_
6#define GPR_LINALG_SCALAR_GEBP_KERNEL_H_
7
8// Eigen's matrix products, its triangular solves with a matrix right-hand
9// side, its blocked Cholesky factor and its rank updates all run their inner
10// products in gebp_kernel. Under EIGEN_DONT_VECTORIZE the double kernel holds
11// one row by four columns of the result in scalars. This specialization keeps
12// that kernel's arithmetic on every entry and holds the four columns of a
13// panel in one vector:
14// - within a call, the even and odd steps of the depth peeled to a multiple
15// of eight accumulate separately from +0, are added, and the remaining
16// steps follow in order;
17// - the last cols % 4 columns accumulate every step in one sum from +0;
18// - each product and each sum rounds on its own, and the entry becomes
19// acc * alpha + entry.
20// Lanes never mix, so every entry takes the bits of Eigen's scalar kernel.
21// An explicit specialization must precede every use, so meson.build forces
22// this header ahead of each translation unit compiled with gpridge_args.
23// tests/gpr/linalg/ScalarGebpKernelTest.cpp compares it with Eigen's kernel.
24
25#if defined(__cplusplus) && __has_include(<Eigen/Core>)
26
27#include <Eigen/Core>
28
29#if defined(EIGEN_DONT_VECTORIZE) && EIGEN_WORLD_VERSION == 3 && \
30 EIGEN_MAJOR_VERSION == 4 && (defined(__GNUC__) || defined(__clang__))
31
32#include <cstring>
33
34#define GPR_SCALAR_GEBP_KERNEL 1
35
36namespace Eigen {
37namespace internal {
38namespace gpr_scalar_gebp {
39
40typedef double Lanes4 __attribute__((vector_size(32)));
41
42// Rows [i, i + Rows) of the four-column panel at blB.
43template <int Rows, typename DataMapper>
44EIGEN_ALWAYS_INLINE void panelRows(const DataMapper& res, const double* blockA,
45 const double* blB, double alpha, Index i,
46 Index j2, Index strideA, Index offsetA,
47 Index peeled_kc, Index depth)
48{
49 const double* blA[Rows];
50 Lanes4 C[Rows], D[Rows];
51 for (int r = 0; r < Rows; ++r) {
52 blA[r] = &blockA[(i + r) * strideA + offsetA];
53 C[r] = Lanes4{0.0, 0.0, 0.0, 0.0};
54 D[r] = Lanes4{0.0, 0.0, 0.0, 0.0};
55 }
56 for (Index k = 0; k < peeled_kc; k += 2) {
57 Lanes4 b0, b1;
58 std::memcpy(&b0, &blB[4 * k], sizeof(b0));
59 std::memcpy(&b1, &blB[4 * (k + 1)], sizeof(b1));
60 for (int r = 0; r < Rows; ++r) {
61 const Lanes4 t0 = blA[r][k] * b0;
62 C[r] = C[r] + t0;
63 const Lanes4 t1 = blA[r][k + 1] * b1;
64 D[r] = D[r] + t1;
65 }
66 }
67 for (int r = 0; r < Rows; ++r)
68 C[r] = C[r] + D[r];
69 for (Index k = peeled_kc; k < depth; ++k) {
70 Lanes4 b;
71 std::memcpy(&b, &blB[4 * k], sizeof(b));
72 for (int r = 0; r < Rows; ++r) {
73 const Lanes4 t = blA[r][k] * b;
74 C[r] = C[r] + t;
75 }
76 }
77 for (int r = 0; r < Rows; ++r) {
78 for (int c = 0; c < 4; ++c) {
79 double& entry = res(i + r, j2 + c);
80 const double scaled = C[r][c] * alpha;
81 entry = scaled + entry;
82 }
83 }
84}
85
86} // namespace gpr_scalar_gebp
87
88// Conjugation is the identity on double, so all four flags share the kernel.
89template <bool ConjugateLhs, bool ConjugateRhs>
90struct gebp_kernel<double, double, Index,
91 blas_data_mapper<double, Index, ColMajor, Unaligned, 1>, 1,
92 4, ConjugateLhs, ConjugateRhs> {
93 typedef blas_data_mapper<double, Index, ColMajor, Unaligned, 1> DataMapper;
94
95 EIGEN_DONT_INLINE
96 void operator()(const DataMapper& res, const double* blockA,
97 const double* blockB, Index rows, Index depth, Index cols,
98 double alpha, Index strideA = -1, Index strideB = -1,
99 Index offsetA = 0, Index offsetB = 0)
100 {
101 using gpr_scalar_gebp::panelRows;
102 if (strideA == -1) strideA = depth;
103 if (strideB == -1) strideB = depth;
104 const Index packet_cols4 = (cols / 4) * 4;
105 const Index peeled_kc = depth & ~Index(7);
106 constexpr int kRows = 4;
107 const Index peeled_rows = (rows / kRows) * kRows;
108
109 for (Index j2 = 0; j2 < packet_cols4; j2 += 4) {
110 const double* blB = &blockB[j2 * strideB + offsetB * 4];
111 Index i = 0;
112 for (; i < peeled_rows; i += kRows)
113 panelRows<kRows>(res, blockA, blB, alpha, i, j2, strideA,
114 offsetA, peeled_kc, depth);
115 for (; i < rows; ++i)
116 panelRows<1>(res, blockA, blB, alpha, i, j2, strideA, offsetA,
117 peeled_kc, depth);
118 }
119
120 for (Index j2 = packet_cols4; j2 < cols; ++j2) {
121 const double* blB = &blockB[j2 * strideB + offsetB];
122 for (Index i = 0; i < rows; ++i) {
123 const double* blA = &blockA[i * strideA + offsetA];
124 double C0 = 0.0;
125 for (Index k = 0; k < depth; ++k) {
126 const double t = blA[k] * blB[k];
127 C0 = C0 + t;
128 }
129 double& entry = res(i, j2);
130 const double scaled = C0 * alpha;
131 entry = scaled + entry;
132 }
133 }
134 }
135};
136
137} // namespace internal
138} // namespace Eigen
139
140#endif
141#endif
142
143#endif // GPR_LINALG_SCALAR_GEBP_KERNEL_H_
__attribute__((visibility("default"))) void *gpr_training_product_create_v1(char *error
Kokkos::IndexType< std::size_t > Index