| 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 "KokkosBlas3_gemm.hpp" |
| 18 |
#include <Kokkos_Random.hpp> |
| 19 |
#include "KokkosKernels_TestStringUtils.hpp" |
| 20 |
#include "KokkosKernels_perf_test_utilities.hpp" |
| 21 |
#include "Benchmark_Context.hpp" |
| 22 |
#include <benchmark/benchmark.h> |
| 23 |
|
| 24 |
struct blas3_gemm_params : public perf_test::CommonInputParams { |
| 25 |
int m = 1000; |
| 26 |
int n = 1000; |
| 27 |
int k = 1000; |
| 28 |
|
| 29 |
static blas3_gemm_params get_params(int& argc, char** argv) { |
| 30 |
blas3_gemm_params params; |
| 31 |
perf_test::parse_common_options(argc, argv, params); |
| 32 |
|
| 33 |
for (int i = 1; i < argc; ++i) { |
| 34 |
if (perf_test::check_arg_int(i, argc, argv, "--m", params.m)) { |
| 35 |
++i; |
| 36 |
} else if (perf_test::check_arg_int(i, argc, argv, "--n", params.n)) { |
| 37 |
++i; |
| 38 |
} else if (perf_test::check_arg_int(i, argc, argv, "--k", params.k)) { |
| 39 |
++i; |
| 40 |
} else if (std::string(argv[i]).find("--benchmark") == 0) { |
| 41 |
continue; // ignore benchmark arguments |
| 42 |
} else { |
| 43 |
std::cerr << "Unrecognized command line argument #" << i << ": " << argv[i] << std::endl; |
| 44 |
print_options(); |
| 45 |
exit(1); |
| 46 |
} |
| 47 |
} |
| 48 |
return params; |
| 49 |
} |
| 50 |
|
| 51 |
static void print_options() { |
| 52 |
std::cerr << "Options\n" << std::endl; |
| 53 |
std::cerr << perf_test::list_common_options(); |
| 54 |
|
| 55 |
std::cerr << "\t[Optional] --m :: Rows in A (default 1000)" << std::endl; |
| 56 |
std::cerr << "\t[Optional] --n :: Columns in A / Rows in B (default 1000)" << std::endl; |
| 57 |
std::cerr << "\t[Optional] --k :: Columns in B (default 1000)" << std::endl; |
| 58 |
} |
| 59 |
}; |
| 60 |
|
| 61 |
template <typename ExecSpace, typename Scalar, typename ALayout, typename BLayout> |
| 62 |
static void KokkosBlas3_GEMM(benchmark::State& state) { |
| 63 |
const auto m = state.range(0); |
| 64 |
const auto n = state.range(1); |
| 65 |
const auto k = state.range(2); |
| 66 |
|
| 67 |
using MemSpace = typename ExecSpace::memory_space; |
| 68 |
using Device = Kokkos::Device<ExecSpace, MemSpace>; |
| 69 |
Kokkos::View<Scalar**, ALayout, Device> A(Kokkos::view_alloc(Kokkos::WithoutInitializing, "A"), m, n); |
| 70 |
Kokkos::View<Scalar**, BLayout, Device> B(Kokkos::view_alloc(Kokkos::WithoutInitializing, "B"), n, k); |
| 71 |
Kokkos::View<Scalar**, Kokkos::LayoutLeft, Device> C(Kokkos::view_alloc(Kokkos::WithoutInitializing, "C"), m, k); |
| 72 |
Kokkos::Random_XorShift64_Pool<ExecSpace> pool(123); |
| 73 |
Kokkos::fill_random(A, pool, 10.0); |
| 74 |
Kokkos::fill_random(B, pool, 10.0); |
| 75 |
|
| 76 |
// Do a warm-up run |
| 77 |
KokkosBlas::gemm("N", "N", 1.0, A, B, 0.0, C); |
| 78 |
Kokkos::fence(); |
| 79 |
double total_time = 0.0; |
| 80 |
|
| 81 |
for (auto _ : state) { |
| 82 |
Kokkos::Timer timer; |
| 83 |
KokkosBlas::gemm("N", "N", 1.0, A, B, 0.0, C); |
| 84 |
ExecSpace().fence(); |
| 85 |
|
| 86 |
double time = timer.seconds(); |
| 87 |
total_time += time; |
| 88 |
state.SetIterationTime(time); |
| 89 |
} |
| 90 |
|
| 91 |
state.counters[ExecSpace::name()] = 1; |
| 92 |
state.counters["Avg GEMM time (s):"] = benchmark::Counter(total_time, benchmark::Counter::kAvgIterations); |
| 93 |
size_t flopsPerRun = (size_t)2 * m * n * k; |
| 94 |
state.counters["Avg GEMM (FLOP/s):"] = benchmark::Counter(flopsPerRun, benchmark::Counter::kIsIterationInvariantRate); |
| 95 |
if constexpr (std::is_same_v<ALayout, Kokkos::LayoutLeft>) { |
| 96 |
state.counters["Memory Layout in A: LayoutLeft"] = 1; |
| 97 |
} else { |
| 98 |
state.counters["Memory Layout in A: LayoutRight"] = 1; |
| 99 |
} |
| 100 |
if constexpr (std::is_same_v<BLayout, Kokkos::LayoutLeft>) { |
| 101 |
state.counters["Memory Layout in B: LayoutLeft"] = 1; |
| 102 |
} else { |
| 103 |
state.counters["Memory Layout in B: LayoutRight"] = 1; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
template <typename ExecSpace> |
| 108 |
void run(const blas3_gemm_params& params) { |
| 109 |
using LL = Kokkos::LayoutLeft; |
| 110 |
using LR = Kokkos::LayoutRight; |
| 111 |
using Scalar = double; |
| 112 |
|
| 113 |
const auto name = "KokkosBlas3_GEMM"; |
| 114 |
const auto arg_names = std::vector<std::string>{"m", "n", "k"}; |
| 115 |
const auto args = std::vector<int64_t>{params.m, params.n, params.k}; |
| 116 |
|
| 117 |
KokkosKernelsBenchmark::register_benchmark(name, KokkosBlas3_GEMM<ExecSpace, Scalar, LL, LL>, arg_names, args, |
| 118 |
params.repeat); |
| 119 |
KokkosKernelsBenchmark::register_benchmark(name, KokkosBlas3_GEMM<ExecSpace, Scalar, LL, LR>, arg_names, args, |
| 120 |
params.repeat); |
| 121 |
KokkosKernelsBenchmark::register_benchmark(name, KokkosBlas3_GEMM<ExecSpace, Scalar, LR, LL>, arg_names, args, |
| 122 |
params.repeat); |
| 123 |
KokkosKernelsBenchmark::register_benchmark(name, KokkosBlas3_GEMM<ExecSpace, Scalar, LR, LR>, arg_names, args, |
| 124 |
params.repeat); |
| 125 |
} |
| 126 |
|
| 127 |
int main(int argc, char** argv) { |
| 128 |
const auto params = blas3_gemm_params::get_params(argc, argv); |
| 129 |
const int num_threads = params.use_openmp; |
| 130 |
|
| 131 |
// the common parameter parser takes the requested device ID and |
| 132 |
// adds 1 to it (e.g. --cuda 0 -> params.use_cuda = 1) |
| 133 |
// this is presumably so that 0 can be a sentinel value, |
| 134 |
// even though device ID 0 is valid |
| 135 |
// here, we use CUDA, SYCL, or HIP, whichever is set first, to |
| 136 |
// choose which device Kokkos should initialize on |
| 137 |
// or -1, for no such selection |
| 138 |
const int device_id = params.use_cuda |
| 139 |
? params.use_cuda - 1 |
| 140 |
: (params.use_sycl ? params.use_sycl - 1 : (params.use_hip ? params.use_hip - 1 : -1)); |
| 141 |
|
| 142 |
Kokkos::initialize(Kokkos::InitializationSettings().set_num_threads(num_threads).set_device_id(device_id)); |
| 143 |
benchmark::Initialize(&argc, argv); |
| 144 |
benchmark::SetDefaultTimeUnit(benchmark::kSecond); |
| 145 |
KokkosKernelsBenchmark::add_benchmark_context(true); |
| 146 |
|
| 147 |
if (params.use_threads) { |
| 148 |
#if defined(KOKKOS_ENABLE_THREADS) |
| 149 |
run<Kokkos::Threads>(params); |
| 150 |
#else |
| 151 |
std::cout << "ERROR: PThreads requested, but not available.\n"; |
| 152 |
return 1; |
| 153 |
#endif |
| 154 |
} |
| 155 |
|
| 156 |
if (params.use_openmp) { |
| 157 |
#if defined(KOKKOS_ENABLE_OPENMP) |
| 158 |
run<Kokkos::OpenMP>(params); |
| 159 |
#else |
| 160 |
std::cout << "ERROR: OpenMP requested, but not available.\n"; |
| 161 |
return 1; |
| 162 |
#endif |
| 163 |
} |
| 164 |
|
| 165 |
if (params.use_cuda) { |
| 166 |
#if defined(KOKKOS_ENABLE_CUDA) |
| 167 |
run<Kokkos::Cuda>(params); |
| 168 |
#else |
| 169 |
std::cout << "ERROR: CUDA requested, but not available.\n"; |
| 170 |
return 1; |
| 171 |
#endif |
| 172 |
} |
| 173 |
|
| 174 |
if (params.use_hip) { |
| 175 |
#if defined(KOKKOS_ENABLE_HIP) |
| 176 |
run<Kokkos::HIP>(params); |
| 177 |
#else |
| 178 |
std::cout << "ERROR: HIP requested, but not available.\n"; |
| 179 |
return 1; |
| 180 |
#endif |
| 181 |
} |
| 182 |
|
| 183 |
if (params.use_sycl) { |
| 184 |
#if defined(KOKKOS_ENABLE_SYCL) |
| 185 |
run<Kokkos::Experimental::SYCL>(params); |
| 186 |
#else |
| 187 |
std::cout << "ERROR: SYCL requested, but not available.\n"; |
| 188 |
return 1; |
| 189 |
#endif |
| 190 |
} |
| 191 |
|
| 192 |
// use serial if no backend is specified |
| 193 |
if (!params.use_cuda and !params.use_hip and !params.use_openmp and !params.use_sycl and !params.use_threads) { |
| 194 |
#if defined(KOKKOS_ENABLE_SERIAL) |
| 195 |
run<Kokkos::Serial>(params); |
| 196 |
#else |
| 197 |
std::cout << "ERROR: Serial device requested, but not available.\n"; |
| 198 |
return 1; |
| 199 |
#endif |
| 200 |
} |
| 201 |
|
| 202 |
benchmark::RunSpecifiedBenchmarks(); |
| 203 |
|
| 204 |
benchmark::Shutdown(); |
| 205 |
Kokkos::finalize(); |
| 206 |
return 0; |
| 207 |
} |