TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
gemv.hpp
Go to the documentation of this file.
1// Copyright (c) 2019-2023 Simons Foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0.txt
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Authors: Miguel Morales, Olivier Parcollet, Nils Wentzell
16
17/**
18 * @file
19 * @brief Provides a generic interface to the BLAS `gemv` routine.
20 */
21
22#pragma once
23
24#include "./interface/cxx_interface.hpp"
25#include "./tools.hpp"
26#include "../concepts.hpp"
27#include "../macros.hpp"
28#include "../mem/address_space.hpp"
29#include "../traits.hpp"
30
31#ifndef NDA_HAVE_DEVICE
32#include "../device.hpp"
33#endif
34
35#include <tuple>
36
37namespace nda::blas {
38
39 /**
40 * @addtogroup linalg_blas
41 * @{
42 */
43
44 /**
45 * @brief Generic nda::blas::gemv implementation for types not supported by BLAS/LAPACK.
46 *
47 * @tparam A Some matrix type.
48 * @tparam X Some vector type.
49 * @tparam Y Some vector type.
50 * @param alpha Input scalar.
51 * @param a Input matrix of size m-by-n.
52 * @param x Input vector of size n.
53 * @param beta Input scalar.
54 * @param y Input/Output vector of size m.
55 */
56 template <typename A, typename X, typename Y>
57 void gemv_generic(get_value_t<A> alpha, A const &a, X const &x, get_value_t<A> beta, Y &&y) { // NOLINT (temporary views are allowed here)
58 EXPECTS(a.extent(1) == x.extent(0));
59 EXPECTS(a.extent(0) == y.extent(0));
60 for (int i = 0; i < a.extent(0); ++i) {
61 y(i) = beta * y(i);
62 for (int k = 0; k < a.extent(1); ++k) y(i) += alpha * a(i, k) * x(k);
63 }
64 }
65
66 /**
67 * @brief Interface to the BLAS `gemv` routine.
68 *
69 * @details This function performs one of the matrix-vector operations
70 *
71 * - \f$ \mathbf{y} \leftarrow \alpha \mathbf{A} \mathbf{x} + \beta \mathbf{y} \f$,
72 * - \f$ \mathbf{y} \leftarrow \alpha \mathbf{A}^T \mathbf{x} + \beta \mathbf{y} \f$,
73 * - \f$ \mathbf{y} \leftarrow \alpha \mathbf{A}^H \mathbf{x} + \beta \mathbf{y} \f$,
74 *
75 * where \f$ \alpha \f$ and \f$ \beta \f$ are scalars, \f$ \mathbf{x} \f$ and \f$ \mathbf{y} \f$ are vectors and
76 * \f$ \mathbf{A} \f$ is an m-by-n matrix.
77 *
78 * @tparam A nda::Matrix type.
79 * @tparam X nda::MemoryVector type.
80 * @tparam Y nda::MemoryVector type.
81 * @param alpha Input scalar.
82 * @param a Input matrix of size m-by-n.
83 * @param x Input vector of size n.
84 * @param beta Input scalar.
85 * @param y Input/Output vector of size m.
86 */
87 template <Matrix A, MemoryVector X, MemoryVector Y>
88 requires((MemoryMatrix<A> or is_conj_array_expr<A>) and have_same_value_type_v<A, X, Y> and is_blas_lapack_v<get_value_t<A>>)
89 void gemv(get_value_t<A> alpha, A const &a, X const &x, get_value_t<A> beta, Y &&y) { // NOLINT (temporary views are allowed here)
90 // get underlying matrix in case it is given as a lazy expression
91 auto to_mat = []<Matrix Z>(Z const &z) -> decltype(auto) {
92 if constexpr (is_conj_array_expr<Z>)
93 return std::get<0>(z.a);
94 else
95 return z;
96 };
97 auto &mat = to_mat(a);
98
99 // compile-time checks
100 using mat_type = decltype(mat);
101 static_assert(mem::have_compatible_addr_space<mat_type, X, Y>);
102
103 // runtime checks
104 EXPECTS(mat.extent(1) == x.extent(0));
105 EXPECTS(mat.extent(0) == y.extent(0));
106 EXPECTS(mat.indexmap().min_stride() == 1);
107 EXPECTS(x.indexmap().min_stride() == 1);
108 EXPECTS(y.indexmap().min_stride() == 1);
109
110 // gather parameters for gemv call
111 static constexpr bool conj_A = is_conj_array_expr<A>;
112 char op_a = get_op<conj_A, /* transpose = */ !has_F_layout<mat_type>>;
113 auto [m, n] = mat.shape();
114 if constexpr (has_C_layout<mat_type>) std::swap(m, n);
115
116 if constexpr (mem::have_device_compatible_addr_space<mat_type, X, Y>) {
117#if defined(NDA_HAVE_DEVICE)
118 device::gemv(op_a, m, n, alpha, mat.data(), get_ld(mat), x.data(), x.indexmap().strides()[0], beta, y.data(), y.indexmap().strides()[0]);
119#else
121#endif
122 } else {
123 f77::gemv(op_a, m, n, alpha, mat.data(), get_ld(mat), x.data(), x.indexmap().strides()[0], beta, y.data(), y.indexmap().strides()[0]);
124 }
125 }
126
127 /** @} */
128
129} // namespace nda::blas
void gemv_generic(get_value_t< A > alpha, A const &a, X const &x, get_value_t< A > beta, Y &&y)
Generic nda::blas::gemv implementation for types not supported by BLAS/LAPACK.
Definition gemv.hpp:57
void gemv(get_value_t< A > alpha, A const &a, X const &x, get_value_t< A > beta, Y &&y)
Interface to the BLAS gemv routine.
Definition gemv.hpp:89
void compile_error_no_gpu()
Trigger a compilation error in case GPU specific functionality is used without configuring the projec...
Definition device.hpp:47
#define EXPECTS(X)
Definition macros.hpp:59