Debian Codegraph @

packages/kokkos-kernels/blas/tpls/KokkosBlas2_serial_gemv_tpl_spec_decl.hpp

raw
1 //@HEADER
2 // ************************************************************************
3 //
4 // Kokkos v. 4.0
5 // Copyright (2022) National Technology & Engineering
6 // Solutions of Sandia, LLC (NTESS).
7 //
8 // Under the terms of Contract DE-NA0003525 with NTESS,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12 // See https://kokkos.org/LICENSE for license information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //@HEADER
16 #ifndef KOKKOSBLAS2_SERIAL_GEMV_TPL_SPEC_DECL_HPP_
17 #define KOKKOSBLAS2_SERIAL_GEMV_TPL_SPEC_DECL_HPP_
18
19 #include "KokkosBlas_util.hpp"
20 #include "KokkosBatched_Vector.hpp"
21
22 #if defined(KOKKOSKERNELS_ENABLE_TPL_MKL) && !defined(KOKKOS_ENABLE_SYCL)
23 #include "mkl_version.h"
24 #if __INTEL_MKL__ >= 2018
25 #define __KOKKOSBLAS_ENABLE_INTEL_MKL_COMPACT__ 1
26 #endif
27 #endif
28
29 #ifdef __KOKKOSBLAS_ENABLE_INTEL_MKL_COMPACT__
30
31 #include "mkl_compact.h"
32
33 namespace KokkosBlas {
34 namespace Impl {
35
36 // Note: using GEMM because there is no GEMV in MKL compact routines
37
38 #define KOKKOSBLAS_IMPL_MKL_DGEMM_COMPACT(SCALAR, MKL_ROUTINE) \
39 inline void kk_mkl_gemm_compact(MKL_LAYOUT layout, MKL_TRANSPOSE transa, MKL_TRANSPOSE transb, MKL_INT m, MKL_INT n, \
40 MKL_INT k, SCALAR alpha, const SCALAR *a, MKL_INT ldap, const SCALAR *b, \
41 MKL_INT ldbp, SCALAR beta, SCALAR *c, MKL_INT ldcp, MKL_COMPACT_PACK format, \
42 MKL_INT nm) { \
43 MKL_ROUTINE(layout, transa, transb, m, n, k, alpha, a, ldap, b, ldbp, beta, c, ldcp, format, nm); \
44 }
45
46 KOKKOSBLAS_IMPL_MKL_DGEMM_COMPACT(double, mkl_dgemm_compact)
47 KOKKOSBLAS_IMPL_MKL_DGEMM_COMPACT(float, mkl_sgemm_compact)
48 // Note: MKL compact format packs real and imaginary components separately
49 // which makes it not directly compatible with our Vector types
50
51 #undef KOKKOSBLAS_IMPL_MKL_DGEMM_COMPACT
52
53 template <typename ScalarType, int VecLen>
54 inline MKL_COMPACT_PACK mkl_compact_format() {
55 Kokkos::abort("vector size not supported");
56 }
57 template <>
58 inline MKL_COMPACT_PACK mkl_compact_format<double, 2>() {
59 return MKL_COMPACT_SSE;
60 }
61 template <>
62 inline MKL_COMPACT_PACK mkl_compact_format<float, 4>() {
63 return MKL_COMPACT_SSE;
64 }
65 template <>
66 inline MKL_COMPACT_PACK mkl_compact_format<double, 4>() {
67 return MKL_COMPACT_AVX;
68 }
69 template <>
70 inline MKL_COMPACT_PACK mkl_compact_format<float, 8>() {
71 return MKL_COMPACT_AVX;
72 }
73 template <>
74 inline MKL_COMPACT_PACK mkl_compact_format<double, 8>() {
75 return MKL_COMPACT_AVX512;
76 }
77 template <>
78 inline MKL_COMPACT_PACK mkl_compact_format<float, 16>() {
79 return MKL_COMPACT_AVX512;
80 }
81
82 template <typename ScalarType, typename AViewType, typename xViewType, typename yViewType>
83 void kk_mkl_gemv(MKL_TRANSPOSE trans, const ScalarType alpha, const AViewType &A, const xViewType &x,
84 const ScalarType beta, const yViewType &y) {
85 typedef typename yViewType::value_type vector_type;
86
87 static_assert(KokkosBatched::is_vector<vector_type>::value, "value type is not vector type");
88 using value_type = typename vector_type::value_type;
89 static_assert(std::is_same<typename AViewType::value_type::value_type, value_type>::value &&
90 std::is_same<typename xViewType::value_type::value_type, value_type>::value,
91 "scalar type mismatch");
92 if (A.stride_0() != 1 && A.stride_1() != 1 && x.stride_0() != 1 && y.stride_0() != 1) {
93 Kokkos::abort("Strided inputs are not supported in MKL gemv/gemm");
94 }
95
96 // Note: not checking 0-sizes as MKL handles it fine
97 const bool transposed = trans == MKL_TRANS || trans == MKL_CONJTRANS;
98 const int m = A.extent_int(transposed ? 1 : 0);
99 const int n = 1;
100 const int k = A.extent_int(transposed ? 0 : 1);
101
102 const bool col_major = A.stride_0() == 1;
103 const MKL_LAYOUT layout = col_major ? MKL_COL_MAJOR : MKL_ROW_MAJOR;
104 const MKL_INT A_ld = KOKKOSKERNELS_MACRO_MAX(1, A.extent(col_major ? 0 : 1));
105 const MKL_COMPACT_PACK format = Impl::mkl_compact_format<value_type, vector_type::vector_length>();
106
107 // cast away simd-vector pointers
108 auto A_data = reinterpret_cast<const value_type *>(A.data());
109 auto x_data = reinterpret_cast<const value_type *>(x.data());
110 auto y_data = reinterpret_cast<value_type *>(y.data());
111
112 Impl::kk_mkl_gemm_compact(layout, trans, MKL_NOTRANS, m, n, k, (value_type)alpha, A_data, A_ld, x_data, 1,
113 (value_type)beta, y_data, 1, format, (MKL_INT)vector_type::vector_length);
114 }
115
116 } // namespace Impl
117
118 ///
119 /// NT
120 ///
121
122 template <>
123 template <typename ScalarType, typename AViewType, typename xViewType, typename yViewType>
124 KOKKOS_INLINE_FUNCTION int SerialGemv<Trans::NoTranspose, Algo::Gemv::CompactMKL>::invoke(
125 const ScalarType alpha, const AViewType &A, const xViewType &x, const ScalarType beta, const yViewType &y) {
126 Impl::kk_mkl_gemv(MKL_NOTRANS, alpha, A, x, beta, y);
127 return 0;
128 }
129
130 ///
131 /// T
132 ///
133
134 template <>
135 template <typename ScalarType, typename AViewType, typename xViewType, typename yViewType>
136 KOKKOS_INLINE_FUNCTION int SerialGemv<Trans::Transpose, Algo::Gemv::CompactMKL>::invoke(
137 const ScalarType alpha, const AViewType &A, const xViewType &x, const ScalarType beta, const yViewType &y) {
138 Impl::kk_mkl_gemv(MKL_TRANS, alpha, A, x, beta, y);
139 return 0;
140 }
141
142 ///
143 /// CT
144 ///
145
146 template <>
147 template <typename ScalarType, typename AViewType, typename xViewType, typename yViewType>
148 KOKKOS_INLINE_FUNCTION int SerialGemv<Trans::ConjTranspose, Algo::Gemv::CompactMKL>::invoke(
149 const ScalarType alpha, const AViewType &A, const xViewType &x, const ScalarType beta, const yViewType &y) {
150 Impl::kk_mkl_gemv(MKL_CONJTRANS, alpha, A, x, beta, y);
151 return 0;
152 }
153
154 } // namespace KokkosBlas
155
156 #endif // __KOKKOSBLAS_ENABLE_INTEL_MKL_COMPACT__
157 #endif // KOKKOSBLAS2_SERIAL_GEMV_TPL_SPEC_DECL_HPP_