Debian Codegraph @

packages/kokkos-kernels/perf_test/blas/blas3/KokkosBlas3_gemm_perf_test.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 KOKKOSBLAS3_GEMM_PERF_TEST_H_
17 #define KOKKOSBLAS3_GEMM_PERF_TEST_H_
18
19 // #include <complex.h>
20
21 #include "Kokkos_MathematicalFunctions.hpp"
22
23 #include "KokkosBlas3_common.hpp"
24 #include <Kokkos_Random.hpp>
25
26 #include <KokkosBlas3_gemm.hpp>
27
28 #include "Kokkos_ArithTraits.hpp"
29 #include "KokkosBatched_HostLevel_Gemm.hpp"
30 #include "KokkosBatched_Gemm_Decl.hpp"
31 #include "KokkosBatched_Util.hpp"
32 #include "KokkosKernels_TestStringUtils.hpp"
33 #include "KokkosKernels_TestVanilla.hpp"
34
35 #include <chrono>
36
37 #if defined(KOKKOSKERNELS_ENABLE_TPL_ARMPL)
38 #include "armpl.h"
39 #else
40 using armpl_int_t = int;
41 #endif // KOKKOSKERNELS_ENABLE_TPL_ARMPL
42
43 // #define GEMM_PERF_TEST_DEBUG
44
45 // Forward declarations
46 void do_gemm_serial_blas(options_t options);
47 void do_gemm_serial_batched(options_t options);
48 void do_gemm_serial_batched_blocked(options_t options);
49 // void do_gemm_experiment(options_t options);
50
51 // void do_gemm_serial_blas_parallel(options_t options);
52 // Not valid! The KokkosBlas::gemm function may take the entire device per
53 // invocation!
54 void do_gemm_heuristic_batched_parallel(options_t options);
55 void do_gemm_serial_batched_parallel(options_t options);
56 void do_gemm_serial_batched_blocked_parallel(options_t options);
57 void do_gemm_serial_simd_batched_parallel(options_t options);
58 void do_gemm_serial_simd_batched_blocked_parallel(options_t options);
59 void do_gemm_serial_batched_compact_mkl_parallel(options_t options);
60 void do_gemm_team_batched_parallel(options_t options);
61 void do_gemm_team_batched_blocked_parallel(options_t options);
62 void do_gemm_team_vector_batched_parallel(options_t options);
63 void do_gemm_team_vector_batched_blocked_parallel(options_t options);
64 void do_gemm_team_simd_batched_parallel(options_t options);
65 void do_gemm_team_simd_batched_blocked_parallel(options_t options);
66 void do_gemm_experiment_parallel(options_t options);
67
68 struct SerialTag {};
69 struct SerialBatchDim3Tag {};
70 struct SerialSimdTag {};
71 struct SerialSimdBatchDim3Tag {};
72 struct TeamTag {};
73 struct TeamBatchDim3Tag {};
74 struct TeamVectorTag {};
75 struct TeamVectorBatchDim3Tag {};
76 struct TeamSimdTag {};
77 struct TeamSimdBatchDim4Tag {};
78 struct LayoutLeftTag {};
79 struct LayoutRightTag {};
80 struct SimdCpuTag {};
81
82 // gemm invoke table
83 void (*do_gemm_invoke[LOOP_N][TEST_N])(options_t) = {
84 {
85 do_gemm_serial_blas, // BLAS
86 do_gemm_serial_batched, do_gemm_serial_batched_blocked, // Serial
87 NULL, NULL, // Team
88 NULL, NULL, // TeamVector
89 NULL, NULL, // TeamSimd
90 NULL, // Serial Experiment
91 },
92 {
93 NULL, // BLAS
94 do_gemm_heuristic_batched_parallel,
95 do_gemm_serial_batched_parallel, // Serial
96 do_gemm_serial_batched_blocked_parallel, do_gemm_serial_simd_batched_parallel,
97 do_gemm_serial_simd_batched_blocked_parallel, do_gemm_serial_batched_compact_mkl_parallel,
98 do_gemm_team_batched_parallel,
99 do_gemm_team_batched_blocked_parallel, // Team
100 do_gemm_team_vector_batched_parallel, NULL, // TeamVector
101 do_gemm_team_simd_batched_parallel,
102 do_gemm_team_simd_batched_blocked_parallel, // TeamSimd
103 do_gemm_experiment_parallel // Parallel Experiment
104 }};
105
106 /*************************** Test types and defaults **************************/
107 #define DEFAULT_GEMM_ARGS "NN"
108 #define DEFAULT_GEMM_ALPHA 1.0
109 #define DEFAULT_GEMM_BETA 1.0
110
111 using view_type_3d =
112 Kokkos::View<KokkosKernels::default_scalar ***, KokkosKernels::default_layout, KokkosKernels::default_device>;
113 using view_type_4d =
114 Kokkos::View<KokkosKernels::default_scalar ****, KokkosKernels::default_layout, KokkosKernels::default_device>;
115 using view_type_5d =
116 Kokkos::View<KokkosKernels::default_scalar *****, KokkosKernels::default_layout, KokkosKernels::default_device>;
117
118 // Construct the vector type
119 using memory_space = typename KokkosKernels::default_device::execution_space::memory_space;
120 constexpr int simd_vector_size = KokkosBatched::DefaultVectorLength<KokkosKernels::default_scalar, memory_space>::value;
121 constexpr int simd_internal_vector_size =
122 KokkosBatched::DefaultInternalVectorLength<KokkosKernels::default_scalar, memory_space>::value;
123 using vector_type = KokkosBatched::Vector<KokkosBatched::SIMD<KokkosKernels::default_scalar>, simd_vector_size>;
124 using internal_vector_type =
125 KokkosBatched::Vector<KokkosBatched::SIMD<KokkosKernels::default_scalar>, simd_internal_vector_size>;
126 using vector_view_type_3d = Kokkos::View<vector_type ***, KokkosKernels::default_layout, KokkosKernels::default_device>;
127 using internal_vector_view_type_4d =
128 Kokkos::View<internal_vector_type ****, KokkosKernels::default_layout, KokkosKernels::default_device>;
129
130 struct batched_params {
131 int team_size;
132 int vector_len;
133 };
134 typedef struct batched_params batched_params_t;
135
136 /**
137 * @brief struct gemm_simd_args encapsulates the data types required
138 * for allocating and passing a single matrix to the KokkosBatched gemm
139 * kernels. To invoke gemm on a batch of matrices, three instances of this
140 * struct are required, one for each matrix, A, B, and C.
141 *
142 * @var vec_3d: 3-rank view type used for allocating the underlying data.
143 * A reference must be kept to this object to ensure the
144 * data is not free'd by the C++ runtime.
145 * @var mat_4d: 4-rank view type used for populating the simd view with
146 random values.
147 * @var ivec_4d: 4-rank view type used for passing to math kernels. This
148 * view type is used for leveraging simd instructions on
149 * both the host and device.
150 */
151 struct gemm_simd_args {
152 vector_view_type_3d vec_3d;
153 view_type_4d mat_4d;
154 internal_vector_view_type_4d ivec_4d;
155 };
156 typedef struct gemm_simd_args gemm_simd_args_t;
157
158 /**
159 * @brief struct gemm_armpl_args encapsulates the data required for allocating
160 * and passing a single matrix to the armpl interleave gemm api. To invoke gemm,
161 * three instances of this struct are needed, one for each matrix: A, B, and C.
162 *
163 * @var mat: A flat copy of the 3-rank matrix in interleaved format.
164 * @var jstrd: See
165 * https://developer.arm.com/documentation/101004/2100/Interleave-batch-functions/armpl-dgemm-interleave-batch.
166 * @var istrd: See
167 * https://developer.arm.com/documentation/101004/2100/Interleave-batch-functions/armpl-dgemm-interleave-batch.
168 * @var bstrd: See
169 * https://developer.arm.com/documentation/101004/2100/Interleave-batch-functions/armpl-dgemm-interleave-batch.
170 */
171 struct gemm_armpl_args {
172 KokkosKernels::default_scalar *mat;
173 armpl_int_t jstrd, istrd, bstrd;
174 };
175 typedef struct gemm_armpl_args gemm_armpl_args_t;
176
177 /**
178 * @brief struct gemm_args are common arguments passed to
179 * both gemm implementations in the KokkosBlas and KokkosBatched
180 * namespaces throughout these performance tests.
181 *
182 * @var transA: transpose type for A matrix.
183 * supported types: 'n' - no transpose, 't' - transpose.
184 * unsupported types: 'c' - conjugate transpose.
185 * @var transB: transpose type for B matrix.
186 * supported types: 'n' - no transpose, 't' - transpose.
187 * unsupported types: 'c' - conjugate transpose.
188 * @var alpha: scalar applied to A matrix.
189 * @var beta: scalar applied to B matrix.
190 * @var A: 3-rank view type used in all non-simd tests.
191 * @var B: 3-rank view type used in all non-simd tests.
192 * @var C: 3-rank view type used in all non-simd tests.
193 * @var bp: team_size and vector_length for tests that use
194 * Kokkos::TeamPolicy.
195 * @var Av: 3-rank and 4-rank vector view types for simd tests.
196 * @var Bv: 3-rank and 4-rank vector view types for simd tests.
197 * @var Cv: 3-rank and 4-rank vector view types for simd tests.
198 * @var A_pl: flat array with strides for armpl interleave API.
199 * @var B_pl: flat array with strides for armpl interleave API.
200 * @var C_pl: flat array with strides for armpl interleave API.
201 * @var ninter: number of interleaved matrices (sub-batch size) for armpl
202 * interleave API.
203 * @var nbatch: number of batches of interleaves matrices for armpl interleave
204 * API.
205 */
206 struct gemm_args {
207 char transA, transB;
208 KokkosKernels::default_scalar alpha;
209 KokkosKernels::default_scalar beta;
210 view_type_3d A, B, C;
211 batched_params_t bp;
212 // Below are matrices for simd tests
213 gemm_simd_args_t Av, Bv, Cv;
214 // Below are matrices and args for armpl interleave batch tests
215 gemm_armpl_args_t A_pl, B_pl, C_pl;
216 armpl_int_t ninter;
217 armpl_int_t nbatch;
218 matrix_dims_t dims;
219 };
220 typedef struct gemm_args gemm_args_t;
221
222 static std::string gemm_csv_header_str =
223 "algorithm,vector_type,transAtransB,alpha,beta,team_size,vector_len,loop_"
224 "type,A_dims,B_"
225 "dims,C_dims,warm_up_n,"
226 "iter,total_time(s),average_time(s),FLOPS,GFLOP/average_time(s)";
227
228 /*************************** Internal helper fns **************************/
229 // Flop count formula from lapack working note 41:
230 // http://www.icl.utk.edu/~mgates3/docs/lawn41.pdf
231 static inline double __gemm_flop_count(double a_m, double a_n, double b_n) {
232 // TODO: if not Kokkos::complex.
233 if (std::is_same<double, KokkosKernels::default_scalar>::value ||
234 std::is_same<float, KokkosKernels::default_scalar>::value ||
235 std::is_same<Kokkos::Experimental::half_t, KokkosKernels::default_scalar>::value ||
236 std::is_same<Kokkos::Experimental::bhalf_t, KokkosKernels::default_scalar>::value)
237 return 2 * a_m * b_n * a_n;
238 else
239 // For complex, we need to count 2 flops for each add and 6 flops for each
240 // multiply.
241 return (2 + 6) * a_m * b_n * a_n;
242 }
243
244 static inline std::string __gemm_output_dim_string(options_t options, matrix_dim_t dim) {
245 std::string x = "x";
246 std::string ret = std::to_string(dim.m) + x + std::to_string(dim.n);
247
248 if (options.blas_args.batch_size_last_dim)
249 return ret + x + std::to_string(dim.k);
250 else
251 return std::to_string(dim.k) + x + ret;
252 }
253
254 static void __gemm_output_csv_row(options_t options, gemm_args_t gemm_args, double time_in_seconds,
255 const char *experiment_name = nullptr, const char *team_size = nullptr,
256 const char *vec_len = nullptr, const char *vec_type = nullptr) {
257 std::string algo_name = !experiment_name ? test_e_str[options.test] : std::string(experiment_name);
258 std::string ts = !team_size ? std::to_string(gemm_args.bp.team_size) : std::string(team_size);
259 std::string vlen = !vec_len ? std::to_string(gemm_args.bp.vector_len) : std::string(vec_len);
260 std::string vtype = !vec_type ? internal_vector_type::label() : std::string(vec_type);
261 if (options.blas_args.use_auto) ts = vlen = "Kokkos::AUTO";
262
263 double flops;
264 double gflops;
265 double average_time = time_in_seconds / options.n;
266
267 if (options.verify) return;
268
269 flops = gemm_args.dims.a.k * __gemm_flop_count(gemm_args.dims.a.m, gemm_args.dims.a.n, gemm_args.dims.b.n);
270
271 gflops = flops / 1e9;
272
273 options.out[0] << algo_name << "," << vtype << "," << options.blas_args.gemm.gemm_args << ","
274 << static_cast<double>(options.blas_args.gemm.alpha) << ","
275 << static_cast<double>(options.blas_args.gemm.beta) << "," << ts << "," << vlen << ","
276 << loop_e_str[options.loop] << "," << __gemm_output_dim_string(options, gemm_args.dims.a) << ","
277 << __gemm_output_dim_string(options, gemm_args.dims.b) << ","
278 << __gemm_output_dim_string(options, gemm_args.dims.c) << "," << options.warm_up_n << "," << options.n
279 << "," << time_in_seconds << "," << time_in_seconds / options.n << "," << flops << ","
280 << gflops / average_time << std::endl;
281 }
282
283 #ifdef PERF_TEST_DEBUG
284 static void __print_gemm_perf_test_options(options_t options) {
285 printf("options.test = %s\n", test_e_str[options.test].c_str());
286 printf("options.loop = %s\n", loop_e_str[options.loop].c_str());
287 printf("options.start = %dx%d,%dx%d\n", options.start.a.m, options.start.a.n, options.start.b.m,
288 options.start.b.n);
289 printf("options.stop = %dx%d,%dx%d\n", options.stop.a.m, options.stop.a.n, options.stop.b.m, options.stop.b.n);
290 printf("options.step = %d\n", options.step);
291 printf("options.warm_up_n = %d\n", options.warm_up_n);
292 printf("options.n = %d\n", options.n);
293 printf("options.blas_args.gemm.gemm_args = %s\n", options.blas_args.gemm.gemm_args.c_str());
294 printf("options.out_file = %s\n", options.out_file.c_str());
295 if (std::is_same<double, KokkosKernels::default_scalar>::value)
296 printf("options.alpha = %lf\n", options.blas_args.gemm.alpha);
297 else if (std::is_same<float, KokkosKernels::default_scalar>::value)
298 printf("options.alpha = %f\n", options.blas_args.gemm.alpha);
299 return;
300 }
301 #else
302 static void __print_gemm_perf_test_options(options_t /*options*/) { return; }
303 #endif // PERF_TEST_DEBUG
304
305 /*************************** Internal templated fns **************************/
306 #if !defined(KOKKOS_ENABLE_CUDA)
307 template <class scalar_type, class vta, class vtb, class device_type>
308 void __do_gemm_serial_blas(options_t options, gemm_args_t gemm_args) {
309 // Need to take subviews on the device
310 Kokkos::Timer timer;
311
312 STATUS;
313
314 auto __do_loop = [](uint32_t n, gemm_args_t _gemm_args, bool batch_size_last_dim) {
315 for (uint32_t i = 0; i < n; ++i) {
316 for (int j = 0; j < _gemm_args.dims.c.k; j++) {
317 auto A = Kokkos::subview(_gemm_args.A, j, Kokkos::ALL(), Kokkos::ALL());
318 auto B = Kokkos::subview(_gemm_args.B, j, Kokkos::ALL(), Kokkos::ALL());
319 auto C = Kokkos::subview(_gemm_args.C, j, Kokkos::ALL(), Kokkos::ALL());
320 if (batch_size_last_dim) {
321 A = Kokkos::subview(_gemm_args.A, Kokkos::ALL(), Kokkos::ALL(), j);
322 B = Kokkos::subview(_gemm_args.B, Kokkos::ALL(), Kokkos::ALL(), j);
323 C = Kokkos::subview(_gemm_args.C, Kokkos::ALL(), Kokkos::ALL(), j);
324 }
325
326 KokkosBlas::gemm(&_gemm_args.transA, &_gemm_args.transB, _gemm_args.alpha, A, B, _gemm_args.beta, C);
327 }
328 }
329 };
330 __do_loop(options.warm_up_n, gemm_args, options.blas_args.batch_size_last_dim);
331 Kokkos::fence();
332
333 timer.reset();
334 __do_loop(options.n, gemm_args, options.blas_args.batch_size_last_dim);
335 Kokkos::fence();
336
337 __gemm_output_csv_row(options, gemm_args, timer.seconds());
338 return;
339 }
340 #else
341 template <class scalar_type, class vta, class vtb, class device_type>
342 void __do_gemm_serial_blas(options_t /*options*/, gemm_args_t /*gemm_args*/) {
343 std::cerr << std::string(__func__) << " disabled since KOKKOS_ENABLE_CUDA is defined." << std::endl;
344 return;
345 }
346 #endif // !KOKKOS_ENABLE_CUDA
347
348 #if !defined(KOKKOS_ENABLE_CUDA)
349 template <class TransAType, class TransBType, class AlgoType>
350 void __do_gemm_serial_batched_template(options_t options, gemm_args_t gemm_args) {
351 // Need to take subviews on the device
352 Kokkos::Timer timer;
353
354 auto __do_loop = [](uint32_t n, gemm_args_t _gemm_args, bool batch_size_last_dim) {
355 for (uint32_t i = 0; i < n; ++i) {
356 for (int j = 0; j < _gemm_args.dims.c.k; j++) {
357 auto A = Kokkos::subview(_gemm_args.A, j, Kokkos::ALL(), Kokkos::ALL());
358 auto B = Kokkos::subview(_gemm_args.B, j, Kokkos::ALL(), Kokkos::ALL());
359 auto C = Kokkos::subview(_gemm_args.C, j, Kokkos::ALL(), Kokkos::ALL());
360 if (batch_size_last_dim) {
361 A = Kokkos::subview(_gemm_args.A, Kokkos::ALL(), Kokkos::ALL(), j);
362 B = Kokkos::subview(_gemm_args.B, Kokkos::ALL(), Kokkos::ALL(), j);
363 C = Kokkos::subview(_gemm_args.C, Kokkos::ALL(), Kokkos::ALL(), j);
364 }
365
366 KokkosBatched::SerialGemm<TransAType, TransBType, AlgoType>::invoke(_gemm_args.alpha, A, B, _gemm_args.beta, C);
367 }
368 }
369 };
370
371 __do_loop(options.warm_up_n, gemm_args, options.blas_args.batch_size_last_dim);
372 Kokkos::fence();
373
374 timer.reset();
375 __do_loop(options.n, gemm_args, options.blas_args.batch_size_last_dim);
376 Kokkos::fence();
377 __gemm_output_csv_row(options, gemm_args, timer.seconds());
378 }
379 #else
380 template <class TransAType, class TransBType, class AlgoType>
381 void __do_gemm_serial_batched_template(options_t /*options*/, gemm_args_t /*gemm_args*/) {
382 std::cerr << std::string(__func__) << " disabled since KOKKOS_ENABLE_CUDA is defined." << std::endl;
383 }
384 #endif // !KOKKOS_ENABLE_CUDA
385
386 template <class scalar_type, class vta, class vtb, class vtc, class device_type, class algo_type>
387 void __do_gemm_serial_batched(options_t options, gemm_args_t gemm_args) {
388 char a = toupper(gemm_args.transA);
389 char b = toupper(gemm_args.transB);
390 using N = KokkosBatched::Trans::NoTranspose;
391 using T = KokkosBatched::Trans::Transpose;
392 // using C = KokkosBatched::Trans::ConjTranspose;
393
394 STATUS;
395
396 if (a == 'N' && b == 'N') {
397 __do_gemm_serial_batched_template<N, N, algo_type>(options, gemm_args);
398 } else if (a == 'N' && b == 'T') {
399 __do_gemm_serial_batched_template<N, T, algo_type>(options, gemm_args);
400 //} else if (a == 'N' && b == 'C') {
401 // __do_gemm_serial_batched_template<N, C, algo_type>(options, gemm_args);
402 } else if (a == 'T' && b == 'N') {
403 __do_gemm_serial_batched_template<T, N, algo_type>(options, gemm_args);
404 } else if (a == 'T' && b == 'T') {
405 __do_gemm_serial_batched_template<T, T, algo_type>(options, gemm_args);
406 //} else if (a == 'T' && b == 'C') {
407 // __do_gemm_serial_batched_template<T, C, algo_type>(options, gemm_args);
408 //} else if (a == 'C' && b == 'N') {
409 // __do_gemm_serial_batched_template<C, N, algo_type>(options, gemm_args);
410 //} else if (a == 'C' && b == 'T') {
411 // __do_gemm_serial_batched_template<C, T, algo_type>(options, gemm_args);
412 //} else if (a == 'C' && b == 'C') {
413 // __do_gemm_serial_batched_template<C, C, algo_type>(options, gemm_args);
414 } else {
415 FATAL_ERROR("Bad gemm_args TransA or TransB value");
416 }
417 return;
418 }
419
420 template <class algo_tag, class blocking_type, class device_type, class algo_mode = void>
421 void __do_gemm_parallel_batched_heuristic_template(options_t options, gemm_args_t gemm_args) {
422 KokkosBatched::BatchedGemmHandle batchedGemmHandle(KokkosBatched::BaseHeuristicAlgos::SQUARE);
423 char a = toupper(gemm_args.transA);
424 char b = toupper(gemm_args.transB);
425 using N = KokkosBatched::Trans::NoTranspose;
426 using T = KokkosBatched::Trans::Transpose;
427 // using C = KokkosBatched::Trans::ConjTranspose;
428 using KokkosBatched::BatchLayout;
429
430 STATUS;
431 if (a == 'N' && b == 'N') {
432 if constexpr (std::is_same_v<typename vector_view_type_3d::array_layout, Kokkos::LayoutLeft>) {
433 if (options.use_simd) {
434 KokkosBatched::BatchedGemm<N, N, BatchLayout::Right>(&batchedGemmHandle, gemm_args.alpha, gemm_args.Av.vec_3d,
435 gemm_args.Bv.vec_3d, gemm_args.beta, gemm_args.Cv.vec_3d);
436 } else if constexpr (std::is_same_v<typename view_type_3d::array_layout, Kokkos::LayoutLeft>) {
437 KokkosBatched::BatchedGemm<N, N, BatchLayout::Right>(&batchedGemmHandle, gemm_args.alpha, gemm_args.A,
438 gemm_args.B, gemm_args.beta, gemm_args.C);
439 }
440 } else if (options.use_simd) {
441 KokkosBatched::BatchedGemm<N, N, BatchLayout::Left>(&batchedGemmHandle, gemm_args.alpha, gemm_args.Av.vec_3d,
442 gemm_args.Bv.vec_3d, gemm_args.beta, gemm_args.Cv.vec_3d);
443 } else if constexpr (std::is_same_v<typename view_type_3d::array_layout, Kokkos::LayoutRight>) {
444 KokkosBatched::BatchedGemm<N, N, BatchLayout::Left>(&batchedGemmHandle, gemm_args.alpha, gemm_args.A, gemm_args.B,
445 gemm_args.beta, gemm_args.C);
446 }
447 } else if (a == 'N' && b == 'T') {
448 if constexpr (std::is_same_v<typename vector_view_type_3d::array_layout, Kokkos::LayoutLeft>) {
449 if (options.use_simd) {
450 KokkosBatched::BatchedGemm<N, T, BatchLayout::Right>(&batchedGemmHandle, gemm_args.alpha, gemm_args.Av.vec_3d,
451 gemm_args.Bv.vec_3d, gemm_args.beta, gemm_args.Cv.vec_3d);
452 } else if constexpr (std::is_same_v<typename view_type_3d::array_layout, Kokkos::LayoutLeft>) {
453 KokkosBatched::BatchedGemm<N, T, BatchLayout::Right>(&batchedGemmHandle, gemm_args.alpha, gemm_args.A,
454 gemm_args.B, gemm_args.beta, gemm_args.C);
455 }
456 } else if (options.use_simd) {
457 KokkosBatched::BatchedGemm<N, T, BatchLayout::Left>(&batchedGemmHandle, gemm_args.alpha, gemm_args.Av.vec_3d,
458 gemm_args.Bv.vec_3d, gemm_args.beta, gemm_args.Cv.vec_3d);
459 } else if constexpr (std::is_same_v<typename view_type_3d::array_layout, Kokkos::LayoutRight>) {
460 KokkosBatched::BatchedGemm<N, T, BatchLayout::Left>(&batchedGemmHandle, gemm_args.alpha, gemm_args.A, gemm_args.B,
461 gemm_args.beta, gemm_args.C);
462 }
463 //} else if (a == 'N' && b == 'C') {
464 // __do_gemm_serial_batched_template<N, C, algo_type>(options, gemm_args);
465 } else if (a == 'T' && b == 'N') {
466 if constexpr (std::is_same_v<typename vector_view_type_3d::array_layout, Kokkos::LayoutLeft>) {
467 if (options.use_simd) {
468 KokkosBatched::BatchedGemm<T, N, BatchLayout::Right>(&batchedGemmHandle, gemm_args.alpha, gemm_args.Av.vec_3d,
469 gemm_args.Bv.vec_3d, gemm_args.beta, gemm_args.Cv.vec_3d);
470 } else if constexpr (std::is_same_v<typename view_type_3d::array_layout, Kokkos::LayoutLeft>) {
471 KokkosBatched::BatchedGemm<T, N, BatchLayout::Right>(&batchedGemmHandle, gemm_args.alpha, gemm_args.A,
472 gemm_args.B, gemm_args.beta, gemm_args.C);
473 }
474 } else if (options.use_simd) {
475 KokkosBatched::BatchedGemm<T, N, BatchLayout::Left>(&batchedGemmHandle, gemm_args.alpha, gemm_args.Av.vec_3d,
476 gemm_args.Bv.vec_3d, gemm_args.beta, gemm_args.Cv.vec_3d);
477 } else if constexpr (std::is_same_v<typename view_type_3d::array_layout, Kokkos::LayoutRight>) {
478 KokkosBatched::BatchedGemm<T, N, BatchLayout::Left>(&batchedGemmHandle, gemm_args.alpha, gemm_args.A, gemm_args.B,
479 gemm_args.beta, gemm_args.C);
480 }
481 } else if (a == 'T' && b == 'T') {
482 if constexpr (std::is_same_v<typename vector_view_type_3d::array_layout, Kokkos::LayoutLeft>) {
483 if (options.use_simd) {
484 KokkosBatched::BatchedGemm<T, T, BatchLayout::Right>(&batchedGemmHandle, gemm_args.alpha, gemm_args.Av.vec_3d,
485 gemm_args.Bv.vec_3d, gemm_args.beta, gemm_args.Cv.vec_3d);
486 } else if constexpr (std::is_same_v<typename view_type_3d::array_layout, Kokkos::LayoutLeft>) {
487 KokkosBatched::BatchedGemm<T, T, BatchLayout::Right>(&batchedGemmHandle, gemm_args.alpha, gemm_args.A,
488 gemm_args.B, gemm_args.beta, gemm_args.C);
489 }
490 } else if (options.use_simd) {
491 KokkosBatched::BatchedGemm<T, T, BatchLayout::Left>(&batchedGemmHandle, gemm_args.alpha, gemm_args.Av.vec_3d,
492 gemm_args.Bv.vec_3d, gemm_args.beta, gemm_args.Cv.vec_3d);
493 } else if constexpr (std::is_same_v<typename view_type_3d::array_layout, Kokkos::LayoutRight>) {
494 KokkosBatched::BatchedGemm<T, T, BatchLayout::Left>(&batchedGemmHandle, gemm_args.alpha, gemm_args.A, gemm_args.B,
495 gemm_args.beta, gemm_args.C);
496 }
497 //} else if (a == 'T' && b == 'C') {
498 // __do_gemm_serial_batched_template<T, C, algo_type>(options, gemm_args);
499 //} else if (a == 'C' && b == 'N') {
500 // __do_gemm_serial_batched_template<C, N, algo_type>(options, gemm_args);
501 //} else if (a == 'C' && b == 'T') {
502 // __do_gemm_serial_batched_template<C, T, algo_type>(options, gemm_args);
503 //} else if (a == 'C' && b == 'C') {
504 // __do_gemm_serial_batched_template<C, C, algo_type>(options, gemm_args);
505 } else {
506 FATAL_ERROR("Bad gemm_args TransA or TransB value");
507 }
508 }
509
510 template <class algo_tag, class blocking_type, class device_type, class algo_mode = void>
511 void __do_gemm_parallel_batched_heuristic(options_t options, gemm_args_t gemm_args) {
512 Kokkos::Timer timer;
513
514 for (uint32_t i = 0; i < options.warm_up_n; ++i)
515 __do_gemm_parallel_batched_heuristic_template<algo_tag, blocking_type, device_type, algo_mode>(options, gemm_args);
516 Kokkos::fence();
517
518 timer.reset();
519 for (uint32_t i = 0; i < options.n; ++i)
520 __do_gemm_parallel_batched_heuristic_template<algo_tag, blocking_type, device_type, algo_mode>(options, gemm_args);
521 Kokkos::fence();
522
523 __gemm_output_csv_row(options, gemm_args, timer.seconds(), nullptr, "-", "-", "-");
524 }
525
526 template <class TransAType, class TransBType, class BlockingType>
527 struct parallel_batched_gemm_range_policy {
528 gemm_args_t gemm_args_;
529
530 parallel_batched_gemm_range_policy(gemm_args_t gemm_args) : gemm_args_(gemm_args) {}
531
532 KOKKOS_INLINE_FUNCTION
533 void operator()(const SerialTag &, const int &i) const {
534 auto svA = Kokkos::subview(gemm_args_.A, i, Kokkos::ALL(), Kokkos::ALL());
535 auto svB = Kokkos::subview(gemm_args_.B, i, Kokkos::ALL(), Kokkos::ALL());
536 auto svC = Kokkos::subview(gemm_args_.C, i, Kokkos::ALL(), Kokkos::ALL());
537
538 KokkosBatched::SerialGemm<TransAType, TransBType, BlockingType>::invoke(gemm_args_.alpha, svA, svB, gemm_args_.beta,
539 svC);
540 }
541
542 KOKKOS_INLINE_FUNCTION
543 void operator()(const SerialBatchDim3Tag &, const int &i) const {
544 auto svA = Kokkos::subview(gemm_args_.A, Kokkos::ALL(), Kokkos::ALL(), i);
545 auto svB = Kokkos::subview(gemm_args_.B, Kokkos::ALL(), Kokkos::ALL(), i);
546 auto svC = Kokkos::subview(gemm_args_.C, Kokkos::ALL(), Kokkos::ALL(), i);
547
548 KokkosBatched::SerialGemm<TransAType, TransBType, BlockingType>::invoke(gemm_args_.alpha, svA, svB, gemm_args_.beta,
549 svC);
550 }
551
552 KOKKOS_INLINE_FUNCTION
553 void operator()(const SerialSimdTag &, const int &i) const {
554 auto svA = Kokkos::subview(gemm_args_.Av.vec_3d, i, Kokkos::ALL(), Kokkos::ALL());
555 auto svB = Kokkos::subview(gemm_args_.Bv.vec_3d, i, Kokkos::ALL(), Kokkos::ALL());
556 auto svC = Kokkos::subview(gemm_args_.Cv.vec_3d, i, Kokkos::ALL(), Kokkos::ALL());
557
558 KokkosBatched::SerialGemm<TransAType, TransBType, BlockingType>::invoke(gemm_args_.alpha, svA, svB, gemm_args_.beta,
559 svC);
560 }
561
562 KOKKOS_INLINE_FUNCTION
563 void operator()(const SerialSimdBatchDim3Tag &, const int &i) const {
564 auto svA = Kokkos::subview(gemm_args_.Av.vec_3d, Kokkos::ALL(), Kokkos::ALL(), i);
565 auto svB = Kokkos::subview(gemm_args_.Bv.vec_3d, Kokkos::ALL(), Kokkos::ALL(), i);
566 auto svC = Kokkos::subview(gemm_args_.Cv.vec_3d, Kokkos::ALL(), Kokkos::ALL(), i);
567
568 KokkosBatched::SerialGemm<TransAType, TransBType, BlockingType>::invoke(gemm_args_.alpha, svA, svB, gemm_args_.beta,
569 svC);
570 }
571
572 KOKKOS_INLINE_FUNCTION
573 void operator()(const TeamTag &, const int & /*i*/) const {
574 Kokkos::abort("TeamTag not supported using RangePolicy.");
575 }
576
577 KOKKOS_INLINE_FUNCTION
578 void operator()(const TeamBatchDim3Tag &, const int & /*i*/) const {
579 Kokkos::abort("TeamBatchDim3Tag not supported using RangePolicy.");
580 }
581
582 KOKKOS_INLINE_FUNCTION
583 void operator()(const TeamVectorTag &, const int & /*i*/) const {
584 Kokkos::abort("TeamVectorTag not supported using RangePolicy.");
585 }
586
587 KOKKOS_INLINE_FUNCTION
588 void operator()(const TeamVectorBatchDim3Tag &, const int & /*i*/) const {
589 Kokkos::abort("TeamVectorBatchDim3Tag not supported using RangePolicy.");
590 }
591
592 KOKKOS_INLINE_FUNCTION
593 void operator()(const TeamSimdTag &, const int & /*i*/) const {
594 Kokkos::abort("TeamSimdTag not supported using RangePolicy.");
595 }
596
597 KOKKOS_INLINE_FUNCTION
598 void operator()(const TeamSimdBatchDim4Tag &, const int & /*i*/) const {
599 Kokkos::abort("TeamSimdBatchDim4Tag not supported using RangePolicy.");
600 }
601 };
602
603 template <class MemberType, class TransAType, class TransBType, class BlockingType, class AlgoMode = void>
604 struct parallel_batched_gemm {
605 gemm_args_t gemm_args_;
606
607 parallel_batched_gemm(gemm_args_t gemm_args) : gemm_args_(gemm_args) {}
608
609 KOKKOS_INLINE_FUNCTION
610 void operator()(const SerialTag &, const MemberType &member) const {
611 auto i = member.league_rank();
612 auto svA = Kokkos::subview(gemm_args_.A, i, Kokkos::ALL(), Kokkos::ALL());
613 auto svB = Kokkos::subview(gemm_args_.B, i, Kokkos::ALL(), Kokkos::ALL());
614 auto svC = Kokkos::subview(gemm_args_.C, i, Kokkos::ALL(), Kokkos::ALL());
615
616 KokkosBatched::SerialGemm<TransAType, TransBType, BlockingType>::invoke(gemm_args_.alpha, svA, svB, gemm_args_.beta,
617 svC);
618 }
619
620 KOKKOS_INLINE_FUNCTION
621 void operator()(const SerialBatchDim3Tag &, const MemberType &member) const {
622 auto i = member.league_rank();
623 auto svA = Kokkos::subview(gemm_args_.A, Kokkos::ALL(), Kokkos::ALL(), i);
624 auto svB = Kokkos::subview(gemm_args_.B, Kokkos::ALL(), Kokkos::ALL(), i);
625 auto svC = Kokkos::subview(gemm_args_.C, Kokkos::ALL(), Kokkos::ALL(), i);
626
627 KokkosBatched::SerialGemm<TransAType, TransBType, BlockingType>::invoke(gemm_args_.alpha, svA, svB, gemm_args_.beta,
628 svC);
629 }
630
631 KOKKOS_INLINE_FUNCTION
632 void operator()(const TeamTag &, const MemberType &member) const {
633 auto i = member.league_rank();
634 auto svA = Kokkos::subview(gemm_args_.A, i, Kokkos::ALL(), Kokkos::ALL());
635 auto svB = Kokkos::subview(gemm_args_.B, i, Kokkos::ALL(), Kokkos::ALL());
636 auto svC = Kokkos::subview(gemm_args_.C, i, Kokkos::ALL(), Kokkos::ALL());
637
638 KokkosBatched::TeamGemm<MemberType, TransAType, TransBType, BlockingType>::invoke(member, gemm_args_.alpha, svA,
639 svB, gemm_args_.beta, svC);
640 }
641
642 KOKKOS_INLINE_FUNCTION
643 void operator()(const TeamBatchDim3Tag &, const MemberType &member) const {
644 auto i = member.league_rank();
645 auto svA = Kokkos::subview(gemm_args_.A, Kokkos::ALL(), Kokkos::ALL(), i);
646 auto svB = Kokkos::subview(gemm_args_.B, Kokkos::ALL(), Kokkos::ALL(), i);
647 auto svC = Kokkos::subview(gemm_args_.C, Kokkos::ALL(), Kokkos::ALL(), i);
648
649 KokkosBatched::TeamGemm<MemberType, TransAType, TransBType, BlockingType>::invoke(member, gemm_args_.alpha, svA,
650 svB, gemm_args_.beta, svC);
651 }
652
653 KOKKOS_INLINE_FUNCTION
654 void operator()(const TeamVectorTag &, const MemberType &member) const {
655 auto team_idx = member.league_rank();
656 auto svA = Kokkos::subview(gemm_args_.A, team_idx, Kokkos::ALL(), Kokkos::ALL());
657 auto svB = Kokkos::subview(gemm_args_.B, team_idx, Kokkos::ALL(), Kokkos::ALL());
658 auto svC = Kokkos::subview(gemm_args_.C, team_idx, Kokkos::ALL(), Kokkos::ALL());
659
660 KokkosBatched::TeamVectorGemm<MemberType, TransAType, TransBType, BlockingType>::invoke(
661 member, gemm_args_.alpha, svA, svB, gemm_args_.beta, svC);
662 }
663
664 KOKKOS_INLINE_FUNCTION
665 void operator()(const TeamVectorBatchDim3Tag &, const MemberType &member) const {
666 auto team_idx = member.league_rank();
667 auto svA = Kokkos::subview(gemm_args_.A, Kokkos::ALL(), Kokkos::ALL(), team_idx);
668 auto svB = Kokkos::subview(gemm_args_.B, Kokkos::ALL(), Kokkos::ALL(), team_idx);
669 auto svC = Kokkos::subview(gemm_args_.C, Kokkos::ALL(), Kokkos::ALL(), team_idx);
670
671 KokkosBatched::TeamVectorGemm<MemberType, TransAType, TransBType, BlockingType>::invoke(
672 member, gemm_args_.alpha, svA, svB, gemm_args_.beta, svC);
673 }
674
675 KOKKOS_INLINE_FUNCTION
676 void operator()(const TeamSimdTag &, const MemberType &member) const {
677 auto i = member.league_rank();
678 Kokkos::parallel_for(
679 Kokkos::ThreadVectorRange(member, gemm_args_.Cv.ivec_4d.extent(3)), [&](const int &vector_lane) {
680 auto svA = Kokkos::subview(gemm_args_.Av.ivec_4d, i, Kokkos::ALL(), Kokkos::ALL(), vector_lane);
681 auto svB = Kokkos::subview(gemm_args_.Bv.ivec_4d, i, Kokkos::ALL(), Kokkos::ALL(), vector_lane);
682 auto svC = Kokkos::subview(gemm_args_.Cv.ivec_4d, i, Kokkos::ALL(), Kokkos::ALL(), vector_lane);
683
684 KokkosBatched::Gemm<MemberType, TransAType, TransBType, AlgoMode, BlockingType>::invoke(
685 member, gemm_args_.alpha, svA, svB, gemm_args_.beta, svC);
686 });
687 }
688
689 KOKKOS_INLINE_FUNCTION
690 void operator()(const TeamSimdBatchDim4Tag &, const MemberType &member) const {
691 auto i = member.league_rank();
692 Kokkos::parallel_for(
693 Kokkos::ThreadVectorRange(member, gemm_args_.Cv.ivec_4d.extent(0)), [&](const int &vector_lane) {
694 auto svA = Kokkos::subview(gemm_args_.Av.ivec_4d, vector_lane, Kokkos::ALL(), Kokkos::ALL(), i);
695 auto svB = Kokkos::subview(gemm_args_.Bv.ivec_4d, vector_lane, Kokkos::ALL(), Kokkos::ALL(), i);
696 auto svC = Kokkos::subview(gemm_args_.Cv.ivec_4d, vector_lane, Kokkos::ALL(), Kokkos::ALL(), i);
697
698 KokkosBatched::Gemm<MemberType, TransAType, TransBType, AlgoMode, BlockingType>::invoke(
699 member, gemm_args_.alpha, svA, svB, gemm_args_.beta, svC);
700 });
701 }
702
703 KOKKOS_INLINE_FUNCTION
704 void operator()(const SerialSimdTag &, const MemberType & /*member*/) const {
705 Kokkos::abort("SerialSimdTag not supported using RangePolicy.");
706 }
707
708 KOKKOS_INLINE_FUNCTION
709 void operator()(const SerialSimdBatchDim3Tag &, const MemberType & /*member*/) const {
710 Kokkos::abort("SerialSimdBatchDim3Tag not supported using RangePolicy.");
711 }
712 };
713
714 template <class TransAType, class TransBType, class BlockingType, class AlgoTag, class device_type>
715 void __do_gemm_parallel_batched_template_range_policy(options_t options, gemm_args_t gemm_args) {
716 using execution_space = typename device_type::execution_space;
717 using policy_type = Kokkos::RangePolicy<AlgoTag, execution_space>;
718 using functor_type = parallel_batched_gemm_range_policy<TransAType, TransBType, BlockingType>;
719
720 uint32_t warm_up_n = options.warm_up_n;
721 uint32_t n = options.n;
722 auto batch_size = options.start.c.k;
723 Kokkos::Timer timer;
724
725 STATUS;
726
727 functor_type parallel_batched_gemm_functor(gemm_args);
728
729 if (std::is_same<AlgoTag, SerialSimdTag>::value || std::is_same<AlgoTag, SerialSimdBatchDim3Tag>::value) {
730 batch_size = options.blas_args.batch_size_last_dim ? gemm_args.Cv.vec_3d.extent(2) : gemm_args.Cv.vec_3d.extent(0);
731 }
732
733 for (uint32_t i = 0; i < warm_up_n; i++) {
734 Kokkos::parallel_for("parallelBatchedWarmUpLoopGemm", policy_type(0, batch_size), parallel_batched_gemm_functor);
735 Kokkos::fence();
736 }
737
738 timer.reset();
739 for (uint32_t i = 0; i < n; i++) {
740 Kokkos::parallel_for("parallelBatchedTimedLoopGemm", policy_type(0, batch_size), parallel_batched_gemm_functor);
741 Kokkos::fence();
742 }
743
744 __gemm_output_csv_row(options, gemm_args, timer.seconds());
745
746 return;
747 }
748
749 template <class TransAType, class TransBType, class BlockingType, class AlgoTag, class device_type,
750 class algo_mode = void>
751 void __do_gemm_parallel_batched_template(options_t options, gemm_args_t gemm_args) {
752 using execution_space = typename device_type::execution_space;
753 using policy_type = Kokkos::TeamPolicy<AlgoTag, execution_space>;
754 using member_type = typename policy_type::member_type;
755 using functor_type = parallel_batched_gemm<member_type, TransAType, TransBType, BlockingType, algo_mode>;
756
757 uint32_t warm_up_n = options.warm_up_n;
758 uint32_t n = options.n;
759 auto league_size = options.start.c.k;
760 auto team_size = gemm_args.bp.team_size;
761 auto vector_len = gemm_args.bp.vector_len;
762 Kokkos::Timer timer;
763
764 if (std::is_same<AlgoTag, SerialTag>::value || std::is_same<AlgoTag, SerialBatchDim3Tag>::value ||
765 std::is_same<AlgoTag, SerialSimdTag>::value || std::is_same<AlgoTag, SerialSimdBatchDim3Tag>::value) {
766 return __do_gemm_parallel_batched_template_range_policy<TransAType, TransBType, BlockingType, AlgoTag, device_type>(
767 options, gemm_args);
768 }
769
770 if (std::is_same<AlgoTag, TeamSimdTag>::value || std::is_same<AlgoTag, TeamSimdBatchDim4Tag>::value) {
771 league_size =
772 options.blas_args.batch_size_last_dim ? gemm_args.Cv.ivec_4d.extent(3) : gemm_args.Cv.ivec_4d.extent(0);
773 vector_len = simd_vector_size / simd_internal_vector_size; // TODO: use bp.vector_len?
774 }
775
776 STATUS;
777
778 functor_type parallel_batched_gemm_functor(gemm_args);
779
780 if (options.blas_args.use_auto) {
781 for (uint32_t i = 0; i < warm_up_n; i++) {
782 Kokkos::parallel_for("parallelBatchedWarmUpLoopGemm", policy_type(league_size, Kokkos::AUTO, Kokkos::AUTO),
783 parallel_batched_gemm_functor);
784 Kokkos::fence();
785 }
786
787 timer.reset();
788 for (uint32_t i = 0; i < n; i++) {
789 Kokkos::parallel_for("parallelBatchedTimedLoopGemm", policy_type(league_size, Kokkos::AUTO, Kokkos::AUTO),
790 parallel_batched_gemm_functor);
791 Kokkos::fence();
792 }
793 } else {
794 for (uint32_t i = 0; i < warm_up_n; i++) {
795 Kokkos::parallel_for("parallelBatchedWarmUpLoopGemm", policy_type(league_size, team_size, vector_len),
796 parallel_batched_gemm_functor);
797 Kokkos::fence();
798 }
799
800 timer.reset();
801 for (uint32_t i = 0; i < n; i++) {
802 Kokkos::parallel_for("parallelBatchedTimedLoopGemm", policy_type(league_size, team_size, vector_len),
803 parallel_batched_gemm_functor);
804 Kokkos::fence();
805 }
806 }
807
808 __gemm_output_csv_row(options, gemm_args, timer.seconds());
809
810 return;
811 }
812
813 template <class algo_tag, class blocking_type, class device_type, class algo_mode = void>
814 void __do_gemm_parallel_batched(options_t options, gemm_args_t gemm_args) {
815 char a = gemm_args.transA;
816 char b = gemm_args.transB;
817 using N = KokkosBatched::Trans::NoTranspose;
818 using T = KokkosBatched::Trans::Transpose;
819 // using C = KokkosBatched::Trans::ConjTranspose;
820
821 STATUS;
822
823 if (a == 'N' && b == 'N') {
824 __do_gemm_parallel_batched_template<N, N, blocking_type, algo_tag, device_type, algo_mode>(options, gemm_args);
825 } else if (a == 'N' && b == 'T') {
826 __do_gemm_parallel_batched_template<N, T, blocking_type, algo_tag, device_type, algo_mode>(options, gemm_args);
827 //} else if (a == 'N' && b == 'C') {
828 // __do_gemm_parallel_batched_template<N, C, blocking_type, algo_tag,
829 // device_type>(options, gemm_args);
830 } else if (a == 'T' && b == 'N') {
831 __do_gemm_parallel_batched_template<T, N, blocking_type, algo_tag, device_type, algo_mode>(options, gemm_args);
832 } else if (a == 'T' && b == 'T') {
833 __do_gemm_parallel_batched_template<T, T, blocking_type, algo_tag, device_type, algo_mode>(options, gemm_args);
834 //} else if (a == 'T' && b == 'C') {
835 // __do_gemm_parallel_batched_template<T, C, blocking_type, algo_tag,
836 // device_type>(options, gemm_args);
837 //} else if (a == 'C' && b == 'N') {
838 // __do_gemm_parallel_batched_template<C, N, blocking_type, algo_tag,
839 // device_type>(options, gemm_args);
840 //} else if (a == 'C' && b == 'T') {
841 // __do_gemm_parallel_batched_template<C, T, blocking_type, algo_tag,
842 // device_type>(options, gemm_args);
843 //} else if (a == 'C' && b == 'C') {
844 // __do_gemm_parallel_batched_template<C, C, blocking_type, algo_tag,
845 // device_type>(options, gemm_args);
846 } else {
847 FATAL_ERROR("Bad gemm_args TransA or TransB value");
848 }
849
850 return;
851 }
852
853 template <class TransAType, class TransBType, class BlockingType>
854 struct parallel_batched_gemm_experiment1 {
855 gemm_args_t gemm_args_;
856
857 parallel_batched_gemm_experiment1(gemm_args_t gemm_args) : gemm_args_(gemm_args) {}
858
859 KOKKOS_INLINE_FUNCTION
860
861 void operator()(const SerialTag &, const int &i) const {
862 auto svA = Kokkos::subview(gemm_args_.A, i, Kokkos::ALL(), Kokkos::ALL());
863 auto svB = Kokkos::subview(gemm_args_.B, i, Kokkos::ALL(), Kokkos::ALL());
864 auto svC = Kokkos::subview(gemm_args_.C, i, Kokkos::ALL(), Kokkos::ALL());
865
866 // Uses two serial for-loops internally
867 KokkosBatched::SerialGemm<TransAType, TransBType, BlockingType>::invoke(gemm_args_.alpha, svA, svB, gemm_args_.beta,
868 svC);
869 }
870 };
871
872 /**
873 * 1. parallel_for(rangePolicy<Kokkos::DefaultExecutionSpace>(N)): serialGemm
874 *
875 */
876 template <class TransAType, class TransBType, class BlockingType, class device_type>
877 void __do_gemm_parallel_experiment1(options_t options, gemm_args_t gemm_args) {
878 using execution_space = typename device_type::execution_space;
879 using policy_type = Kokkos::RangePolicy<SerialTag, execution_space>;
880 using functor_type = parallel_batched_gemm_experiment1<TransAType, TransBType, BlockingType>;
881
882 uint32_t warm_up_n = options.warm_up_n;
883 uint32_t n = options.n;
884 auto k = options.start.c.k;
885 Kokkos::Timer timer;
886 STATUS;
887
888 functor_type experiment1_functor(gemm_args);
889
890 for (uint32_t i = 0; i < warm_up_n; ++i) {
891 Kokkos::parallel_for("parallelBatchedUntimedExperiment1Gemm", policy_type(0, k), experiment1_functor);
892 }
893 Kokkos::fence();
894
895 timer.reset();
896 for (uint32_t i = 0; i < n; ++i) {
897 Kokkos::parallel_for("parallelBatchedTimedExperiment1Gemm", policy_type(0, k), experiment1_functor);
898 }
899 Kokkos::fence();
900
901 __gemm_output_csv_row(options, gemm_args, timer.seconds(), "experiment1");
902 return;
903 }
904
905 template <class TransAType, class TransBType, class BlockingType, class MemberType>
906 struct parallel_batched_gemm_experiment2_3_4 {
907 gemm_args_t gemm_args_;
908
909 parallel_batched_gemm_experiment2_3_4(gemm_args_t gemm_args) : gemm_args_(gemm_args) {}
910
911 // Experiment 2
912 KOKKOS_INLINE_FUNCTION
913 void operator()(const TeamVectorTag &, const MemberType &member) const {
914 auto i = member.league_rank();
915 auto svA = Kokkos::subview(gemm_args_.A, i, Kokkos::ALL(), Kokkos::ALL());
916 auto svB = Kokkos::subview(gemm_args_.B, i, Kokkos::ALL(), Kokkos::ALL());
917 auto svC = Kokkos::subview(gemm_args_.C, i, Kokkos::ALL(), Kokkos::ALL());
918
919 // Uses TeamThreadRange over C-rows
920 // ThreadVectorRange over C-cols
921 KokkosBatched::TeamVectorGemm<MemberType, TransAType, TransBType, BlockingType>::invoke(
922 member, gemm_args_.alpha, svA, svB, gemm_args_.beta, svC);
923 }
924
925 // Experiment 3
926 KOKKOS_INLINE_FUNCTION
927 void operator()(const LayoutLeftTag &, const MemberType &member) const {
928 auto team_idx = member.league_rank();
929 auto svA = Kokkos::subview(gemm_args_.A, team_idx, Kokkos::ALL(), Kokkos::ALL());
930 auto svB = Kokkos::subview(gemm_args_.B, team_idx, Kokkos::ALL(), Kokkos::ALL());
931 auto svC = Kokkos::subview(gemm_args_.C, team_idx, Kokkos::ALL(), Kokkos::ALL());
932
933 // TeamThreadRange: splits the index range over the threads of the team
934 // ThreadVectorRange: splits the index range over the vector lanes of the
935 // calling thread
936
937 auto svC_cols = svC.extent(1);
938 // In a given team, for each vector lane, compute zero or more output
939 // columns of C depending on the index range
940 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, svC_cols), [&](const int &lane_idx) {
941 auto svB_col = Kokkos::subview(svB, Kokkos::ALL(), lane_idx);
942 auto svC_col = Kokkos::subview(svC, Kokkos::ALL(), lane_idx);
943 // TeamGemm Calls TeamThreadRange over M*N meaning the flat M*N array
944 // is split over all threads of the team
945 KokkosBatched::TeamGemm<MemberType, TransAType, TransBType, BlockingType>::invoke(
946 member, gemm_args_.alpha, svA, svB_col, gemm_args_.beta, svC_col);
947 });
948 }
949
950 // TODO: Why is this faster than the LayoutLeftTag operator above for both
951 // LayoutLeft and LayoutRight? Experiment 4
952 KOKKOS_INLINE_FUNCTION
953 void operator()(const LayoutRightTag &, const MemberType &member) const {
954 auto team_idx = member.league_rank();
955 auto svA = Kokkos::subview(gemm_args_.A, team_idx, Kokkos::ALL(), Kokkos::ALL());
956 auto svB = Kokkos::subview(gemm_args_.B, team_idx, Kokkos::ALL(), Kokkos::ALL());
957 auto svC = Kokkos::subview(gemm_args_.C, team_idx, Kokkos::ALL(), Kokkos::ALL());
958
959 // TeamThreadRange: splits the index range over the threads of the team
960 // ThreadVectorRange: splits the index range over the vector lanes of the
961 // calling thread
962
963 auto svC_rows = svC.extent(0);
964 // In a given team, for each vector lane, compute zero or more output rows
965 // of C depending on the index range
966 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, svC_rows), [&](const int &lane_idx) {
967 auto svA_row = Kokkos::subview(svA, lane_idx, Kokkos::ALL());
968 auto svC_row = Kokkos::subview(svC, lane_idx, Kokkos::ALL());
969 // TeamGemm Calls TeamThreadRange over M*N meaning the flat M*N array
970 // is split over all threads of the team
971 KokkosBatched::TeamGemm<MemberType, TransAType, TransBType, BlockingType>::invoke(
972 member, gemm_args_.alpha, svA_row, svB, gemm_args_.beta, svC_row);
973 });
974 }
975 };
976
977 /**
978 * 2. case a)
979 * parallel_for(teamPolicy): TeamVectorGemm
980 *
981 */
982 template <class TransAType, class TransBType, class BlockingType, class device_type>
983 void __do_gemm_parallel_experiment2(options_t options, gemm_args_t gemm_args) {
984 using execution_space = typename device_type::execution_space;
985 using policy_type = Kokkos::TeamPolicy<TeamVectorTag, execution_space>;
986 using member_type = typename policy_type::member_type;
987 using functor_type = parallel_batched_gemm_experiment2_3_4<TransAType, TransBType, BlockingType, member_type>;
988
989 uint32_t warm_up_n = options.warm_up_n;
990 uint32_t n = options.n;
991 auto league_size = options.start.c.k;
992 Kokkos::Timer timer;
993 STATUS;
994
995 functor_type experiment2_functor(gemm_args);
996
997 auto team_size = gemm_args.bp.team_size;
998 auto vector_len = gemm_args.bp.vector_len;
999
1000 for (uint32_t i = 0; i < warm_up_n; ++i) {
1001 Kokkos::parallel_for("parallelBatchedUntimedExperiment2Gemm", policy_type(league_size, team_size, vector_len),
1002 experiment2_functor);
1003 }
1004 Kokkos::fence();
1005
1006 timer.reset();
1007
1008 for (uint32_t i = 0; i < n; ++i) {
1009 Kokkos::parallel_for("parallelBatchedTimedExperiment2Gemm", policy_type(league_size, team_size, vector_len),
1010 experiment2_functor);
1011 }
1012 Kokkos::fence();
1013
1014 __gemm_output_csv_row(options, gemm_args, timer.seconds(), "experiment2");
1015 return;
1016 }
1017
1018 /**
1019 * 3. case b)
1020 * parallel_for(teamPolicy):
1021 * parallel_for(TeamThreadRange):
1022 * VectorGemm
1023 *
1024 * VectorGemm has not been implemented!
1025 * I think this experiment can be removed. TeamGemm calls TeamThreadRange
1026 * internally! TeamVectorGemm calls both TeamThreadRange and ThreadVectorRange
1027 * internally!
1028 */
1029 template <class TransAType, class TransBType, class BlockingType, class device_type>
1030 void __do_gemm_parallel_experiment3(options_t options, gemm_args_t gemm_args) {
1031 using execution_space = typename device_type::execution_space;
1032 // using layout_tag = std::conditional<std::is_same<default_layout,
1033 // Kokkos::LayoutLeft>::value, LayoutLeftTag, LayoutRightTag>::type;
1034 using policy_type = Kokkos::TeamPolicy<LayoutLeftTag, execution_space>;
1035 using member_type = typename policy_type::member_type;
1036 using functor_type = parallel_batched_gemm_experiment2_3_4<TransAType, TransBType, BlockingType, member_type>;
1037
1038 uint32_t warm_up_n = options.warm_up_n;
1039 uint32_t n = options.n;
1040 auto league_size = options.start.c.k;
1041 Kokkos::Timer timer;
1042 STATUS;
1043
1044 functor_type experiment3_functor(gemm_args);
1045
1046 auto team_size = gemm_args.bp.team_size;
1047 auto vector_len = gemm_args.bp.vector_len;
1048
1049 for (uint32_t i = 0; i < warm_up_n; ++i) {
1050 Kokkos::parallel_for("parallelBatchedUntimedExperiment3Gemm", policy_type(league_size, team_size, vector_len),
1051 experiment3_functor);
1052 }
1053 Kokkos::fence();
1054
1055 timer.reset();
1056
1057 for (uint32_t i = 0; i < n; ++i) {
1058 Kokkos::parallel_for("parallelBatchedTimedExperiment3Gemm", policy_type(league_size, team_size, vector_len),
1059 experiment3_functor);
1060 }
1061 Kokkos::fence();
1062
1063 __gemm_output_csv_row(options, gemm_args, timer.seconds(), "experiment3");
1064 return;
1065 }
1066
1067 /**
1068 * 4. case c)
1069 * parallel_for(teamPolicy):
1070 * parallel_for(ThreadVectorRange)
1071 * TeamGemm
1072 */
1073 template <class TransAType, class TransBType, class BlockingType, class device_type>
1074 void __do_gemm_parallel_experiment4(options_t options, gemm_args_t gemm_args) {
1075 using execution_space = typename device_type::execution_space;
1076 // using layout_tag = std::conditional<std::is_same<default_layout,
1077 // Kokkos::LayoutLeft>::value, LayoutLeftTag, LayoutRightTag>::type;
1078 using policy_type = Kokkos::TeamPolicy<LayoutRightTag, execution_space>;
1079 using member_type = typename policy_type::member_type;
1080 using functor_type = parallel_batched_gemm_experiment2_3_4<TransAType, TransBType, BlockingType, member_type>;
1081
1082 uint32_t warm_up_n = options.warm_up_n;
1083 uint32_t n = options.n;
1084 auto league_size = options.start.c.k;
1085 Kokkos::Timer timer;
1086 STATUS;
1087
1088 functor_type experiment4_functor(gemm_args);
1089
1090 auto team_size = gemm_args.bp.team_size;
1091 auto vector_len = gemm_args.bp.vector_len;
1092
1093 for (uint32_t i = 0; i < warm_up_n; ++i) {
1094 Kokkos::parallel_for("parallelBatchedUntimedExperiment4Gemm", policy_type(league_size, team_size, vector_len),
1095 experiment4_functor);
1096 }
1097 Kokkos::fence();
1098
1099 timer.reset();
1100
1101 for (uint32_t i = 0; i < n; ++i) {
1102 Kokkos::parallel_for("parallelBatchedTimedExperiment4Gemm", policy_type(league_size, team_size, vector_len),
1103 experiment4_functor);
1104 }
1105 Kokkos::fence();
1106
1107 __gemm_output_csv_row(options, gemm_args, timer.seconds(), "experiment4");
1108 return;
1109 }
1110
1111 template <class SimdViewType, class TransAType, class TransBType, class BlockingType>
1112 class parallel_batched_gemm_experiment5 {
1113 private:
1114 SimdViewType &A, &B, &C;
1115 gemm_args_t gemm_args;
1116
1117 public:
1118 parallel_batched_gemm_experiment5(SimdViewType &_A, SimdViewType &_B, SimdViewType &_C, gemm_args_t _gemm_args)
1119 : A(_A), B(_B), C(_C), gemm_args(_gemm_args) {}
1120
1121 KOKKOS_INLINE_FUNCTION
1122 void operator()(const SimdCpuTag &, const int &i) const {
1123 auto svA = Kokkos::subview(A, i, Kokkos::ALL(), Kokkos::ALL());
1124 auto svB = Kokkos::subview(B, i, Kokkos::ALL(), Kokkos::ALL());
1125 auto svC = Kokkos::subview(C, i, Kokkos::ALL(), Kokkos::ALL());
1126
1127 // Uses two serial for-loops internally
1128 KokkosBatched::SerialGemm<TransAType, TransBType, BlockingType>::invoke(gemm_args.alpha, svA, svB, gemm_args.beta,
1129 svC);
1130 }
1131 };
1132
1133 /**
1134 * 5.
1135 * parallel_for(RangePolicy<Kokkos:DefaultHostExecutionSpace>(N/vl+(N%vl>0)>):
1136 * serialGemm
1137 *
1138 * Not portable to GPU
1139 */
1140 #if !defined(KOKKOS_ENABLE_CUDA) && !defined(KOKKOS_ENABLE_HIP)
1141 template <class TransAType, class TransBType, class BlockingType, class device_type>
1142 void __do_gemm_parallel_experiment5(options_t options, gemm_args_t gemm_args) {
1143 using execution_space = typename device_type::execution_space;
1144 using policy_type = Kokkos::RangePolicy<SimdCpuTag, execution_space>;
1145
1146 // Construct the SimdType
1147 using scalar_type = typename view_type_3d::value_type;
1148 constexpr int vl = KokkosBatched::DefaultVectorLength<scalar_type, execution_space>::value;
1149 using simd_type = KokkosBatched::Vector<KokkosBatched::SIMD<scalar_type>, simd_vector_size>;
1150 using simd_view_type = Kokkos::View<simd_type ***, KokkosKernels::default_layout, KokkosKernels::default_device>;
1151 using functor_type = parallel_batched_gemm_experiment5<simd_view_type, TransAType, TransBType, BlockingType>;
1152
1153 uint32_t warm_up_n = options.warm_up_n;
1154 uint32_t n = options.n;
1155 auto k = options.start.c.k;
1156 Kokkos::Timer timer;
1157 auto simd_batch_size = k / vl + (k % vl > 0);
1158 STATUS;
1159
1160 // Increases each array size by sizeof(scalar_type) * (vl-1) bytes!
1161 simd_view_type A("A", simd_batch_size, gemm_args.A.extent(0), gemm_args.A.extent(1));
1162 simd_view_type B("B", simd_batch_size, gemm_args.B.extent(0), gemm_args.B.extent(1));
1163 simd_view_type C("C", simd_batch_size, gemm_args.C.extent(0), gemm_args.C.extent(1));
1164
1165 // uint64_t seed =
1166 // std::chrono::high_resolution_clock::now().time_since_epoch().count();
1167 // Kokkos::Random_XorShift64_Pool<execution_space> rand_pool(seed);
1168 // Kokkos::fill_random(A, rand_pool,
1169 // Kokkos::rand<Kokkos::Random_XorShift64<execution_space>,
1170 // simd_type>::max()); Kokkos::fill_random(B, rand_pool,
1171 // Kokkos::rand<Kokkos::Random_XorShift64<execution_space>,
1172 // simd_type>::max()); Kokkos::fill_random(C, rand_pool,
1173 // Kokkos::rand<Kokkos::Random_XorShift64<execution_space>,
1174 // simd_type>::max()); execution_space::fence();
1175
1176 functor_type experiment5_functor(A, B, C, gemm_args);
1177
1178 for (uint32_t i = 0; i < warm_up_n; ++i) {
1179 Kokkos::parallel_for("parallelBatchedUntimedExperiment5Gemm", policy_type(0, simd_batch_size), experiment5_functor);
1180 }
1181 Kokkos::fence();
1182
1183 timer.reset();
1184
1185 for (uint32_t i = 0; i < n; ++i) {
1186 Kokkos::parallel_for("parallelBatchedTimedExperiment5Gemm", policy_type(0, simd_batch_size), experiment5_functor);
1187 }
1188 Kokkos::fence();
1189
1190 __gemm_output_csv_row(options, gemm_args, timer.seconds(), "experiment5");
1191 return;
1192 }
1193 #else
1194 template <class TransAType, class TransBType, class BlockingType, class device_type>
1195 void __do_gemm_parallel_experiment5(options_t /*options*/, gemm_args_t /*gemm_args*/) {
1196 std::cerr << std::string(__func__) << " disabled since KOKKOS_ENABLE_CUDA or KOKKOS_ENABLE_HIP is defined."
1197 << std::endl;
1198 return;
1199 }
1200 #endif // !KOKKOS_ENABLE_CUDA || !KOKKOS_ENABLE_HIP
1201
1202 template <class MemberType, class SimdViewType, class TransAType, class TransBType, class BlockingType>
1203 class parallel_batched_gemm_experiment6 {
1204 private:
1205 SimdViewType &A, &B, &C;
1206 gemm_args_t gemm_args;
1207
1208 public:
1209 parallel_batched_gemm_experiment6(SimdViewType &_A, SimdViewType &_B, SimdViewType &_C, gemm_args_t _gemm_args)
1210 : A(_A), B(_B), C(_C), gemm_args(_gemm_args) {}
1211
1212 KOKKOS_INLINE_FUNCTION
1213 void operator()(const MemberType &member) const {
1214 auto i = member.league_rank();
1215 auto svA = Kokkos::subview(A, i, Kokkos::ALL(), Kokkos::ALL());
1216 auto svB = Kokkos::subview(B, i, Kokkos::ALL(), Kokkos::ALL());
1217 auto svC = Kokkos::subview(C, i, Kokkos::ALL(), Kokkos::ALL());
1218
1219 // Uses two serial for-loops internally
1220 KokkosBatched::TeamVectorGemm<MemberType, TransAType, TransBType, BlockingType>::invoke(
1221 member, gemm_args.alpha, svA, svB, gemm_args.beta, svC);
1222 }
1223 };
1224
1225 #if 0
1226 template <class TransAType, class TransBType, class BlockingType,
1227 class device_type>
1228 void __do_gemm_parallel_experiment6(options_t options, gemm_args_t gemm_args) {
1229 using execution_space = typename device_type::execution_space;
1230 using policy_type = Kokkos::TeamPolicy<execution_space>;
1231 using member_type = typename policy_type::member_type;
1232
1233 // Construct the vector type
1234 using scalar_type = typename view_type_3d::value_type;
1235 constexpr int vl =
1236 KokkosBatched::DefaultVectorLength<scalar_type, execution_space>::value;
1237 constexpr int il =
1238 KokkosBatched::DefaultInternalVectorLength<scalar_type, execution_space>::value;
1239 using view_type = Kokkos::View<scalar_type***[vl], KokkosKernels::default_layout, KokkosKernels::default_device>;
1240 using vector_view_type = Kokkos::View<vector_type***, KokkosKernels::default_layout, KokkosKernels::default_device>;
1241 using internal_vector_view_type = Kokkos::View<internal_vector_type***, KokkosKernels::default_layout, KokkosKernels::default_device>;
1242 using functor_type =
1243 parallel_batched_gemm_experiment6<member_type, internal_vector_view_type,
1244 TransAType, TransBType, BlockingType>;
1245
1246 uint32_t warm_up_n = options.warm_up_n;
1247 uint32_t n = options.n;
1248 auto k = options.start.c.k;
1249 Kokkos::Timer timer;
1250 auto simd_batch_size = k / vl + (k % vl > 0);
1251 STATUS;
1252
1253 // Construct matrices
1254 vector_view_type A_vector("A_vector", simd_batch_size, gemm_args.A.extent(0), gemm_args.A.extent(1));
1255 view_type A((scalar_type *)A_vector.data(), simd_batch_size, gemm_args.A.extent(0), gemm_args.A.extent(1));
1256 internal_vector_view_type A_vector_internal(A_vector.data(), simd_batch_size, gemm_args.A.extent(0), gemm_args.A.extent(1));
1257
1258 vector_view_type B_vector("B_vector", simd_batch_size, gemm_args.B.extent(0), gemm_args.B.extent(1));
1259 view_type B((scalar_type *)B_vector.data(), simd_batch_size, gemm_args.B.extent(0), gemm_args.B.extent(1));
1260 internal_vector_view_type B_vector_internal(B_vector.data(), simd_batch_size, gemm_args.B.extent(0), gemm_args.B.extent(1));
1261
1262 vector_view_type C_vector("C_vector", simd_batch_size, gemm_args.C.extent(0), gemm_args.C.extent(1));
1263 view_type C((scalar_type *)C_vector.data(), simd_batch_size, gemm_args.C.extent(0), gemm_args.C.extent(1));
1264 internal_vector_view_type C_vector_internal(C_vector.data(), simd_batch_size, gemm_args.C.extent(0), gemm_args.C.extent(1));
1265
1266 uint64_t seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
1267 Kokkos::Random_XorShift64_Pool<execution_space> rand_pool(seed);
1268 Kokkos::fill_random(A, rand_pool, Kokkos::rand<Kokkos::Random_XorShift64<execution_space>, scalar_type>::max());
1269 Kokkos::fill_random(B, rand_pool, Kokkos::rand<Kokkos::Random_XorShift64<execution_space>, scalar_type>::max());
1270 Kokkos::fill_random(C, rand_pool, Kokkos::rand<Kokkos::Random_XorShift64<execution_space>, scalar_type>::max());
1271 Kokkos::fence();
1272
1273 functor_type experiment6_functor(A_vector_internal, B_vector_internal, C_vector_internal, gemm_args);
1274
1275 for (uint32_t i = 0; i < warm_up_n; ++i) {
1276 Kokkos::parallel_for("parallelBatchedUntimedExperiment6Gemm",
1277 policy_type(simd_batch_size, Kokkos::AUTO, vl/il), experiment6_functor);
1278 Kokkos::fence();
1279 }
1280
1281 timer.reset();
1282 for (uint32_t i = 0; i < n; ++i) {
1283 Kokkos::parallel_for("parallelBatchedTimedExperiment6Gemm",
1284 policy_type(simd_batch_size, Kokkos::AUTO, vl/il), experiment6_functor);
1285 Kokkos::fence();
1286 }
1287
1288 __gemm_output_csv_row(options, gemm_args, timer.seconds(), "experiment6");
1289 return;
1290 }
1291 #else
1292 template <class TransAType, class TransBType, class BlockingType, class device_type>
1293 void __do_gemm_parallel_experiment6(options_t /*options*/, gemm_args_t /*gemm_args*/) {
1294 return;
1295 }
1296 #endif
1297
1298 /**
1299 * examples/armpl_dgemm_interleave_batch_c_example.c was used as a reference
1300 * when writing this.
1301 **/
1302 template <class TransAType, class TransBType, class BlockingType, class device_type>
1303 void __do_gemm_armpl(options_t options, gemm_args_t gemm_args) {
1304 #if defined(KOKKOSKERNELS_ENABLE_TPL_ARMPL) && ARMPL_BUILD >= 1058
1305 uint32_t warm_up_n = options.warm_up_n;
1306 uint32_t n = options.n;
1307 Kokkos::Timer timer;
1308 char transa = std::is_same<TransAType, KokkosBatched::Trans::NoTranspose>::value ? 'N' : 'T';
1309 char transb = std::is_same<TransBType, KokkosBatched::Trans::NoTranspose>::value ? 'N' : 'T';
1310
1311 if (!std::is_same<KokkosKernels::default_scalar, double>::value) FATAL_ERROR("only double scalars are supported!");
1312
1313 STATUS;
1314
1315 for (uint32_t i = 0; i < warm_up_n; i++) {
1316 armpl_dgemm_interleave_batch(gemm_args.ninter, gemm_args.nbatch, transa, transb, gemm_args.dims.c.m,
1317 gemm_args.dims.c.n, gemm_args.dims.a.n, gemm_args.alpha, gemm_args.A_pl.mat,
1318 gemm_args.A_pl.bstrd, gemm_args.A_pl.istrd, gemm_args.A_pl.jstrd, gemm_args.B_pl.mat,
1319 gemm_args.B_pl.bstrd, gemm_args.B_pl.istrd, gemm_args.B_pl.jstrd, gemm_args.beta,
1320 gemm_args.C_pl.mat, gemm_args.C_pl.bstrd, gemm_args.C_pl.istrd, gemm_args.C_pl.jstrd);
1321 }
1322
1323 timer.reset();
1324 for (uint32_t i = 0; i < n; i++) {
1325 armpl_dgemm_interleave_batch(gemm_args.ninter, gemm_args.nbatch, transa, transb, gemm_args.dims.c.m,
1326 gemm_args.dims.c.n, gemm_args.dims.a.n, gemm_args.alpha, gemm_args.A_pl.mat,
1327 gemm_args.A_pl.bstrd, gemm_args.A_pl.istrd, gemm_args.A_pl.jstrd, gemm_args.B_pl.mat,
1328 gemm_args.B_pl.bstrd, gemm_args.B_pl.istrd, gemm_args.B_pl.jstrd, gemm_args.beta,
1329 gemm_args.C_pl.mat, gemm_args.C_pl.bstrd, gemm_args.C_pl.istrd, gemm_args.C_pl.jstrd);
1330 }
1331
1332 __gemm_output_csv_row(options, gemm_args, timer.seconds());
1333 #else
1334 // Cast to void to supress unused param warnings
1335 (void)options;
1336 (void)gemm_args;
1337 std::cerr << std::string(__func__) << " disabled since KOKKOSKERNELS_ENABLE_TPL_ARMPL is undefined." << std::endl;
1338 #endif // KOKKOSKERNELS_ENABLE_TPL_ARMPL
1339 return;
1340 }
1341
1342 /**
1343 * Check difference of scalars expected and actual at indexes i,j,k
1344 * @var expected: The expected result.
1345 * @var actual: The actual result.
1346 * @var epsilon: The tolerance to use when comparing.
1347 * @return true if the comparison fails and false if the comparison succeeds.
1348 */
1349 template <class ViewType>
1350 static inline bool __gemm_print_compare_failure(ViewType h_expected, ViewType h_actual, int i, int j, int k,
1351 double epsilon) {
1352 STATUS;
1353 auto diff = std::fabs(static_cast<double>(h_expected(i, j, k) - h_actual(i, j, k)));
1354
1355 if (diff > epsilon) {
1356 printf("fabs(expected(%d,%d,%d):%g - actual(%d,%d,%d):%g):%g > epsilon:%g\n", i, j, k,
1357 static_cast<double>(h_expected(i, j, k)), i, j, k, static_cast<double>(h_actual(i, j, k)), diff, epsilon);
1358 FATAL_ERROR("Comparison failure!");
1359 return true;
1360 }
1361 return false;
1362 }
1363
1364 /**
1365 * Compare all values of expected with all values of actual.
1366 * @var expected: the expected results
1367 * @var actual: the actual results
1368 * @return false if expected matches actual within epsilon, otherwise true.
1369 */
1370 template <class ScalarType, class LayoutType>
1371 static inline bool __gemm_do_compare(view_type_3d expected, view_type_3d actual) {
1372 double epsilon = Kokkos::ArithTraits<ScalarType>::eps() * 1e3;
1373
1374 STATUS;
1375
1376 typename view_type_3d::HostMirror h_expected = Kokkos::create_mirror_view(expected);
1377 typename view_type_3d::HostMirror h_actual = Kokkos::create_mirror_view(actual);
1378
1379 // Copy to host for comparision
1380 Kokkos::deep_copy(h_expected, expected);
1381 Kokkos::deep_copy(h_actual, actual);
1382 Kokkos::fence();
1383
1384 if (std::is_same<LayoutType, Kokkos::LayoutRight>::value) {
1385 for (size_t i = 0; i < h_expected.extent(0); i++) {
1386 for (size_t j = 0; j < h_expected.extent(1); j++) {
1387 for (size_t k = 0; k < h_expected.extent(2); k++) {
1388 if (__gemm_print_compare_failure<decltype(h_expected)>(h_expected, h_actual, i, j, k, epsilon)) return true;
1389 }
1390 }
1391 }
1392 }
1393
1394 if (std::is_same<LayoutType, Kokkos::LayoutLeft>::value) {
1395 for (size_t k = 0; k < h_expected.extent(2); k++) {
1396 for (size_t j = 0; j < h_expected.extent(1); j++) {
1397 for (size_t i = 0; i < h_expected.extent(0); i++) {
1398 if (__gemm_print_compare_failure<decltype(h_expected)>(h_expected, h_actual, i, j, k, epsilon)) return true;
1399 }
1400 }
1401 }
1402 }
1403
1404 return false;
1405 }
1406
1407 template <class dstViewType>
1408 static inline void __gemm_copy_simd_view_to_3d_view(gemm_simd_args_t src, dstViewType dst, options_t options) {
1409 // clang-format off
1410 // Related issue: https://github.com/kokkos/kokkos-kernels/issues/998
1411 // CUDA VERSION 10.2.2 generates a compiler error:
1412 // KokkosBlas3_gemm_perf_test.hpp: error: ‘h_subview_type_2d’ was not declared in this scope
1413 // clang-format on
1414 #if (CUDA_VERSION != 10020)
1415 using dst_scalar_type = typename dstViewType::value_type;
1416 using src_scalar_type = typename view_type_5d::value_type;
1417 size_t remainder, vector_batch_size, simd_batch_size, last_batch;
1418 bool data_layout_same_as_3d_view = false;
1419 typename dstViewType::HostMirror h_dst = Kokkos::create_mirror_view(dst);
1420 typename view_type_4d::HostMirror h_src = Kokkos::create_mirror_view(src.mat_4d);
1421 Kokkos::deep_copy(h_src, src.mat_4d);
1422 Kokkos::fence();
1423
1424 if (options.blas_args.batch_size_last_dim) {
1425 remainder = dst.extent(2) % simd_internal_vector_size;
1426 vector_batch_size = src.ivec_4d.extent(0);
1427 simd_batch_size = src.ivec_4d.extent(3);
1428 last_batch = dst.extent(2);
1429 if (std::is_same<KokkosKernels::default_layout, Kokkos::LayoutRight>::value && remainder == 0)
1430 data_layout_same_as_3d_view = true;
1431
1432 } else {
1433 remainder = dst.extent(0) % simd_internal_vector_size;
1434 vector_batch_size = src.ivec_4d.extent(3);
1435 simd_batch_size = src.ivec_4d.extent(0);
1436 last_batch = dst.extent(0);
1437 if (std::is_same<KokkosKernels::default_layout, Kokkos::LayoutLeft>::value && remainder == 0)
1438 data_layout_same_as_3d_view = true;
1439 }
1440
1441 // When the batch_size is a multiple of the simd_vector_size and the
1442 // batch_size dimension is nearest to the simd_vector_size dimension, each
1443 // 2-rank matrix lies in the correct location and the data can simply be cast
1444 // to the 3d view.
1445 if (data_layout_same_as_3d_view) {
1446 // We can just re-cast the data to the 3d view but we'll copy it for
1447 // verification
1448 memcpy(h_dst.data(), h_src.data(), sizeof(dst_scalar_type) * dst.extent(0) * dst.extent(1) * dst.extent(2));
1449 Kokkos::deep_copy(dst, h_dst);
1450 Kokkos::fence();
1451 return;
1452 }
1453
1454 // If the remainder is 0, we have simd_vector_size sub-batches to copy out...
1455 // this is a bad data access pattern but for these perf_tests we will support
1456 // it. If the remainder is non-zero, we have simd_vector_size sub-batches +
1457 // remainder to copy out.
1458 remainder += simd_internal_vector_size;
1459
1460 // Views needed for slow manual copy
1461 using h_view_type_5d = Kokkos::View<src_scalar_type *****, KokkosKernels::default_layout, Kokkos::HostSpace>;
1462 using h_subview_type_2d = Kokkos::View<src_scalar_type **, Kokkos::LayoutStride, Kokkos::HostSpace>;
1463 using h_subview_type_3d = Kokkos::View<src_scalar_type ***, Kokkos::LayoutStride, Kokkos::HostSpace>;
1464 using h_subview_type_4d = Kokkos::View<src_scalar_type ****, Kokkos::LayoutStride, Kokkos::HostSpace>;
1465 h_view_type_5d h_src_raw;
1466 h_subview_type_4d h_sv0;
1467 h_subview_type_3d h_sv1;
1468 h_subview_type_2d h_sv2;
1469
1470 // TODO: Clean everything below this point up...
1471 if (std::is_same<KokkosKernels::default_layout, Kokkos::LayoutRight>::value)
1472 h_src_raw = h_view_type_5d((src_scalar_type *)h_src.data(), src.ivec_4d.extent(0), src.ivec_4d.extent(1),
1473 src.ivec_4d.extent(2), src.ivec_4d.extent(3), simd_internal_vector_size);
1474 else
1475 h_src_raw = h_view_type_5d((src_scalar_type *)h_src.data(), simd_internal_vector_size, src.ivec_4d.extent(0),
1476 src.ivec_4d.extent(1), src.ivec_4d.extent(2), src.ivec_4d.extent(3));
1477
1478 // The below loops copies each corresponding 2-rank matrix within the simd
1479 // view back to the 3-rank view.
1480 for (size_t simd_internal_vec_idx = 0; simd_internal_vec_idx < remainder; simd_internal_vec_idx++) {
1481 if (std::is_same<KokkosKernels::default_layout, Kokkos::LayoutRight>::value)
1482 h_sv0 =
1483 Kokkos::subview(h_src_raw, Kokkos::ALL(), Kokkos::ALL(), Kokkos::ALL(), Kokkos::ALL(), simd_internal_vec_idx);
1484 else
1485 h_sv0 =
1486 Kokkos::subview(h_src_raw, simd_internal_vec_idx, Kokkos::ALL(), Kokkos::ALL(), Kokkos::ALL(), Kokkos::ALL());
1487
1488 for (size_t vector_batch_idx = 0; vector_batch_idx < vector_batch_size; vector_batch_idx++) {
1489 if (options.blas_args.batch_size_last_dim)
1490 h_sv1 = Kokkos::subview(h_sv0, vector_batch_idx, Kokkos::ALL(), Kokkos::ALL(), Kokkos::ALL());
1491 else
1492 h_sv1 = Kokkos::subview(h_sv0, Kokkos::ALL(), Kokkos::ALL(), Kokkos::ALL(), vector_batch_idx);
1493 for (size_t simd_batch_size_idx = 0; simd_batch_size_idx < simd_batch_size; simd_batch_size_idx++) {
1494 if (options.blas_args.batch_size_last_dim)
1495 h_sv2 = Kokkos::subview(h_sv1, Kokkos::ALL(), Kokkos::ALL(), simd_batch_size_idx);
1496 else
1497 h_sv2 = Kokkos::subview(h_sv1, simd_batch_size_idx, Kokkos::ALL(), Kokkos::ALL());
1498 for (size_t m = 0; m < src.ivec_4d.extent(1); m++) {
1499 for (size_t n = 0; n < src.ivec_4d.extent(2); n++) {
1500 if (options.blas_args.batch_size_last_dim)
1501 h_dst(m, n, simd_internal_vec_idx + simd_batch_size_idx + vector_batch_idx) = h_sv2(m, n);
1502 else
1503 h_dst(simd_internal_vec_idx + simd_batch_size_idx + vector_batch_idx, m, n) = h_sv2(m, n);
1504 }
1505 }
1506 if (simd_internal_vec_idx + simd_batch_size_idx + vector_batch_idx == last_batch - 1) goto out;
1507 }
1508 }
1509 }
1510 out:
1511 Kokkos::deep_copy(dst, h_dst);
1512 Kokkos::fence();
1513 #else
1514 // Avoid unused parameter warnings:
1515 (void)src;
1516 (void)dst;
1517 (void)options;
1518
1519 Kokkos::abort("Cannot perform simd verification with cuda/10.2.2, rerun with -v 0");
1520 #endif // #if (CUDA_VERSION != 10020)
1521 }
1522
1523 /**
1524 * Compare all values of expected with all values of actual.
1525 * @var expected: the expected results
1526 * @var actual: the actual results
1527 * @return false if expected matches actual within epsilon, otherwise true.
1528 */
1529 template <class ScalarType, class LayoutType>
1530 static inline bool __gemm_do_compare(view_type_3d expected, gemm_simd_args_t actual, options_t options) {
1531 decltype(expected) actual_data("actual_data", expected.extent(0), expected.extent(1), expected.extent(2));
1532
1533 STATUS;
1534
1535 // Copy the simd view to a 3d view for comparision.
1536 // NOTE: The raw results are different when batch_size % simd_vector_size !=
1537 // 0. Also note that when batch_size % simd_vector_size != 0, the simd
1538 // operation calculates results that we do not require. So, we end up running
1539 // an extra batch_size % simd_vector_size GEMMs!
1540 __gemm_copy_simd_view_to_3d_view(actual, actual_data, options);
1541 return __gemm_do_compare<ScalarType, LayoutType>(expected, actual_data);
1542 }
1543
1544 template <class ScalarType, class LayoutType, class DeviceType>
1545 static inline void __gemm_do_verify(options_t options, gemm_args_t gemm_args, void (*fn)(options_t, gemm_args_t)) {
1546 using execution_space = typename DeviceType::execution_space;
1547 // Just create "expected" types using non-simd types.
1548 decltype(gemm_args.C) C_expected;
1549 decltype(gemm_args.A) A_expected;
1550 decltype(gemm_args.B) B_expected;
1551 STATUS;
1552
1553 if (options.blas_args.batch_size_last_dim) {
1554 C_expected = decltype(C_expected)("C_expected", gemm_args.dims.c.m, gemm_args.dims.c.n, gemm_args.dims.c.k);
1555 A_expected = decltype(A_expected)("A_expected", gemm_args.dims.a.m, gemm_args.dims.a.n, gemm_args.dims.a.k);
1556 B_expected = decltype(B_expected)("B_expected", gemm_args.dims.b.m, gemm_args.dims.b.n, gemm_args.dims.b.k);
1557 } else {
1558 C_expected = decltype(C_expected)("C_expected", gemm_args.dims.c.k, gemm_args.dims.c.m, gemm_args.dims.c.n);
1559 A_expected = decltype(A_expected)("A_expected", gemm_args.dims.a.k, gemm_args.dims.a.m, gemm_args.dims.a.n);
1560 B_expected = decltype(B_expected)("B_expected", gemm_args.dims.b.k, gemm_args.dims.b.m, gemm_args.dims.b.n);
1561 }
1562
1563 // Initialize "expected" matrices.
1564 if (gemm_args.C.data() != nullptr) {
1565 Kokkos::deep_copy(C_expected, gemm_args.C);
1566 Kokkos::deep_copy(A_expected, gemm_args.A);
1567 Kokkos::deep_copy(B_expected, gemm_args.B);
1568
1569 Kokkos::fence(); // Ensure that deep_copy has completed
1570
1571 // Check that initial values match
1572 if (__gemm_do_compare<ScalarType, LayoutType>(C_expected, gemm_args.C)) FATAL_ERROR("Inital values mismatch!");
1573 } else if (gemm_args.Cv.vec_3d.data() != nullptr) {
1574 __gemm_copy_simd_view_to_3d_view<decltype(C_expected)>(gemm_args.Cv, C_expected, options);
1575 __gemm_copy_simd_view_to_3d_view<decltype(A_expected)>(gemm_args.Av, A_expected, options);
1576 __gemm_copy_simd_view_to_3d_view<decltype(B_expected)>(gemm_args.Bv, B_expected, options);
1577
1578 // Check that initial values match
1579 if (__gemm_do_compare<ScalarType, LayoutType>(C_expected, gemm_args.Cv, options))
1580 FATAL_ERROR("Inital values mismatch!");
1581 } else {
1582 FATAL_ERROR("Input arguments are empty!");
1583 }
1584
1585 // Populate "expected" matrices via VanillaGemm
1586 Test::Functor_BatchedVanillaGEMM<decltype(A_expected), decltype(B_expected), decltype(C_expected), execution_space>
1587 vgemm;
1588 vgemm.A_t = toupper(gemm_args.transA) == 'T';
1589 vgemm.B_t = toupper(gemm_args.transB) == 'T';
1590 vgemm.A_c = vgemm.B_c = false;
1591 vgemm.batch_size_last_dim = options.blas_args.batch_size_last_dim;
1592 vgemm.A = A_expected;
1593 vgemm.B = B_expected;
1594 vgemm.C = C_expected;
1595 vgemm.alpha = gemm_args.alpha;
1596 vgemm.beta = gemm_args.beta;
1597 vgemm.run(); // Compute C_expected
1598
1599 // Run routine with warm_up_n = 1 and n = 0.
1600 auto warm_up_n_bak = options.warm_up_n;
1601 options.warm_up_n = 1;
1602 auto n_bak = options.n;
1603 options.n = 0;
1604 fn(options, gemm_args);
1605
1606 Kokkos::fence(); // Redundant fence.
1607
1608 // Check the result
1609 if (gemm_args.C.data() != nullptr) {
1610 #if defined(KOKKOSKERNELS_ENABLE_TPL_ARMPL) && ARMPL_BUILD >= 1058
1611 if (options.test == EXPERIMENT) {
1612 using view_type_2d =
1613 Kokkos::View<KokkosKernels::default_scalar **, Kokkos::LayoutStride, KokkosKernels::default_device>;
1614 view_type_2d C;
1615 for (int ib = 0; ib < gemm_args.nbatch; ++ib) {
1616 for (int i = 0; i < gemm_args.ninter; ++i) {
1617 if (options.blas_args.batch_size_last_dim) {
1618 C = Kokkos::subview(gemm_args.C, Kokkos::ALL(), Kokkos::ALL(), ib * gemm_args.ninter + i);
1619 } else {
1620 C = Kokkos::subview(gemm_args.C, ib * gemm_args.ninter + i, Kokkos::ALL(), Kokkos::ALL());
1621 }
1622 auto info = armpl_dge_deinterleave(gemm_args.ninter, i, gemm_args.dims.c.m, gemm_args.dims.c.n, C.data(),
1623 C.stride(0), C.stride(1), &gemm_args.C_pl.mat[gemm_args.C_pl.bstrd * ib],
1624 gemm_args.C_pl.istrd, gemm_args.C_pl.jstrd);
1625 if (info != ARMPL_STATUS_SUCCESS) {
1626 FATAL_ERROR("armpl_dge_deinterleave (C)\n");
1627 }
1628 }
1629 }
1630 }
1631 #endif // KOKKOSKERNELS_ENABLE_TPL_ARMPL && ARMPL_BUILD >= 1058
1632 if (__gemm_do_compare<ScalarType, LayoutType>(C_expected, gemm_args.C)) FATAL_ERROR("Result value mismatch!");
1633 }
1634
1635 if (gemm_args.Cv.vec_3d.data() != nullptr) {
1636 if (__gemm_do_compare<ScalarType, LayoutType>(C_expected, gemm_args.Cv, options))
1637 FATAL_ERROR("Result value mismatch!");
1638 }
1639
1640 // Run actual timed test.
1641 options.verify = false; // Set verify to false for csv output.
1642 options.warm_up_n = warm_up_n_bak;
1643 options.n = n_bak;
1644 fn(options, gemm_args);
1645
1646 // Reset verify for next matrix size.
1647 options.verify = true;
1648 }
1649
1650 /*************************** Internal setup fns **************************/
1651 template <class scalar_type, class vta, class vtb, class vtc, class device_type>
1652 gemm_args_t __do_setup(options_t options, matrix_dims_t dims) {
1653 using execution_space = typename device_type::execution_space;
1654
1655 gemm_args_t gemm_args;
1656 uint64_t seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
1657 Kokkos::Random_XorShift64_Pool<execution_space> rand_pool(seed);
1658 STATUS;
1659
1660 gemm_args.A_pl.mat = nullptr;
1661 gemm_args.B_pl.mat = nullptr;
1662 gemm_args.C_pl.mat = nullptr;
1663 gemm_args.dims = dims;
1664 gemm_args.transA = options.blas_args.gemm.gemm_args.c_str()[0];
1665 gemm_args.transB = options.blas_args.gemm.gemm_args.c_str()[1];
1666 if (options.use_simd) {
1667 // Calculate the batch size for simd views
1668 auto a_simd_batch_size = dims.a.k / simd_vector_size + (dims.a.k % simd_vector_size > 0);
1669 auto b_simd_batch_size = dims.b.k / simd_vector_size + (dims.b.k % simd_vector_size > 0);
1670 auto c_simd_batch_size = dims.c.k / simd_vector_size + (dims.c.k % simd_vector_size > 0);
1671
1672 // Reference gemm simd arguments for allocating A, B, and C matrices
1673 gemm_simd_args_t &A = gemm_args.Av, &B = gemm_args.Bv, &C = gemm_args.Cv;
1674
1675 if (options.blas_args.batch_size_last_dim) {
1676 // Construct simd matrices with batch_size in the last dimension (better
1677 // for LayoutLeft views)
1678 A.vec_3d = vector_view_type_3d("A_vector", dims.a.m, dims.a.n, a_simd_batch_size);
1679 A.mat_4d = view_type_4d((scalar_type *)A.vec_3d.data(), simd_vector_size, dims.a.m, dims.a.n, a_simd_batch_size);
1680 A.ivec_4d = internal_vector_view_type_4d((internal_vector_type *)A.mat_4d.data(),
1681 simd_vector_size / simd_internal_vector_size, dims.a.m, dims.a.n,
1682 a_simd_batch_size);
1683
1684 B.vec_3d = vector_view_type_3d("B_vector", dims.b.m, dims.b.n, b_simd_batch_size);
1685 B.mat_4d = view_type_4d((scalar_type *)B.vec_3d.data(), simd_vector_size, dims.b.m, dims.b.n, b_simd_batch_size);
1686 B.ivec_4d = internal_vector_view_type_4d((internal_vector_type *)B.mat_4d.data(),
1687 simd_vector_size / simd_internal_vector_size, dims.b.m, dims.b.n,
1688 b_simd_batch_size);
1689
1690 C.vec_3d = vector_view_type_3d("C_vector", dims.c.m, dims.c.n, c_simd_batch_size);
1691 C.mat_4d = view_type_4d((scalar_type *)C.vec_3d.data(), simd_vector_size, dims.c.m, dims.c.n, c_simd_batch_size);
1692 C.ivec_4d = internal_vector_view_type_4d((internal_vector_type *)C.mat_4d.data(),
1693 simd_vector_size / simd_internal_vector_size, dims.c.m, dims.c.n,
1694 c_simd_batch_size);
1695
1696 } else {
1697 // Construct simd matrices with batch_size in the first dimension (better
1698 // for LayoutRight views)
1699 A.vec_3d = vector_view_type_3d("A_vector", a_simd_batch_size, dims.a.m, dims.a.n);
1700 A.mat_4d = view_type_4d((scalar_type *)A.vec_3d.data(), a_simd_batch_size, dims.a.m, dims.a.n, simd_vector_size);
1701 A.ivec_4d = internal_vector_view_type_4d((internal_vector_type *)A.mat_4d.data(), a_simd_batch_size, dims.a.m,
1702 dims.a.n, simd_vector_size / simd_internal_vector_size);
1703
1704 B.vec_3d = vector_view_type_3d("B_vector", b_simd_batch_size, dims.b.m, dims.b.n);
1705 B.mat_4d = view_type_4d((scalar_type *)B.vec_3d.data(), b_simd_batch_size, dims.b.m, dims.b.n, simd_vector_size);
1706 B.ivec_4d = internal_vector_view_type_4d((internal_vector_type *)B.mat_4d.data(), b_simd_batch_size, dims.b.m,
1707 dims.b.n, simd_vector_size / simd_internal_vector_size);
1708
1709 C.vec_3d = vector_view_type_3d("C_vector", c_simd_batch_size, dims.c.m, dims.c.n);
1710 C.mat_4d = view_type_4d((scalar_type *)C.vec_3d.data(), c_simd_batch_size, dims.c.m, dims.c.n, simd_vector_size);
1711 C.ivec_4d = internal_vector_view_type_4d((internal_vector_type *)C.mat_4d.data(), c_simd_batch_size, dims.c.m,
1712 dims.c.n, simd_vector_size / simd_internal_vector_size);
1713 }
1714
1715 // Use the non-simd 4-rank view type to randomly populate the gemm simd
1716 // arguments
1717 using tmp_view_type_4d = Kokkos::View<double ****, KokkosKernels::default_layout, KokkosKernels::default_device>;
1718 tmp_view_type_4d tmpA("tmpA", gemm_args.Av.mat_4d.extent(0), gemm_args.Av.mat_4d.extent(1),
1719 gemm_args.Av.mat_4d.extent(2), gemm_args.Av.mat_4d.extent(3));
1720 Kokkos::fill_random(tmpA, rand_pool, Kokkos::rand<Kokkos::Random_XorShift64<execution_space>, double>::max());
1721 tmp_view_type_4d tmpB("tmpB", gemm_args.Bv.mat_4d.extent(0), gemm_args.Bv.mat_4d.extent(1),
1722 gemm_args.Bv.mat_4d.extent(2), gemm_args.Bv.mat_4d.extent(3));
1723 Kokkos::fill_random(tmpB, rand_pool, Kokkos::rand<Kokkos::Random_XorShift64<execution_space>, double>::max());
1724 tmp_view_type_4d tmpC("tmpC", gemm_args.Cv.mat_4d.extent(0), gemm_args.Cv.mat_4d.extent(1),
1725 gemm_args.Cv.mat_4d.extent(2), gemm_args.Cv.mat_4d.extent(3));
1726 Kokkos::fill_random(tmpC, rand_pool, Kokkos::rand<Kokkos::Random_XorShift64<execution_space>, double>::max());
1727 Kokkos::fence();
1728 Kokkos::deep_copy(gemm_args.Av.mat_4d, tmpA);
1729 Kokkos::deep_copy(gemm_args.Bv.mat_4d, tmpB);
1730 Kokkos::deep_copy(gemm_args.Cv.mat_4d, tmpC);
1731 Kokkos::fence();
1732 } else {
1733 if (options.blas_args.batch_size_last_dim) {
1734 gemm_args.A = vta("gemm_args.A", dims.a.m, dims.a.n, dims.a.k);
1735 gemm_args.B = vtb("gemm_args.B", dims.b.m, dims.b.n, dims.b.k);
1736 gemm_args.C = vtc("gemm_args.C", dims.c.m, dims.c.n, dims.c.k);
1737 } else {
1738 gemm_args.A = vta("gemm_args.A", dims.a.k, dims.a.m, dims.a.n);
1739 gemm_args.B = vtb("gemm_args.B", dims.b.k, dims.b.m, dims.b.n);
1740 gemm_args.C = vtc("gemm_args.C", dims.c.k, dims.c.m, dims.c.n);
1741 }
1742
1743 using tmp_view_type_3d = Kokkos::View<double ***, KokkosKernels::default_layout, KokkosKernels::default_device>;
1744 tmp_view_type_3d tmpA("tmpA", gemm_args.A.extent(0), gemm_args.A.extent(1), gemm_args.A.extent(2));
1745 Kokkos::fill_random(tmpA, rand_pool, Kokkos::rand<Kokkos::Random_XorShift64<execution_space>, double>::max());
1746 tmp_view_type_3d tmpB("tmpB", gemm_args.B.extent(0), gemm_args.B.extent(1), gemm_args.B.extent(2));
1747 Kokkos::fill_random(tmpB, rand_pool, Kokkos::rand<Kokkos::Random_XorShift64<execution_space>, double>::max());
1748 tmp_view_type_3d tmpC("tmpC", gemm_args.C.extent(0), gemm_args.C.extent(1), gemm_args.C.extent(2));
1749 Kokkos::fill_random(tmpC, rand_pool, Kokkos::rand<Kokkos::Random_XorShift64<execution_space>, double>::max());
1750
1751 Kokkos::fence();
1752 Kokkos::deep_copy(gemm_args.A, tmpA);
1753 Kokkos::deep_copy(gemm_args.B, tmpB);
1754 Kokkos::deep_copy(gemm_args.C, tmpC);
1755 Kokkos::fence();
1756 }
1757
1758 #if defined(KOKKOSKERNELS_ENABLE_TPL_ARMPL) && ARMPL_BUILD >= 1058
1759 if (options.test == EXPERIMENT) {
1760 armpl_int_t bstrd_A, istrd_A, jstrd_A, bstrd_B, istrd_B, jstrd_B, bstrd_C, istrd_C, jstrd_C;
1761
1762 armpl_int_t ninter = (armpl_int_t)options.ninter, nbatch = gemm_args.dims.c.k / ninter;
1763
1764 if (gemm_args.dims.c.k % ninter) FATAL_ERROR("batch size must be evenly divisible by ninter!");
1765
1766 jstrd_A = ninter;
1767 istrd_A = jstrd_A * gemm_args.dims.a.n;
1768 bstrd_A = istrd_A * gemm_args.dims.a.m;
1769
1770 jstrd_B = ninter;
1771 istrd_B = jstrd_B * gemm_args.dims.b.n;
1772 bstrd_B = istrd_B * gemm_args.dims.b.m;
1773
1774 jstrd_C = ninter;
1775 istrd_C = jstrd_C * gemm_args.dims.c.n;
1776 bstrd_C = istrd_C * gemm_args.dims.c.m;
1777
1778 gemm_args.ninter = ninter;
1779 gemm_args.nbatch = nbatch;
1780 gemm_args.A_pl.jstrd = jstrd_A;
1781 gemm_args.B_pl.jstrd = jstrd_B;
1782 gemm_args.C_pl.jstrd = jstrd_C;
1783 gemm_args.A_pl.istrd = istrd_A;
1784 gemm_args.B_pl.istrd = istrd_B;
1785 gemm_args.C_pl.istrd = istrd_C;
1786 gemm_args.A_pl.bstrd = bstrd_A;
1787 gemm_args.B_pl.bstrd = bstrd_B;
1788 gemm_args.C_pl.bstrd = bstrd_C;
1789
1790 KokkosKernels::default_scalar *A_p =
1791 (KokkosKernels::default_scalar *)malloc(sizeof(KokkosKernels::default_scalar) * bstrd_A * nbatch);
1792 KokkosKernels::default_scalar *B_p =
1793 (KokkosKernels::default_scalar *)malloc(sizeof(KokkosKernels::default_scalar) * bstrd_B * nbatch);
1794 KokkosKernels::default_scalar *C_p =
1795 (KokkosKernels::default_scalar *)malloc(sizeof(KokkosKernels::default_scalar) * bstrd_C * nbatch);
1796
1797 using view_type_2d = Kokkos::View<KokkosKernels::default_scalar **, Kokkos::LayoutStride, default_device>;
1798 view_type_2d A, B, C;
1799
1800 // Populate interleave-batch matrices
1801 for (int ib = 0; ib < nbatch; ++ib) {
1802 for (int i = 0; i < ninter; ++i) {
1803 if (options.blas_args.batch_size_last_dim) {
1804 A = Kokkos::subview(gemm_args.A, Kokkos::ALL(), Kokkos::ALL(), ib * ninter + i);
1805 B = Kokkos::subview(gemm_args.B, Kokkos::ALL(), Kokkos::ALL(), ib * ninter + i);
1806 C = Kokkos::subview(gemm_args.C, Kokkos::ALL(), Kokkos::ALL(), ib * ninter + i);
1807 } else {
1808 A = Kokkos::subview(gemm_args.A, ib * ninter + i, Kokkos::ALL(), Kokkos::ALL());
1809 B = Kokkos::subview(gemm_args.B, ib * ninter + i, Kokkos::ALL(), Kokkos::ALL());
1810 C = Kokkos::subview(gemm_args.C, ib * ninter + i, Kokkos::ALL(), Kokkos::ALL());
1811 }
1812
1813 auto info = armpl_dge_interleave(ninter, i, gemm_args.dims.a.m, gemm_args.dims.a.n, A.data(), A.stride(0),
1814 A.stride(1), &A_p[bstrd_A * ib], istrd_A, jstrd_A);
1815 if (info != ARMPL_STATUS_SUCCESS) {
1816 FATAL_ERROR("armpl_dge_interleave (A)\n");
1817 }
1818 info = armpl_dge_interleave(ninter, i, gemm_args.dims.b.m, gemm_args.dims.b.n, B.data(), B.stride(0),
1819 B.stride(1), &B_p[bstrd_B * ib], istrd_B, jstrd_B);
1820 if (info != ARMPL_STATUS_SUCCESS) {
1821 FATAL_ERROR("armpl_dge_interleave (B)\n");
1822 }
1823 info = armpl_dge_interleave(ninter, i, gemm_args.dims.c.m, gemm_args.dims.c.n, C.data(), C.stride(0),
1824 C.stride(1), &C_p[bstrd_C * ib], istrd_C, jstrd_C);
1825 if (info != ARMPL_STATUS_SUCCESS) {
1826 FATAL_ERROR("armpl_dge_interleave (C)\n");
1827 }
1828 }
1829 }
1830
1831 gemm_args.A_pl.mat = A_p;
1832 gemm_args.B_pl.mat = B_p;
1833 gemm_args.C_pl.mat = C_p;
1834 }
1835 #endif // KOKKOSKERNELS_ENABLE_TPL_ARMPL && ARMPL_BUILD >= 1058
1836
1837 gemm_args.alpha = options.blas_args.gemm.alpha;
1838 gemm_args.beta = options.blas_args.gemm.beta;
1839 gemm_args.bp.team_size = options.blas_args.team_size;
1840 gemm_args.bp.vector_len = options.blas_args.vector_len;
1841
1842 Kokkos::fence(); // Ensure that fill_random has completed.
1843
1844 return gemm_args;
1845 }
1846
1847 /*************************** Interal run helper fns **************************/
1848 void __do_loop_and_invoke(options_t options, void (*fn)(options_t, gemm_args_t)) {
1849 matrix_dims_t cur_dims;
1850 gemm_args_t gemm_args;
1851 STATUS;
1852
1853 __print_gemm_perf_test_options(options);
1854 std::cout << "SCALAR:" << typeid(KokkosKernels::default_scalar).name()
1855 << ", LAYOUT:" << typeid(KokkosKernels::default_layout).name()
1856 << ", DEVICE:" << typeid(KokkosKernels::default_device).name() << ", SPACE:" << typeid(memory_space).name()
1857 << std::endl;
1858
1859 options.out[0] << gemm_csv_header_str << std::endl;
1860
1861 for (cur_dims = options.start;
1862 cur_dims.a.m <= options.stop.a.m && cur_dims.a.n <= options.stop.a.n && cur_dims.b.m <= options.stop.b.m &&
1863 cur_dims.b.n <= options.stop.b.n && cur_dims.c.m <= options.stop.c.m && cur_dims.c.n <= options.stop.c.n;
1864 cur_dims.a.m += options.step, cur_dims.a.n += options.step, cur_dims.b.m += options.step,
1865 cur_dims.b.n += options.step, cur_dims.c.m += options.step, cur_dims.c.n += options.step) {
1866 gemm_args = __do_setup<KokkosKernels::default_scalar, view_type_3d, view_type_3d, view_type_3d,
1867 KokkosKernels::default_device>(options, cur_dims);
1868
1869 if (options.verify) {
1870 __gemm_do_verify<KokkosKernels::default_scalar, KokkosKernels::default_layout, KokkosKernels::default_device>(
1871 options, gemm_args, fn);
1872 } else {
1873 fn(options, gemm_args);
1874 }
1875
1876 if (gemm_args.A_pl.mat != nullptr) {
1877 free(gemm_args.A_pl.mat);
1878 free(gemm_args.B_pl.mat);
1879 free(gemm_args.C_pl.mat);
1880 }
1881 }
1882 return;
1883 }
1884
1885 /*************************** External fns **************************/
1886 void do_gemm_serial_blas(options_t options) {
1887 STATUS;
1888 __do_loop_and_invoke(
1889 options,
1890 __do_gemm_serial_blas<KokkosKernels::default_scalar, view_type_3d, view_type_3d, KokkosKernels::default_device>);
1891 return;
1892 }
1893
1894 void do_gemm_serial_batched(options_t options) {
1895 STATUS;
1896 __do_loop_and_invoke(options,
1897 __do_gemm_serial_batched<KokkosKernels::default_scalar, view_type_3d, view_type_3d, view_type_3d,
1898 KokkosKernels::default_device, KokkosBatched::Algo::Gemm::Unblocked>);
1899 return;
1900 }
1901
1902 void do_gemm_serial_batched_blocked(options_t options) {
1903 STATUS;
1904 __do_loop_and_invoke(options,
1905 __do_gemm_serial_batched<KokkosKernels::default_scalar, view_type_3d, view_type_3d, view_type_3d,
1906 KokkosKernels::default_device, KokkosBatched::Algo::Gemm::Blocked>);
1907 return;
1908 }
1909
1910 void do_gemm_heuristic_batched_parallel(options_t options) {
1911 STATUS;
1912 if (options.blas_args.use_auto) {
1913 fprintf(stderr, "ERROR: --test=%s does not support --use_auto=%d\n", test_e_str[options.test].c_str(),
1914 (int)options.blas_args.use_auto);
1915 exit(-EINVAL);
1916 }
1917
1918 __do_loop_and_invoke(options, __do_gemm_parallel_batched_heuristic<void, void, KokkosKernels::default_device>);
1919 return;
1920 }
1921
1922 void do_gemm_serial_batched_parallel(options_t options) {
1923 STATUS;
1924 if (options.blas_args.batch_size_last_dim)
1925 __do_loop_and_invoke(options, __do_gemm_parallel_batched<SerialBatchDim3Tag, KokkosBatched::Algo::Gemm::Unblocked,
1926 KokkosKernels::default_device>);
1927 else
1928 __do_loop_and_invoke(
1929 options,
1930 __do_gemm_parallel_batched<SerialTag, KokkosBatched::Algo::Gemm::Unblocked, KokkosKernels::default_device>);
1931 return;
1932 }
1933
1934 void do_gemm_serial_batched_blocked_parallel(options_t options) {
1935 STATUS;
1936 if (options.blas_args.batch_size_last_dim)
1937 __do_loop_and_invoke(options, __do_gemm_parallel_batched<SerialBatchDim3Tag, KokkosBatched::Algo::Gemm::Blocked,
1938 KokkosKernels::default_device>);
1939 else
1940 __do_loop_and_invoke(
1941 options,
1942 __do_gemm_parallel_batched<SerialTag, KokkosBatched::Algo::Gemm::Blocked, KokkosKernels::default_device>);
1943 return;
1944 }
1945
1946 void do_gemm_serial_simd_batched_parallel(options_t options) {
1947 STATUS;
1948 // SerialBatchDim3Tag
1949 // SerialSimdTag
1950 options.use_simd = true;
1951 if (options.blas_args.batch_size_last_dim)
1952 __do_loop_and_invoke(options,
1953 __do_gemm_parallel_batched<TeamSimdBatchDim4Tag, KokkosBatched::Algo::Gemm::Unblocked,
1954 KokkosKernels::default_device, KokkosBatched::Mode::Serial>);
1955 else
1956 __do_loop_and_invoke(options,
1957 __do_gemm_parallel_batched<TeamSimdTag, KokkosBatched::Algo::Gemm::Unblocked,
1958 KokkosKernels::default_device, KokkosBatched::Mode::Serial>);
1959 return;
1960 }
1961
1962 void do_gemm_serial_simd_batched_blocked_parallel(options_t options) {
1963 STATUS;
1964 // SerialBatchDim3Tag
1965 // SerialSimdTag
1966 options.use_simd = true;
1967 if (options.blas_args.batch_size_last_dim)
1968 __do_loop_and_invoke(options,
1969 __do_gemm_parallel_batched<TeamSimdBatchDim4Tag, KokkosBatched::Algo::Gemm::Blocked,
1970 KokkosKernels::default_device, KokkosBatched::Mode::Serial>);
1971 else
1972 __do_loop_and_invoke(options,
1973 __do_gemm_parallel_batched<TeamSimdTag, KokkosBatched::Algo::Gemm::Blocked,
1974 KokkosKernels::default_device, KokkosBatched::Mode::Serial>);
1975 return;
1976 }
1977
1978 #if defined(KOKKOSBATCHED_IMPL_ENABLE_INTEL_MKL) && defined(KOKKOSBATCHED_IMPL_ENABLE_INTEL_MKL_BATCHED) && \
1979 defined(__KOKKOSBATCHED_ENABLE_INTEL_MKL_COMPACT_BATCHED__)
1980 void do_gemm_serial_batched_compact_mkl_parallel(options_t options) {
1981 STATUS;
1982 if (options.blas_args.batch_size_last_dim)
1983 __do_loop_and_invoke(options,
1984 __do_gemm_parallel_batched<SerialSimdBatchDim3Tag, KokkosBatched::Algo::Gemm::CompactMKL,
1985 KokkosKernels::default_device>);
1986 else
1987 __do_loop_and_invoke(options, __do_gemm_parallel_batched<SerialSimdTag, KokkosBatched::Algo::Gemm::CompactMKL,
1988 KokkosKernels::default_device>);
1989 return;
1990 }
1991 #else
1992 void do_gemm_serial_batched_compact_mkl_parallel(options_t) {
1993 STATUS;
1994 #if !defined(KOKKOSBATCHED_IMPL_ENABLE_INTEL_MKL)
1995 std::cerr << std::string(__func__) << " disabled since KOKKOSBATCHED_IMPL_ENABLE_INTEL_MKL is undefined."
1996 << std::endl;
1997 #elif !defined(KOKKOSBATCHED_IMPL_ENABLE_INTEL_MKL_BATCHED)
1998 std::cerr << std::string(__func__)
1999 << " disabled since KOKKOSBATCHED_IMPL_ENABLE_INTEL_MKL_BATCHED is "
2000 "undefined."
2001 << std::endl;
2002 #elif !defined(__KOKKOSBATCHED_ENABLE_INTEL_MKL_COMPACT_BATCHED__)
2003 std::cerr << std::string(__func__)
2004 << " disabled since __KOKKOSBATCHED_ENABLE_INTEL_MKL_COMPACT_BATCHED__ "
2005 "is undefined."
2006 << std::endl;
2007 #endif
2008 return;
2009 }
2010 #endif
2011
2012 void do_gemm_team_batched_parallel(options_t options) {
2013 STATUS;
2014 if (options.blas_args.batch_size_last_dim)
2015 __do_loop_and_invoke(options, __do_gemm_parallel_batched<TeamBatchDim3Tag, KokkosBatched::Algo::Gemm::Unblocked,
2016 KokkosKernels::default_device>);
2017 else
2018 __do_loop_and_invoke(
2019 options,
2020 __do_gemm_parallel_batched<TeamTag, KokkosBatched::Algo::Gemm::Unblocked, KokkosKernels::default_device>);
2021 return;
2022 }
2023
2024 void do_gemm_team_batched_blocked_parallel(options_t options) {
2025 STATUS;
2026 if (options.blas_args.batch_size_last_dim)
2027 __do_loop_and_invoke(options, __do_gemm_parallel_batched<TeamBatchDim3Tag, KokkosBatched::Algo::Gemm::Blocked,
2028 KokkosKernels::default_device>);
2029 else
2030 __do_loop_and_invoke(
2031 options,
2032 __do_gemm_parallel_batched<TeamTag, KokkosBatched::Algo::Gemm::Blocked, KokkosKernels::default_device>);
2033 return;
2034 }
2035
2036 void do_gemm_team_vector_batched_parallel(options_t options) {
2037 STATUS;
2038 if (options.blas_args.batch_size_last_dim)
2039 __do_loop_and_invoke(options,
2040 __do_gemm_parallel_batched<TeamVectorBatchDim3Tag, KokkosBatched::Algo::Gemm::Unblocked,
2041 KokkosKernels::default_device>);
2042 else
2043 __do_loop_and_invoke(
2044 options,
2045 __do_gemm_parallel_batched<TeamVectorTag, KokkosBatched::Algo::Gemm::Unblocked, KokkosKernels::default_device>);
2046 return;
2047 }
2048
2049 void do_gemm_team_simd_batched_parallel(options_t options) {
2050 STATUS;
2051 options.use_simd = true;
2052 if (options.blas_args.batch_size_last_dim)
2053 __do_loop_and_invoke(options, __do_gemm_parallel_batched<TeamSimdBatchDim4Tag, KokkosBatched::Algo::Gemm::Unblocked,
2054 KokkosKernels::default_device, KokkosBatched::Mode::Team>);
2055 else
2056 __do_loop_and_invoke(options, __do_gemm_parallel_batched<TeamSimdTag, KokkosBatched::Algo::Gemm::Unblocked,
2057 KokkosKernels::default_device, KokkosBatched::Mode::Team>);
2058 return;
2059 }
2060
2061 void do_gemm_team_simd_batched_blocked_parallel(options_t options) {
2062 STATUS;
2063 options.use_simd = true;
2064 if (options.blas_args.batch_size_last_dim)
2065 __do_loop_and_invoke(options, __do_gemm_parallel_batched<TeamSimdBatchDim4Tag, KokkosBatched::Algo::Gemm::Blocked,
2066 KokkosKernels::default_device, KokkosBatched::Mode::Team>);
2067 else
2068 __do_loop_and_invoke(options, __do_gemm_parallel_batched<TeamSimdTag, KokkosBatched::Algo::Gemm::Blocked,
2069 KokkosKernels::default_device, KokkosBatched::Mode::Team>);
2070 return;
2071 }
2072
2073 // Blocked algo not yet implemented for TeamVectorGemm.
2074 /* void do_gemm_team_vector_batched_blocked_parallel(options_t options) {
2075 STATUS;
2076 __do_loop_and_invoke(
2077 options, __do_gemm_parallel_batched<TeamVectorTag,
2078 KokkosBatched::Algo::Gemm::Blocked, KokkosKernels::default_device>); return;
2079 } */
2080
2081 void do_gemm_experiment_parallel(options_t options) {
2082 STATUS;
2083 using TransAType = KokkosBatched::Trans::NoTranspose;
2084 using TransBType = KokkosBatched::Trans::NoTranspose;
2085 using BlockingType = KokkosBatched::Algo::Gemm::Unblocked;
2086
2087 // __do_loop_and_invoke(
2088 // options, __do_gemm_parallel_experiment1<TransAType, TransBType,
2089 // BlockingType, KokkosKernels::default_device>);
2090 // __do_loop_and_invoke(
2091 // options, __do_gemm_parallel_experiment2<TransAType, TransBType,
2092 // BlockingType, KokkosKernels::default_device>);
2093 // __do_loop_and_invoke(
2094 // options, __do_gemm_parallel_experiment3<TransAType, TransBType,
2095 // BlockingType, KokkosKernels::default_device>);
2096 // __do_loop_and_invoke(
2097 // options, __do_gemm_parallel_experiment4<TransAType, TransBType,
2098 // BlockingType, KokkosKernels::default_device>);
2099 // __do_loop_and_invoke(
2100 // options, __do_gemm_parallel_experiment5<TransAType, TransBType,
2101 // BlockingType, KokkosKernels::default_device>);
2102 // __do_loop_and_invoke(
2103 // options, __do_gemm_parallel_experiment6<TransAType, TransBType,
2104 // BlockingType, KokkosKernels::default_device>);
2105 __do_loop_and_invoke(options, __do_gemm_armpl<TransAType, TransBType, BlockingType, KokkosKernels::default_device>);
2106 }
2107
2108 #endif // KOKKOSBLAS3_GEMM_PERF_TEST_H_