Debian Codegraph @

packages/kokkos-kernels/perf_test/blas/blas2/KokkosBlas2_gemv_perf_test_benchmark.cpp

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
17 #include <Kokkos_Core.hpp>
18 #include <Kokkos_Random.hpp>
19
20 #include "KokkosBlas2_gemv.hpp"
21
22 #include "KokkosKernels_TestStringUtils.hpp"
23 #include "KokkosKernels_perf_test_utilities.hpp"
24
25 #include <Benchmark_Context.hpp>
26 #include <benchmark/benchmark.h>
27
28 struct blas2_gemv_params : public perf_test::CommonInputParams {
29 int m = 5000;
30 int n = 5000;
31 bool layoutLeft = true;
32
33 static blas2_gemv_params get_params(int& argc, char** argv) {
34 blas2_gemv_params params;
35 perf_test::parse_common_options(argc, argv, params);
36
37 for (int i = 1; i < argc; ++i) {
38 if (perf_test::check_arg_int(i, argc, argv, "--m", params.m)) {
39 ++i;
40 } else if (perf_test::check_arg_int(i, argc, argv, "--n", params.n)) {
41 ++i;
42 } else if (std::string layout; perf_test::check_arg_str(i, argc, argv, "--layout", layout)) {
43 if (0 == Test::string_compare_no_case(layout, "left"))
44 params.layoutLeft = true;
45 else if (0 == Test::string_compare_no_case(layout, "right"))
46 params.layoutLeft = false;
47 else {
48 std::cerr << "Invalid layout: must be 'left' or 'right'.\n";
49 exit(1);
50 }
51 ++i;
52 } else {
53 std::cerr << "Unrecognized command line argument #" << i << ": " << argv[i] << std::endl;
54 print_options();
55 exit(1);
56 }
57 }
58 return params;
59 }
60
61 static void print_options() {
62 std::cerr << "Options\n" << std::endl;
63 std::cerr << perf_test::list_common_options();
64
65 std::cerr << "\t[Optional] --m :: number of rows to generate (default 5000)" << std::endl;
66 std::cerr << "\t[Optional] --n :: number of cols to generate (default 5000)" << std::endl;
67 std::cerr << "\t[Optional] --layout :: matrix layout ('left' or 'right', "
68 "default 'left')"
69 << std::endl;
70 }
71 };
72
73 template <typename Scalar, typename Layout, typename ExecSpace>
74 static void KokkosBlas2_GEMV(benchmark::State& state) {
75 const auto m = state.range(0);
76 const auto n = state.range(1);
77
78 // Declare type aliases
79 using MemSpace = typename ExecSpace::memory_space;
80 using Device = Kokkos::Device<ExecSpace, MemSpace>;
81
82 // Create a View containing a 2D matrix; allocate KokkosView with template
83 // args of Scalar**, a layout, and
84 Kokkos::View<Scalar**, Layout, Device> A(Kokkos::view_alloc(Kokkos::WithoutInitializing, "A"), m, n);
85 // Create Views containing 1D matrix; allocate (without) matrix "x" of size n
86 Kokkos::View<Scalar*, Device> x(Kokkos::view_alloc(Kokkos::WithoutInitializing, "x"), n);
87 // Create Views containing 1D matrix; allocate (without) matrix "y" of size m
88 Kokkos::View<Scalar*, Device> y(Kokkos::view_alloc(Kokkos::WithoutInitializing, "y"), m);
89
90 // Declaring variable pool w/ a number seed;
91 // a parallel random number generator, so you
92 // won't get the same number with a given seed each time
93 Kokkos::Random_XorShift64_Pool<ExecSpace> pool(123);
94
95 // Fill 2D Matrix "A" and 1D matrix (i.e., a vector) "x" with random values;
96 // Here, 10 is the max value of the random generator between 1 and 10
97 // (uniform )
98 Kokkos::fill_random(A, pool, 10.0);
99 Kokkos::fill_random(x, pool, 10.0);
100
101 // Do a warm-up run
102 KokkosBlas::gemv("N", 1.0, A, x, 0.0, y);
103 Kokkos::fence();
104 double total_time = 0.0;
105
106 for (auto _ : state) {
107 // Start timing
108 Kokkos::Timer timer;
109 KokkosBlas::gemv("N", 1.0, A, x, 0.0, y);
110 ExecSpace().fence();
111
112 double time = timer.seconds();
113 total_time += time;
114 state.SetIterationTime(time);
115 }
116
117 state.counters[ExecSpace::name()] = 1;
118 state.counters["Avg GEMV time (s):"] = benchmark::Counter(total_time, benchmark::Counter::kAvgIterations);
119 size_t flopsPerRun = (size_t)2 * m * n;
120 state.counters["Avg GEMV FLOP/s:"] = benchmark::Counter(flopsPerRun, benchmark::Counter::kIsIterationInvariantRate);
121 }
122
123 template <typename ExecSpace>
124 void run(const blas2_gemv_params& params) {
125 using Scalar = double;
126
127 const auto name = "KokkosBlas2_GEMV";
128 const auto arg_names = std::vector<std::string>{"m", "n", params.layoutLeft ? "LayoutLeft" : "LayoutRight"};
129 const auto args = std::vector<int64_t>{params.m, params.n, 1};
130
131 if (params.layoutLeft) {
132 KokkosKernelsBenchmark::register_benchmark(name, KokkosBlas2_GEMV<Scalar, Kokkos::LayoutLeft, ExecSpace>, arg_names,
133 args, params.repeat);
134 } else {
135 KokkosKernelsBenchmark::register_benchmark(name, KokkosBlas2_GEMV<Scalar, Kokkos::LayoutRight, ExecSpace>,
136 arg_names, args, params.repeat);
137 }
138 }
139
140 int main(int argc, char** argv) {
141 Kokkos::initialize(argc, argv);
142 benchmark::Initialize(&argc, argv);
143 benchmark::SetDefaultTimeUnit(benchmark::kSecond);
144 KokkosKernelsBenchmark::add_benchmark_context(true);
145
146 const auto params = blas2_gemv_params::get_params(argc, argv);
147
148 if (params.use_threads) {
149 #if defined(KOKKOS_ENABLE_THREADS)
150 run<Kokkos::Threads>(params);
151 #else
152 std::cout << "ERROR: PThreads requested, but not available.\n";
153 return 1;
154 #endif
155 }
156
157 if (params.use_openmp) {
158 #if defined(KOKKOS_ENABLE_OPENMP)
159 run<Kokkos::OpenMP>(params);
160 #else
161 std::cout << "ERROR: OpenMP requested, but not available.\n";
162 return 1;
163 #endif
164 }
165
166 if (params.use_cuda) {
167 #if defined(KOKKOS_ENABLE_CUDA)
168 run<Kokkos::Cuda>(params);
169 #else
170 std::cout << "ERROR: CUDA requested, but not available.\n";
171 return 1;
172 #endif
173 }
174
175 if (params.use_hip) {
176 #if defined(KOKKOS_ENABLE_HIP)
177 run<Kokkos::HIP>(params);
178 #else
179 std::cout << "ERROR: HIP requested, but not available.\n";
180 return 1;
181 #endif
182 }
183
184 if (params.use_sycl) {
185 #if defined(KOKKOS_ENABLE_SYCL)
186 run<Kokkos::Experimental::SYCL>(params);
187 #else
188 std::cout << "ERROR: SYCL requested, but not available.\n";
189 return 1;
190 #endif
191 }
192
193 // use serial if no backend is specified
194 if (!params.use_cuda and !params.use_hip and !params.use_openmp and !params.use_sycl and !params.use_threads) {
195 #if defined(KOKKOS_ENABLE_SERIAL)
196 run<Kokkos::Serial>(params);
197 #else
198 std::cout << "ERROR: Serial device requested, but not available.\n";
199 return 1;
200 #endif
201 }
202
203 benchmark::RunSpecifiedBenchmarks();
204
205 benchmark::Shutdown();
206 Kokkos::finalize();
207 return 0;
208 }