TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
ger.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: Miguel Morales, Olivier Parcollet, Nils Wentzell
16
22#pragma once
23
25#include "./tools.hpp"
27#include "../concepts.hpp"
28#include "../exceptions.hpp"
30#include "../macros.hpp"
32#include "../stdutil/array.hpp"
33#include "../traits.hpp"
34
35#ifndef NDA_HAVE_DEVICE
36#include "../device.hpp"
37#endif
38
39#include <array>
40
41namespace nda::blas {
42
66 template <MemoryVector X, MemoryVector Y, MemoryMatrix M>
68 void ger(get_value_t<X> alpha, X const &x, Y const &y, M &&m) { // NOLINT (temporary views are allowed here)
69 EXPECTS(m.extent(0) == x.extent(0));
70 EXPECTS(m.extent(1) == y.extent(0));
71
72 // must be lapack compatible
73 EXPECTS(m.indexmap().min_stride() == 1);
74
75 // if in C, we need to call fortran with transposed matrix
76 if (has_C_layout<M>) {
77 ger(alpha, y, x, transpose(m));
78 return;
79 }
80
82#if defined(NDA_HAVE_DEVICE)
83 device::ger(m.extent(0), m.extent(1), alpha, x.data(), x.indexmap().strides()[0], y.data(), y.indexmap().strides()[0], m.data(), get_ld(m));
84#else
86#endif
87 } else {
88 f77::ger(m.extent(0), m.extent(1), alpha, x.data(), x.indexmap().strides()[0], y.data(), y.indexmap().strides()[0], m.data(), get_ld(m));
89 }
90 }
91
110 template <ArrayOrScalar A, ArrayOrScalar B>
111 auto outer_product(A const &a, B const &b) {
112 if constexpr (Scalar<A> or Scalar<B>) {
113 return a * b;
114 } else {
115 if (not a.is_contiguous()) NDA_RUNTIME_ERROR << "Error in nda::blas::outer_product: First argument has non-contiguous layout";
116 if (not b.is_contiguous()) NDA_RUNTIME_ERROR << "Error in nda::blas::outer_product: Second argument has non-contiguous layout";
117
118 // use BLAS ger to calculate the outer product
119 auto res = zeros<get_value_t<A>, mem::common_addr_space<A, B>>(stdutil::join(a.shape(), b.shape()));
120 auto a_vec = reshape(a, std::array{a.size()});
121 auto b_vec = reshape(b, std::array{b.size()});
122 auto mat = reshape(res, std::array{a.size(), b.size()});
123 ger(1.0, a_vec, b_vec, mat);
124
125 return res;
126 }
127 }
128
131} // namespace nda::blas
Provides definitions and type traits involving the different memory address spaces supported by nda.
Provides utility functions for std::array.
Provides basic functions to create and manipulate arrays and views.
Provides a C++ interface for various BLAS routines.
Check if a given type is either an arithmetic or complex type.
Definition concepts.hpp:119
Provides concepts for the nda library.
Provides GPU and non-GPU specific functionality.
Provides a custom runtime error class and macros to assert conditions and throw exceptions.
auto zeros(std::array< Int, Rank > const &shape)
Make an array of the given shape on the given address space and zero-initialize it.
auto transpose(A &&a)
Transpose the memory layout of an nda::MemoryArray or an nda::expr_call.
auto reshape(A &&a, std::array< Int, R > const &new_shape)
Reshape an nda::basic_array or nda::basic_array_view.
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:196
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
auto outer_product(A const &a, B const &b)
Calculate the outer product of two contiguous arrays/views/scalars.
Definition ger.hpp:111
void ger(get_value_t< X > alpha, X const &x, Y const &y, M &&m)
Interface to the BLAS ger routine.
Definition ger.hpp:68
static constexpr bool have_compatible_addr_space
Constexpr variable that is true if all given types have compatible address spaces.
static constexpr bool have_device_compatible_addr_space
Constexpr variable that is true if all given types have an address space compatible with Device.
constexpr AddressSpace common_addr_space
Get common address space for a number of given nda::MemoryArray types.
void compile_error_no_gpu()
Trigger a compilation error in case GPU specific functionality is used without configuring the projec...
Definition device.hpp:47
constexpr std::array< T, R1+R2 > join(std::array< T, R1 > const &a1, std::array< T, R2 > const &a2)
Make a new std::array by joining two existing std::array objects.
Definition array.hpp:309
constexpr bool is_blas_lapack_v
Alias for nda::is_double_or_complex_v.
Definition traits.hpp:102
Provides functions to transform the memory layout of an nda::basic_array or nda::basic_array_view.
Macros used in the nda library.
Provides various traits and utilities for the BLAS interface.
Provides type traits for the nda library.