| 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_TRMM_PERF_TEST_H_ |
| 17 |
#define KOKKOSBLAS3_TRMM_PERF_TEST_H_ |
| 18 |
|
| 19 |
// #include <complex.h> |
| 20 |
#include "KokkosBlas3_common.hpp" |
| 21 |
|
| 22 |
#include <Kokkos_Random.hpp> |
| 23 |
|
| 24 |
#include <KokkosBlas3_trmm.hpp> |
| 25 |
|
| 26 |
#include "KokkosBatched_Trmm_Decl.hpp" |
| 27 |
#include "KokkosBatched_Trmm_Serial_Impl.hpp" |
| 28 |
#include "KokkosBatched_Util.hpp" |
| 29 |
|
| 30 |
#include <chrono> |
| 31 |
|
| 32 |
// #define PERF_TEST_DEBUG |
| 33 |
|
| 34 |
// Forward declarations |
| 35 |
void do_trmm_serial_blas(options_t options); |
| 36 |
void do_trmm_serial_batched(options_t options); |
| 37 |
void do_trmm_parallel_blas(options_t options); |
| 38 |
void do_trmm_parallel_batched(options_t options); |
| 39 |
|
| 40 |
// trmm invoke table |
| 41 |
void (*do_trmm_invoke[LOOP_N][TEST_N])(options_t) = {{do_trmm_serial_blas, do_trmm_serial_batched}, |
| 42 |
{do_trmm_parallel_blas, do_trmm_parallel_batched}}; |
| 43 |
|
| 44 |
/*************************** Test types and defaults **************************/ |
| 45 |
#define DEFAULT_TRMM_ARGS "LUNU" |
| 46 |
#define DEFAULT_TRMM_ALPHA 1.0 |
| 47 |
|
| 48 |
/** |
| 49 |
* The KokkosBatched::SerialTrmm implementation performs dot products on |
| 50 |
* non-zero elements of the triangular matrices. The flop calculation below |
| 51 |
* assumes KokkosBatched::SerialTrmm is being used. Since the dot products |
| 52 |
* do a multiply and add we can calculate the flops for any element in the last |
| 53 |
* column of the LHS to be 2*columns_LHS, any element in the last-1 column of |
| 54 |
* the LHS to be 2*(columns_LHS-1), and so on. We do this for every row of the |
| 55 |
* LHS giving us this flop count: flops = columns_LHS * (columns_LHS + 1) flops |
| 56 |
* = (flops / 2) * 2 flops = flops * rows_LHS |
| 57 |
*/ |
| 58 |
static inline int __trmm_impl_flop_count(char side, int b_m, int b_n, int /*a_m*/, int /*a_n*/) { |
| 59 |
int flops; |
| 60 |
|
| 61 |
if (side == 'L' || side == 'l') { |
| 62 |
flops = (b_m * (b_m + 1)) * b_n; |
| 63 |
} else { |
| 64 |
flops = (b_n * (b_n + 1)) * b_m; |
| 65 |
} |
| 66 |
|
| 67 |
if (std::is_same<double, KokkosKernels::default_scalar>::value || |
| 68 |
std::is_same<float, KokkosKernels::default_scalar>::value || |
| 69 |
std::is_same<Kokkos::Experimental::half_t, KokkosKernels::default_scalar>::value) |
| 70 |
return flops; |
| 71 |
|
| 72 |
// Account for 6 additional flops when complex numbers are used. |
| 73 |
// Above we have counted 1 flop for each add and 1 flop for each multiply. |
| 74 |
// For complex, we need to count 2 flops for each add and 6 flops for each |
| 75 |
// multiply. |
| 76 |
return flops * 4; |
| 77 |
} |
| 78 |
|
| 79 |
// Flop count formula from lapack working note 41: |
| 80 |
// http://www.icl.utk.edu/~mgates3/docs/lawn41.pdf |
| 81 |
static inline double __trmm_flop_count(char side, double b_m, double b_n, double /*a_m*/, double /*a_n*/) { |
| 82 |
double flops; |
| 83 |
|
| 84 |
if (side == 'L' || side == 'l') { |
| 85 |
flops = b_m * b_m * b_n; |
| 86 |
} else { |
| 87 |
flops = b_n * b_n * b_m; |
| 88 |
} |
| 89 |
|
| 90 |
if (std::is_same<double, KokkosKernels::default_scalar>::value || |
| 91 |
std::is_same<float, KokkosKernels::default_scalar>::value || |
| 92 |
std::is_same<Kokkos::Experimental::half_t, KokkosKernels::default_scalar>::value) |
| 93 |
return flops; |
| 94 |
|
| 95 |
// Account for 6 additional flops when complex numbers are used. |
| 96 |
// Above we have counted 1 flop for each add and 1 flop for each multiply. |
| 97 |
// For complex, we need to count 2 flops for each add and 6 flops for each |
| 98 |
// multiply. |
| 99 |
return flops * 4; |
| 100 |
} |
| 101 |
|
| 102 |
using view_type_3d = |
| 103 |
Kokkos::View<KokkosKernels::default_scalar***, KokkosKernels::default_layout, KokkosKernels::default_device>; |
| 104 |
struct trmm_args { |
| 105 |
char side, uplo, trans, diag; |
| 106 |
KokkosKernels::default_scalar alpha; |
| 107 |
view_type_3d A, B; |
| 108 |
}; |
| 109 |
typedef struct trmm_args trmm_args_t; |
| 110 |
|
| 111 |
static std::string trmm_csv_header_str = |
| 112 |
"algorithm,side-uplo-trans-diag,alpha,loop_type,A_dims,B_dims,warm_up_n," |
| 113 |
"iter,total_time(s),average_time(s),FLOPS,GFLOP/" |
| 114 |
"average_time(s),min_achieved_bandwidth(GB/s),max_achieved_bandwidth(GB/s)"; |
| 115 |
|
| 116 |
/*************************** Internal helper fns **************************/ |
| 117 |
static void __trmm_output_csv_row(options_t options, trmm_args_t trmm_args, double time_in_seconds) { |
| 118 |
double flops = trmm_args.A.extent(0) * __trmm_flop_count(trmm_args.side, trmm_args.B.extent(1), trmm_args.B.extent(2), |
| 119 |
trmm_args.A.extent(1), trmm_args.A.extent(2)); |
| 120 |
double gflops = flops / 1e9; |
| 121 |
double average_time = time_in_seconds / options.n; |
| 122 |
double gbytes_in_matrix = |
| 123 |
(trmm_args.B.extent(0) * trmm_args.B.extent(1) * trmm_args.B.extent(2) * sizeof(KokkosKernels::default_scalar)) / |
| 124 |
1e9; |
| 125 |
double min_memory_transactions, max_memory_transactions; |
| 126 |
|
| 127 |
// Assuming infinite cache size |
| 128 |
// We have to read A and B into the cache once and then write |
| 129 |
// B back out to main memory once. |
| 130 |
min_memory_transactions = 3; |
| 131 |
|
| 132 |
// Assuming no register or real caching |
| 133 |
// We have to go out to memory for every element we read from A and B as well |
| 134 |
// as every element we write to B. We use the trmm flops from lapack note 41 |
| 135 |
// and multiple by 3/2 to account for the write to B since this flop count is |
| 136 |
// for one multiply and one add. |
| 137 |
if (trmm_args.side == 'l' || trmm_args.side == 'L') |
| 138 |
max_memory_transactions = trmm_args.B.extent(1) * trmm_args.B.extent(1) * trmm_args.B.extent(2) * (3. / 2.); |
| 139 |
else |
| 140 |
max_memory_transactions = trmm_args.B.extent(2) * trmm_args.B.extent(2) * trmm_args.B.extent(1) * (3. / 2.); |
| 141 |
|
| 142 |
options.out[0] << test_e_str[options.test] << "," << options.blas_args.trmm.trmm_args << "," |
| 143 |
<< static_cast<double>(options.blas_args.trmm.alpha) << "," << loop_e_str[options.loop] << "," |
| 144 |
<< trmm_args.A.extent(0) << "x" << trmm_args.A.extent(1) << "x" << trmm_args.A.extent(2) << "," |
| 145 |
<< trmm_args.B.extent(0) << "x" << trmm_args.B.extent(1) << "x" << trmm_args.B.extent(2) << "," |
| 146 |
<< options.warm_up_n << "," << options.n << "," << time_in_seconds << "," << average_time << "," |
| 147 |
<< flops << "," << gflops / average_time << "," |
| 148 |
<< (gbytes_in_matrix * min_memory_transactions) / average_time << "," |
| 149 |
<< (gbytes_in_matrix * max_memory_transactions) / average_time << std::endl; |
| 150 |
} |
| 151 |
|
| 152 |
#ifdef PERF_TEST_DEBUG |
| 153 |
static void __print_trmm_perf_test_options(options_t options) { |
| 154 |
printf("options.test = %s\n", test_e_str[options.test].c_str()); |
| 155 |
printf("options.loop = %s\n", loop_e_str[options.loop].c_str()); |
| 156 |
printf("options.start = %dx%d,%dx%d\n", options.start.a.m, options.start.a.n, options.start.b.m, |
| 157 |
options.start.b.n); |
| 158 |
printf("options.stop = %dx%d,%dx%d\n", options.stop.a.m, options.stop.a.n, options.stop.b.m, options.stop.b.n); |
| 159 |
printf("options.step = %d\n", options.step); |
| 160 |
printf("options.warm_up_n = %d\n", options.warm_up_n); |
| 161 |
printf("options.n = %d\n", options.n); |
| 162 |
printf("options.blas_args.trmm.trmm_args = %s\n", options.blas_args.trmm.trmm_args.c_str()); |
| 163 |
printf("options.out_file = %s\n", options.out_file.c_str()); |
| 164 |
if (std::is_same<double, KokkosKernels::default_scalar>::value) |
| 165 |
printf("options.alpha = %lf\n", options.blas_args.trmm.alpha); |
| 166 |
else if (std::is_same<float, KokkosKernels::default_scalar>::value) |
| 167 |
printf("options.alpha = %f\n", options.blas_args.trmm.alpha); |
| 168 |
return; |
| 169 |
} |
| 170 |
#else |
| 171 |
static void __print_trmm_perf_test_options(options_t /*options*/) { return; } |
| 172 |
#endif // PERF_TEST_DEBUG |
| 173 |
|
| 174 |
/*************************** Internal templated fns **************************/ |
| 175 |
// Need to take subviews on the device |
| 176 |
#if !defined(KOKKOS_ENABLE_CUDA) && !defined(KOKKOS_ENABLE_HIP) && !defined(KOKKOS_ENABLE_OPENMPTARGET) |
| 177 |
template <class scalar_type, class vta, class vtb, class device_type> |
| 178 |
void __do_trmm_serial_blas(options_t options, trmm_args_t trmm_args) { |
| 179 |
uint32_t warm_up_n = options.warm_up_n; |
| 180 |
uint32_t n = options.n; |
| 181 |
Kokkos::Timer timer; |
| 182 |
|
| 183 |
STATUS; |
| 184 |
|
| 185 |
for (uint32_t j = 0; j < warm_up_n; ++j) { |
| 186 |
for (int i = 0; i < options.start.a.k; ++i) { |
| 187 |
auto A = Kokkos::subview(trmm_args.A, i, Kokkos::ALL(), Kokkos::ALL()); |
| 188 |
auto B = Kokkos::subview(trmm_args.B, i, Kokkos::ALL(), Kokkos::ALL()); |
| 189 |
|
| 190 |
KokkosBlas::trmm(&trmm_args.side, &trmm_args.uplo, &trmm_args.trans, &trmm_args.diag, trmm_args.alpha, A, B); |
| 191 |
} |
| 192 |
// Fence after submitting each batch operation |
| 193 |
Kokkos::fence(); |
| 194 |
} |
| 195 |
|
| 196 |
timer.reset(); |
| 197 |
for (uint32_t j = 0; j < n; ++j) { |
| 198 |
for (int i = 0; i < options.start.a.k; ++i) { |
| 199 |
auto A = Kokkos::subview(trmm_args.A, i, Kokkos::ALL(), Kokkos::ALL()); |
| 200 |
auto B = Kokkos::subview(trmm_args.B, i, Kokkos::ALL(), Kokkos::ALL()); |
| 201 |
|
| 202 |
KokkosBlas::trmm(&trmm_args.side, &trmm_args.uplo, &trmm_args.trans, &trmm_args.diag, trmm_args.alpha, A, B); |
| 203 |
} |
| 204 |
// Fence after submitting each batch operation |
| 205 |
Kokkos::fence(); |
| 206 |
} |
| 207 |
__trmm_output_csv_row(options, trmm_args, timer.seconds()); |
| 208 |
return; |
| 209 |
} |
| 210 |
#else |
| 211 |
template <class scalar_type, class vta, class vtb, class device_type> |
| 212 |
void __do_trmm_serial_blas(options_t /*options*/, trmm_args_t /*trmm_args*/) { |
| 213 |
std::cerr << std::string(__func__) |
| 214 |
<< " disabled since KOKKOS_ENABLE_CUDA or " |
| 215 |
"KOKKOS_ENABLE_OPENMPTARGET is defined." |
| 216 |
<< std::endl; |
| 217 |
return; |
| 218 |
} |
| 219 |
#endif // !KOKKOS_ENABLE_CUDA && !KOKKOS_ENABLE_OPENMPTARGET |
| 220 |
|
| 221 |
// Need to take subviews on the device |
| 222 |
#if !defined(KOKKOS_ENABLE_CUDA) && !defined(KOKKOS_ENABLE_HIP) && !defined(KOKKOS_ENABLE_OPENMPTARGET) |
| 223 |
template <class side, class uplo, class trans, class diag> |
| 224 |
void __do_trmm_serial_batched_template(options_t options, trmm_args_t trmm_args) { |
| 225 |
uint32_t warm_up_n = options.warm_up_n; |
| 226 |
uint32_t n = options.n; |
| 227 |
Kokkos::Timer timer; |
| 228 |
using tag = KokkosBatched::Algo::Trmm::Unblocked; |
| 229 |
|
| 230 |
for (uint32_t j = 0; j < warm_up_n; ++j) { |
| 231 |
for (int i = 0; i < options.start.a.k; ++i) { |
| 232 |
auto A = Kokkos::subview(trmm_args.A, i, Kokkos::ALL(), Kokkos::ALL()); |
| 233 |
auto B = Kokkos::subview(trmm_args.B, i, Kokkos::ALL(), Kokkos::ALL()); |
| 234 |
|
| 235 |
KokkosBatched::SerialTrmm<side, uplo, trans, diag, tag>::invoke(trmm_args.alpha, A, B); |
| 236 |
} |
| 237 |
// Fence after submitting each batch operation |
| 238 |
Kokkos::fence(); |
| 239 |
} |
| 240 |
|
| 241 |
timer.reset(); |
| 242 |
for (uint32_t j = 0; j < n; ++j) { |
| 243 |
for (int i = 0; i < options.start.a.k; ++i) { |
| 244 |
auto A = Kokkos::subview(trmm_args.A, i, Kokkos::ALL(), Kokkos::ALL()); |
| 245 |
auto B = Kokkos::subview(trmm_args.B, i, Kokkos::ALL(), Kokkos::ALL()); |
| 246 |
|
| 247 |
KokkosBatched::SerialTrmm<side, uplo, trans, diag, tag>::invoke(trmm_args.alpha, A, B); |
| 248 |
} |
| 249 |
// Fence after submitting each batch operation |
| 250 |
Kokkos::fence(); |
| 251 |
} |
| 252 |
__trmm_output_csv_row(options, trmm_args, timer.seconds()); |
| 253 |
} |
| 254 |
#else |
| 255 |
template <class side, class uplo, class trans, class diag> |
| 256 |
void __do_trmm_serial_batched_template(options_t /*options*/, trmm_args_t /*trmm_args*/) { |
| 257 |
std::cerr << std::string(__func__) |
| 258 |
<< " disabled since KOKKOS_ENABLE_CUDA or " |
| 259 |
"KOKKOS_ENABLE_OPENMPTARGET is defined." |
| 260 |
<< std::endl; |
| 261 |
} |
| 262 |
#endif // !KOKKOS_ENABLE_CUDA && !KOKKOS_ENABLE_OPENMPTARGET |
| 263 |
|
| 264 |
template <class scalar_type, class vta, class vtb, class device_type> |
| 265 |
void __do_trmm_serial_batched(options_t options, trmm_args_t trmm_args) { |
| 266 |
char __side = tolower(trmm_args.side), __uplo = tolower(trmm_args.uplo), __trans = tolower(trmm_args.trans); |
| 267 |
//__diag = tolower(diag[0]); |
| 268 |
|
| 269 |
using KokkosBatched::Diag; |
| 270 |
using KokkosBatched::Side; |
| 271 |
using KokkosBatched::Trans; |
| 272 |
using KokkosBatched::Uplo; |
| 273 |
|
| 274 |
STATUS; |
| 275 |
|
| 276 |
//// Lower non-transpose //// |
| 277 |
if (__side == 'l' && __uplo == 'l' && __trans == 'n') { |
| 278 |
STATUS; |
| 279 |
__do_trmm_serial_batched_template<Side::Left, Uplo::Lower, Trans::NoTranspose, Diag::Unit>(options, trmm_args); |
| 280 |
} |
| 281 |
if (__side == 'r' && __uplo == 'l' && __trans == 'n') { |
| 282 |
STATUS; |
| 283 |
__do_trmm_serial_batched_template<Side::Right, Uplo::Lower, Trans::NoTranspose, Diag::Unit>(options, trmm_args); |
| 284 |
} |
| 285 |
//// Lower transpose ///// |
| 286 |
// Transpose A by simply swapping the dimensions (extent) and stride |
| 287 |
// parameters |
| 288 |
if (__side == 'l' && __uplo == 'l' && __trans == 't') { |
| 289 |
STATUS; |
| 290 |
__do_trmm_serial_batched_template<Side::Left, Uplo::Lower, Trans::Transpose, Diag::Unit>(options, trmm_args); |
| 291 |
} |
| 292 |
if (__side == 'r' && __uplo == 'l' && __trans == 't') { |
| 293 |
STATUS; |
| 294 |
__do_trmm_serial_batched_template<Side::Right, Uplo::Lower, Trans::Transpose, Diag::Unit>(options, trmm_args); |
| 295 |
} |
| 296 |
//// Lower conjugate-transpose //// |
| 297 |
// Conjugate-Transpose A by simply swapping the dimensions (extent) and stride |
| 298 |
// parameters |
| 299 |
if (__side == 'l' && __uplo == 'l' && __trans == 'c') { |
| 300 |
STATUS; |
| 301 |
__do_trmm_serial_batched_template<Side::Left, Uplo::Lower, Trans::ConjTranspose, Diag::Unit>(options, trmm_args); |
| 302 |
} |
| 303 |
if (__side == 'r' && __uplo == 'l' && __trans == 'c') { |
| 304 |
STATUS; |
| 305 |
__do_trmm_serial_batched_template<Side::Right, Uplo::Lower, Trans::ConjTranspose, Diag::Unit>(options, trmm_args); |
| 306 |
} |
| 307 |
//// Upper non-transpose //// |
| 308 |
if (__side == 'l' && __uplo == 'u' && __trans == 'n') { |
| 309 |
STATUS; |
| 310 |
__do_trmm_serial_batched_template<Side::Left, Uplo::Upper, Trans::NoTranspose, Diag::Unit>(options, trmm_args); |
| 311 |
} |
| 312 |
if (__side == 'r' && __uplo == 'u' && __trans == 'n') { |
| 313 |
STATUS; |
| 314 |
__do_trmm_serial_batched_template<Side::Right, Uplo::Upper, Trans::NoTranspose, Diag::Unit>(options, trmm_args); |
| 315 |
} |
| 316 |
//// Upper transpose |
| 317 |
// Transpose A by simply swapping the dimensions (extent) and stride |
| 318 |
// parameters |
| 319 |
if (__side == 'l' && __uplo == 'u' && __trans == 't') { |
| 320 |
STATUS; |
| 321 |
__do_trmm_serial_batched_template<Side::Left, Uplo::Upper, Trans::Transpose, Diag::Unit>(options, trmm_args); |
| 322 |
} |
| 323 |
if (__side == 'r' && __uplo == 'u' && __trans == 't') { |
| 324 |
STATUS; |
| 325 |
__do_trmm_serial_batched_template<Side::Right, Uplo::Upper, Trans::Transpose, Diag::Unit>(options, trmm_args); |
| 326 |
} |
| 327 |
|
| 328 |
//// Upper conjugate-transpose //// |
| 329 |
// Conjugate-Transpose A by simply swapping the dimensions (extent) and stride |
| 330 |
// parameters |
| 331 |
if (__side == 'l' && __uplo == 'u' && __trans == 'c') { |
| 332 |
STATUS; |
| 333 |
__do_trmm_serial_batched_template<Side::Left, Uplo::Upper, Trans::ConjTranspose, Diag::Unit>(options, trmm_args); |
| 334 |
} |
| 335 |
if (__side == 'r' && __uplo == 'u' && __trans == 'c') { |
| 336 |
STATUS; |
| 337 |
__do_trmm_serial_batched_template<Side::Right, Uplo::Upper, Trans::ConjTranspose, Diag::Unit>(options, trmm_args); |
| 338 |
} |
| 339 |
|
| 340 |
return; |
| 341 |
} |
| 342 |
|
| 343 |
#if !defined(KOKKOS_ENABLE_CUDA) && !defined(KOKKOS_ENABLE_HIP) && !defined(KOKKOS_ENABLE_OPENMPTARGET) |
| 344 |
template <class ExecutionSpace> |
| 345 |
struct parallel_blas_trmm { |
| 346 |
trmm_args_t trmm_args_; |
| 347 |
|
| 348 |
parallel_blas_trmm(trmm_args_t trmm_args) : trmm_args_(trmm_args) {} |
| 349 |
|
| 350 |
KOKKOS_INLINE_FUNCTION |
| 351 |
void operator()(const int& i) const { |
| 352 |
auto svA = Kokkos::subview(trmm_args_.A, i, Kokkos::ALL(), Kokkos::ALL()); |
| 353 |
auto svB = Kokkos::subview(trmm_args_.B, i, Kokkos::ALL(), Kokkos::ALL()); |
| 354 |
|
| 355 |
KokkosBlas::trmm(&trmm_args_.side, &trmm_args_.uplo, &trmm_args_.trans, &trmm_args_.diag, trmm_args_.alpha, svA, |
| 356 |
svB); |
| 357 |
} |
| 358 |
}; |
| 359 |
#endif // !KOKKOSKERNELS_ENABLE_DEVICE |
| 360 |
|
| 361 |
template <class scalar_type, class vta, class vtb, class device_type> |
| 362 |
void __do_trmm_parallel_blas(options_t options, trmm_args_t trmm_args) { |
| 363 |
// TODO: Note why this is disabled on CUDA, OPENMPTARGET and HIP |
| 364 |
#if !defined(KOKKOS_ENABLE_CUDA) && !defined(KOKKOS_ENABLE_HIP) && !defined(KOKKOS_ENABLE_OPENMPTARGET) |
| 365 |
uint32_t warm_up_n = options.warm_up_n; |
| 366 |
uint32_t n = options.n; |
| 367 |
Kokkos::Timer timer; |
| 368 |
using execution_space = typename device_type::execution_space; |
| 369 |
using functor_type = parallel_blas_trmm<execution_space>; |
| 370 |
functor_type parallel_blas_trmm_functor(trmm_args); |
| 371 |
|
| 372 |
STATUS; |
| 373 |
|
| 374 |
for (uint32_t j = 0; j < warm_up_n; ++j) { |
| 375 |
Kokkos::parallel_for("parallelBlasWarmUpLoopTrmm", Kokkos::RangePolicy<execution_space>(0, options.start.a.k), |
| 376 |
parallel_blas_trmm_functor); |
| 377 |
// Fence after each batch operation |
| 378 |
Kokkos::fence(); |
| 379 |
} |
| 380 |
|
| 381 |
timer.reset(); |
| 382 |
for (uint32_t j = 0; j < n; ++j) { |
| 383 |
Kokkos::parallel_for("parallelBlasTimedLoopTrmm", Kokkos::RangePolicy<execution_space>(0, options.start.a.k), |
| 384 |
parallel_blas_trmm_functor); |
| 385 |
// Fence after each batch operation |
| 386 |
Kokkos::fence(); |
| 387 |
} |
| 388 |
__trmm_output_csv_row(options, trmm_args, timer.seconds()); |
| 389 |
#else |
| 390 |
std::cerr << std::string(__func__) |
| 391 |
<< " disabled since KOKKOS_ENABLE_CUDA, KOKKOS_ENABLE_HIP " |
| 392 |
"or KOKKOS_ENABLE_OPENMPTARGET is defined." |
| 393 |
<< std::endl; |
| 394 |
__trmm_output_csv_row(options, trmm_args, -1); |
| 395 |
#endif // !KOKKOS_ENABLE_DEVICE |
| 396 |
return; |
| 397 |
} |
| 398 |
|
| 399 |
template <class side, class uplo, class trans, class diag, class tag, class ExecutionSpace> |
| 400 |
struct parallel_batched_trmm { |
| 401 |
trmm_args_t trmm_args_; |
| 402 |
|
| 403 |
parallel_batched_trmm(trmm_args_t trmm_args) : trmm_args_(trmm_args) {} |
| 404 |
|
| 405 |
KOKKOS_INLINE_FUNCTION |
| 406 |
void operator()(const int& i) const { |
| 407 |
auto svA = Kokkos::subview(trmm_args_.A, i, Kokkos::ALL(), Kokkos::ALL()); |
| 408 |
auto svB = Kokkos::subview(trmm_args_.B, i, Kokkos::ALL(), Kokkos::ALL()); |
| 409 |
|
| 410 |
KokkosBatched::SerialTrmm<side, uplo, trans, diag, tag>::invoke(trmm_args_.alpha, svA, svB); |
| 411 |
} |
| 412 |
}; |
| 413 |
|
| 414 |
template <class side, class uplo, class trans, class diag, class device_type> |
| 415 |
void __do_trmm_parallel_batched_template(options_t options, trmm_args_t trmm_args) { |
| 416 |
uint32_t warm_up_n = options.warm_up_n; |
| 417 |
uint32_t n = options.n; |
| 418 |
Kokkos::Timer timer; |
| 419 |
using tag = KokkosBatched::Algo::Trmm::Unblocked; |
| 420 |
using execution_space = typename device_type::execution_space; |
| 421 |
using functor_type = parallel_batched_trmm<side, uplo, trans, diag, tag, execution_space>; |
| 422 |
functor_type parallel_batched_trmm_functor(trmm_args); |
| 423 |
|
| 424 |
STATUS; |
| 425 |
|
| 426 |
for (uint32_t j = 0; j < warm_up_n; ++j) { |
| 427 |
Kokkos::parallel_for("parallelBatchedWarmUpLoopTrmm", Kokkos::RangePolicy<execution_space>(0, options.start.a.k), |
| 428 |
parallel_batched_trmm_functor); |
| 429 |
// Fence after each batch operation |
| 430 |
Kokkos::fence(); |
| 431 |
} |
| 432 |
|
| 433 |
timer.reset(); |
| 434 |
for (uint32_t j = 0; j < n; ++j) { |
| 435 |
Kokkos::parallel_for("parallelBatchedTimedLoopTrmm", Kokkos::RangePolicy<execution_space>(0, options.start.a.k), |
| 436 |
parallel_batched_trmm_functor); |
| 437 |
// Fence after each batch operation |
| 438 |
Kokkos::fence(); |
| 439 |
} |
| 440 |
__trmm_output_csv_row(options, trmm_args, timer.seconds()); |
| 441 |
|
| 442 |
return; |
| 443 |
} |
| 444 |
|
| 445 |
template <class scalar_type, class vta, class vtb, class device_type> |
| 446 |
void __do_trmm_parallel_batched(options_t options, trmm_args_t trmm_args) { |
| 447 |
char __side = tolower(trmm_args.side), __uplo = tolower(trmm_args.uplo), __trans = tolower(trmm_args.trans); |
| 448 |
//__diag = tolower(diag[0]); |
| 449 |
|
| 450 |
using KokkosBatched::Diag; |
| 451 |
using KokkosBatched::Side; |
| 452 |
using KokkosBatched::Trans; |
| 453 |
using KokkosBatched::Uplo; |
| 454 |
|
| 455 |
STATUS; |
| 456 |
|
| 457 |
//// Lower non-transpose //// |
| 458 |
if (__side == 'l' && __uplo == 'l' && __trans == 'n') { |
| 459 |
STATUS; |
| 460 |
__do_trmm_parallel_batched_template<Side::Left, Uplo::Lower, Trans::NoTranspose, Diag::Unit, device_type>( |
| 461 |
options, trmm_args); |
| 462 |
} |
| 463 |
if (__side == 'r' && __uplo == 'l' && __trans == 'n') { |
| 464 |
STATUS; |
| 465 |
__do_trmm_parallel_batched_template<Side::Right, Uplo::Lower, Trans::NoTranspose, Diag::Unit, device_type>( |
| 466 |
options, trmm_args); |
| 467 |
} |
| 468 |
//// Lower transpose ///// |
| 469 |
// Transpose A by simply swapping the dimensions (extent) and stride |
| 470 |
// parameters |
| 471 |
if (__side == 'l' && __uplo == 'l' && __trans == 't') { |
| 472 |
STATUS; |
| 473 |
__do_trmm_parallel_batched_template<Side::Left, Uplo::Lower, Trans::Transpose, Diag::Unit, device_type>(options, |
| 474 |
trmm_args); |
| 475 |
} |
| 476 |
if (__side == 'r' && __uplo == 'l' && __trans == 't') { |
| 477 |
STATUS; |
| 478 |
__do_trmm_parallel_batched_template<Side::Right, Uplo::Lower, Trans::Transpose, Diag::Unit, device_type>(options, |
| 479 |
trmm_args); |
| 480 |
} |
| 481 |
//// Lower conjugate-transpose //// |
| 482 |
// Conjugate-Transpose A by simply swapping the dimensions (extent) and stride |
| 483 |
// parameters |
| 484 |
if (__side == 'l' && __uplo == 'l' && __trans == 'c') { |
| 485 |
STATUS; |
| 486 |
__do_trmm_parallel_batched_template<Side::Left, Uplo::Lower, Trans::ConjTranspose, Diag::Unit, device_type>( |
| 487 |
options, trmm_args); |
| 488 |
} |
| 489 |
if (__side == 'r' && __uplo == 'l' && __trans == 'c') { |
| 490 |
STATUS; |
| 491 |
__do_trmm_parallel_batched_template<Side::Right, Uplo::Lower, Trans::ConjTranspose, Diag::Unit, device_type>( |
| 492 |
options, trmm_args); |
| 493 |
} |
| 494 |
//// Upper non-transpose //// |
| 495 |
if (__side == 'l' && __uplo == 'u' && __trans == 'n') { |
| 496 |
STATUS; |
| 497 |
__do_trmm_parallel_batched_template<Side::Left, Uplo::Upper, Trans::NoTranspose, Diag::Unit, device_type>( |
| 498 |
options, trmm_args); |
| 499 |
} |
| 500 |
if (__side == 'r' && __uplo == 'u' && __trans == 'n') { |
| 501 |
STATUS; |
| 502 |
__do_trmm_parallel_batched_template<Side::Right, Uplo::Upper, Trans::NoTranspose, Diag::Unit, device_type>( |
| 503 |
options, trmm_args); |
| 504 |
} |
| 505 |
//// Upper transpose |
| 506 |
// Transpose A by simply swapping the dimensions (extent) and stride |
| 507 |
// parameters |
| 508 |
if (__side == 'l' && __uplo == 'u' && __trans == 't') { |
| 509 |
STATUS; |
| 510 |
__do_trmm_parallel_batched_template<Side::Left, Uplo::Upper, Trans::Transpose, Diag::Unit, device_type>(options, |
| 511 |
trmm_args); |
| 512 |
} |
| 513 |
if (__side == 'r' && __uplo == 'u' && __trans == 't') { |
| 514 |
STATUS; |
| 515 |
__do_trmm_parallel_batched_template<Side::Right, Uplo::Upper, Trans::Transpose, Diag::Unit, device_type>(options, |
| 516 |
trmm_args); |
| 517 |
} |
| 518 |
|
| 519 |
//// Upper conjugate-transpose //// |
| 520 |
// Conjugate-Transpose A by simply swapping the dimensions (extent) and stride |
| 521 |
// parameters |
| 522 |
if (__side == 'l' && __uplo == 'u' && __trans == 'c') { |
| 523 |
STATUS; |
| 524 |
__do_trmm_parallel_batched_template<Side::Left, Uplo::Upper, Trans::ConjTranspose, Diag::Unit, device_type>( |
| 525 |
options, trmm_args); |
| 526 |
} |
| 527 |
if (__side == 'r' && __uplo == 'u' && __trans == 'c') { |
| 528 |
STATUS; |
| 529 |
__do_trmm_parallel_batched_template<Side::Right, Uplo::Upper, Trans::ConjTranspose, Diag::Unit, device_type>( |
| 530 |
options, trmm_args); |
| 531 |
} |
| 532 |
|
| 533 |
return; |
| 534 |
} |
| 535 |
|
| 536 |
/*************************** Internal setup fns **************************/ |
| 537 |
template <class scalar_type, class vta, class vtb, class device_type> |
| 538 |
trmm_args_t __do_setup(options_t options, matrix_dims_t dim) { |
| 539 |
using execution_space = typename device_type::execution_space; |
| 540 |
|
| 541 |
trmm_args_t trmm_args; |
| 542 |
uint64_t seed = std::chrono::high_resolution_clock::now().time_since_epoch().count(); |
| 543 |
Kokkos::Random_XorShift64_Pool<execution_space> rand_pool(seed); |
| 544 |
decltype(dim.a.m) min_dim = dim.a.m < dim.a.n ? dim.a.m : dim.a.n; |
| 545 |
typename vta::HostMirror host_A; |
| 546 |
STATUS; |
| 547 |
|
| 548 |
trmm_args.side = options.blas_args.trmm.trmm_args.c_str()[0]; |
| 549 |
trmm_args.uplo = options.blas_args.trmm.trmm_args.c_str()[1]; |
| 550 |
trmm_args.trans = options.blas_args.trmm.trmm_args.c_str()[2]; |
| 551 |
trmm_args.diag = options.blas_args.trmm.trmm_args.c_str()[3]; |
| 552 |
trmm_args.A = vta("trmm_args.A", dim.a.k, dim.a.m, dim.a.n); |
| 553 |
trmm_args.B = vtb("trmm_args.B", dim.b.k, dim.b.m, dim.b.n); |
| 554 |
trmm_args.alpha = options.blas_args.trmm.alpha; |
| 555 |
host_A = Kokkos::create_mirror_view(trmm_args.A); |
| 556 |
|
| 557 |
{ |
| 558 |
Kokkos::View<double***, KokkosKernels::default_layout, KokkosKernels::default_device> tmp( |
| 559 |
"tmp", trmm_args.A.extent(0), trmm_args.A.extent(1), trmm_args.A.extent(2)); |
| 560 |
Kokkos::fill_random(tmp, rand_pool, Kokkos::rand<Kokkos::Random_XorShift64<execution_space>, double>::max()); |
| 561 |
Kokkos::deep_copy(host_A, tmp); |
| 562 |
} |
| 563 |
|
| 564 |
if (trmm_args.uplo == 'U' || trmm_args.uplo == 'u') { |
| 565 |
// Make A upper triangular |
| 566 |
for (int k = 0; k < dim.a.k; ++k) { |
| 567 |
auto A = Kokkos::subview(host_A, k, Kokkos::ALL(), Kokkos::ALL()); |
| 568 |
for (int i = 1; i < dim.a.m; i++) { |
| 569 |
for (int j = 0; j < i; j++) { |
| 570 |
A(i, j) = scalar_type(0); |
| 571 |
} |
| 572 |
} |
| 573 |
} |
| 574 |
} else { |
| 575 |
// Make A lower triangular |
| 576 |
// Kokkos::parallel_for("toLowerLoop", options.n, KOKKOS_LAMBDA (const int& |
| 577 |
// i) { |
| 578 |
for (int k = 0; k < dim.a.k; ++k) { |
| 579 |
auto A = Kokkos::subview(host_A, k, Kokkos::ALL(), Kokkos::ALL()); |
| 580 |
for (int i = 0; i < dim.a.m - 1; i++) { |
| 581 |
for (int j = i + 1; j < dim.a.n; j++) { |
| 582 |
A(i, j) = scalar_type(0); |
| 583 |
} |
| 584 |
} |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
if (trmm_args.diag == 'U' || trmm_args.diag == 'u') { |
| 589 |
for (int k = 0; k < dim.a.k; ++k) { |
| 590 |
auto A = Kokkos::subview(host_A, k, Kokkos::ALL(), Kokkos::ALL()); |
| 591 |
for (int i = 0; i < min_dim; i++) { |
| 592 |
A(i, i) = scalar_type(1); |
| 593 |
} |
| 594 |
} |
| 595 |
} |
| 596 |
Kokkos::deep_copy(trmm_args.A, host_A); |
| 597 |
|
| 598 |
{ |
| 599 |
Kokkos::View<double***, KokkosKernels::default_layout, KokkosKernels::default_device> tmp( |
| 600 |
"tmp", trmm_args.B.extent(0), trmm_args.B.extent(1), trmm_args.B.extent(2)); |
| 601 |
Kokkos::fill_random(tmp, rand_pool, Kokkos::rand<Kokkos::Random_XorShift64<execution_space>, double>::max()); |
| 602 |
Kokkos::deep_copy(trmm_args.B, tmp); |
| 603 |
} |
| 604 |
|
| 605 |
return trmm_args; |
| 606 |
} |
| 607 |
|
| 608 |
/*************************** Interal run helper fns **************************/ |
| 609 |
void __do_loop_and_invoke(options_t options, void (*fn)(options_t, trmm_args_t)) { |
| 610 |
matrix_dims_t cur_dims; |
| 611 |
trmm_args_t trmm_args; |
| 612 |
STATUS; |
| 613 |
|
| 614 |
__print_trmm_perf_test_options(options); |
| 615 |
std::cout << "SCALAR:" << typeid(KokkosKernels::default_scalar).name() |
| 616 |
<< ", LAYOUT:" << typeid(KokkosKernels::default_layout).name() |
| 617 |
<< ", DEVICE:" << typeid(KokkosKernels::default_device).name() << std::endl; |
| 618 |
|
| 619 |
options.out[0] << trmm_csv_header_str << std::endl; |
| 620 |
|
| 621 |
for (cur_dims = options.start; cur_dims.a.m <= options.stop.a.m && cur_dims.a.n <= options.stop.a.n && |
| 622 |
cur_dims.b.m <= options.stop.b.m && cur_dims.b.n <= options.stop.b.n; |
| 623 |
cur_dims.a.m += options.step, cur_dims.a.n += options.step, cur_dims.b.m += options.step, |
| 624 |
cur_dims.b.n += options.step) { |
| 625 |
trmm_args = __do_setup<KokkosKernels::default_scalar, view_type_3d, view_type_3d, KokkosKernels::default_device>( |
| 626 |
options, cur_dims); |
| 627 |
fn(options, trmm_args); |
| 628 |
} |
| 629 |
return; |
| 630 |
} |
| 631 |
|
| 632 |
/*************************** External fns **************************/ |
| 633 |
void do_trmm_serial_blas(options_t options) { |
| 634 |
STATUS; |
| 635 |
__do_loop_and_invoke( |
| 636 |
options, |
| 637 |
__do_trmm_serial_blas<KokkosKernels::default_scalar, view_type_3d, view_type_3d, KokkosKernels::default_device>); |
| 638 |
return; |
| 639 |
} |
| 640 |
|
| 641 |
void do_trmm_serial_batched(options_t options) { |
| 642 |
STATUS; |
| 643 |
__do_loop_and_invoke(options, __do_trmm_serial_batched<KokkosKernels::default_scalar, view_type_3d, view_type_3d, |
| 644 |
KokkosKernels::default_device>); |
| 645 |
return; |
| 646 |
} |
| 647 |
|
| 648 |
void do_trmm_parallel_blas(options_t options) { |
| 649 |
STATUS; |
| 650 |
__do_loop_and_invoke(options, __do_trmm_parallel_blas<KokkosKernels::default_scalar, view_type_3d, view_type_3d, |
| 651 |
KokkosKernels::default_device>); |
| 652 |
return; |
| 653 |
} |
| 654 |
|
| 655 |
void do_trmm_parallel_batched(options_t options) { |
| 656 |
STATUS; |
| 657 |
__do_loop_and_invoke(options, __do_trmm_parallel_batched<KokkosKernels::default_scalar, view_type_3d, view_type_3d, |
| 658 |
KokkosKernels::default_device>); |
| 659 |
return; |
| 660 |
} |
| 661 |
|
| 662 |
#endif // KOKKOSBLAS3_TRMM_PERF_TEST_H_ |