TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
getri.hpp
Go to the documentation of this file.
1// Copyright (c) 2021-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, Nils Wentzell
16
17/**
18 * @file
19 * @brief Provides a generic interface to the LAPACK `getri` 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 "../macros.hpp"
29#include "../mem/address_space.hpp"
30#include "../traits.hpp"
31
32#include <algorithm>
33#include <cmath>
34#include <complex>
35#include <type_traits>
36
37namespace nda::lapack {
38
39 /**
40 * @ingroup linalg_lapack
41 * @brief Interface to the LAPACK `getri` routine.
42 *
43 * @details Computes the inverse of a matrix using the LU factorization computed by `getrf`.
44 *
45 * This method inverts \f$ \mathbf{U} \f$ and then computes \f$ \mathrm{inv}(\mathbf{A}) \f$ by solving the system
46 * \f$ \mathrm{inv}(\mathbf{A}) L = \mathrm{inv}(\mathbf{U}) \f$ for \f$ \mathrm{inv}(\mathbf{A}) \f$.
47 *
48 * @tparam A nda::MemoryMatrix type.
49 * @tparam IPIV nda::MemoryVector type.
50 * @param a Input/output matrix. On entry, the factors \f$ \mathbf{L} \f$ and \f$ \mathbf{U} \f$ from the
51 * factorization \f$ \mathbf{A} = \mathbf{P L U} \f$ as computed by `getrf`. On exit, if `INFO == 0`, the inverse of
52 * the original matrix \f$ \mathbf{A} \f$.
53 * @param ipiv Input vector. The pivot indices from `getrf`, i.e. for `1 <= i <= N`, row i of the matrix was
54 * interchanged with row `ipiv(i)`.
55 * @return Integer return code from the LAPACK call.
56 */
57 template <MemoryMatrix A, MemoryVector IPIV>
58 requires(mem::have_compatible_addr_space<A, IPIV> and is_blas_lapack_v<get_value_t<A>>)
59 int getri(A &&a, IPIV const &ipiv) { // NOLINT (temporary views are allowed here)
60 static_assert(std::is_same_v<get_value_t<IPIV>, int>, "Error in nda::lapack::getri: Pivoting array must have elements of type int");
61 auto dm = std::min(a.extent(0), a.extent(1));
62
63 if (ipiv.size() < dm)
64 NDA_RUNTIME_ERROR << "Error in nda::lapack::getri: Pivot index array size " << ipiv.size() << " smaller than required size " << dm;
65
66 // must be lapack compatible
67 EXPECTS(a.indexmap().min_stride() == 1);
68 EXPECTS(ipiv.indexmap().min_stride() == 1);
69
70 int info = 0;
71 if constexpr (mem::have_device_compatible_addr_space<A, IPIV>) {
72#if defined(NDA_HAVE_DEVICE)
73 device::getri(a.extent(0), a.data(), get_ld(a), ipiv.data(), NULL, 0, info);
74#else
76#endif
77 } else {
78 // first call to get the optimal buffersize
79 using value_type = get_value_t<A>;
80 value_type bufferSize_T{};
81 f77::getri(a.extent(0), a.data(), get_ld(a), ipiv.data(), &bufferSize_T, -1, info);
82 int bufferSize = static_cast<int>(std::ceil(std::real(bufferSize_T)));
83
84 // allocate work buffer and perform actual library call
85 array<value_type, 1> work(bufferSize);
86#if defined(__has_feature)
87#if __has_feature(memory_sanitizer)
88 work = 0;
89#endif
90#endif
91 f77::getri(a.extent(0), a.data(), get_ld(a), ipiv.data(), work.data(), bufferSize, info);
92 }
93 return info;
94 }
95
96} // namespace nda::lapack
#define NDA_RUNTIME_ERROR
int getri(A &&a, IPIV const &ipiv)
Interface to the LAPACK getri routine.
Definition getri.hpp:59
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