TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
dot.hpp
Go to the documentation of this file.
1// Copyright (c) 2022--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 "../blas/dot.hpp"
14#include "../declarations.hpp"
17#include "../mem/policies.hpp"
18
19#include <type_traits>
20
21namespace nda {
22
27
42 template <typename X, typename Y>
43 auto dot(X &&x, Y &&y) { // NOLINT (temporary views are allowed here)
44 // check address space compatibility
45 static constexpr auto L_adr_spc = mem::get_addr_space<X>;
46 static constexpr auto R_adr_spc = mem::get_addr_space<Y>;
47 static_assert(L_adr_spc != mem::None);
48 static_assert(R_adr_spc != mem::None);
49
50 // get resulting value type and vector type
51 using value_t = decltype(get_value_t<X>{} * get_value_t<Y>{});
52 using vector_t = basic_array<value_t, 1, C_layout, 'V', nda::heap<mem::combine<L_adr_spc, R_adr_spc>>>;
53
54 if constexpr (is_blas_lapack_v<value_t>) {
55 // for double value types we use blas::dot
56 // lambda to form a new vector with the correct value type if necessary
57 auto as_container = []<typename A>(A const &a) -> decltype(auto) {
58 if constexpr (is_regular_or_view_v<A> and std::is_same_v<get_value_t<A>, value_t>)
59 return a;
60 else
61 return vector_t{a};
62 };
63
64 return blas::dot(as_container(x), as_container(y));
65 } else {
66 // for other value types we use a generic implementation
67 return blas::dot_generic(x, y);
68 }
69 }
70
85 template <typename X, typename Y>
86 auto dotc(X &&x, Y &&y) { // NOLINT (temporary views are allowed here)
87 // check address space compatibility
88 static constexpr auto L_adr_spc = mem::get_addr_space<X>;
89 static constexpr auto R_adr_spc = mem::get_addr_space<Y>;
90 static_assert(L_adr_spc != mem::None);
91 static_assert(R_adr_spc != mem::None);
92
93 // get resulting value type and vector type
94 using value_t = decltype(get_value_t<X>{} * get_value_t<Y>{});
95 using vector_t = basic_array<value_t, 1, C_layout, 'V', nda::heap<mem::combine<L_adr_spc, R_adr_spc>>>;
96
97 if constexpr (is_blas_lapack_v<value_t>) {
98 // for double or complex value types we use blas::dotc
99 // lambda to form a new vector with the correct value type if necessary
100 auto as_container = []<typename A>(A const &a) -> decltype(auto) {
101 if constexpr (is_regular_or_view_v<A> and std::is_same_v<get_value_t<A>, value_t>)
102 return a;
103 else
104 return vector_t{a};
105 };
106
107 return blas::dotc(as_container(x), as_container(y));
108 } else {
109 // for other value types we use a generic implementation
110 return blas::dotc_generic(x, y);
111 }
112 }
113
115
116} // namespace nda
Provides definitions and type traits involving the different memory address spaces supported by nda.
Provides a generic interface to the BLAS dot routine.
A generic multi-dimensional array.
Provides various convenient aliases and helper functions for nda::basic_array and nda::basic_array_vi...
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:181
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:152
auto dot_generic(X const &x, Y const &y)
Generic implementation of nda::blas::dot for types not supported by BLAS/LAPACK.
Definition dot.hpp:165
auto dotc(X const &x, Y const &y)
Interface to the BLAS dotc routine.
Definition dot.hpp:92
auto dotc_generic(X const &x, Y const &y)
Generic implementation of nda::blas::dotc for types not supported by BLAS/LAPACK.
Definition dot.hpp:183
auto dot(X const &x, Y const &y)
Interface to the BLAS dot routine.
Definition dot.hpp:50
auto dotc(X &&x, Y &&y)
Compute the dot product of two complex arrays/views.
Definition dot.hpp:86
auto dot(X &&x, Y &&y)
Compute the dot product of two real arrays/views.
Definition dot.hpp:43
static constexpr AddressSpace get_addr_space
Variable template providing the address space for different types.
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
Alias for nda::is_double_or_complex_v.
Definition traits.hpp:91
Provides definitions of various layout policies.
Defines various memory handling policies.
Contiguous layout policy with C-order (row-major order).
Definition policies.hpp:36