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 <tuple>
25#include <type_traits>
26#include <utility>
27
28namespace nda {
29
34 using dcomplex = std::complex<double>;
35
36} // namespace nda
37
38namespace nda::blas_lapack {
39
44
46 template <typename A>
47 static constexpr bool is_conj_array_expr = false;
48
50 template <MemoryArray A>
52
53 // Specialization of nda::blas_lapack::is_conj_array_expr for cvref types.
54 template <typename A>
55 requires(!std::is_same_v<A, std::remove_cvref_t<A>>)
56 static constexpr bool is_conj_array_expr<A> = is_conj_array_expr<std::remove_cvref_t<A>>;
57
66 template <Array A>
68 MemoryArray decltype(auto) get_array(A &&a) {
69 if constexpr (is_conj_array_expr<A>) {
70 return std::get<0>(std::forward<A>(a).a);
71 } else {
72 return std::forward<A>(a);
73 }
74 }
75
77 template <Array... As>
78 requires((MemoryArray<As> or is_conj_array_expr<As>) and ...)
79 static constexpr bool has_F_layout = ([]<typename A>() constexpr {
80 if constexpr (is_conj_array_expr<A>)
81 return has_F_layout<decltype(std::get<0>(std::declval<A>().a))>;
82 else
83 return std::remove_cvref_t<A>::is_stride_order_Fortran();
84 }.template operator()<As>() and ...);
85
87 template <Array... As>
88 requires((MemoryArray<As> or is_conj_array_expr<As>) and ...)
89 static constexpr bool has_C_layout = ([]<typename A>() constexpr {
90 if constexpr (is_conj_array_expr<A>)
91 return has_C_layout<decltype(std::get<0>(std::declval<A>().a))>;
92 else
93 return std::remove_cvref_t<A>::is_stride_order_C();
94 }.template operator()<As>() and ...);
95
103 template <Array A>
104 static constexpr char get_op = []() {
105 auto constexpr conj = is_conj_array_expr<A>;
106 auto constexpr transpose = has_C_layout<A>;
107 static_assert(!(conj and not transpose), "Error in nda::blas_lapack::get_op: Cannot use conjugate operation alone in BLAS operations");
108 if constexpr (conj and transpose)
109 return 'C';
110 else if constexpr (transpose)
111 return 'T';
112 else // !conj and !transpose
113 return 'N';
114 }();
115
126 template <MemoryArray A>
127 requires(get_rank<A> == 1 or get_rank<A> == 2)
128 int get_ld(A const &a) {
129 if constexpr (get_rank<A> == 1) {
130 return a.size();
131 } else {
132 return a.indexmap().strides()[has_F_layout<A> ? 1 : 0];
133 }
134 }
135
146 template <MemoryArray A>
147 requires(get_rank<A> == 1 or get_rank<A> == 2)
148 int get_ncols(A const &a) {
149 if constexpr (get_rank<A> == 1) {
150 return 1;
151 } else {
152 return a.shape()[has_F_layout<A> ? 1 : 0];
153 }
154 }
155
160 template <MemoryArray A>
162
169 template <MemoryArray A>
171
180 template <Array A>
182 auto batch_ptrs(A &&a) { // NOLINT (temporary views are allowed here)
183 using ptr_t = decltype(a.data());
184 auto const idx = (has_C_layout<A> ? 0 : (get_rank<A> == 3 ? 2 : 1));
185 auto const batch_size = a.shape()[idx];
186 auto const stride = a.indexmap().strides()[idx];
187
188 auto ptrs = vector<ptr_t>(batch_size);
189 for (int i = 0; auto &ptr : ptrs) ptr = a.data() + i++ * stride;
190 return ptrs;
191 }
192
205 template <typename A>
206 requires(is_regular_or_view_v<A> and get_rank<A> == 1)
207 void resize_or_check_work_buffer(A &a, long min_size) {
208 if (a.size() >= min_size) {
209 EXPECTS(a.indexmap().min_stride() == 1);
210 return;
211 }
212 if constexpr (is_regular_v<A>) {
213 a.resize(min_size);
214 } else {
215 NDA_RUNTIME_ERROR << "Error in nda::blas_lapack::resize_or_check_work_buffer: Size mismatch: " << a.size() << " < " << min_size;
216 }
217 }
218
225 template <typename A, int R = -1>
227
234 template <typename A, int R = -1>
236
243 template <typename A, int R = -1>
244 concept BlasArrayCplx = BlasArray<A, R> and AnyOf<get_value_t<A>, std::complex<float>, std::complex<double>>;
245
252 template <typename A, int R = -1>
255
264 template <typename A, typename B, int R = -1>
266
275 template <typename A, typename B, int R = -1>
278
286 template <typename A, typename B, int R = -1>
288 and std::is_same_v<get_value_t<A>, int> and mem::have_compatible_addr_space<A, B>;
289
298 template <typename A, typename B, int R = -1>
301
303
304} // 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:244
BLAS/LAPACK compatible array type that has the same value type as the reference array type and a comp...
Definition tools.hpp:265
BLAS/LAPACK compatible array type.
Definition tools.hpp:226
BLAS/LAPACK compatible array or conjugate lazy expression type that has the same value type as the re...
Definition tools.hpp:276
BLAS/LAPACK compatible array or conjugate lazy expression type.
Definition tools.hpp:253
BLAS/LAPACK compatible array type that has a compatible floating-point value type and address space w...
Definition tools.hpp:299
BLAS/LAPACK compatible array type with real value type.
Definition tools.hpp:235
BLAS/LAPACK compatible pivot array type that has a compatible address space with the reference array ...
Definition tools.hpp:287
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:165
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:225
constexpr int get_rank
Constexpr variable that specifies the rank of an nda::Array or of a contiguous 1-dimensional range.
Definition traits.hpp:147
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:212
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:183
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:221
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:68
std::complex< double > dcomplex
Alias for std::complex<double> type.
Definition tools.hpp:34
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:104
static constexpr bool is_conj_array_expr
Constexpr variable that is true if the given type is a conjugate lazy expression.
Definition tools.hpp:47
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:128
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:182
static constexpr bool has_C_layout
Constexpr variable that is true if all given nda::Array types have nda::C_layout.
Definition tools.hpp:89
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:148
void resize_or_check_work_buffer(A &a, long min_size)
Resize or check the size of a 1D array/view.
Definition tools.hpp:207
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:161
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:170
static constexpr bool has_F_layout
Constexpr variable that is true if all given nda::Array types have nda::F_layout.
Definition tools.hpp:79
static constexpr bool have_compatible_addr_space
Constexpr variable that is true if all given types have compatible address spaces.
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.