Debian Codegraph @

packages/kokkos-kernels/batched/dense/src/KokkosBatched_Kernel_Handle.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
17 #ifndef KOKKOSKERNELS_KOKKOSBATCHED_KERNEL_HEADER_HPP
18 #define KOKKOSKERNELS_KOKKOSBATCHED_KERNEL_HEADER_HPP
19
20 #include <sstream>
21 #include "KokkosKernels_Error.hpp"
22
23 #if defined(KOKKOSKERNELS_ENABLE_TPL_MKL)
24 #include <mkl.h>
25 #endif // KOKKOSKERNELS_ENABLE_TPL_MKL
26
27 #if defined(KOKKOSKERNELS_ENABLE_TPL_ARMPL)
28 #include "armpl.h"
29 #endif // KOKKOSKERNELS_ENABLE_TPL_ARMPL
30
31 #if defined(KOKKOSKERNELS_ENABLE_TPL_CUBLAS)
32 #include "cuda_runtime.h"
33 #include "cublas_v2.h"
34 #endif // KOKKOSKERNELS_ENABLE_TPL_CUBLAS
35
36 #if defined(KOKKOSKERNELS_ENABLE_TPL_MAGMA)
37 #include <magma_v2.h>
38 #include <magma_batched.h>
39 #endif // KOKKOSKERNELS_ENABLE_TPL_MAGMA
40
41 namespace KokkosBatched {
42
43 /// \brief Heuristic algorithm types. See BatchedKernelHandle for details.
44 namespace BaseHeuristicAlgos {
45 enum BASE_HEURISTIC_ALGOS : int { SQUARE = 0, TALL, WIDE, N };
46 }
47
48 /// \brief Tpl algorithm types. See BatchedKernelHandle for details.
49 namespace BaseTplAlgos {
50 enum BASE_TPL_ALGOS : int { ARMPL = BaseHeuristicAlgos::N, MKL, N };
51 }
52
53 /// \brief KokkosBatched algorithm types. See BatchedKernelHandle for details.
54 namespace BaseKokkosBatchedAlgos {
55 enum BASE_KOKKOS_BATCHED_ALGOS : int { KK_SERIAL = BaseTplAlgos::N, N };
56 }
57
58 #define N_BASE_ALGOS BaseKokkosBatchedAlgos::N
59 #define BASE_ALGO_STRS \
60 "BaseHeuristicAlgos::SQUARE", "BaseHeuristicAlgos::TALL", "BaseHeuristicAlgos::WIDE", "BaseTplAlgos::ARMPL", \
61 "BaseTplAlgosMKL", "BaseKokkosBatchedAlgos::KK_SERIAL"
62
63 /// \brief TplParams abstracts underlying handle or execution queue type.
64 struct TplParams {
65 union {
66 #if defined(KOKKOSKERNELS_ENABLE_TPL_MKL)
67 // queue mkl_queue;
68 // TODO: Add queue header? Cannot find any declarations in intel-18, let
69 // alone oneAPI 2021
70 #endif // KOKKOSKERNELS_ENABLE_TPL_MKL
71
72 #if defined(KOKKOSKERNELS_ENABLE_TPL_ARMPL)
73 armpl_int_t ninter = 1;
74 #endif // KOKKOSKERNELS_ENABLE_TPL_ARMPL
75
76 #if defined(KOKKOSKERNELS_ENABLE_TPL_CUBLAS)
77 cublasHandle_t *cublas_handle;
78 #endif // KOKKOSKERNELS_ENABLE_TPL_CUBLAS
79
80 #if defined(KOKKOSKERNELS_ENABLE_TPL_MAGMA)
81 magma_queue_t *magma_queue;
82 #endif // KOKKOSKERNELS_ENABLE_TPL_MAGMA
83
84 uint8_t empty_union; // suppress empty union build warning
85 };
86 };
87
88 // clang-format off
89 /// \brief Handle for selecting runtime behavior of the BatchedGemm interface.
90 ///
91 /// \param kernelAlgoType Specifies which algorithm to use for invocation (default, SQUARE).
92 ///
93 /// Specifies whether to select optimal invocations based on inputs and
94 /// heuristics:
95 /// SQUARE select invocations based on square matrix heuristics where M=N
96 /// TALL select invocations based on tall matrix heuristics where M>N
97 /// WIDE select invocations based on wide matrix heuristics where M<N
98 /// Note: If the heuristics indicate SIMD views are required for optimal
99 /// performance, notify the user that SIMD views are required for
100 /// optimal performance.
101 ///
102 /// Specifies which cmake-enabled TPL algorithm to invoke:
103 /// TPL algorithms currently SUPPORTED by all batched routines:
104 ///
105 /// TPL algorithms currently UNSUPPORTED by all batched routines:
106 /// ARMPL Invoke the ArmPL TPL interface
107 /// MKL Invoke the MKL TPL interface
108 /// CUBLAS Invoke the CuBLAS TPL interface
109 /// MAGMA Invoke the Magma TPL interface
110 /// Note: See each routine's handle for details about which TPLs are supported.
111 /// Note: Requires that input views for A, B, and C reside on either host
112 /// or device depending on the TPL selected.
113 /// Note: If the user selects a TPL, an error will be thrown if:
114 /// 1. The TPL is not enabled via cmake
115 /// 2. The input views do not reside on the host/device as needed
116 ///
117 /// Specifies which kokkos-kernels (KK) algorithm to invoke:
118 /// KK algorithms currently SUPPORTED by all KK batched routines:
119 /// KK_SERIAL Invoke SerialFUNC via RangePolicy(BatchSz)
120 /// KK algorithms currently UNSUPPORTED by all KK batched routines:
121 /// KK_TEAM Invoke TeamFUNC via TeamPolicy(BatchSz)
122 /// KK_TEAMVECTOR Invoke TeamVectorFUNC via TeamPolicy(BatchSz)
123 /// KK_SERIALSIMD Invoke SerialFUNC via TeamPolicy(BatchSz)
124 /// KK_TEAMSIMD Invoke TeamFUNC via TeamPolicy(BatchSz)
125 /// KK_SERIAL_RANK0 Invoke SerialFUNC via RangePolicy(BatchSz*LHS_N*LHS_M)
126 /// KK_SERIAL_SHMEM Invoke SerialFUNC via TeamPolicy(BatchSz)
127 /// KK_DBLBUF Solve FUNC via TeamPolicy(BatchSz*TILES)
128 /// Uses a tuned functor with tiling and double buffering
129 /// via shared memory and register buffers.
130 /// Note: See each routine's handle for details about which KK algorithms are
131 /// supported.
132 /// \param teamSz Specifies the team size that will affect any KK algorithm which uses
133 /// TeamPolicy (default, Kokkos::AUTO).
134 /// Note: Only applied if useAlgo_type == KK_*
135 /// \param vecLen Specifies the vector length that will affect any KK algorithm which
136 /// uses TeamPolicy and Kokkos::ThreadVectorRange or Kokkos::TeamVectorRange
137 /// (default, Kokkos::AUTO).
138 /// Note: Only applied if useAlgo_type == KK_*
139 /// \param enabledDebug toggle debug messages.
140 // clang-format on
141 class BatchedKernelHandle {
142 public:
143 int teamSz = 0;
144 int vecLen = 0;
145 bool enableDebug = false;
146
147 BatchedKernelHandle(int kernelAlgoType = BaseHeuristicAlgos::SQUARE, int teamSize = 0, int vecLength = 0)
148 : teamSz(teamSize), vecLen(vecLength), _kernelAlgoType(kernelAlgoType) {
149 #if !defined(KOKKOSKERNELS_ENABLE_TPL_ARMPL) || ARMPL_BUILD < 1058
150 if (_kernelAlgoType == BaseTplAlgos::ARMPL) {
151 std::ostringstream os;
152 os << "KokkosBatched::BatchedKernelHandle requires "
153 "KOKKOSKERNELS_ENABLE_TPL_ARMPL and armpl version 21.0.0+"
154 << std::endl;
155 KokkosKernels::Impl::throw_runtime_exception(os.str());
156 }
157 #endif // !defined(KOKKOSKERNELS_ENABLE_TPL_ARMPL)
158 };
159
160 int get_kernel_algo_type() const { return _kernelAlgoType; }
161
162 std::string get_kernel_algo_type_str() const { return algo_type_strs[_kernelAlgoType]; }
163
164 decltype(auto) get_tpl_params() const {
165 #if _kernelAlgoType == ARMPL && defined(KOKKOSKERNELS_ENABLE_TPL_ARMPL)
166 return &_tplParamsSingleton.ninter;
167 #elif _kernelAlgoType == MKL && defined(KOKKOSKERNELS_ENABLE_TPL_MKL)
168 return "BaseTplAlgos::MKL does not support any tpl parameters";
169 #else
170 return "Unsupported kernelAlgoType:" + get_kernel_algo_type_str() + ".";
171 #endif
172 }
173
174 // clang-format off
175 /// kernelAlgoType: Specifies which algorithm to use for invocation (default, SQUARE).
176 /// _tplParams: a handle or queue specific to the TPL API.
177 /// managed internally unless provided by user via
178 /// constructor overload
179 // clang-format on
180 protected:
181 // Define TPL params singleton as static class method variable
182 // Only one instance of tplParamsGlobalStorage may exist per process.
183 static TplParams &_get_tpl_params_singleton() {
184 static TplParams tplParamsGlobalStorage;
185 return tplParamsGlobalStorage;
186 }
187
188 int _kernelAlgoType = BaseHeuristicAlgos::SQUARE;
189 TplParams &_tplParamsSingleton = _get_tpl_params_singleton();
190 bool _tplParamsSet = false;
191
192 private:
193 const char *algo_type_strs[N_BASE_ALGOS] = {BASE_ALGO_STRS};
194 };
195
196 } // namespace KokkosBatched
197
198 #endif // KOKKOSKERNELS_KOKKOSBATCHED_KERNEL_HEADER_HPP