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-2024 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: Thomas Hahn, Nils Wentzell
16
22#pragma once
23
24#include "../blas/dot.hpp"
25#include "../declarations.hpp"
28#include "../mem/policies.hpp"
29
30#include <type_traits>
31
32namespace nda {
33
53 template <typename X, typename Y>
54 auto dot(X &&x, Y &&y) { // NOLINT (temporary views are allowed here)
55 // check address space compatibility
56 static constexpr auto L_adr_spc = mem::get_addr_space<X>;
57 static constexpr auto R_adr_spc = mem::get_addr_space<Y>;
58 static_assert(L_adr_spc != mem::None);
59 static_assert(R_adr_spc != mem::None);
60
61 // get resulting value type and vector type
62 using value_t = decltype(get_value_t<X>{} * get_value_t<Y>{});
63 using vector_t = basic_array<value_t, 1, C_layout, 'V', nda::heap<mem::combine<L_adr_spc, R_adr_spc>>>;
64
65 if constexpr (is_blas_lapack_v<value_t>) {
66 // for double value types we use blas::dot
67 // lambda to form a new vector with the correct value type if necessary
68 auto as_container = []<typename A>(A const &a) -> decltype(auto) {
69 if constexpr (is_regular_or_view_v<A> and std::is_same_v<get_value_t<A>, value_t>)
70 return a;
71 else
72 return vector_t{a};
73 };
74
75 return blas::dot(as_container(x), as_container(y));
76 } else {
77 // for other value types we use a generic implementation
78 return blas::dot_generic(x, y);
79 }
80 }
81
96 template <typename X, typename Y>
97 auto dotc(X &&x, Y &&y) { // NOLINT (temporary views are allowed here)
98 // check address space compatibility
99 static constexpr auto L_adr_spc = mem::get_addr_space<X>;
100 static constexpr auto R_adr_spc = mem::get_addr_space<Y>;
101 static_assert(L_adr_spc != mem::None);
102 static_assert(R_adr_spc != mem::None);
103
104 // get resulting value type and vector type
105 using value_t = decltype(get_value_t<X>{} * get_value_t<Y>{});
106 using vector_t = basic_array<value_t, 1, C_layout, 'V', nda::heap<mem::combine<L_adr_spc, R_adr_spc>>>;
107
108 if constexpr (is_blas_lapack_v<value_t>) {
109 // for double or complex value types we use blas::dotc
110 // lambda to form a new vector with the correct value type if necessary
111 auto as_container = []<typename A>(A const &a) -> decltype(auto) {
112 if constexpr (is_regular_or_view_v<A> and std::is_same_v<get_value_t<A>, value_t>)
113 return a;
114 else
115 return vector_t{a};
116 };
117
118 return blas::dotc(as_container(x), as_container(y));
119 } else {
120 // for other value types we use a generic implementation
121 return blas::dotc_generic(x, y);
122 }
123 }
124
127} // 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:192
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:163
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:176
auto dotc(X const &x, Y const &y)
Interface to the BLAS dotc routine.
Definition dot.hpp:103
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:194
auto dot(X const &x, Y const &y)
Interface to the BLAS dot routine.
Definition dot.hpp:61
auto dotc(X &&x, Y &&y)
Compute the dot product of two complex arrays/views.
Definition dot.hpp:97
auto dot(X &&x, Y &&y)
Compute the dot product of two real arrays/views.
Definition dot.hpp:54
static constexpr AddressSpace get_addr_space
Variable template providing the address space for different types.
constexpr bool is_blas_lapack_v
Alias for nda::is_double_or_complex_v.
Definition traits.hpp:102
Provides definitions of various layout policies.
Defines various memory handling policies.
Contiguous layout policy with C-order (row-major order).
Definition policies.hpp:47
Memory policy using an nda::mem::handle_heap.
Definition policies.hpp:44