TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
gemm.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"
17#include "../macros.hpp"
19#include "../traits.hpp"
20
21#ifndef NDA_HAVE_DEVICE
22#include "../device.hpp"
23#endif
24
25#include <tuple>
26#include <utility>
27
28namespace nda::blas {
29
34
47 template <Matrix A, Matrix B, MemoryMatrix C>
48 void gemm_generic(typename A::value_type alpha, A const &a, B const &b, typename A::value_type beta,
49 C &&c) { // NOLINT (temporary views are allowed here)
50 EXPECTS(a.extent(1) == b.extent(0));
51 EXPECTS(a.extent(0) == c.extent(0));
52 EXPECTS(b.extent(1) == c.extent(1));
53
54 if (beta == 0.0) {
55 c = 0 * alpha;
56 } else {
57 c *= beta;
58 }
59
60 for (int i = 0; i < a.extent(0); ++i) {
61 for (int j = 0; j < b.extent(1); ++j) {
62 for (int k = 0; k < a.extent(1); ++k) c(i, j) += alpha * a(i, k) * b(k, j);
63 }
64 }
65 }
66
93 template <Matrix A, Matrix B, MemoryMatrix C>
94 requires((MemoryMatrix<A> or is_conj_array_expr<A>) and (MemoryMatrix<B> or is_conj_array_expr<B>)
96 void gemm(get_value_t<A> alpha, A const &a, B const &b, get_value_t<A> beta, C &&c) {
97 // get underlying matrix in case it is given as a lazy expression
98 auto to_mat = []<typename Z>(Z const &z) -> auto & {
99 if constexpr (is_conj_array_expr<Z>)
100 return std::get<0>(z.a);
101 else
102 return z;
103 };
104 auto &mat_a = to_mat(a);
105 auto &mat_b = to_mat(b);
106
107 // compile-time checks
108 using mat_a_type = decltype(mat_a);
109 using mat_b_type = decltype(mat_b);
110 static_assert(mem::have_compatible_addr_space<mat_a_type, mat_b_type, C>, "Error in nda::blas::gemm: Incompatible memory address spaces");
111
112 // runtime checks
113 EXPECTS(mat_a.extent(1) == mat_b.extent(0));
114 EXPECTS(mat_a.extent(0) == c.extent(0));
115 EXPECTS(mat_b.extent(1) == c.extent(1));
116 EXPECTS(mat_a.indexmap().min_stride() == 1);
117 EXPECTS(mat_b.indexmap().min_stride() == 1);
118 EXPECTS(c.indexmap().min_stride() == 1);
119
120 // c is in C order: compute the transpose of the product in Fortran order
121 if constexpr (has_C_layout<C>) {
122 gemm(alpha, transpose(b), transpose(a), beta, transpose(std::forward<C>(c)));
123 } else { // c is in Fortran order
124 static constexpr bool conj_A = is_conj_array_expr<A>;
125 static constexpr bool conj_B = is_conj_array_expr<B>;
126 char op_a = get_op<conj_A, /* transpose = */ has_C_layout<mat_a_type>>;
127 char op_b = get_op<conj_B, /* transpose = */ has_C_layout<mat_b_type>>;
128 auto [m, k] = mat_a.shape();
129 auto n = mat_b.extent(1);
130
132#if defined(NDA_HAVE_DEVICE)
133 device::gemm(op_a, op_b, m, n, k, alpha, mat_a.data(), get_ld(mat_a), mat_b.data(), get_ld(mat_b), beta, c.data(), get_ld(c));
134#else
136#endif
137 } else {
138 f77::gemm(op_a, op_b, m, n, k, alpha, mat_a.data(), get_ld(mat_a), mat_b.data(), get_ld(mat_b), beta, c.data(), get_ld(c));
139 }
140 }
141 }
142
144
145} // 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 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.
auto transpose(A &&a)
Transpose the memory layout of an nda::MemoryArray or an nda::expr_call.
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 gemm_generic(typename A::value_type alpha, A const &a, B const &b, typename A::value_type beta, C &&c)
Generic nda::blas::gemm implementation for types not supported by BLAS/LAPACK.
Definition gemm.hpp:48
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 gemm(get_value_t< A > alpha, A const &a, B const &b, get_value_t< A > beta, C &&c)
Interface to the BLAS gemm routine.
Definition gemm.hpp:96
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
Provides functions to transform the memory layout of an nda::basic_array or nda::basic_array_view.
Macros used in the nda library.
Provides various traits and utilities for the BLAS interface.
Provides type traits for the nda library.