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
22#pragma once
23
25#include "../concepts.hpp"
26#include "../declarations.hpp"
27#include "../exceptions.hpp"
29#include "../macros.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
73 template <MemoryMatrix A, MemoryVector S, MemoryMatrix U, MemoryMatrix VT>
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) {
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{};
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
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
Provides definitions and type traits involving the different memory address spaces supported by nda.
A generic multi-dimensional array.
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.
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
int gesvd(A &&a, S &&s, U &&u, VT &&vt)
Interface to the LAPACK gesvd routine.
Definition gesvd.hpp:75
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:47
constexpr bool is_blas_lapack_v
Alias for nda::is_double_or_complex_v.
Definition traits.hpp:102
Provides a C++ interface for various LAPACK routines.
Provides definitions of various layout policies.
Macros used in the nda library.
Defines various memory handling policies.
Provides type traits for the nda library.