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-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: Thomas Hahn, Miguel Morales, Olivier Parcollet, Nils Wentzell
16
17/**
18 * @file
19 * @brief Provides a generic interface to the LAPACK `gesvd` routine.
20 */
21
22#pragma once
23
24#include "./interface/cxx_interface.hpp"
25#include "../concepts.hpp"
26#include "../declarations.hpp"
27#include "../exceptions.hpp"
28#include "../layout/policies.hpp"
29#include "../macros.hpp"
30#include "../mem/address_space.hpp"
31#include "../mem/policies.hpp"
32#include "../traits.hpp"
33
34#ifndef NDA_HAVE_DEVICE
35#include "../device.hpp"
36#endif // NDA_HAVE_DEVICE
37
38#include <algorithm>
39#include <cmath>
40#include <complex>
41#include <utility>
42
43namespace nda::lapack {
44
45 /**
46 * @ingroup linalg_lapack
47 * @brief Interface to the LAPACK `gesvd` routine.
48 *
49 * @details Computes the singular value decomposition (SVD) of a complex m-by-n matrix \f$ \mathbf{A} \f$, optionally
50 * computing the left and/or right singular vectors. The SVD is written
51 * \f[
52 * \mathbf{A} = \mathbf{U} \mathbf{S} \mathbf{V}^H
53 * \f]
54 * where \f$ \mathbf{S} \f$ is an m-by-n matrix which is zero except for its `min(m,n)` diagonal elements,
55 * \f$ \mathbf{U} \f$ is an m-by-m unitary matrix, and \f$ \mathbf{V} \f$ is an n-by-n unitary matrix. The diagonal
56 * elements of \f$ \mathbf{S} \f$ are the singular values of \f$ \mathbf{A} \f$; they are real and non-negative, and
57 * are returned in descending order. The first `min(m,n)` columns of \f$ \mathbf{U} \f$ and \f$ \mathbf{V} \f$ are the
58 * left and right singular vectors of \f$ \mathbf{A} \f$.
59 *
60 * Note that the routine returns \f$ \mathbf{V}^H \f$, not \f$ \mathbf{V} \f$.
61 *
62 * @tparam A nda::MemoryMatrix type.
63 * @tparam S nda::MemoryVector type.
64 * @tparam U nda::MemoryMatrix type.
65 * @tparam VT nda::MemoryMatrix type.
66 * @param a Input/output matrix. On entry, the m-by-n matrix \f$ \mathbf{A} \f$. On exit, the contents of
67 * \f$ \mathbf{A} \f$ are destroyed.
68 * @param s Output vector. The singular values of \f$ \mathbf{A} \f$, sorted so that `s(i) >= s(i+1)`.
69 * @param u Output matrix. It contains the m-by-m unitary matrix \f$ \mathbf{U} \f$.
70 * @param vt Output matrix. It contains contains the n-by-n unitary matrix \f$ \mathbf{V}^H \f$.
71 * @return Integer return code from the LAPACK call.
72 */
73 template <MemoryMatrix A, MemoryVector S, MemoryMatrix U, MemoryMatrix VT>
74 requires(have_same_value_type_v<A, U, VT> and mem::have_compatible_addr_space<A, S, U, VT> and is_blas_lapack_v<get_value_t<A>>)
75 int gesvd(A &&a, S &&s, U &&u, VT &&vt) { // NOLINT (temporary views are allowed here)
76 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");
77
78 auto dm = std::min(a.extent(0), a.extent(1));
79 if (s.size() < dm) s.resize(dm);
80
81 // must be lapack compatible
82 EXPECTS(a.indexmap().min_stride() == 1);
83 EXPECTS(s.indexmap().min_stride() == 1);
84 EXPECTS(u.indexmap().min_stride() == 1);
85 EXPECTS(vt.indexmap().min_stride() == 1);
86
87 // call host/device implementation depending on input type
88 auto gesvd_call = []<typename... Ts>(Ts &&...args) {
89 if constexpr (mem::have_device_compatible_addr_space<A, S, U, VT>) {
90#if defined(NDA_HAVE_DEVICE)
91 lapack::device::gesvd(std::forward<Ts>(args)...);
92#else
94#endif
95 } else {
96 lapack::f77::gesvd(std::forward<Ts>(args)...);
97 }
98 };
99
100 // first call to get the optimal buffersize
101 using value_type = get_value_t<A>;
102 value_type bufferSize_T{};
103 auto rwork = array<double, 1, C_layout, heap<mem::get_addr_space<A>>>(5 * dm);
104 int info = 0;
105 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,
106 rwork.data(), info);
107 int bufferSize = static_cast<int>(std::ceil(std::real(bufferSize_T)));
108
109 // allocate work buffer and perform actual library call
110 nda::array<value_type, 1, C_layout, heap<mem::get_addr_space<A>>> work(bufferSize);
111 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,
112 rwork.data(), info);
113
114 if (info) NDA_RUNTIME_ERROR << "Error in nda::lapack::gesvd: info = " << info;
115 return info;
116 }
117
118} // namespace nda::lapack
#define NDA_RUNTIME_ERROR
int gesvd(A &&a, S &&s, U &&u, VT &&vt)
Interface to the LAPACK gesvd routine.
Definition gesvd.hpp:75
void compile_error_no_gpu()
Trigger a compilation error in case GPU specific functionality is used without configuring the projec...
Definition device.hpp:47
#define EXPECTS(X)
Definition macros.hpp:59
Contiguous layout policy with C-order (row-major order).
Definition policies.hpp:47