Debian Codegraph @

packages/kokkos-kernels/perf_test/blas/blas3/KokkosBlas_trtri_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 KOKKOSBLAS_TRTRI_PERF_TEST_H_
17 #define KOKKOSBLAS_TRTRI_PERF_TEST_H_
18
19 // #include <complex.h>
20 #include "KokkosBlas_common.hpp"
21
22 #include <Kokkos_Random.hpp>
23
24 #include <KokkosLapack_trtri.hpp>
25
26 #include "KokkosBatched_Trtri_Decl.hpp"
27 #include "KokkosBatched_Trtri_Serial_Impl.hpp"
28 #include "KokkosBatched_Util.hpp"
29
30 #include <chrono>
31
32 // #define TRTRI_PERF_TEST_DEBUG
33
34 // Forward declarations
35 void do_trtri_serial_blas(options_t options);
36 void do_trtri_serial_batched(options_t options);
37 void do_trtri_parallel_blas(options_t options);
38 void do_trtri_parallel_batched(options_t options);
39
40 // trtri invoke table
41 void (*do_trtri_invoke[LOOP_N][TEST_N])(options_t) = {{do_trtri_serial_blas, do_trtri_serial_batched},
42 {do_trtri_parallel_blas, do_trtri_parallel_batched}};
43
44 /*************************** Print macros **************************/
45 #ifdef TRTRI_PERF_TEST_DEBUG
46 #define STATUS printf("STATUS: %s:%d.\n", __func__, __LINE__);
47 #else
48 #define STATUS
49 #endif // TRTRI_PERF_TEST_DEBUG
50
51 /*************************** Test types and defaults **************************/
52 #define DEFAULT_TRTRI_ARGS "UU"
53
54 /**
55 * The KokkosBatched::SerialTrtri implementation performs trmm and scal on
56 * subblocks of the A matrix. a_m subblocks are selected.
57 */
58 static inline double __trtri_impl_flop_count(double a_m, double /*a_n*/) {
59 double flop_count = 0;
60 double flops_per_div, flops_per_mul, flops_per_add;
61
62 if (std::is_same<double, KokkosKernels::default_scalar>::value ||
63 std::is_same<float, KokkosKernels::default_scalar>::value ||
64 std::is_same<Kokkos::Experimental::half_t, KokkosKernels::default_scalar>::value) {
65 flops_per_div = 1;
66 flops_per_mul = 1;
67 flops_per_add = 1;
68 } else {
69 // For complex, we need to count 2 flops for each add and 6 flops for each
70 // multiply or divide.
71 flops_per_div = 6;
72 flops_per_mul = 6;
73 flops_per_add = 2;
74 }
75
76 for (int i = 0; i < a_m; i++) {
77 flop_count += flops_per_div; // 1 / A[i,j]
78 flop_count += ((i * (i + 1)) / 2) * (flops_per_mul + flops_per_add); // TRMM FLOPS
79 flop_count += i * flops_per_mul; // SCAL FLOPS
80 }
81
82 return flop_count;
83 }
84
85 // Flop count formula from lapack working note 41:
86 // http://www.icl.utk.edu/~mgates3/docs/lawn41.pdf
87 static inline double __trtri_flop_count(double a_m, double a_n) {
88 double flops;
89 double flops_per_mul;
90 double flops_per_add;
91
92 if (a_m != a_n) {
93 fprintf(stderr, "%s:%d:ERROR: a_m != a_n.\n", __FILE__, __LINE__);
94 exit(255);
95 }
96
97 if (std::is_same<double, KokkosKernels::default_scalar>::value ||
98 std::is_same<float, KokkosKernels::default_scalar>::value ||
99 std::is_same<Kokkos::Experimental::half_t, KokkosKernels::default_scalar>::value) {
100 flops_per_mul = 1;
101 flops_per_add = 1;
102 } else {
103 // For complex, we need to count 2 flops for each add and 6 flops for each
104 // multiply.
105 flops_per_mul = 6;
106 flops_per_add = 2;
107 }
108
109 flops = (1. / 6. * a_n * a_n * a_n + 1. / 2. * a_n * a_n + 1. / 3. * a_n) * flops_per_mul +
110 (1. / 6. * a_n * a_n * a_n - 1. / 2. * a_n * a_n + 1. / 3. * a_n) * flops_per_add;
111
112 return flops;
113 }
114
115 using view_type_3d =
116 Kokkos::View<KokkosKernels::default_scalar***, KokkosKernels::default_layout, KokkosKernels::default_device>;
117 struct trtri_args {
118 char uplo, diag;
119 view_type_3d A;
120 };
121 typedef struct trtri_args trtri_args_t;
122
123 static std::string trtri_csv_header_str =
124 "algorithm,side-uplo-trans-diag,loop_type,A_dims,warm_up_n,iter,"
125 "total_time(s),average_time(s),FLOPS,GFLOP/average_time(s)";
126
127 /*************************** Internal helper fns **************************/
128 static void __trtri_output_csv_row(options_t options, trtri_args_t trtri_args, double time_in_seconds) {
129 double flops = trtri_args.A.extent(0) * __trtri_flop_count(trtri_args.A.extent(1), trtri_args.A.extent(2));
130 double gflops = flops / 1e9;
131 double average_time = time_in_seconds / options.n;
132
133 options.out[0] << test_e_str[options.test] << "," << options.blas_args.trtri.trtri_args << ","
134 << loop_e_str[options.loop] << "," << trtri_args.A.extent(0) << "x" << trtri_args.A.extent(1) << "x"
135 << trtri_args.A.extent(2) << "," << options.warm_up_n << "," << options.n << "," << time_in_seconds
136 << "," << average_time << "," << flops << "," << gflops / average_time << std::endl;
137 }
138
139 #ifdef TRTRI_PERF_TEST_DEBUG
140 static void __print_trtri_perf_test_options(options_t options) {
141 printf("options.test = %s\n", test_e_str[options.test].c_str());
142 printf("options.loop = %s\n", loop_e_str[options.loop].c_str());
143 printf("options.start = %dx%d,%dx%d\n", options.start.a.m, options.start.a.n, options.start.b.m,
144 options.start.b.n);
145 printf("options.stop = %dx%d,%d,%d\n", options.stop.a.m, options.stop.a.n, options.stop.b.m, options.stop.b.n);
146 printf("options.step = %d\n", options.step);
147 printf("options.warm_up_n = %d\n", options.warm_up_n);
148 printf("options.n = %d\n", options.n);
149 printf("options.blas_args.trtri.trtri_args = %s\n", options.blas_args.trtri.trtri_args.c_str());
150 printf("options.out_file = %s\n", options.out_file.c_str());
151 std::cout << "SCALAR:" << typeid(KokkosKernels::default_scalar).name()
152 << ", LAYOUT:" << typeid(KokkosKernels::default_layout).name() << ", DEVICE:."
153 << typeid(KokkosKernels::default_device).name() << std::endl;
154 #else
155 static void __print_trtri_perf_test_options(options_t) {
156 #endif // TRTRI_PERF_TEST_DEBUG
157 return;
158 }
159
160 /*************************** Internal templated fns **************************/
161 #if !defined(KOKKOS_ENABLE_CUDA) && !defined(KOKKOS_ENABLE_HIP) && !defined(KOKKOS_ENABLE_OPENMPTARGET)
162 template <class scalar_type, class vta, class device_type>
163 void __do_trtri_serial_blas(options_t options, trtri_args_t trtri_args) {
164 // Need to take subviews on the device
165 uint32_t warm_up_n = options.warm_up_n;
166 uint32_t n = options.n;
167 Kokkos::Timer timer;
168
169 STATUS;
170
171 for (uint32_t j = 0; j < warm_up_n; ++j) {
172 for (int i = 0; i < options.start.a.k; ++i) {
173 auto A = Kokkos::subview(trtri_args.A, i, Kokkos::ALL(), Kokkos::ALL());
174
175 KokkosLapack::trtri(&trtri_args.uplo, &trtri_args.diag, A);
176 }
177 // Fence after each batch operation
178 Kokkos::fence();
179 }
180
181 timer.reset();
182 for (uint32_t j = 0; j < n; ++j) {
183 for (int i = 0; i < options.start.a.k; ++i) {
184 auto A = Kokkos::subview(trtri_args.A, i, Kokkos::ALL(), Kokkos::ALL());
185
186 KokkosLapack::trtri(&trtri_args.uplo, &trtri_args.diag, A);
187 }
188 // Fence after each batch operation
189 Kokkos::fence();
190 }
191 __trtri_output_csv_row(options, trtri_args, timer.seconds());
192 return;
193 }
194 #else
195 template <class scalar_type, class vta, class device_type>
196 void __do_trtri_serial_blas(options_t /*options*/, trtri_args_t /*trtri_args*/) {
197 std::cerr << std::string(__func__) << " disabled since KOKKOS_ENABLE_DEVICE is defined." << std::endl;
198 return;
199 }
200 #endif // !KOKKOS_ENABLE_CUDA
201
202 // Need to take subviews on the device
203 #if !defined(KOKKOS_ENABLE_CUDA) && !defined(KOKKOS_ENABLE_HIP) && !defined(KOKKOS_ENABLE_OPENMPTARGET)
204 template <class uplo, class diag>
205 void __do_trtri_serial_batched_template(options_t options, trtri_args_t trtri_args) {
206 uint32_t warm_up_n = options.warm_up_n;
207 uint32_t n = options.n;
208 Kokkos::Timer timer;
209 using tag = KokkosBatched::Algo::Trtri::Unblocked;
210
211 for (uint32_t j = 0; j < warm_up_n; ++j) {
212 for (int i = 0; i < options.start.a.k; ++i) {
213 auto A = Kokkos::subview(trtri_args.A, i, Kokkos::ALL(), Kokkos::ALL());
214
215 KokkosBatched::SerialTrtri<uplo, diag, tag>::invoke(A);
216 }
217 // Fence after each batch operation
218 Kokkos::fence();
219 }
220
221 timer.reset();
222 for (uint32_t j = 0; j < n; ++j) {
223 for (int i = 0; i < options.start.a.k; ++i) {
224 auto A = Kokkos::subview(trtri_args.A, i, Kokkos::ALL(), Kokkos::ALL());
225
226 KokkosBatched::SerialTrtri<uplo, diag, tag>::invoke(A);
227 }
228 // Fence after each batch operation
229 Kokkos::fence();
230 }
231 __trtri_output_csv_row(options, trtri_args, timer.seconds());
232 }
233 #else
234 template <class uplo, class diag>
235 void __do_trtri_serial_batched_template(options_t /*options*/, trtri_args_t /*trtri_args*/) {
236 std::cerr << std::string(__func__) << " disabled since KOKKOS_ENABLE_DEVICE is defined." << std::endl;
237 }
238 #endif // !KOKKOS_ENABLE_CUDA
239
240 template <class scalar_type, class vta, class device_type>
241 void __do_trtri_serial_batched(options_t options, trtri_args_t trtri_args) {
242 using KokkosBatched::Diag;
243 using KokkosBatched::Uplo;
244
245 char __uplo = tolower(trtri_args.uplo), __diag = tolower(trtri_args.diag);
246
247 STATUS;
248
249 //// Lower ////
250 if (__uplo == 'l') {
251 if (__diag == 'u') {
252 __do_trtri_serial_batched_template<Uplo::Lower, Diag::Unit>(options, trtri_args);
253 } else {
254 __do_trtri_serial_batched_template<Uplo::Lower, Diag::NonUnit>(options, trtri_args);
255 }
256 } else {
257 //// Upper ////
258 if (__diag == 'u') {
259 __do_trtri_serial_batched_template<Uplo::Upper, Diag::Unit>(options, trtri_args);
260 } else {
261 __do_trtri_serial_batched_template<Uplo::Upper, Diag::NonUnit>(options, trtri_args);
262 }
263 }
264
265 return;
266 }
267
268 #if !defined(KOKKOS_ENABLE_CUDA) && !defined(KOKKOS_ENABLE_HIP) && !defined(KOKKOS_ENABLE_OPENMPTARGET)
269 template <class ExecutionSpace>
270 struct parallel_blas_trtri {
271 trtri_args_t trtri_args_;
272
273 parallel_blas_trtri(trtri_args_t trtri_args) : trtri_args_(trtri_args) {}
274
275 KOKKOS_INLINE_FUNCTION
276 void operator()(const int& i) const {
277 auto svA = Kokkos::subview(trtri_args_.A, i, Kokkos::ALL(), Kokkos::ALL());
278
279 KokkosLapack::trtri(&trtri_args_.uplo, &trtri_args_.diag, svA);
280 }
281 };
282 #endif // !KOKKOS_ENABLE_CUDA && !KOKKOS_ENABLE_HIP &&
283 // !KOKKOS_ENABLE_OPENMPTARGET
284
285 template <class scalar_type, class vta, class device_type>
286 void __do_trtri_parallel_blas(options_t options, trtri_args_t trtri_args) {
287 #if !defined(KOKKOS_ENABLE_CUDA) && !defined(KOKKOS_ENABLE_HIP) && !defined(KOKKOS_ENABLE_OPENMPTARGET)
288 uint32_t warm_up_n = options.warm_up_n;
289 uint32_t n = options.n;
290 Kokkos::Timer timer;
291 using execution_space = typename device_type::execution_space;
292 using functor_type = parallel_blas_trtri<execution_space>;
293 functor_type parallel_blas_trtri_functor(trtri_args);
294
295 STATUS;
296
297 for (uint32_t i = 0; i < warm_up_n; ++i) {
298 Kokkos::parallel_for("parallelBlasWarmUpLoopTrtri", Kokkos::RangePolicy<execution_space>(0, options.start.a.k),
299 parallel_blas_trtri_functor);
300 // Fence after each batch operation
301 Kokkos::fence();
302 }
303
304 timer.reset();
305 for (uint32_t i = 0; i < n; ++i) {
306 Kokkos::parallel_for("parallelBlasTimedLoopTrtri", Kokkos::RangePolicy<execution_space>(0, options.start.a.k),
307 parallel_blas_trtri_functor);
308 // Fence after each batch operation
309 Kokkos::fence();
310 }
311 __trtri_output_csv_row(options, trtri_args, timer.seconds());
312 #else
313 std::cerr << std::string(__func__)
314 << " disabled since KOKKOS_ENABLE_CUDA, KOKKOS_ENABLE_HIP or "
315 "KOKKOS_ENABLE_OPENMPTARGET is defined."
316 << std::endl;
317 __trtri_output_csv_row(options, trtri_args, -1);
318 #endif // !KOKKOS_ENABLE_CUDA && !KOKKOS_ENABLE_HIP &&
319 // !defined(KOKKOS_ENABLE_OPENMPTARGET)
320 return;
321 }
322
323 template <class uplo, class diag, class tag, class ExecutionSpace>
324 struct parallel_batched_trtri {
325 trtri_args_t trtri_args_;
326
327 parallel_batched_trtri(trtri_args_t trtri_args) : trtri_args_(trtri_args) {}
328
329 KOKKOS_INLINE_FUNCTION
330 void operator()(const int& i) const {
331 auto svA = Kokkos::subview(trtri_args_.A, i, Kokkos::ALL(), Kokkos::ALL());
332
333 KokkosBatched::SerialTrtri<uplo, diag, tag>::invoke(svA);
334 }
335 };
336
337 template <class uplo, class diag, class device_type>
338 void __do_trtri_parallel_batched_template(options_t options, trtri_args_t trtri_args) {
339 uint32_t warm_up_n = options.warm_up_n;
340 uint32_t n = options.n;
341 Kokkos::Timer timer;
342 using tag = KokkosBatched::Algo::Trtri::Unblocked;
343 using execution_space = typename device_type::execution_space;
344 using functor_type = parallel_batched_trtri<uplo, diag, tag, execution_space>;
345 functor_type parallel_batched_trtri_functor(trtri_args);
346
347 STATUS;
348
349 for (uint32_t i = 0; i < warm_up_n; ++i) {
350 Kokkos::parallel_for("parallelBatchedWarmUpLoopTrtri", Kokkos::RangePolicy<execution_space>(0, options.start.a.k),
351 parallel_batched_trtri_functor);
352 // Fence after each batch operation
353 Kokkos::fence();
354 }
355
356 timer.reset();
357
358 for (uint32_t i = 0; i < n; ++i) {
359 Kokkos::parallel_for("parallelBatchedTimedLoopTrtri", Kokkos::RangePolicy<execution_space>(0, options.start.a.k),
360 parallel_batched_trtri_functor);
361 // Fence after each batch operation
362 Kokkos::fence();
363 }
364 __trtri_output_csv_row(options, trtri_args, timer.seconds());
365
366 return;
367 }
368
369 template <class scalar_type, class vta, class device_type>
370 void __do_trtri_parallel_batched(options_t options, trtri_args_t trtri_args) {
371 using KokkosBatched::Diag;
372 using KokkosBatched::Uplo;
373
374 char __uplo = tolower(trtri_args.uplo), __diag = tolower(trtri_args.diag);
375
376 STATUS;
377
378 //// Lower ////
379 if (__uplo == 'l') {
380 if (__diag == 'u') {
381 __do_trtri_parallel_batched_template<Uplo::Lower, Diag::Unit, device_type>(options, trtri_args);
382 } else {
383 __do_trtri_parallel_batched_template<Uplo::Lower, Diag::NonUnit, device_type>(options, trtri_args);
384 }
385 } else {
386 //// Upper ////
387 if (__diag == 'u') {
388 __do_trtri_parallel_batched_template<Uplo::Upper, Diag::Unit, device_type>(options, trtri_args);
389 } else {
390 __do_trtri_parallel_batched_template<Uplo::Upper, Diag::NonUnit, device_type>(options, trtri_args);
391 }
392 }
393
394 return;
395 }
396
397 /*************************** Internal setup fns **************************/
398 template <class scalar_type, class vta, class device_type>
399 trtri_args_t __do_setup(options_t options, matrix_dims_t dim) {
400 using execution_space = typename device_type::execution_space;
401
402 trtri_args_t trtri_args;
403 uint64_t seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
404 Kokkos::Random_XorShift64_Pool<execution_space> rand_pool(seed);
405 decltype(dim.a.m) min_dim = dim.a.m < dim.a.n ? dim.a.m : dim.a.n;
406 typename vta::HostMirror host_A;
407 STATUS;
408
409 trtri_args.uplo = options.blas_args.trtri.trtri_args.c_str()[0];
410 trtri_args.diag = options.blas_args.trtri.trtri_args.c_str()[1];
411 trtri_args.A = vta("trtri_args.A", dim.a.k, dim.a.m, dim.a.n);
412 host_A = Kokkos::create_mirror_view(trtri_args.A);
413
414 Kokkos::fill_random(trtri_args.A, rand_pool,
415 Kokkos::rand<Kokkos::Random_XorShift64<execution_space>, scalar_type>::max());
416 Kokkos::deep_copy(host_A, trtri_args.A);
417
418 if (trtri_args.uplo == 'U' || trtri_args.uplo == 'u') {
419 // Make A upper triangular
420 for (int k = 0; k < dim.a.k; ++k) {
421 auto A = Kokkos::subview(host_A, k, Kokkos::ALL(), Kokkos::ALL());
422 for (int i = 1; i < dim.a.m; i++) {
423 for (int j = 0; j < i; j++) {
424 A(i, j) = scalar_type(0);
425 }
426 }
427 }
428 } else {
429 // Make A lower triangular
430 // Kokkos::parallel_for("toLowerLoop", options.n, KOKKOS_LAMBDA (const int&
431 // i) {
432 for (int k = 0; k < dim.a.k; ++k) {
433 auto A = Kokkos::subview(host_A, k, Kokkos::ALL(), Kokkos::ALL());
434 for (int i = 0; i < dim.a.m - 1; i++) {
435 for (int j = i + 1; j < dim.a.n; j++) {
436 A(i, j) = scalar_type(0);
437 }
438 }
439 }
440 }
441
442 if (trtri_args.diag == 'U' || trtri_args.diag == 'u') {
443 for (int k = 0; k < dim.a.k; ++k) {
444 auto A = Kokkos::subview(host_A, k, Kokkos::ALL(), Kokkos::ALL());
445 for (int i = 0; i < min_dim; i++) {
446 A(i, i) = scalar_type(1);
447 }
448 }
449 }
450
451 Kokkos::deep_copy(trtri_args.A, host_A);
452
453 return trtri_args;
454 }
455
456 /*************************** Interal run helper fns **************************/
457 void __do_loop_and_invoke(options_t options, void (*fn)(options_t, trtri_args_t)) {
458 matrix_dims_t cur_dims;
459 trtri_args_t trtri_args;
460 STATUS;
461
462 __print_trtri_perf_test_options(options);
463 std::cout << "SCALAR:" << typeid(KokkosKernels::default_scalar).name()
464 << ", LAYOUT:" << typeid(KokkosKernels::default_layout).name() << ", DEVICE:."
465 << typeid(KokkosKernels::default_device).name() << std::endl;
466
467 options.out[0] << trtri_csv_header_str << std::endl;
468
469 for (cur_dims = options.start; cur_dims.a.m <= options.stop.a.m && cur_dims.a.n <= options.stop.a.n &&
470 cur_dims.b.m <= options.stop.b.m && cur_dims.b.n <= options.stop.b.n;
471 cur_dims.a.m += options.step, cur_dims.a.n += options.step, cur_dims.b.m += options.step,
472 cur_dims.b.n += options.step) {
473 trtri_args =
474 __do_setup<KokkosKernels::default_scalar, view_type_3d, KokkosKernels::default_device>(options, cur_dims);
475 fn(options, trtri_args);
476 }
477 return;
478 }
479
480 /*************************** External fns **************************/
481 void do_trtri_serial_blas(options_t options) {
482 STATUS;
483 __do_loop_and_invoke(
484 options, __do_trtri_serial_blas<KokkosKernels::default_scalar, view_type_3d, KokkosKernels::default_device>);
485 return;
486 }
487
488 void do_trtri_serial_batched(options_t options) {
489 STATUS;
490 __do_loop_and_invoke(
491 options, __do_trtri_serial_batched<KokkosKernels::default_scalar, view_type_3d, KokkosKernels::default_device>);
492 return;
493 }
494
495 void do_trtri_parallel_blas(options_t options) {
496 STATUS;
497 __do_loop_and_invoke(
498 options, __do_trtri_parallel_blas<KokkosKernels::default_scalar, view_type_3d, KokkosKernels::default_device>);
499 return;
500 }
501
502 void do_trtri_parallel_batched(options_t options) {
503 STATUS;
504 __do_loop_and_invoke(
505 options, __do_trtri_parallel_batched<KokkosKernels::default_scalar, view_type_3d, KokkosKernels::default_device>);
506 return;
507 }
508
509 #endif // KOKKOSBLAS_TRTRI_PERF_TEST_H_