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--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
14#include "./tools.hpp"
16#include "../concepts.hpp"
17#include "../exceptions.hpp"
19#include "../macros.hpp"
21#include "../stdutil/array.hpp"
22#include "../traits.hpp"
23
24#ifndef NDA_HAVE_DEVICE
25#include "../device.hpp"
26#endif
27
28#include <array>
29
30namespace nda::blas {
31
36
55 template <MemoryVector X, MemoryVector Y, MemoryMatrix M>
57 void ger(get_value_t<X> alpha, X const &x, Y const &y, M &&m) { // NOLINT (temporary views are allowed here)
58 EXPECTS(m.extent(0) == x.extent(0));
59 EXPECTS(m.extent(1) == y.extent(0));
60
61 // must be lapack compatible
62 EXPECTS(m.indexmap().min_stride() == 1);
63
64 // if in C, we need to call fortran with transposed matrix
65 if (has_C_layout<M>) {
66 ger(alpha, y, x, transpose(m));
67 return;
68 }
69
71#if defined(NDA_HAVE_DEVICE)
72 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));
73#else
75#endif
76 } else {
77 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));
78 }
79 }
80
99 template <ArrayOrScalar A, ArrayOrScalar B>
100 auto outer_product(A const &a, B const &b) {
101 if constexpr (Scalar<A> or Scalar<B>) {
102 return a * b;
103 } else {
104 if (not a.is_contiguous()) NDA_RUNTIME_ERROR << "Error in nda::blas::outer_product: First argument has non-contiguous layout";
105 if (not b.is_contiguous()) NDA_RUNTIME_ERROR << "Error in nda::blas::outer_product: Second argument has non-contiguous layout";
106
107 // use BLAS ger to calculate the outer product
108 auto res = zeros<get_value_t<A>, mem::common_addr_space<A, B>>(stdutil::join(a.shape(), b.shape()));
109 auto a_vec = reshape(a, std::array{a.size()});
110 auto b_vec = reshape(b, std::array{b.size()});
111 auto mat = reshape(res, std::array{a.size(), b.size()});
112 ger(1.0, a_vec, b_vec, mat);
113
114 return res;
115 }
116 }
117
119
120} // 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:108
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:185
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
int get_ld(A const &a)
Get the leading dimension in LAPACK jargon of an nda::MemoryMatrix.
Definition tools.hpp:98
static constexpr bool has_C_layout
Constexpr variable that is true if the given nda::Array type has a C memory layout.
Definition tools.hpp:65
auto outer_product(A const &a, B const &b)
Calculate the outer product of two contiguous arrays/views/scalars.
Definition ger.hpp:100
void ger(get_value_t< X > alpha, X const &x, Y const &y, M &&m)
Interface to the BLAS ger routine.
Definition ger.hpp:57
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:36
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:299
constexpr bool is_blas_lapack_v
Alias for nda::is_double_or_complex_v.
Definition traits.hpp:91
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.