KokkosBatched::Trmv¶
Defined in header: KokkosBatched_Trmv.hpp
template <typename ArgUplo, typename ArgTrans, typename ArgDiag>
struct SerialTrmv {
template <typename ScalarType, typename AViewType, typename XViewType>
KOKKOS_INLINE_FUNCTION static int invoke(const ScalarType alpha, const AViewType &A, const XViewType &x);
};
Performs the matrix-vector operations \(A\) by vector \(x\) with scaling factor \(alpha\)
This operation is equivalent to the BLAS routine STRMV (CTRMV) or DTRMV (ZTRMV) for single or double precision for real (complex) triangular matrix.
Parameters¶
- alpha:
Scalar multiplier for \(x\) (usually set to one).
- A:
\(A\) is an n by n upper or lower triangular matrix.
- x:
On entry, \(x\) is a length n vector. On exit, \(x\) contains the result of the matrix-vector operation.
Type Requirements¶
ArgUplomust be one of the following:KokkosBatched::Uplo::Upperfor upper triangular solveKokkosBatched::Uplo::Lowerfor lower triangular solve
ArgTransmust be one of the following:KokkosBatched::Trans::NoTransposeto perform \(x = \alpha A * x\)KokkosBatched::Trans::Transposeto perform \(x = \alpha A^T * x\)KokkosBatched::Trans::ConjTransposeto perform \(x = \alpha A^H * x\)
ArgDiagmust be one of the following:KokkosBatched::Diag::Unitfor the unit triangular matrix \(A\)KokkosBatched::Diag::NonUnitfor the non-unit triangular matrix \(A\)
ScalarTypemust be a built-in arithmetic type likefloat,double,Kokkos::complex<float>, orKokkos::complex<double>.AViewTypemust be a Kokkos View of rank 2 containing the triangular matrix AxViewTypemust be a Kokkos View of rank 1 containing the right-hand side that satisfies -std::is_same_v<typename xViewType::value_type, typename xViewType::non_const_value_type> == true
Example¶
1#include <iostream>
2#include <Kokkos_Core.hpp>
3#include <KokkosBatched_Trmv.hpp>
4
5using ExecutionSpace = Kokkos::DefaultExecutionSpace;
6
7/// \brief Example of batched trmv
8/// x:= alpha * op(A) * x
9/// A: [[1, -3, -2, 0],
10/// [0, 3, -1, -2],
11/// [0, 0, 9, 5],
12/// [0, 0, 0, 27]]
13/// x: [1, 2, 3, 4]
14/// alpha: 1.5
15///
16/// In upper, no-transposed and non-unit case
17/// x: [-16.5, -7.5, 70.5, 162]
18///
19int main(int /*argc*/, char** /*argv*/) {
20 Kokkos::initialize();
21 {
22 using View2DType = Kokkos::View<double**, ExecutionSpace>;
23 using View3DType = Kokkos::View<double***, ExecutionSpace>;
24 const int Nb = 10, n = 4;
25
26 // Matrix A
27 View3DType A("A", Nb, n, n);
28
29 // Vector x
30 View2DType x("x", Nb, n);
31
32 // Upper triangular matrix
33 // Initialize A and x
34 auto h_A = Kokkos::create_mirror_view(A);
35 auto h_x = Kokkos::create_mirror_view(x);
36
37 // Upper triangular matrix
38 for (int ib = 0; ib < Nb; ib++) {
39 // Set A
40 h_A(ib, 0, 0) = 1.0;
41 h_A(ib, 0, 1) = -3.0;
42 h_A(ib, 0, 2) = -2.0;
43 h_A(ib, 0, 3) = 0.0;
44 h_A(ib, 1, 0) = 0.0;
45 h_A(ib, 1, 1) = 3.0;
46 h_A(ib, 1, 2) = -1.0;
47 h_A(ib, 1, 3) = -2.0;
48 h_A(ib, 2, 2) = 9.0;
49 h_A(ib, 2, 3) = 5.0;
50 h_A(ib, 3, 3) = 27.0;
51
52 // Set x
53 // Fill vector x
54 for (int i = 0; i < n; i++) {
55 h_x(ib, i) = i + 1;
56 }
57 }
58 Kokkos::deep_copy(A, h_A);
59 Kokkos::deep_copy(x, h_x);
60
61 // Compute x := alpha * A * x with trmv
62 const double alpha = 1.5;
63 ExecutionSpace exec;
64 using policy_type = Kokkos::RangePolicy<ExecutionSpace, Kokkos::IndexType<int>>;
65 policy_type policy{exec, 0, Nb};
66 Kokkos::parallel_for(
67 "trmv", policy, KOKKOS_LAMBDA(int ib) {
68 auto sub_A = Kokkos::subview(A, ib, Kokkos::ALL, Kokkos::ALL);
69 auto sub_x = Kokkos::subview(x, ib, Kokkos::ALL);
70
71 // Compute x := alpha * A * x with trmv
72 KokkosBatched::SerialTrmv<KokkosBatched::Uplo::Upper, KokkosBatched::Trans::NoTranspose,
73 KokkosBatched::Diag::NonUnit>::invoke(alpha, sub_A, sub_x);
74 });
75
76 // Confirm that the results are correct
77 Kokkos::deep_copy(h_x, x);
78 bool correct = true;
79 double eps = 1.0e-12;
80 for (int ib = 0; ib < Nb; ib++) {
81 if (Kokkos::abs(h_x(ib, 0) + 16.5) > eps) correct = false;
82 if (Kokkos::abs(h_x(ib, 1) + 7.5) > eps) correct = false;
83 if (Kokkos::abs(h_x(ib, 2) - 70.5) > eps) correct = false;
84 if (Kokkos::abs(h_x(ib, 3) - 162) > eps) correct = false;
85 }
86
87 if (correct) {
88 std::cout << "trmv works correctly!" << std::endl;
89 }
90 }
91 Kokkos::finalize();
92}
output:
trmv works correctly!