<T>LAPACK 0.1.1
C++ Template Linear Algebra PACKage
Loading...
Searching...
No Matches
gebrd.hpp
Go to the documentation of this file.
1
5//
6// Copyright (c) 2021-2023, University of Colorado Denver. All rights reserved.
7//
8// This file is part of <T>LAPACK.
9// <T>LAPACK is free software: you can redistribute it and/or modify it under
10// the terms of the BSD 3-Clause license. See the accompanying LICENSE file.
11
12#ifndef TLAPACK_GEBRD_HH
13#define TLAPACK_GEBRD_HH
14
16#include "tlapack/blas/gemm.hpp"
18
19namespace tlapack {
20
24struct GebrdOpts {
25 size_t nb = 32;
26};
27
46template <class T, TLAPACK_SMATRIX matrix_t, TLAPACK_SVECTOR vector_t>
48 const vector_t& tauq,
49 const vector_t& taup,
50 const GebrdOpts& opts = {})
51{
52 using idx_t = size_type<matrix_t>;
53 using work_t = matrix_type<matrix_t, vector_t>;
54
55 const idx_t m = nrows(A);
56 const idx_t n = ncols(A);
57 const idx_t nb = min((idx_t)opts.nb, min(m, n));
58
59 if constexpr (is_same_v<T, type_t<work_t>>)
60 return WorkInfo(m + n, nb);
61 else
62 return WorkInfo(0);
63}
64
73template <TLAPACK_SMATRIX matrix_t,
74 TLAPACK_SVECTOR vector_t,
75 TLAPACK_WORKSPACE work_t>
79 work_t& work,
80 const GebrdOpts& opts = {})
81{
82 using idx_t = size_type<matrix_t>;
83 using range = pair<idx_t, idx_t>;
84 using TA = type_t<matrix_t>;
85 using real_t = real_type<TA>;
86
87 // constants
88 const real_t one(1);
89 const type_t<work_t> zero(0);
90 const idx_t m = nrows(A);
91 const idx_t n = ncols(A);
92 const idx_t k = min(m, n);
93 const idx_t nb = min((idx_t)opts.nb, k);
94
95 // Matrices X and Y
96 auto [X, work2] = reshape(work, m, nb);
97 auto [Y, work3] = reshape(work2, n, nb);
98 laset(GENERAL, zero, zero, X);
99 laset(GENERAL, zero, zero, Y);
100
101 for (idx_t i = 0; i < k; i = i + nb) {
102 idx_t ib = min(nb, k - i);
103 // Reduce rows and columns i:i+ib-1 to bidiagonal form and return
104 // the matrices X and Y which are needed to update the unreduced
105 // part of the matrix
106 auto A2 = slice(A, range{i, m}, range{i, n});
107 auto tauq = slice(tauv, range{i, i + ib});
108 auto taup = slice(tauw, range{i, i + ib});
109 auto X2 = slice(X, range{i, m}, range{0, ib});
110 auto Y2 = slice(Y, range{i, n}, range{0, ib});
111 labrd(A2, tauq, taup, X2, Y2);
112
113 //
114 // Update the trailing submatrix A(i+nb:m,i+nb:n), using an update
115 // of the form A := A - V*Y**H - X*U**H
116 //
117 if (i + ib < m && i + ib < n) {
118 real_t e;
119 auto A3 = slice(A, range{i + ib, m}, range{i + ib, n});
120
121 if (m >= n) {
122 e = real(A(i + ib - 1, i + ib));
123 A(i + ib - 1, i + ib) = one;
124 }
125 else {
126 e = real(A(i + ib, i + ib - 1));
127 A(i + ib, i + ib - 1) = one;
128 }
129
130 auto V = slice(A, range{i + ib, m}, range{i, i + ib});
131 auto Y3 = slice(Y, range{i + ib, n}, range{0, ib});
132 gemm(NO_TRANS, CONJ_TRANS, -one, V, Y3, one, A3);
133 auto U = slice(A, range{i, i + ib}, range{i + ib, n});
134 auto X3 = slice(X, range{i + ib, m}, range{0, ib});
135 gemm(NO_TRANS, NO_TRANS, -one, X3, U, one, A3);
136
137 if (m >= n)
138 A(i + ib - 1, i + ib) = e;
139 else
140 A(i + ib, i + ib - 1) = e;
141 }
142 }
143
144 return 0;
145}
146
201template <TLAPACK_SMATRIX matrix_t, TLAPACK_SVECTOR vector_t>
203 vector_t& tauv,
204 vector_t& tauw,
205 const GebrdOpts& opts = {})
206{
207 using work_t = matrix_type<matrix_t, vector_t>;
208 using T = type_t<work_t>;
209
210 // Functor
211 Create<work_t> new_matrix;
212
213 // Allocates workspace
214 WorkInfo workinfo = gebrd_worksize<T>(A, tauv, tauw, opts);
215 std::vector<T> work_;
216 auto work = new_matrix(work_, workinfo.m, workinfo.n);
217
218 return gebrd_work(A, tauv, tauw, work, opts);
219}
220
221} // namespace tlapack
222
223#endif // TLAPACK_GEBRD_HH
constexpr real_type< T > real(const T &x) noexcept
Extends std::real() to real datatypes.
Definition utils.hpp:71
#define TLAPACK_SVECTOR
Macro for tlapack::concepts::SliceableVector compatible with C++17.
Definition concepts.hpp:909
#define TLAPACK_SMATRIX
Macro for tlapack::concepts::SliceableMatrix compatible with C++17.
Definition concepts.hpp:899
#define TLAPACK_WORKSPACE
Macro for tlapack::concepts::Workspace compatible with C++17.
Definition concepts.hpp:912
int gebrd(matrix_t &A, vector_t &tauv, vector_t &tauw, const GebrdOpts &opts={})
Reduces a general m by n matrix A to an upper real bidiagonal form B by a unitary transformation:
Definition gebrd.hpp:202
void laset(uplo_t uplo, const type_t< matrix_t > &alpha, const type_t< matrix_t > &beta, matrix_t &A)
Initializes a matrix to diagonal and off-diagonal values.
Definition laset.hpp:38
int labrd(A_t &A, vector_t &tauq, vector_t &taup, X_t &X, matrixY_t &Y)
Reduces the first nb rows and columns of a general m by n matrix A to upper or lower bidiagonal form ...
Definition labrd.hpp:55
void gemm(Op transA, Op transB, const alpha_t &alpha, const matrixA_t &A, const matrixB_t &B, const beta_t &beta, matrixC_t &C)
General matrix-matrix multiply:
Definition gemm.hpp:61
int gebrd_work(matrix_t &A, vector_t &tauv, vector_t &tauw, work_t &work, const GebrdOpts &opts={})
Reduces a general m by n matrix A to an upper real bidiagonal form B by a unitary transformation: W...
Definition gebrd.hpp:76
constexpr WorkInfo gebrd_worksize(const matrix_t &A, const vector_t &tauq, const vector_t &taup, const GebrdOpts &opts={})
Worspace query of gebrd()
Definition gebrd.hpp:47
typename traits::real_type_traits< Types..., int >::type real_type
The common real type of the list of types.
Definition scalar_type_traits.hpp:113
Options struct for gebrd()
Definition gebrd.hpp:24
size_t nb
Block size used in the blocked reduction.
Definition gebrd.hpp:25
Output information in the workspace query.
Definition workspace.hpp:16