Loading...
Searching...
No Matches
gpr::linalg::schemeii::Context Class Reference

#include <gpr/linalg/schemeii/SchemeII.h>

Classes

struct  Impl

Public Member Functions

 Context (Options options)
 ~Context ()
 Context (const Context &)=delete
Context & operator= (const Context &)=delete
const Options & options () const noexcept
std::string describe (std::int64_t k) const
 One line naming engine, provider, bits and moduli for k.
Plan plan (std::int64_t k) const
void setThreadSource (int(*source)())
 Thread count used when Options::threads is zero.
Operand prepareRows (const double *a, std::int64_t lda, bool transpose, std::int64_t m, std::int64_t k, const Plan &plan, const int *exponents=nullptr) const
 Row operand from op(A) (m x k). Exponents override the row maxima.
Operand prepareColumns (const double *b, std::int64_t ldb, bool transpose, std::int64_t k, std::int64_t n, const Plan &plan, const int *exponents=nullptr) const
 Column operand from op(B) (k x n).
void run (std::span< const Task > tasks) const
void gemm (bool transpose_a, bool transpose_b, std::int64_t m, std::int64_t n, std::int64_t k, double alpha, const double *a, std::int64_t lda, const double *b, std::int64_t ldb, double beta, double *c, std::int64_t ldc) const
void residues (const Task &task, std::vector< std::uint32_t > &out) const
void reconstruct (const Plan &plan, std::span< const std::uint32_t > residues, std::span< const int > row_exponents, std::span< const int > column_exponents, std::int64_t m, std::int64_t n, double alpha, double beta, double *c, std::int64_t ldc) const
 C block from summed residues (see residues()).

Detailed Description

Definition at line 151 of file SchemeII.h.

Constructor & Destructor Documentation

◆ Context() [1/2]

gpr::linalg::schemeii::Context::Context ( Options options)
explicit

Definition at line 1975 of file SchemeII.cpp.

1975 : impl_(std::make_unique<Impl>())
1976{
1977 impl_->options = std::move(options);
1978 if (const char* flag = std::getenv("GPR_SCHEME2_PROFILE"); flag && *flag)
1979 impl_->profile.enabled = true;
1980 if (const char* flag = std::getenv("GPR_SCHEME2_VERIFY"); flag && *flag)
1981 impl_->verify = true;
1982 if (impl_->options.engine == Engine::Int8)
1983 impl_->int8 = makeInt8(impl_->options);
1984 impl_->table(1); // validates bits and moduli
1985}
const Options & options() const noexcept

◆ ~Context()

gpr::linalg::schemeii::Context::~Context ( )
default

◆ Context() [2/2]

gpr::linalg::schemeii::Context::Context ( const Context & )
delete

Member Function Documentation

◆ describe()

std::string gpr::linalg::schemeii::Context::describe ( std::int64_t k) const
nodiscard

One line naming engine, provider, bits and moduli for k.

Definition at line 2005 of file SchemeII.cpp.

2006{
2007 const Plan p = plan(k);
2008 std::string text = impl_->options.engine == Engine::Int8 ? "int8" : "fp64";
2009 if (impl_->int8) text += " provider=" + impl_->int8->name();
2010 text += " bits=" + std::to_string(p.bits) +
2011 " moduli=" + std::to_string(p.moduli.size()) +
2012 " chunk=" + std::to_string(p.chunk) +
2013 " log2M=" + std::to_string(p.log2_modulus_product);
2014 return text;
2015}
Plan plan(std::int64_t k) const

◆ gemm()

void gpr::linalg::schemeii::Context::gemm ( bool transpose_a,
bool transpose_b,
std::int64_t m,
std::int64_t n,
std::int64_t k,
double alpha,
const double * a,
std::int64_t lda,
const double * b,
std::int64_t ldb,
double beta,
double * c,
std::int64_t ldc ) const

C = alpha op(A) op(B) + beta C with column-major storage. Workspace is bounded by Options::panel_bytes plus the block residues.

Definition at line 2036 of file SchemeII.cpp.

2041{
2042 if (m < 0 || n < 0 || k < 0 || ldc < std::max<std::int64_t>(1, m) ||
2043 !std::isfinite(alpha) || !std::isfinite(beta))
2044 throw std::invalid_argument("invalid Scheme II product");
2045 if (m == 0 || n == 0) return;
2046 if (k == 0 || alpha == 0.0) {
2047 for (std::int64_t j = 0; j < n; ++j)
2048 for (std::int64_t i = 0; i < m; ++i)
2049 c[i + j * ldc] = beta == 0.0 ? 0.0 : beta * c[i + j * ldc];
2050 return;
2051 }
2052 const Plan p = plan(k);
2053 const std::size_t per_entry =
2054 p.moduli.size() *
2055 (impl_->options.engine == Engine::Int8 ? 1 : sizeof(double)) *
2056 static_cast<std::size_t>(k);
2057 // Panels of op(A) rows and op(B) columns whose residues fit the budget:
2058 // whole operands when they fit, else one whole operand and panels of
2059 // the other, else panels of both (op(B) panels are then prepared once
2060 // per op(A) panel).
2061 const std::size_t budget = impl_->options.panel_bytes;
2062 const std::size_t a_bytes = static_cast<std::size_t>(m) * per_entry;
2063 const std::size_t b_bytes = static_cast<std::size_t>(n) * per_entry;
2064 const auto entries = [&](std::size_t bytes, std::int64_t floor) {
2065 return std::max<std::int64_t>(
2066 floor, static_cast<std::int64_t>(bytes / per_entry));
2067 };
2068 std::int64_t row_panel = m, col_panel = n;
2069 if (a_bytes + b_bytes > budget) {
2070 if (a_bytes <= budget / 2)
2071 col_panel = entries(budget - a_bytes, impl_->options.block_cols);
2072 else if (b_bytes <= budget / 2)
2073 row_panel = entries(budget - b_bytes, impl_->options.block_rows);
2074 else {
2075 row_panel = entries(budget / 2, impl_->options.block_rows);
2076 col_panel = entries(budget / 2, impl_->options.block_cols);
2077 }
2078 }
2079 row_panel = std::min(row_panel, m);
2080 col_panel = std::min(col_panel, n);
2081 const auto prepare_columns = [&](std::int64_t j, std::int64_t cols) {
2082 const double* b_panel = transpose_b ? b + j : b + j * ldb;
2083 return impl_->prepare(false, b_panel, ldb, transpose_b, cols, k, p,
2084 nullptr, false, true);
2085 };
2086 Operand whole_right;
2087 if (col_panel == n) whole_right = prepare_columns(0, n);
2088 for (std::int64_t i = 0; i < m; i += row_panel) {
2089 const std::int64_t rows = std::min(row_panel, m - i);
2090 const double* a_panel = transpose_a ? a + i * lda : a + i;
2091 const Operand left = impl_->prepare(true, a_panel, lda, transpose_a,
2092 rows, k, p, nullptr, false, true);
2093 for (std::int64_t j = 0; j < n; j += col_panel) {
2094 const std::int64_t cols = std::min(col_panel, n - j);
2095 Operand panel_right;
2096 if (col_panel != n) panel_right = prepare_columns(j, cols);
2097 const Operand& right = col_panel == n ? whole_right : panel_right;
2098 const Task task{&left, 0, rows, &right, 0,
2099 cols, alpha, beta, c + i + j * ldc, ldc};
2100 run(std::span<const Task>(&task, 1));
2101 }
2102 }
2103}
void run(std::span< const Task > tasks) const

◆ operator=()

Context & gpr::linalg::schemeii::Context::operator= ( const Context & )
delete

◆ options()

const Options & gpr::linalg::schemeii::Context::options ( ) const
nodiscardnoexcept

Definition at line 1989 of file SchemeII.cpp.

1990{
1991 return impl_->options;
1992}

◆ plan()

Plan gpr::linalg::schemeii::Context::plan ( std::int64_t k) const
nodiscard

Definition at line 1999 of file SchemeII.cpp.

2000{
2001 if (k < 0) throw std::invalid_argument("invalid Scheme II inner dimension");
2002 return impl_->table(k)->plan;
2003}

◆ prepareColumns()

Operand gpr::linalg::schemeii::Context::prepareColumns ( const double * b,
std::int64_t ldb,
bool transpose,
std::int64_t k,
std::int64_t n,
const Plan & plan,
const int * exponents = nullptr ) const

Column operand from op(B) (k x n).

Definition at line 2024 of file SchemeII.cpp.

2027{
2028 return impl_->prepare(false, b, ldb, transpose, n, k, plan, exponents);
2029}

◆ prepareRows()

Operand gpr::linalg::schemeii::Context::prepareRows ( const double * a,
std::int64_t lda,
bool transpose,
std::int64_t m,
std::int64_t k,
const Plan & plan,
const int * exponents = nullptr ) const

Row operand from op(A) (m x k). Exponents override the row maxima.

Definition at line 2017 of file SchemeII.cpp.

2020{
2021 return impl_->prepare(true, a, lda, transpose, m, k, plan, exponents);
2022}

◆ reconstruct()

void gpr::linalg::schemeii::Context::reconstruct ( const Plan & plan,
std::span< const std::uint32_t > residues,
std::span< const int > row_exponents,
std::span< const int > column_exponents,
std::int64_t m,
std::int64_t n,
double alpha,
double beta,
double * c,
std::int64_t ldc ) const

C block from summed residues (see residues()).

Definition at line 2119 of file SchemeII.cpp.

2125{
2126 const auto table = impl_->table(plan.k);
2127 if (table->plan.moduli != plan.moduli ||
2128 residues.size() !=
2129 plan.moduli.size() * static_cast<std::size_t>(m * n) ||
2130 row_exponents.size() != static_cast<std::size_t>(m) ||
2131 column_exponents.size() != static_cast<std::size_t>(n))
2132 throw std::invalid_argument("invalid Scheme II reconstruction");
2133 std::vector<double> values(residues.begin(), residues.end());
2134 for (std::size_t t = 0; t < plan.moduli.size(); ++t)
2135 for (std::size_t i = 0; i < static_cast<std::size_t>(m * n); ++i)
2136 if (residues[t * m * n + i] >= plan.moduli[t])
2137 throw std::invalid_argument("Scheme II residue out of range");
2139 impl_->blockReconstruct(*table, values.data(), row_exponents.data(),
2140 column_exponents.data(), m, n, alpha, beta, c, ldc,
2141 w);
2142}
void residues(const Task &task, std::vector< std::uint32_t > &out) const

◆ residues()

void gpr::linalg::schemeii::Context::residues ( const Task & task,
std::vector< std::uint32_t > & out ) const

Residues in [0, p_t) of A'B' for the block of a task, stored as out[t * m * n + i + j * m]. With a split inner dimension, sum these over the parts modulo p_t (exact in int64) and pass the sums to reconstruct(); prepare every part with the exponents of the whole rows and columns and the plan of the whole k.

Definition at line 2105 of file SchemeII.cpp.

2106{
2107 if (!task.rows || !task.cols || !task.rows->data_ || !task.cols->data_)
2108 throw std::invalid_argument("invalid Scheme II task");
2109 if (task.rows->data_->plan_k != task.cols->data_->plan_k ||
2110 task.rows->data_->depth != task.cols->data_->depth)
2111 throw std::invalid_argument("mismatched Scheme II operands");
2112 const auto table = impl_->table(task.rows->data_->plan_k);
2114 impl_->blockResidues(*table, *task.rows->data_, task.row0,
2115 *task.cols->data_, task.col0, task.m, task.n, w);
2116 out.assign(w.residue.begin(), w.residue.end());
2117}
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out

◆ run()

void gpr::linalg::schemeii::Context::run ( std::span< const Task > tasks) const

Runs every task; the outputs must not overlap and all tasks share k. Throws on a non-finite result.

Definition at line 2031 of file SchemeII.cpp.

2032{
2033 impl_->runBlocks(tasks);
2034}

◆ setThreadSource()

void gpr::linalg::schemeii::Context::setThreadSource ( int(* source )())

Thread count used when Options::threads is zero.

Definition at line 1994 of file SchemeII.cpp.

1995{
1996 impl_->thread_source = source;
1997}

The documentation for this class was generated from the following files: