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-2024 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: Thomas Hahn, Miguel Morales, Olivier Parcollet, Nils Wentzell
16
22#pragma once
23
25#include "./tools.hpp"
26#include "../concepts.hpp"
27#include "../macros.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
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
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);
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
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
129} // namespace nda::blas
Provides definitions and type traits involving the different memory address spaces supported by nda.
Provides a C++ interface for various BLAS routines.
Check if a given type is a matrix, i.e. an nda::ArrayOfRank<2>.
Definition concepts.hpp:290
Check if a given type is a memory matrix, i.e. an nda::MemoryArrayOfRank<2>.
Definition concepts.hpp:306
Provides concepts for the nda library.
Provides GPU and non-GPU specific functionality.
constexpr bool have_same_value_type_v
Constexpr variable that is true if all types in As have the same value type as A0.
Definition traits.hpp:196
std::decay_t< decltype(get_first_element(std::declval< A const >()))> get_value_t
Get the value type of an array/view or a scalar type.
Definition traits.hpp:192
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
static constexpr bool have_compatible_addr_space
Constexpr variable that is true if all given types have compatible address spaces.
static constexpr bool have_device_compatible_addr_space
Constexpr variable that is true if all given types have an address space compatible with Device.
void compile_error_no_gpu()
Trigger a compilation error in case GPU specific functionality is used without configuring the projec...
Definition device.hpp:47
constexpr bool is_blas_lapack_v
Alias for nda::is_double_or_complex_v.
Definition traits.hpp:102
Macros used in the nda library.
Provides various traits and utilities for the BLAS interface.
Provides type traits for the nda library.