TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
tools.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: Olivier Parcollet, Nils Wentzell
16
17/**
18 * @file
19 * @brief Provides various traits and utilities for the BLAS interface.
20 */
21
22#pragma once
23
24#include "../concepts.hpp"
25#include "../map.hpp"
26#include "../mapped_functions.hpp"
27
28#include <complex>
29#include <tuple>
30#include <type_traits>
31#include <utility>
32
33namespace nda {
34
35 /**
36 * @ingroup linalg_blas
37 * @brief Alias for `std::complex<double>` type.
38 */
39 using dcomplex = std::complex<double>;
40
41} // namespace nda
42
43namespace nda::blas {
44
45 /**
46 * @addtogroup linalg_blas
47 * @{
48 */
49
50 /// Constexpr variable that is true if the given type is a conjugate lazy expression.
51 template <typename A>
52 static constexpr bool is_conj_array_expr = false;
53
54 /// Specialization of nda::blas::is_conj_array_expr for the conjugate lazy expressions.
55 template <MemoryArray A>
56 static constexpr bool is_conj_array_expr<expr_call<conj_f, A>> = true;
57
58 // Specialization of nda::blas::is_conj_array_expr for cvref types.
59 template <typename A>
60 requires(!std::is_same_v<A, std::remove_cvref_t<A>>)
61 static constexpr bool is_conj_array_expr<A> = is_conj_array_expr<std::remove_cvref_t<A>>;
62
63 /// Constexpr variable that is true if the given nda::Array type has a Fortran memory layout.
64 template <Array A>
65 requires(MemoryArray<A> or is_conj_array_expr<A>)
66 static constexpr bool has_F_layout = []() {
67 if constexpr (is_conj_array_expr<A>)
68 return has_F_layout<decltype(std::get<0>(std::declval<A>().a))>;
69 else
71 }();
72
73 /// Constexpr variable that is true if the given nda::Array type has a C memory layout.
74 template <Array A>
75 requires(MemoryArray<A> or is_conj_array_expr<A>)
76 static constexpr bool has_C_layout = []() {
77 if constexpr (is_conj_array_expr<A>)
78 return has_C_layout<decltype(std::get<0>(std::declval<A>().a))>;
79 else
81 }();
82
83 /**
84 * @brief Variable template that determines the BLAS matrix operation tag ('N','T','C') based on the given boolean
85 * flags for conjugation and transposition.
86 *
87 * @tparam conj Boolean flag for conjugation.
88 * @tparam transpose Boolean flag for transposition.
89 */
90 template <bool conj, bool transpose>
91 const char get_op = []() {
92 static_assert(!(conj and not transpose), "Error in nda::blas::get_op: Cannot use conjugate operation alone in blas operations");
93 if constexpr (conj and transpose)
94 return 'C';
95 else if constexpr (transpose)
96 return 'T';
97 else // !conj and !transpose
98 return 'N';
99 }();
100
101 /**
102 * @brief Get the leading dimension in LAPACK jargon of an nda::MemoryMatrix.
103 *
104 * @tparam A nda::MemoryMatrix type.
105 * @param a nda::MemoryMatrix object.
106 * @return Leading dimension.
107 */
108 template <MemoryMatrix A>
109 int get_ld(A const &a) {
110 return a.indexmap().strides()[has_F_layout<A> ? 1 : 0];
111 }
112
113 /**
114 * @brief Get the number of columns in LAPACK jargon of an nda::MemoryMatrix.
115 *
116 * @tparam A nda::MemoryMatrix type.
117 * @param a nda::MemoryMatrix object.
118 * @return Number of columns.
119 */
120 template <MemoryMatrix A>
121 int get_ncols(A const &a) {
122 return a.shape()[has_F_layout<A> ? 1 : 0];
123 }
124
125 /** @} */
126
127} // namespace nda::blas
#define CUBLAS_CHECK(X,...)
#define NDA_RUNTIME_ERROR
int get_ld(A const &a)
Get the leading dimension in LAPACK jargon of an nda::MemoryMatrix.
Definition tools.hpp:109
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:76
static constexpr bool is_conj_array_expr
Constexpr variable that is true if the given type is a conjugate lazy expression.
Definition tools.hpp:52
int get_ncols(A const &a)
Get the number of columns in LAPACK jargon of an nda::MemoryMatrix.
Definition tools.hpp:121
static constexpr bool is_conj_array_expr< expr_call< conj_f, A > >
Specialization of nda::blas::is_conj_array_expr for the conjugate lazy expressions.
Definition tools.hpp:56
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:66
const char get_op
Variable template that determines the BLAS matrix operation tag ('N','T','C') based on the given bool...
Definition tools.hpp:91
#define AS_STRING(...)
Definition macros.hpp:31