Loading...
Searching...
No Matches
TileProduct.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3#pragma once
4
5#include <Eigen/Core>
6#include <cstddef>
7#include <type_traits>
8
9#if defined(__x86_64__) && defined(__GNUC__) && \
10 defined(EIGEN_VECTORIZE_FMA) && !defined(EIGEN_VECTORIZE_AVX512) && \
11 !defined(EIGEN_USE_BLAS)
12#include <immintrin.h>
13#endif
14
15namespace gpr::linalg::canonical {
16
17inline bool wideTileProductAvailable() noexcept
18{
19#if defined(__x86_64__) && defined(__GNUC__) && \
20 defined(EIGEN_VECTORIZE_FMA) && !defined(EIGEN_VECTORIZE_AVX512) && \
21 !defined(EIGEN_USE_BLAS)
22 static const bool available =
23 __builtin_cpu_supports("avx512f") && __builtin_cpu_supports("fma");
24 return available;
25#else
26 return false;
27#endif
28}
29
30#if defined(__x86_64__) && defined(__GNUC__) && \
31 defined(EIGEN_VECTORIZE_FMA) && !defined(EIGEN_VECTORIZE_AVX512) && \
32 !defined(EIGEN_USE_BLAS)
35template <bool TransposeRight, int RowVectors>
36__attribute__((target("avx512f,fma"))) inline void subtractFullTileRowsWide(
37 double* out, std::ptrdiff_t out_stride, const double* left,
38 const double* right, int first_row)
39{
40 for (int j = 0; j < 64; j += 8) {
41 __m512d sums[8][RowVectors];
42#pragma GCC unroll 8
43 for (int column = 0; column < 8; ++column)
44#pragma GCC unroll 3
45 for (int row = 0; row < RowVectors; ++row)
46 sums[column][row] = _mm512_setzero_pd();
47 for (int k = 0; k < 64; ++k) {
48 __m512d a[RowVectors];
49#pragma GCC unroll 3
50 for (int row = 0; row < RowVectors; ++row)
51 a[row] = _mm512_loadu_pd(left + first_row + 8 * row + 64 * k);
52 const double* b =
53 right + (TransposeRight ? j + 64 * k : k + 64 * j);
54 constexpr int stride = TransposeRight ? 1 : 64;
55#pragma GCC unroll 8
56 for (int column = 0; column < 8; ++column) {
57 const __m512d value = _mm512_set1_pd(b[column * stride]);
58#pragma GCC unroll 3
59 for (int row = 0; row < RowVectors; ++row)
60 sums[column][row] =
61 _mm512_fmadd_pd(a[row], value, sums[column][row]);
62 }
63 }
64#pragma GCC unroll 8
65 for (int column = 0; column < 8; ++column) {
66#pragma GCC unroll 3
67 for (int row = 0; row < RowVectors; ++row) {
68 const int i = first_row + 8 * row;
69 double* c = out + i + out_stride * (j + column);
70 const __m512d value =
71 _mm512_sub_pd(_mm512_loadu_pd(c), sums[column][row]);
72 _mm512_mask_storeu_pd(c, i == 56 ? 0x0f : 0xff, value);
73 }
74 }
75 }
76}
77
81template <bool TransposeRight>
82__attribute__((target("avx512f,fma"))) inline void subtractFullTileWide(
83 double* out, std::ptrdiff_t out_stride, const double* left,
84 const double* right)
85{
86 subtractFullTileRowsWide<TransposeRight, 3>(out, out_stride, left, right,
87 0);
88 subtractFullTileRowsWide<TransposeRight, 3>(out, out_stride, left, right,
89 24);
90 subtractFullTileRowsWide<TransposeRight, 2>(out, out_stride, left, right,
91 48);
92 for (int j = 0; j < 64; j += 4) {
93 __m256d even[4], odd[4];
94 for (int q = 0; q < 4; ++q)
95 even[q] = odd[q] = _mm256_setzero_pd();
96 for (int k = 0; k < 64; k += 2) {
97 const __m256d ae = _mm256_loadu_pd(left + 60 + 64 * k);
98 const __m256d ao = _mm256_loadu_pd(left + 60 + 64 * (k + 1));
99 for (int q = 0; q < 4; ++q) {
100 const int offset =
101 TransposeRight ? j + q + 64 * k : k + 64 * (j + q);
102 constexpr int next = TransposeRight ? 64 : 1;
103 even[q] =
104 _mm256_fmadd_pd(ae, _mm256_set1_pd(right[offset]), even[q]);
105 odd[q] = _mm256_fmadd_pd(
106 ao, _mm256_set1_pd(right[offset + next]), odd[q]);
107 }
108 }
109 for (int q = 0; q < 4; ++q) {
110 double* c = out + 60 + out_stride * (j + q);
111 _mm256_storeu_pd(c, _mm256_sub_pd(_mm256_loadu_pd(c),
112 _mm256_add_pd(even[q], odd[q])));
113 }
114 }
115}
116#endif
117
120template <bool TransposeRight = false, class Target, class Left, class Right>
121inline void subtractTileProduct(Target&& out, const Left& left,
122 const Right& right)
123{
124#if defined(__x86_64__) && defined(__GNUC__) && \
125 defined(EIGEN_VECTORIZE_FMA) && !defined(EIGEN_VECTORIZE_AVX512) && \
126 !defined(EIGEN_USE_BLAS)
127 if constexpr (!std::decay_t<Target>::IsRowMajor && !Left::IsRowMajor &&
128 !Right::IsRowMajor) {
129 if (out.rows() == 64 && out.cols() == 64 && left.rows() == 64 &&
130 left.cols() == 64 && right.rows() == 64 && right.cols() == 64 &&
131 out.innerStride() == 1 && out.outerStride() >= 64 &&
132 left.innerStride() == 1 && left.outerStride() == 64 &&
133 right.innerStride() == 1 && right.outerStride() == 64 &&
135 subtractFullTileWide<TransposeRight>(out.data(), out.outerStride(),
136 left.data(), right.data());
137 return;
138 }
139 }
140#endif
141 if constexpr (TransposeRight)
142 out.noalias() -= left * right.transpose();
143 else
144 out.noalias() -= left * right;
145}
146
147} // namespace gpr::linalg::canonical
__attribute__((visibility("default"))) void *gpr_training_product_create_v1(char *error
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
bool wideTileProductAvailable() noexcept
Definition TileProduct.h:17
void subtractTileProduct(Target &&out, const Left &left, const Right &right)
STL namespace.