| 1 |
//@HEADER |
| 2 |
// ************************************************************************ |
| 3 |
// |
| 4 |
// Kokkos v. 4.0 |
| 5 |
// Copyright (2022) National Technology & Engineering |
| 6 |
// Solutions of Sandia, LLC (NTESS). |
| 7 |
// |
| 8 |
// Under the terms of Contract DE-NA0003525 with NTESS, |
| 9 |
// the U.S. Government retains certain rights in this software. |
| 10 |
// |
| 11 |
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions. |
| 12 |
// See https://kokkos.org/LICENSE for license information. |
| 13 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 14 |
// |
| 15 |
//@HEADER |
| 16 |
|
| 17 |
#include <Kokkos_Core.hpp> |
| 18 |
#include <Kokkos_Random.hpp> |
| 19 |
|
| 20 |
#include "KokkosKernels_helpers.hpp" |
| 21 |
#include "KokkosBlas2_ger.hpp" |
| 22 |
#include <typeinfo> |
| 23 |
|
| 24 |
#include "KokkosKernels_TestStringUtils.hpp" |
| 25 |
#include "KokkosKernels_perf_test_utilities.hpp" |
| 26 |
|
| 27 |
#include <Benchmark_Context.hpp> |
| 28 |
#include <benchmark/benchmark.h> |
| 29 |
|
| 30 |
struct blas2_ger_params : public perf_test::CommonInputParams { |
| 31 |
int verbosity = 0; |
| 32 |
int m = 5000; |
| 33 |
int n = 5000; |
| 34 |
bool layoutLeft = true; |
| 35 |
std::string scalarType = "double"; |
| 36 |
std::string yMode = "transpose"; |
| 37 |
|
| 38 |
static blas2_ger_params get_params(int& argc, char** argv) { |
| 39 |
blas2_ger_params params; |
| 40 |
perf_test::parse_common_options(argc, argv, params); |
| 41 |
|
| 42 |
for (int i = 1; i < argc; ++i) { |
| 43 |
if (perf_test::check_arg_int(i, argc, argv, "--verbosity", params.verbosity)) { |
| 44 |
++i; |
| 45 |
} else if (perf_test::check_arg_int(i, argc, argv, "--m", params.m)) { |
| 46 |
++i; |
| 47 |
} else if (perf_test::check_arg_int(i, argc, argv, "--n", params.n)) { |
| 48 |
++i; |
| 49 |
} else if (std::string layout; perf_test::check_arg_str(i, argc, argv, "--layout", layout)) { |
| 50 |
if (0 == Test::string_compare_no_case(layout, "left")) |
| 51 |
params.layoutLeft = true; |
| 52 |
else if (0 == Test::string_compare_no_case(layout, "right")) |
| 53 |
params.layoutLeft = false; |
| 54 |
else { |
| 55 |
std::cerr << "Invalid '--layout': must be 'left' or 'right'.\n"; |
| 56 |
exit(1); |
| 57 |
} |
| 58 |
++i; |
| 59 |
} else if (std::string scalarType; perf_test::check_arg_str(i, argc, argv, "--scalarType", scalarType)) { |
| 60 |
if ((0 == Test::string_compare_no_case(scalarType, "int32")) || |
| 61 |
(0 == Test::string_compare_no_case(scalarType, "int64")) || |
| 62 |
(0 == Test::string_compare_no_case(scalarType, "float")) || |
| 63 |
(0 == Test::string_compare_no_case(scalarType, "double")) || |
| 64 |
(0 == Test::string_compare_no_case(scalarType, "complex_float")) || |
| 65 |
(0 == Test::string_compare_no_case(scalarType, "complex_double"))) { |
| 66 |
params.scalarType = scalarType; |
| 67 |
} else { |
| 68 |
std::cerr << "Invalid '--scalarType': must be 'int32' or 'int64' or " |
| 69 |
"'float' or 'double' or 'complex_float' or " |
| 70 |
"'complex_double'.\n"; |
| 71 |
exit(1); |
| 72 |
} |
| 73 |
++i; |
| 74 |
} else if (std::string yMode; perf_test::check_arg_str(i, argc, argv, "--yMode", yMode)) { |
| 75 |
if ((0 == Test::string_compare_no_case(yMode, "transpose")) || |
| 76 |
(0 == Test::string_compare_no_case(yMode, "Hermitian"))) { |
| 77 |
params.yMode = yMode; |
| 78 |
} else { |
| 79 |
std::cerr << "Invalid '--yMode': must be 'transpose' or 'Hermitian'.\n"; |
| 80 |
exit(1); |
| 81 |
} |
| 82 |
++i; |
| 83 |
} else { |
| 84 |
std::cerr << "Unrecognized command line argument #" << i << ": " << argv[i] << std::endl; |
| 85 |
print_options(); |
| 86 |
exit(1); |
| 87 |
} |
| 88 |
} |
| 89 |
return params; |
| 90 |
} |
| 91 |
|
| 92 |
static void print_options() { |
| 93 |
std::cerr << "Options\n" << std::endl; |
| 94 |
std::cerr << perf_test::list_common_options(); |
| 95 |
|
| 96 |
std::cerr << "\t[Optional] --m :: number of rows to generate (default 5000)" << std::endl; |
| 97 |
std::cerr << "\t[Optional] --n :: number of cols to generate (default 5000)" << std::endl; |
| 98 |
std::cerr << "\t[Optional] --layout :: matrix layout ('left' or 'right', " |
| 99 |
"default 'left')" |
| 100 |
<< std::endl; |
| 101 |
std::cerr << "\t[Optional] --scalarType :: scalar type ('int32' or 'int64'" |
| 102 |
" or 'float' or 'double' or 'complex_float' or 'complex_double'" |
| 103 |
", default 'double')" |
| 104 |
<< std::endl; |
| 105 |
std::cerr << "\t[Optional] --yMode :: y mode ('transpose' or 'Hermitian'" |
| 106 |
", default 'transpose')" |
| 107 |
<< std::endl; |
| 108 |
} |
| 109 |
}; |
| 110 |
|
| 111 |
template <typename tScalar, typename tLayout, typename tExecSpace> |
| 112 |
static void KokkosBlas2_GER(benchmark::State& state) { |
| 113 |
const auto verbosity = state.range(0); |
| 114 |
const auto m = state.range(1); |
| 115 |
const auto n = state.range(2); |
| 116 |
const auto yIsTranspose = state.range(3); |
| 117 |
tScalar a(Kokkos::ArithTraits<tScalar>::zero()); |
| 118 |
|
| 119 |
if (verbosity > 0) { |
| 120 |
std::cout << "Entering KokkosBlas2_GER()" |
| 121 |
<< ": m = " << m << ", n = " << n << ", yIsTranspose = " << yIsTranspose |
| 122 |
<< ", tScalar = " << Kokkos::ArithTraits<tScalar>::name() << ", tLayout = " << typeid(tLayout).name() |
| 123 |
<< std::endl; |
| 124 |
} |
| 125 |
|
| 126 |
using MemSpace = typename tExecSpace::memory_space; |
| 127 |
using Device = Kokkos::Device<tExecSpace, MemSpace>; |
| 128 |
|
| 129 |
Kokkos::View<tScalar**, tLayout, Device> A(Kokkos::view_alloc(Kokkos::WithoutInitializing, "A"), m, n); |
| 130 |
Kokkos::View<tScalar*, Device> x(Kokkos::view_alloc(Kokkos::WithoutInitializing, "x"), m); |
| 131 |
Kokkos::View<tScalar*, Device> y(Kokkos::view_alloc(Kokkos::WithoutInitializing, "y"), n); |
| 132 |
|
| 133 |
Kokkos::Random_XorShift64_Pool<tExecSpace> pool(123); |
| 134 |
|
| 135 |
char yMode('t'); |
| 136 |
if (!yIsTranspose) yMode = 'H'; |
| 137 |
|
| 138 |
tScalar rangeValue(Kokkos::ArithTraits<tScalar>::zero()); |
| 139 |
if constexpr (Kokkos::ArithTraits<tScalar>::isOrdinal) { |
| 140 |
rangeValue = 10; |
| 141 |
a = 3; |
| 142 |
} else if constexpr (Kokkos::ArithTraits<tScalar>::is_complex) { |
| 143 |
rangeValue.real() = 10.; |
| 144 |
rangeValue.imag() = 10.; |
| 145 |
a = tScalar(2.5, 3.6); |
| 146 |
} else { |
| 147 |
rangeValue = 10.; |
| 148 |
a = 2.5; |
| 149 |
} |
| 150 |
|
| 151 |
// Fill 'A', 'x', and 'y' with samples from an uniform random variable with |
| 152 |
// range [1,rangeValue) |
| 153 |
Kokkos::fill_random(A, pool, rangeValue); |
| 154 |
Kokkos::fill_random(x, pool, rangeValue); |
| 155 |
Kokkos::fill_random(y, pool, rangeValue); |
| 156 |
|
| 157 |
if (verbosity > 0) { |
| 158 |
std::cout << "In KokkosBlas2_GER()" |
| 159 |
<< ": yMode = " << yMode << ", a = " << a << ", rangeValue = " << rangeValue << std::endl; |
| 160 |
} |
| 161 |
|
| 162 |
// Do a warm-up run |
| 163 |
KokkosBlas::ger(&yMode, a, x, y, A); |
| 164 |
Kokkos::fence(); |
| 165 |
double total_time = 0.0; |
| 166 |
|
| 167 |
for (auto _ : state) { |
| 168 |
// Start timing |
| 169 |
Kokkos::Timer timer; |
| 170 |
KokkosBlas::ger(&yMode, a, x, y, A); |
| 171 |
tExecSpace().fence(); |
| 172 |
|
| 173 |
double time = timer.seconds(); |
| 174 |
total_time += time; |
| 175 |
state.SetIterationTime(time); |
| 176 |
} |
| 177 |
|
| 178 |
state.counters[tExecSpace::name()] = 1; |
| 179 |
state.counters["Avg GER time (s):"] = benchmark::Counter(total_time, benchmark::Counter::kAvgIterations); |
| 180 |
size_t flopsPerRun = (size_t)3 * m * n; |
| 181 |
state.counters["Avg GER FLOP/s:"] = benchmark::Counter(flopsPerRun, benchmark::Counter::kIsIterationInvariantRate); |
| 182 |
|
| 183 |
if (verbosity > 0) { |
| 184 |
std::cout << "Leaving KokkosBlas2_GER()" << std::endl; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
template <typename tExecSpace> |
| 189 |
void run(const blas2_ger_params& params) { |
| 190 |
const auto name = "KokkosBlas2_GER"; |
| 191 |
const auto arg_names = |
| 192 |
std::vector<std::string>{"verbosity", "m", "n", "yMode", params.layoutLeft ? "LayoutLeft" : "LayoutRight"}; |
| 193 |
const auto args = std::vector<int64_t>{params.verbosity, params.m, params.n, (params.yMode == "transpose"), 1}; |
| 194 |
|
| 195 |
if (params.layoutLeft) { |
| 196 |
if (params.scalarType == "int32") { |
| 197 |
KokkosKernelsBenchmark::register_benchmark(name, KokkosBlas2_GER<std::int32_t, Kokkos::LayoutLeft, tExecSpace>, |
| 198 |
arg_names, args, params.repeat); |
| 199 |
} else if (params.scalarType == "int64") { |
| 200 |
KokkosKernelsBenchmark::register_benchmark(name, KokkosBlas2_GER<std::int64_t, Kokkos::LayoutLeft, tExecSpace>, |
| 201 |
arg_names, args, params.repeat); |
| 202 |
} else if (params.scalarType == "float") { |
| 203 |
KokkosKernelsBenchmark::register_benchmark(name, KokkosBlas2_GER<float, Kokkos::LayoutLeft, tExecSpace>, |
| 204 |
arg_names, args, params.repeat); |
| 205 |
} else if (params.scalarType == "double") { |
| 206 |
KokkosKernelsBenchmark::register_benchmark(name, KokkosBlas2_GER<double, Kokkos::LayoutLeft, tExecSpace>, |
| 207 |
arg_names, args, params.repeat); |
| 208 |
} else if (params.scalarType == "complex_float") { |
| 209 |
KokkosKernelsBenchmark::register_benchmark( |
| 210 |
name, KokkosBlas2_GER<Kokkos::complex<float>, Kokkos::LayoutLeft, tExecSpace>, arg_names, args, |
| 211 |
params.repeat); |
| 212 |
} else { |
| 213 |
KokkosKernelsBenchmark::register_benchmark( |
| 214 |
name, KokkosBlas2_GER<Kokkos::complex<double>, Kokkos::LayoutLeft, tExecSpace>, arg_names, args, |
| 215 |
params.repeat); |
| 216 |
} |
| 217 |
} else { |
| 218 |
if (params.scalarType == "int32") { |
| 219 |
KokkosKernelsBenchmark::register_benchmark(name, KokkosBlas2_GER<std::int32_t, Kokkos::LayoutRight, tExecSpace>, |
| 220 |
arg_names, args, params.repeat); |
| 221 |
} else if (params.scalarType == "int64") { |
| 222 |
KokkosKernelsBenchmark::register_benchmark(name, KokkosBlas2_GER<std::int64_t, Kokkos::LayoutRight, tExecSpace>, |
| 223 |
arg_names, args, params.repeat); |
| 224 |
} else if (params.scalarType == "float") { |
| 225 |
KokkosKernelsBenchmark::register_benchmark(name, KokkosBlas2_GER<float, Kokkos::LayoutRight, tExecSpace>, |
| 226 |
arg_names, args, params.repeat); |
| 227 |
} else if (params.scalarType == "double") { |
| 228 |
KokkosKernelsBenchmark::register_benchmark(name, KokkosBlas2_GER<double, Kokkos::LayoutRight, tExecSpace>, |
| 229 |
arg_names, args, params.repeat); |
| 230 |
} else if (params.scalarType == "complex_float") { |
| 231 |
KokkosKernelsBenchmark::register_benchmark( |
| 232 |
name, KokkosBlas2_GER<Kokkos::complex<float>, Kokkos::LayoutRight, tExecSpace>, arg_names, args, |
| 233 |
params.repeat); |
| 234 |
} else { |
| 235 |
KokkosKernelsBenchmark::register_benchmark( |
| 236 |
name, KokkosBlas2_GER<Kokkos::complex<double>, Kokkos::LayoutRight, tExecSpace>, arg_names, args, |
| 237 |
params.repeat); |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
int main(int argc, char** argv) { |
| 243 |
Kokkos::initialize(argc, argv); |
| 244 |
benchmark::Initialize(&argc, argv); |
| 245 |
benchmark::SetDefaultTimeUnit(benchmark::kSecond); |
| 246 |
KokkosKernelsBenchmark::add_benchmark_context(true); |
| 247 |
|
| 248 |
const auto params = blas2_ger_params::get_params(argc, argv); |
| 249 |
|
| 250 |
// std::cout << "In main(): params.repeat = " << params.repeat << std::endl; |
| 251 |
|
| 252 |
if (params.use_threads) { |
| 253 |
#if defined(KOKKOS_ENABLE_THREADS) |
| 254 |
run<Kokkos::Threads>(params); |
| 255 |
#else |
| 256 |
std::cout << "ERROR: PThreads requested, but not available.\n"; |
| 257 |
return 1; |
| 258 |
#endif |
| 259 |
} |
| 260 |
|
| 261 |
if (params.use_openmp) { |
| 262 |
#if defined(KOKKOS_ENABLE_OPENMP) |
| 263 |
run<Kokkos::OpenMP>(params); |
| 264 |
#else |
| 265 |
std::cout << "ERROR: OpenMP requested, but not available.\n"; |
| 266 |
return 1; |
| 267 |
#endif |
| 268 |
} |
| 269 |
|
| 270 |
if (params.use_cuda) { |
| 271 |
#if defined(KOKKOS_ENABLE_CUDA) |
| 272 |
run<Kokkos::Cuda>(params); |
| 273 |
#else |
| 274 |
std::cout << "ERROR: CUDA requested, but not available.\n"; |
| 275 |
return 1; |
| 276 |
#endif |
| 277 |
} |
| 278 |
|
| 279 |
if (params.use_hip) { |
| 280 |
#if defined(KOKKOS_ENABLE_HIP) |
| 281 |
run<Kokkos::HIP>(params); |
| 282 |
#else |
| 283 |
std::cout << "ERROR: HIP requested, but not available.\n"; |
| 284 |
return 1; |
| 285 |
#endif |
| 286 |
} |
| 287 |
|
| 288 |
if (params.use_sycl) { |
| 289 |
#if defined(KOKKOS_ENABLE_SYCL) |
| 290 |
run<Kokkos::Experimental::SYCL>(params); |
| 291 |
#else |
| 292 |
std::cout << "ERROR: SYCL requested, but not available.\n"; |
| 293 |
return 1; |
| 294 |
#endif |
| 295 |
} |
| 296 |
|
| 297 |
// use serial if no backend is specified |
| 298 |
if (!params.use_cuda && !params.use_hip && !params.use_openmp && !params.use_sycl && !params.use_threads) { |
| 299 |
#if defined(KOKKOS_ENABLE_SERIAL) |
| 300 |
run<Kokkos::Serial>(params); |
| 301 |
#else |
| 302 |
std::cout << "ERROR: Serial device requested, but not available.\n"; |
| 303 |
return 1; |
| 304 |
#endif |
| 305 |
} |
| 306 |
|
| 307 |
benchmark::RunSpecifiedBenchmarks(); |
| 308 |
|
| 309 |
benchmark::Shutdown(); |
| 310 |
Kokkos::finalize(); |
| 311 |
return 0; |
| 312 |
} |