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 // NDA_HAVE_DEVICE
24
25#include <utility>
26
27namespace nda::blas {
28
33
60 template <Matrix A, Matrix B, MemoryMatrix C>
61 requires((MemoryMatrix<A> or is_conj_array_expr<A>) and (MemoryMatrix<B> or is_conj_array_expr<B>)
63 void gemm(get_value_t<A> alpha, A const &a, B const &b, get_value_t<A> beta, C &&c) {
64 // if C is in C-layout, compute the transpose of the product in Fortran order
65 if constexpr (has_C_layout<C>) {
66 gemm(alpha, transpose(b), transpose(a), beta, transpose(std::forward<C>(c)));
67 } else {
68 // get underlying matrix in case it is given as a conjugate expression
69 auto &mat_a = get_array(a);
70 auto &mat_b = get_array(b);
71
72 // check the dimensions of the input/output arrays/views
73 auto const [m, k] = mat_a.shape();
74 auto const [l, n] = mat_b.shape();
75 EXPECTS(k == l);
76 EXPECTS(m == c.extent(0));
77 EXPECTS(n == c.extent(1));
78
79 // arrays/views must be BLAS compatible
80 EXPECTS(mat_a.indexmap().min_stride() == 1);
81 EXPECTS(mat_b.indexmap().min_stride() == 1);
82 EXPECTS(c.indexmap().min_stride() == 1);
83
84 // perform the actual library call
86#if defined(NDA_HAVE_DEVICE)
87 device::gemm(get_op<A>, get_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));
88#else
90#endif
91 } else {
92 f77::gemm(get_op<A>, get_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));
93 }
94 }
95 }
96
98
99} // 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.
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
static constexpr bool has_C_layout
Constexpr variable that is true if the given nda::Array type has nda::C_layout.
Definition tools.hpp:83
int get_ld(A const &a)
Get the leading dimension of an nda::MemoryArray with rank 1 or 2 for BLAS/LAPACK calls.
Definition tools.hpp:122
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
static constexpr char get_op
Variable template that determines the BLAS matrix operation tag ('N','T','C') based on the given bool...
Definition tools.hpp:98
MemoryArray decltype(auto) get_array(A &&a)
Get the underlying array of a conjugate lazy expression or return the array itself in case it is an n...
Definition tools.hpp:62
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:63
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.