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--present, The Simons Foundation
2// This file is part of TRIQS/nda and is licensed under the Apache License, Version 2.0.
3// SPDX-License-Identifier: Apache-2.0
4// See LICENSE in the root of this distribution for details.
5
10
11#pragma once
12
14#include "./tools.hpp"
15#include "../concepts.hpp"
16#include "../macros.hpp"
18#include "../traits.hpp"
19
20#ifndef NDA_HAVE_DEVICE
21#include "../device.hpp"
22#endif
23
24#include <tuple>
25
26namespace nda::blas {
27
32
45 template <typename A, typename X, typename Y>
46 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)
47 EXPECTS(a.extent(1) == x.extent(0));
48 EXPECTS(a.extent(0) == y.extent(0));
49
50 if (beta == 0.0) {
51 y = 0 * alpha;
52 } else {
53 y *= beta;
54 }
55
56 for (int i = 0; i < a.extent(0); ++i) {
57 for (int k = 0; k < a.extent(1); ++k) y(i) += alpha * a(i, k) * x(k);
58 }
59 }
60
82 template <Matrix A, MemoryVector X, MemoryVector Y>
83 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>>)
84 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)
85 // get underlying matrix in case it is given as a lazy expression
86 auto to_mat = []<Matrix Z>(Z const &z) -> decltype(auto) {
87 if constexpr (is_conj_array_expr<Z>)
88 return std::get<0>(z.a);
89 else
90 return z;
91 };
92 auto &mat = to_mat(a);
93
94 // compile-time checks
95 using mat_type = decltype(mat);
97
98 // runtime checks
99 EXPECTS(mat.extent(1) == x.extent(0));
100 EXPECTS(mat.extent(0) == y.extent(0));
101 EXPECTS(mat.indexmap().min_stride() == 1);
102 EXPECTS(x.indexmap().min_stride() == 1);
103 EXPECTS(y.indexmap().min_stride() == 1);
104
105 // gather parameters for gemv call
106 static constexpr bool conj_A = is_conj_array_expr<A>;
107 char op_a = get_op<conj_A, /* transpose = */ !has_F_layout<mat_type>>;
108 auto [m, n] = mat.shape();
109 if constexpr (has_C_layout<mat_type>) std::swap(m, n);
110
112#if defined(NDA_HAVE_DEVICE)
113 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]);
114#else
116#endif
117 } else {
118 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]);
119 }
120 }
121
123
124} // namespace nda::blas
Provides definitions and type traits involving the different memory address spaces supported by nda.
void swap(nda::basic_array_view< V1, R1, LP1, A1, AP1, OP1 > &a, nda::basic_array_view< V2, R2, LP2, A2, AP2, OP2 > &b)=delete
std::swap is deleted for nda::basic_array_view.
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:186
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:182
int get_ld(A const &a)
Get the leading dimension in LAPACK jargon of an nda::MemoryMatrix.
Definition tools.hpp:98
static constexpr bool has_C_layout
Constexpr variable that is true if the given nda::Array type has a C memory layout.
Definition tools.hpp:65
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:46
static constexpr bool is_conj_array_expr
Constexpr variable that is true if the given type is a conjugate lazy expression.
Definition tools.hpp:41
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:84
static constexpr bool has_F_layout
Constexpr variable that is true if the given nda::Array type has a Fortran memory layout.
Definition tools.hpp:55
const char get_op
Variable template that determines the BLAS matrix operation tag ('N','T','C') based on the given bool...
Definition tools.hpp:80
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:36
constexpr bool is_blas_lapack_v
Alias for nda::is_double_or_complex_v.
Definition traits.hpp:92
Macros used in the nda library.
Provides various traits and utilities for the BLAS interface.
Provides type traits for the nda library.