TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
gesvd.hpp
Go to the documentation of this file.
1// Copyright (c) 2020--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 "../concepts.hpp"
15#include "../declarations.hpp"
16#include "../exceptions.hpp"
18#include "../macros.hpp"
20#include "../mem/policies.hpp"
21#include "../traits.hpp"
22
23#ifndef NDA_HAVE_DEVICE
24#include "../device.hpp"
25#endif // NDA_HAVE_DEVICE
26
27#include <algorithm>
28#include <cmath>
29#include <complex>
30#include <utility>
31
32namespace nda::lapack {
33
62 template <MemoryMatrix A, MemoryVector S, MemoryMatrix U, MemoryMatrix VT>
64 int gesvd(A &&a, S &&s, U &&u, VT &&vt) { // NOLINT (temporary views are allowed here)
65 static_assert(has_F_layout<A> and has_F_layout<U> and has_F_layout<VT>, "Error in nda::lapack::gesvd: C order not supported");
66
67 auto dm = std::min(a.extent(0), a.extent(1));
68 if (s.size() < dm) s.resize(dm);
69
70 // must be lapack compatible
71 EXPECTS(a.indexmap().min_stride() == 1);
72 EXPECTS(s.indexmap().min_stride() == 1);
73 EXPECTS(u.indexmap().min_stride() == 1);
74 EXPECTS(vt.indexmap().min_stride() == 1);
75
76 // call host/device implementation depending on input type
77 auto gesvd_call = []<typename... Ts>(Ts &&...args) {
79#if defined(NDA_HAVE_DEVICE)
80 lapack::device::gesvd(std::forward<Ts>(args)...);
81#else
83#endif
84 } else {
85 lapack::f77::gesvd(std::forward<Ts>(args)...);
86 }
87 };
88
89 // first call to get the optimal buffersize
90 using value_type = get_value_t<A>;
91 value_type bufferSize_T{};
93 int info = 0;
94 gesvd_call('A', 'A', a.extent(0), a.extent(1), a.data(), get_ld(a), s.data(), u.data(), get_ld(u), vt.data(), get_ld(vt), &bufferSize_T, -1,
95 rwork.data(), info);
96 int bufferSize = static_cast<int>(std::ceil(std::real(bufferSize_T)));
97
98 // allocate work buffer and perform actual library call
100 gesvd_call('A', 'A', a.extent(0), a.extent(1), a.data(), get_ld(a), s.data(), u.data(), get_ld(u), vt.data(), get_ld(vt), work.data(), bufferSize,
101 rwork.data(), info);
102
103 if (info) NDA_RUNTIME_ERROR << "Error in nda::lapack::gesvd: info = " << info;
104 return info;
105 }
106
107} // namespace nda::lapack
Provides definitions and type traits involving the different memory address spaces supported by nda.
ValueType const * data() const noexcept
Get a pointer to the actual data (in general this is not the beginning of the memory block for a view...
Provides concepts for the nda library.
Provides various convenient aliases and helper functions for nda::basic_array and nda::basic_array_vi...
Provides GPU and non-GPU specific functionality.
Provides a custom runtime error class and macros to assert conditions and throw exceptions.
basic_array< ValueType, Rank, Layout, 'A', ContainerPolicy > array
Alias template of an nda::basic_array with an 'A' algebra.
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 gesvd(A &&a, S &&s, U &&u, VT &&vt)
Interface to the LAPACK gesvd routine.
Definition gesvd.hpp:64
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.
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 bool is_blas_lapack_v
Alias for nda::is_double_or_complex_v.
Definition traits.hpp:91
Provides a C++ interface for various LAPACK routines.
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_F_layout
Constexpr variable that is true if the given nda::Array type has a Fortran memory layout.
Definition tools.hpp:55
Provides definitions of various layout policies.
Macros used in the nda library.
Defines various memory handling policies.
Provides type traits for the nda library.