TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
getrs.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: Miguel Morales, Nils Wentzell
16
17/**
18 * @file
19 * @brief Provides a generic interface to the LAPACK `getrs` routine.
20 */
21
22#pragma once
23
24#include "./interface/cxx_interface.hpp"
25#include "../concepts.hpp"
26#include "../macros.hpp"
27#include "../mem/address_space.hpp"
28#include "../traits.hpp"
29
30#ifndef NDA_HAVE_DEVICE
31#include "../device.hpp"
32#endif // NDA_HAVE_DEVICE
33
34#include <algorithm>
35#include <type_traits>
36
37namespace nda::lapack {
38
39 /**
40 * @ingroup linalg_lapack
41 * @brief Interface to the LAPACK `getrs` routine.
42 *
43 * @details Solves a system of linear equations
44 *
45 * - \f$ \mathbf{A X} = \mathbf{B} \f$,
46 * - \f$ \mathbf{A}^T \mathbf{X} = \mathbf{B} \f$ or
47 * - \f$ \mathbf{A}^H \mathbf{X} = \mathbf{B} \f$
48 *
49 * with a general n-by-n matrix \f$ \mathbf{A} \f$ using the LU factorization computed by `getrf`.
50 *
51 * @tparam A nda::MemoryMatrix type.
52 * @tparam B nda::MemoryMatrix type.
53 * @tparam B nda::MemoryVector type.
54 * @param a Input matrix. The factors \f$ \mathbf{L} \f$ and \f$ \mathbf{U} \f$ from the factorization \f$ \mathbf{A}
55 * = \mathbf{P L U} \f$ as computed by `getrf`.
56 * @param b Input/output matrix. On entry, the right hand side matrix \f$ \mathbf{B} \f$. On exit, the solution matrix
57 * \f$ \mathbf{X} \f$.
58 * @param ipiv Input vector. The pivot indices from `getrf`, i.e. for `1 <= i <= N`, row i of the matrix was
59 * interchanged with row `ipiv(i)`.
60 * @return Integer return code from the LAPACK call.
61 */
62 template <MemoryMatrix A, MemoryMatrix B, MemoryVector IPIV>
63 requires(have_same_value_type_v<A, B> and mem::have_compatible_addr_space<A, B, IPIV> and is_blas_lapack_v<get_value_t<A>>)
64 int getrs(A const &a, B &&b, IPIV const &ipiv) { // NOLINT (temporary views are allowed here)
65 static_assert(std::is_same_v<get_value_t<IPIV>, int>, "Error in nda::lapack::getrs: Pivoting array must have elements of type int");
66 EXPECTS(ipiv.size() >= std::min(a.extent(0), a.extent(1)));
67
68 // must be lapack compatible
69 EXPECTS(a.indexmap().min_stride() == 1);
70 EXPECTS(b.indexmap().min_stride() == 1);
71 EXPECTS(ipiv.indexmap().min_stride() == 1);
72
73 // check for lazy expressions
74 static constexpr bool conj_A = is_conj_array_expr<A>;
75 char op_a = get_op<conj_A, /* transpose = */ has_C_layout<A>>;
76
77 // perform actual library call
78 int info = 0;
79 if constexpr (mem::have_device_compatible_addr_space<A, B, IPIV>) {
80#if defined(NDA_HAVE_DEVICE)
81 device::getrs(op_a, get_ncols(a), get_ncols(b), a.data(), get_ld(a), ipiv.data(), b.data(), get_ld(b), info);
82#else
84#endif
85 } else {
86 f77::getrs(op_a, get_ncols(a), get_ncols(b), a.data(), get_ld(a), ipiv.data(), b.data(), get_ld(b), info);
87 }
88 return info;
89 }
90
91} // namespace nda::lapack
int getrs(A const &a, B &&b, IPIV const &ipiv)
Interface to the LAPACK getrs routine.
Definition getrs.hpp:64
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