TRIQS/nda 2.0.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
tools.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
13#include "../concepts.hpp"
14#include "../declarations.hpp"
15#include "../exceptions.hpp"
16#include "../macros.hpp"
17#include "../map.hpp"
20#include "../mem/policies.hpp"
21#include "../traits.hpp"
22
23#include <complex>
24#include <type_traits>
25#include <utility>
26
27namespace nda {
28
33 using dcomplex = std::complex<double>;
34
35} // namespace nda
36
37namespace nda::blas_lapack {
38
43
45 template <typename A>
46 static constexpr bool is_conj_array_expr = false;
47
49 template <MemoryArray A>
51
52 // Specialization of nda::blas_lapack::is_conj_array_expr for cvref types.
53 template <typename A>
54 requires(!std::is_same_v<A, std::remove_cvref_t<A>>)
55 static constexpr bool is_conj_array_expr<A> = is_conj_array_expr<std::remove_cvref_t<A>>;
56
68 template <Array A>
70 MemoryArray decltype(auto) get_array(A &&a) {
71 if constexpr (is_conj_array_expr<A>) {
72 return std::forward<A>(a).operand();
73 } else {
74 return std::forward<A>(a);
75 }
76 }
77
79 template <Array... As>
80 requires((MemoryArray<As> or is_conj_array_expr<As>) and ...)
81 static constexpr bool has_F_layout = ([]<typename A>() constexpr {
82 if constexpr (is_conj_array_expr<A>)
83 return has_F_layout<decltype(std::declval<A>().operand())>;
84 else
85 return std::remove_cvref_t<A>::is_stride_order_Fortran();
86 }.template operator()<As>() and ...);
87
89 template <Array... As>
90 requires((MemoryArray<As> or is_conj_array_expr<As>) and ...)
91 static constexpr bool has_C_layout = ([]<typename A>() constexpr {
92 if constexpr (is_conj_array_expr<A>)
93 return has_C_layout<decltype(std::declval<A>().operand())>;
94 else
95 return std::remove_cvref_t<A>::is_stride_order_C();
96 }.template operator()<As>() and ...);
97
105 template <Array A>
106 static constexpr char get_op = []() {
107 auto constexpr conj = is_conj_array_expr<A>;
108 auto constexpr transpose = has_C_layout<A>;
109 static_assert(!(conj and not transpose), "Error in nda::blas_lapack::get_op: Cannot use conjugate operation alone in BLAS operations");
110 if constexpr (conj and transpose)
111 return 'C';
112 else if constexpr (transpose)
113 return 'T';
114 else // !conj and !transpose
115 return 'N';
116 }();
117
128 template <MemoryArray A>
129 requires(get_rank<A> == 1 or get_rank<A> == 2)
130 int get_ld(A const &a) {
131 if constexpr (get_rank<A> == 1) {
132 return a.size();
133 } else {
134 return a.indexmap().strides()[has_F_layout<A> ? 1 : 0];
135 }
136 }
137
148 template <MemoryArray A>
149 requires(get_rank<A> == 1 or get_rank<A> == 2)
150 int get_ncols(A const &a) {
151 if constexpr (get_rank<A> == 1) {
152 return 1;
153 } else {
154 return a.shape()[has_F_layout<A> ? 1 : 0];
155 }
156 }
157
162 template <MemoryArray A>
164
171 template <MemoryArray A>
173
182 template <Array A>
184 auto batch_ptrs(A &&a) { // NOLINT (temporary views are allowed here)
185 using ptr_t = decltype(a.data());
186 auto const idx = (has_C_layout<A> ? 0 : (get_rank<A> == 3 ? 2 : 1));
187 auto const batch_size = a.shape()[idx];
188 auto const stride = a.indexmap().strides()[idx];
189
190 auto ptrs = vector<ptr_t>(batch_size);
191 for (int i = 0; auto &ptr : ptrs) ptr = a.data() + i++ * stride;
192 return ptrs;
193 }
194
207 template <typename A>
208 requires(is_regular_or_view_v<A> and get_rank<A> == 1)
209 void resize_or_check_work_buffer(A &a, long min_size) {
210 if (a.size() >= min_size) {
211 EXPECTS(a.indexmap().min_stride() == 1);
212 return;
213 }
214 if constexpr (is_regular_v<A>) {
215 a.resize(min_size);
216 } else {
217 NDA_RUNTIME_ERROR << "Error in nda::blas_lapack::resize_or_check_work_buffer: Size mismatch: " << a.size() << " < " << min_size;
218 }
219 }
220
227 template <typename A, int R = -1>
229
236 template <typename A, int R = -1>
238
245 template <typename A, int R = -1>
246 concept BlasArrayCplx = BlasArray<A, R> and AnyOf<get_value_t<A>, std::complex<float>, std::complex<double>>;
247
254 template <typename A, int R = -1>
257
266 template <typename A, typename B, int R = -1>
267 concept BlasArrayFor = BlasArrayOrConj<B> and BlasArray<A, R> and have_same_value_type_v<A, B> and mem::have_compatible_addr_space<A, B>;
268
277 template <typename A, typename B, int R = -1>
279 BlasArrayOrConj<B> and BlasArrayOrConj<A, R> and have_same_value_type_v<A, B> and mem::have_compatible_addr_space<A, B>;
280
288 template <typename A, typename B, int R = -1>
290 and std::is_same_v<get_value_t<A>, int> and mem::have_compatible_addr_space<A, B>;
291
300 template <typename A, typename B, int R = -1>
302 BlasArrayOrConj<B> and BlasArray<A, R> and std::is_same_v<get_value_t<A>, get_fp_t<B>> and mem::have_compatible_addr_space<A, B>;
303
305
306} // namespace nda::blas_lapack
Provides definitions and type traits involving the different memory address spaces supported by nda.
Check if T is the same as any of the types in Us.
Definition concepts.hpp:119
Check if a given type satisfies the array concept.
Definition concepts.hpp:212
Check if a given type is an nda::Array of a certain rank.
Definition concepts.hpp:248
Check if a given type satisfies the memory array concept.
Definition concepts.hpp:230
Check if a given type is an nda::MemoryArray of a certain rank.
Definition concepts.hpp:257
BLAS/LAPACK compatible array type with complex value type.
Definition tools.hpp:246
BLAS/LAPACK compatible array type that has the same value type as the reference array type and a comp...
Definition tools.hpp:267
BLAS/LAPACK compatible array type.
Definition tools.hpp:228
BLAS/LAPACK compatible array or conjugate lazy expression type that has the same value type as the re...
Definition tools.hpp:278
BLAS/LAPACK compatible array or conjugate lazy expression type.
Definition tools.hpp:255
BLAS/LAPACK compatible array type that has a compatible floating-point value type and address space w...
Definition tools.hpp:301
BLAS/LAPACK compatible array type with real value type.
Definition tools.hpp:237
BLAS/LAPACK compatible pivot array type that has a compatible address space with the reference array ...
Definition tools.hpp:289
Provides concepts for the nda library.
Provides various convenient aliases and helper functions for nda::basic_array and nda::basic_array_vi...
Provides a custom runtime error class and macros to assert conditions and throw exceptions.
auto transpose(A &&a)
Transpose the memory layout of an nda::MemoryArray or an nda::expr_call.
decltype(auto) conj(A &&a)
Function conj for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types with a com...
basic_array< ValueType, 1, C_layout, 'V', ContainerPolicy > vector
Alias template of an nda::basic_array with rank 1 and a 'V' algebra.
constexpr bool is_regular_v
Constexpr variable that is true if type A is a regular array, i.e. an nda::basic_array.
Definition traits.hpp:177
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:238
constexpr int get_rank
Constexpr variable that specifies the rank of an nda::Array or of a contiguous 1-dimensional range.
Definition traits.hpp:159
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:225
constexpr bool is_regular_or_view_v
Constexpr variable that is true if type A is either a regular array or a view.
Definition traits.hpp:195
typename remove_complex< get_value_t< A > >::type get_fp_t
Get the floating-point type associated with the value type of an array/view/scalar type.
Definition traits.hpp:234
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:70
std::complex< double > dcomplex
Alias for std::complex<double> type.
Definition tools.hpp:33
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:106
static constexpr bool is_conj_array_expr
Constexpr variable that is true if the given type is a conjugate lazy expression.
Definition tools.hpp:46
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:130
auto batch_ptrs(A &&a)
Given a 2- or 3-dimensional array get an array of pointers to each of the submatrices/subvectors inde...
Definition tools.hpp:184
static constexpr bool has_C_layout
Constexpr variable that is true if all given nda::Array types have nda::C_layout.
Definition tools.hpp:91
int get_ncols(A const &a)
Get the number of columns of an nda::MemoryArray with rank 1 or 2 for BLAS/LAPACK calls.
Definition tools.hpp:150
void resize_or_check_work_buffer(A &a, long min_size)
Resize or check the size of a 1D array/view.
Definition tools.hpp:209
vector< get_value_t< A >, heap< mem::get_addr_space< A > > > vector_value_t
Alias for an nda::vector with the same value type and address space as the given type.
Definition tools.hpp:163
vector< get_fp_t< A >, heap< mem::get_addr_space< A > > > vector_fp_t
Alias for an nda::vector with the same address space as the given type and its value type determined ...
Definition tools.hpp:172
static constexpr bool has_F_layout
Constexpr variable that is true if all given nda::Array types have nda::F_layout.
Definition tools.hpp:81
heap_basic< mem::mallocator< AdrSp > > heap
Alias template of the nda::heap_basic policy using an nda::mem::mallocator.
Definition policies.hpp:52
constexpr bool is_blas_lapack_v
Constexpr variable that is true if type T is either of type 'float', double, std::complex<float>' or ...
Definition traits.hpp:95
Macros used in the nda library.
Provides lazy function calls on arrays/views.
Provides some custom implementations of standard mathematical functions used for lazy,...
Defines various memory handling policies.
Provides type traits for the nda library.