KokkosBatched::Syr2k¶
Defined in header: KokkosBatched_Syr2k.hpp
template <typename ArgUplo, typename ArgTrans>
struct SerialSyr2k {
template <typename AlphaType, typename AViewType, typename BViewType, typename BetaType, typename CViewType>
KOKKOS_INLINE_FUNCTION static int invoke(const AlphaType alpha, const AViewType &A, const BViewType &B, const BetaType beta,
const CViewType &C);
};
template <typename MemberType, typename ArgUplo, typename ArgTrans>
struct TeamSyr2k {
template <typename AlphaType, typename AViewType, typename BViewType, typename BetaType, typename CViewType>
KOKKOS_INLINE_FUNCTION static int invoke(const MemberType &member, const AlphaType alpha, const AViewType &A,
const BViewType &B, const BetaType beta, const CViewType &C);
};
template <typename MemberType, typename ArgUplo, typename ArgTrans>
struct TeamVectorSyr2k {
template <typename AlphaType, typename AViewType, typename BViewType, typename BetaType, typename CViewType>
KOKKOS_INLINE_FUNCTION static int invoke(const MemberType &member, const AlphaType alpha, const AViewType &A,
const BViewType &B, const BetaType beta, const CViewType &C);
};
Performs one of the symmetric or hermitian rank 2k operations
If
ArgTrans == KokkosBatched::Trans::TransposeorArgTrans == KokkosBatched::Trans::NoTranspose, this operation is equivalent to the BLAS routine SSYR2K (CSYR2K) or DSYR2K (ZSYR2K) for single or double precision for real (complex) symmetric matrix.If
ArgTrans == KokkosBatched::Trans::ConjTransposeorArgTrans == KokkosBatched::Trans::ConjNoTranspose, this operation is equivalent to the BLAS routine CHER2K or ZHER2K for single or double precision for complex hermitian matrix.
Parameters¶
- alpha:
Scaling factor
- A:
\(A\) is an n by k matrix if
ArgTrans == KokkosBatched::Trans::NoTransposeorArgTrans == KokkosBatched::Trans::ConjNoTranspose. Otherwise, \(A\) is a k by n matrix.- B:
\(B\) is an n by k matrix if
ArgTrans == KokkosBatched::Trans::NoTransposeorArgTrans == KokkosBatched::Trans::ConjNoTranspose. Otherwise, \(B\) is a k by n matrix.- beta:
Scaling factor
- C:
\(C\) is a n by n symmetric (or hermitian) matrix. Only the upper or lower triangular part of \(C\) is referenced based on
ArgUplo.
Type Requirements¶
MemberTypemust be a Kokkos team member handle (only forTeamSyr2kandTeamVectorSyr2k)ArgUplomust be one of the following:KokkosBatched::Uplo::Upperto update the upper triangular part of \(C\)KokkosBatched::Uplo::Lowerto update the lower triangular part of \(C\)
ArgTransmust be one of the following:KokkosBatched::Trans::NoTransposeto perform \(C = \alpha A * B^T + \alpha B * A^T + \beta C\) on the symmetric matrix \(C\)KokkosBatched::Trans::Transposeto perform \(C = \alpha A^T * B + \alpha B^T * A + \beta C\) on the symmetric matrix \(C\)KokkosBatched::Trans::ConjNoTransposeto perform \(C = \alpha A * B^H + \bar{\alpha} B * A^H + \beta C\) on the hermitian matrix \(C\)KokkosBatched::Trans::ConjTransposeto perform \(C = \alpha A^H * B + \bar{\alpha} B^H * A + \beta C\) on the hermitian matrix \(C\)
ScalarTypemust be a built-in floating point type (float,double,Kokkos::complex<float>,Kokkos::complex<double>)AViewTypemust be a Kokkos View of rank 2 containing a matrix \(A\)BViewTypemust be a Kokkos View of rank 2 containing a matrix \(B\)CViewTypemust be a Kokkos View of rank 2 containing a matrix \(C\) that satisfiesstd::is_same_v<typename CViewType::value_type, typename CViewType::non_const_value_type>
Note
If ArgTrans == KokkosBatched::Trans::ConjTranspose or ArgTrans == KokkosBatched::Trans::ConjNoTranspose, the coefficient \(\beta\) must be real to ensure Hermitian symmetry of the updated matrix \(C\).
Example¶
1#include <Kokkos_Core.hpp>
2#include <Kokkos_Random.hpp>
3#include <KokkosBatched_Syr2k.hpp>
4
5using ExecutionSpace = Kokkos::DefaultExecutionSpace;
6
7/// \brief Example of batched syr2k
8/// Perform a symmetric rank-2 update of matrix A by vectors x and y, where
9/// alpha = 1.5, beta = 1.2
10/// 4x4 matrix (upper)
11/// A: [[1, -3, -2, 0],
12/// [3, 3, -1, -2],
13/// [2, 1, 9, 5],
14/// [0, 2, -5, 27]]
15/// B: [[2, 1, 0, -1],
16/// [-1, 4, 2, 0],
17/// [3, -2, 1, 5],
18/// [1, 0, -3, 2]]
19/// C = A
20/// ArgTrans == NoTranspose
21/// ArgUplo == Upper
22/// C: = alpha*A*B^T + alpha*B*A^T + beta * C
23/// Ref: [[ -1.8 -12.6 8.1 -27. ]
24/// [ 3. 24.6 16.8 -2.4]
25/// [ 2. 1. 124.8 172.5]
26/// [ 0. 2. -5. 239.4]]
27///
28int main(int /*argc*/, char** /*argv*/) {
29 Kokkos::initialize();
30 {
31 using View3DType = Kokkos::View<double***, ExecutionSpace>;
32 const int Nb = 10, n = 4;
33
34 // Matrix A, B, and C
35 View3DType A("A", Nb, n, n), B("B", Nb, n, n), C("C", Nb, n, n), Ref("Ref", Nb, n, n);
36
37 // Initialize A, B and C
38 auto h_A = Kokkos::create_mirror_view(A);
39 auto h_B = Kokkos::create_mirror_view(B);
40 auto h_Ref = Kokkos::create_mirror_view(Ref);
41
42 // Upper triangular matrix
43 for (int ib = 0; ib < Nb; ib++) {
44 h_A(ib, 0, 0) = 1;
45 h_A(ib, 0, 1) = -3;
46 h_A(ib, 0, 2) = -2;
47 h_A(ib, 0, 3) = 0;
48 h_A(ib, 1, 0) = 3;
49 h_A(ib, 1, 1) = 3;
50 h_A(ib, 1, 2) = -1;
51 h_A(ib, 1, 3) = -2;
52 h_A(ib, 2, 0) = 2;
53 h_A(ib, 2, 1) = 1;
54 h_A(ib, 2, 2) = 9;
55 h_A(ib, 2, 3) = 5;
56 h_A(ib, 3, 0) = 0;
57 h_A(ib, 3, 1) = 2;
58 h_A(ib, 3, 2) = -5;
59 h_A(ib, 3, 3) = 27;
60
61 h_B(ib, 0, 0) = 2;
62 h_B(ib, 0, 1) = 1;
63 h_B(ib, 0, 2) = 0;
64 h_B(ib, 0, 3) = -1;
65 h_B(ib, 1, 0) = -1;
66 h_B(ib, 1, 1) = 4;
67 h_B(ib, 1, 2) = 2;
68 h_B(ib, 1, 3) = 0;
69 h_B(ib, 2, 0) = 3;
70 h_B(ib, 2, 1) = -2;
71 h_B(ib, 2, 2) = 1;
72 h_B(ib, 2, 3) = 5;
73 h_B(ib, 3, 0) = 1;
74 h_B(ib, 3, 1) = 0;
75 h_B(ib, 3, 2) = -3;
76 h_B(ib, 3, 3) = 2;
77
78 // Fill the reference matrix
79 h_Ref(ib, 0, 0) = -1.8;
80 h_Ref(ib, 0, 1) = -12.6;
81 h_Ref(ib, 0, 2) = 8.1;
82 h_Ref(ib, 0, 3) = -27.0;
83 h_Ref(ib, 1, 0) = 3;
84 h_Ref(ib, 1, 1) = 24.6;
85 h_Ref(ib, 1, 2) = 16.8;
86 h_Ref(ib, 1, 3) = -2.4;
87 h_Ref(ib, 2, 0) = 2;
88 h_Ref(ib, 2, 1) = 1;
89 h_Ref(ib, 2, 2) = 124.8;
90 h_Ref(ib, 2, 3) = 172.5;
91 h_Ref(ib, 3, 0) = 0;
92 h_Ref(ib, 3, 1) = 2;
93 h_Ref(ib, 3, 2) = -5;
94 h_Ref(ib, 3, 3) = 239.4;
95 }
96 Kokkos::deep_copy(A, h_A);
97 Kokkos::deep_copy(B, h_B);
98 Kokkos::deep_copy(C, A); // Initialize C with A
99
100 // Compute C = alpha * A * B^T + alpha * B * A^T + beta * C
101 const double alpha = 1.5, beta = 1.2;
102 ExecutionSpace exec;
103 using policy_type = Kokkos::RangePolicy<ExecutionSpace, Kokkos::IndexType<int>>;
104 policy_type policy{exec, 0, Nb};
105 Kokkos::parallel_for(
106 "syr2k", policy, KOKKOS_LAMBDA(int ib) {
107 auto sub_A = Kokkos::subview(A, ib, Kokkos::ALL, Kokkos::ALL);
108 auto sub_B = Kokkos::subview(B, ib, Kokkos::ALL, Kokkos::ALL);
109 auto sub_C = Kokkos::subview(C, ib, Kokkos::ALL, Kokkos::ALL);
110
111 // C = alpha * A * B^T + alpha * B * A^T + beta * C
112 KokkosBatched::SerialSyr2k<KokkosBatched::Uplo::Upper, KokkosBatched::Trans::NoTranspose>::invoke(
113 alpha, sub_A, sub_B, beta, sub_C);
114 });
115
116 // Confirm that the results are correct
117 auto h_C = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, C);
118 bool correct = true;
119 double eps = 1.0e-12;
120 for (int ib = 0; ib < Nb; ib++) {
121 for (int i = 0; i < n; i++) {
122 for (int j = 0; j < n; j++) {
123 if (Kokkos::abs(h_C(ib, i, j) - h_Ref(ib, i, j)) > eps) correct = false;
124 }
125 }
126 }
127
128 if (correct) {
129 std::cout << "syr2k works correctly!" << std::endl;
130 }
131 }
132 Kokkos::finalize();
133}
output:
syr2k works correctly!