MTL5 -- Matrix Template Library C++ API
mtl:: -- C++20 header-only linear algebra for mixed-precision algorithm design
Loading...
Searching...
No Matches
gemm_blocked.hpp File Reference
#include <algorithm>
#include <atomic>
#include <cstddef>
#include <cstdlib>
#include <exception>
#include <memory>
#include <thread>
#include <vector>
#include <mtl/concepts/scalar.hpp>
#include <mtl/detail/aligned_allocator.hpp>
#include <mtl/detail/gemm_microkernel.hpp>
#include <mtl/detail/gemm_pack.hpp>
#include <mtl/detail/gemm_quad_microkernel.hpp>
#include <mtl/detail/gemm_quad_pack.hpp>
#include <mtl/detail/nc_model.hpp>
#include <mtl/detail/thread_pool.hpp>
#include <mtl/simd/blocking.hpp>
#include <mtl/simd/scalable.hpp>
#include <mtl/util/cache_info.hpp>

Classes

struct  mtl::detail::gemm_grid
 The threaded nest's shape decision: the ic block size and the ic x jc thread grid. More...
struct  mtl::detail::batch_tile< TC >
 The jc block size this nest will step by, under model (#479's harness arm). More...
struct  mtl::detail::vla_tile< TC, W >
struct  mtl::detail::gemm_plan
 Everything the threaded nest decides for one call: the ic step it will actually use, the block counts that follow, and the thread grid. More...
class  mtl::detail::gemm_barrier
 Reusable sense-reversing barrier for a fixed party count. More...

Namespaces

namespace  mtl
namespace  mtl::detail
 mat*vec multiply into pre-allocated y: y = A * x.

Typedefs

template<typename T>
using mtl::detail::packed_buffer = std::vector<T, aligned_allocator<T>>
 SIMD-aligned scratch buffer for packed panels (reused across the nest).

Functions

constexpr std::size_t mtl::detail::critical_panels (std::size_t m, std::size_t mc, unsigned ic_nt, std::size_t mr) noexcept
 Default GEMM thread count: the persistent pool's size (env MTL5_NUM_THREADS, clamped to hardware concurrency; 1 when unset/invalid).
std::size_t mtl::detail::balanced_mc (std::size_t m, std::size_t mc_max, unsigned ic_nt, std::size_t mr=1)
 Choose the ic-block size for the threaded m-partition (#408, #458).
constexpr double mtl::detail::grid_imbalance (std::size_t blocks, unsigned workers) noexcept
 How much of a round-robin partition's capacity the most-loaded worker wastes: ceil(blocks / workers) * workers / blocks, which is 1.0 exactly when the blocks divide evenly and grows as the remainder does.
constexpr std::size_t mtl::detail::packed_b_bytes (unsigned jc_nt, std::size_t kc, std::size_t nc, std::size_t nr, std::size_t elem_bytes, std::size_t k_group=1) noexcept
 Bytes of packed B held simultaneously: one panel per jc-team, as the packer actually lays it out.
constexpr std::size_t mtl::detail::c_strip_mc_cap (std::size_t l2_bytes, std::size_t kc, std::size_t n, std::size_t nc, std::size_t sdata, bool charge_a=true)
 The largest mc for which the packed A block AND the strip of C it accumulates both fit in L2.
constexpr gemm_grid mtl::detail::plan_gemm_grid (std::size_t m, std::size_t n, std::size_t mc_cache, std::size_t nc, std::size_t mr, unsigned budget, std::size_t mc_extra_cap=0)
 Choose mc and the grid for an m x n problem on budget threads.
unsigned mtl::detail::gemm_default_threads ()
template<typename TC, typename TA, typename TB, bool Quad, typename F>
decltype(auto) mtl::detail::with_gemm_tile (F &&f)
 Run f(tile) with the tile the GEMM uses for (TC, TA, TB, Kern) on this machine: on a scalable target, for a same-type float or double GEMM, the vla_tile whose lane count the hardware has; otherwise batch_tile.
template<typename TC>
simd::blocking_params mtl::detail::gemm_blocking ()
 The register tile and compile-time blocks a same-type TC GEMM runs with on this machine: simd::default_blocking<TC>, except on a scalable target, where it is the vla_tile's (#427).
template<typename TC>
const simd::blocking_params & mtl::detail::gemm_runtime_blocking ()
 The detected-cache blocks of that tile: simd::runtime_blocking<TC>() except on a scalable target (#427).
template<typename TC, typename TB = TC, std::size_t KGroup = 1, typename Tile = batch_tile<TC>>
std::size_t mtl::detail::nc_for_plan (std::size_t m, std::size_t n, unsigned budget, nc_model model)
template<typename TC, typename TB, std::size_t KGroup, typename Tile>
gemm_plan mtl::detail::gemm_plan_for_tiled (std::size_t m, std::size_t n, unsigned nthreads, nc_model model)
 The plan gemm_blocked<TC> will follow for an m x n problem on nthreads.
template<typename TC, typename TB = TC, std::size_t KGroup = 1>
gemm_plan mtl::detail::gemm_plan_for (std::size_t m, std::size_t n, unsigned nthreads, nc_model model=nc_model_selection())
 The plan gemm_blocked runs for a same-type GEMM (TC, operand type TB) on this machine: on a scalable target, the vla_tile the hardware's lane count selects (#427), so what is reported is what the nest steps.
template<typename TC, typename TA, typename TB, gemm_kernel Kern, typename Tile>
requires (mtl::Scalar<TC> && mtl::Scalar<TA> && mtl::Scalar<TB>)
void mtl::detail::gemm_blocked_tiled (std::size_t m, std::size_t n, std::size_t k, TC alpha, const TA *A, std::ptrdiff_t a_rs, std::ptrdiff_t a_cs, const TB *B, std::ptrdiff_t b_rs, std::ptrdiff_t b_cs, TC beta, TC *C, std::size_t ldc, unsigned nthreads, nc_model model)
 C[m x n] (row-major, leading dim ldc) := beta*C + alpha * A[m x k] * B[k x n].
template<typename TC, typename TA = TC, typename TB = TA, gemm_kernel Kern = gemm_kernel::widening>
requires (mtl::Scalar<TC> && mtl::Scalar<TA> && mtl::Scalar<TB>)
void mtl::detail::gemm_blocked (std::size_t m, std::size_t n, std::size_t k, TC alpha, const TA *A, std::ptrdiff_t a_rs, std::ptrdiff_t a_cs, const TB *B, std::ptrdiff_t b_rs, std::ptrdiff_t b_cs, TC beta, TC *C, std::size_t ldc, unsigned nthreads=1, nc_model model=nc_model_selection())
 The blocked GEMM, on the tile this machine runs (see gemm_blocked_tiled for the contract).