26template <
typename T, std::
size_t MR, std::
size_t NR, std::
size_t KR>
class GemmLoop {
37 : Mgrp_(Mgrp), Ngrp_(Ngrp), Kgrp_(Kgrp), alpha_(alpha), beta_(
beta)
45 void run(T
const* A0, T
const* B0, T* C0)
noexcept { this->loopM(0, A0, B0, C0); }
51 T
const alpha_, beta_;
59 void loopM(std::size_t dim, T
const* a_ptr, T
const* b_ptr, T* c_ptr)
noexcept
61 if (dim == Mgrp_.
size())
63 loopN(0, a_ptr, b_ptr, c_ptr);
66 auto extent = Mgrp_[dim].extent;
67 auto sA = Mgrp_[dim].strides[0];
68 auto sC = Mgrp_[dim].strides[1];
69 for (
decltype(extent) i = 0; i < extent; ++i)
71 loopM(dim + 1, a_ptr, b_ptr, c_ptr);
83 void loopN(std::size_t dim, T
const* a_ptr, T
const* b_ptr, T* c_ptr)
noexcept
85 if (dim == Ngrp_.
size())
89 dotK(0, a_ptr, b_ptr, acc);
90 *c_ptr = (beta_ * *c_ptr) + (alpha_ * acc);
93 auto extent = Ngrp_[dim].extent;
94 auto sB = Ngrp_[dim].strides[0];
95 auto sC = Ngrp_[dim].strides[1];
96 for (
decltype(extent) j = 0; j < extent; ++j)
98 loopN(dim + 1, a_ptr, b_ptr, c_ptr);
110 void dotK(std::size_t dim, T
const* a_ptr, T
const* b_ptr, T& acc)
noexcept
112 if (dim == Kgrp_.
size())
115 acc += *a_ptr * *b_ptr;
118 auto extent = Kgrp_[dim].extent;
119 auto sA = Kgrp_[dim].strides[0];
120 auto sB = Kgrp_[dim].strides[1];
121 for (
decltype(extent) k = 0; k < extent; ++k)
123 dotK(dim + 1, a_ptr, b_ptr, acc);
132template <
typename T, std::
size_t MR, std::
size_t NR, std::
size_t KR>
A fixed-capacity vector with stack storage and std::vector-like API.
Definition static_vector.hpp:24
size_type size() const noexcept
Definition static_vector.hpp:211
Generic M×N×K contraction engine with full K-dimension recursion.
Definition kernel/cpu/contract.hpp:26
void run(T const *A0, T const *B0, T *C0) noexcept
Perform C = β·C + α·(A ⋅ B) over all fused M, N, and K dimensions.
Definition kernel/cpu/contract.hpp:45
void contract_strided(static_vector< extent_strides< 2 >, MR > const &Mgrp, static_vector< extent_strides< 2 >, NR > const &Ngrp, static_vector< extent_strides< 2 >, KR > const &Kgrp, T alpha, T const *A, T const *B, T beta, T *C)
Execute the CPU tensor contraction using precomputed stride groupings.
Definition kernel/cpu/contract.hpp:147
GemmLoop(static_vector< extent_strides< 2 >, MR > const &Mgrp, static_vector< extent_strides< 2 >, NR > const &Ngrp, static_vector< extent_strides< 2 >, KR > const &Kgrp, T alpha, T beta) noexcept
Build the loop engine for a fused contraction.
Definition kernel/cpu/contract.hpp:35
Configured gateway to Uni20's mdspan implementation.
Definition kernel/contract.hpp:15
mpreal beta(mpreal const &a, mpreal const &x)
Real Beta function; positive rational/integer pairs retain exact values.
Definition mpreal_special.hpp:164
Stride utilities for mdspan-like tensors.
Structure to represent a common extent and an array of strides.
Definition strides.hpp:28