Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
cpu/gemv.hpp
Go to the documentation of this file.
1#pragma once
2
12
13#include <concepts>
14#include <type_traits>
15
16namespace uni20::linalg::cpu
17{
18
20template <class OutputMdspan, class Scalar, class MatrixMdspan, class InputMdspan>
24 std::same_as<std::remove_cv_t<typename std::remove_cvref_t<OutputMdspan>::element_type>, Scalar> &&
25 std::same_as<std::remove_cv_t<typename std::remove_cvref_t<MatrixMdspan>::element_type>, Scalar> &&
26 std::same_as<std::remove_cv_t<typename std::remove_cvref_t<InputMdspan>::element_type>, Scalar> &&
27 requires(OutputMdspan& output, MatrixMdspan& matrix, InputMdspan& input,
28 typename std::remove_cvref_t<OutputMdspan>::index_type index, Scalar value) {
29 static_cast<Scalar>(output.operator[](index));
30 static_cast<Scalar>(matrix.operator[](index, index));
31 static_cast<Scalar>(input.operator[](index));
32 output.operator[](index) = value;
33 value += value * value;
34 { value == Scalar{} } -> std::convertible_to<bool>;
35 };
36
38template <class OutputMdspan, uni20::Scalar Scalar, class MatrixMdspan, class InputMdspan>
40void gemv(OutputMdspan&& output, Scalar alpha, MatrixMdspan&& matrix, InputMdspan&& input, Scalar beta)
41{
42 using output_type = std::remove_cvref_t<OutputMdspan>;
43 using index_type = typename output_type::index_type;
44
45 CHECK_EQUAL(matrix.extent(1), input.extent(0));
46 CHECK_EQUAL(output.extent(0), matrix.extent(0));
47
48 index_type const rows = static_cast<index_type>(output.extent(0));
49 index_type const inner = static_cast<index_type>(input.extent(0));
50
51 for (index_type row = 0; row < rows; ++row)
52 {
54 if (alpha != Scalar{})
55 {
56 for (index_type col = 0; col < inner; ++col)
57 {
58 product += static_cast<Scalar>(matrix[row, col]) * static_cast<Scalar>(input[col]);
59 }
60 }
61
62 if (beta == Scalar{})
63 {
64 output[row] = alpha * product;
65 }
66 else
67 {
68 output[row] = beta * static_cast<Scalar>(output[row]) + alpha * product;
69 }
70 }
71}
72
73} // namespace uni20::linalg::cpu
A mutable mdspan-like type with a specified static rank.
Definition mdspan/concepts.hpp:494
An mdspan-like type with a specified static rank.
Definition mdspan/concepts.hpp:447
Concept for numeric scalar types (integer, real, or complex).
Definition scalar_concepts.hpp:81
Report whether resolved mdspans support reference CPU GEMV expressions.
Definition cpu/gemv.hpp:21
locale_t value
Definition data_table.cpp:49
Mdspan concept and accessor extensions for Uni20.
T product(std::vector< T > const &values)
Definition mpreal_exact_example.cpp:13
Definition strided_transform.hpp:20
void gemv(OutputMdspan &&output, Scalar alpha, MatrixMdspan &&matrix, InputMdspan &&input, Scalar beta)
Execute reference CPU GEMV on resolved mdspans.
Definition cpu/gemv.hpp:40
std::ptrdiff_t index_type
Definition types.hpp:19
mpreal beta(mpreal const &a, mpreal const &x)
Real Beta function; positive rational/integer pairs retain exact values.
Definition mpreal_special.hpp:164
#define CHECK_EQUAL(a, b,...)
Definition trace.hpp:309