| 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 "KokkosBlas2_gemv.hpp" |
| 18 |
#include <Kokkos_Random.hpp> |
| 19 |
#include "KokkosKernels_TestStringUtils.hpp" |
| 20 |
|
| 21 |
struct Params { |
| 22 |
int use_cuda = 0; |
| 23 |
int use_hip = 0; |
| 24 |
int use_openmp = 0; |
| 25 |
int use_threads = 0; |
| 26 |
int m = 5000; |
| 27 |
int n = 5000; |
| 28 |
int repeat = 1; |
| 29 |
bool layoutLeft = true; |
| 30 |
}; |
| 31 |
|
| 32 |
void print_options() { |
| 33 |
std::cerr << "Options\n" << std::endl; |
| 34 |
|
| 35 |
std::cerr << "\tBACKEND: '--threads[numThreads]' | '--openmp [numThreads]' | " |
| 36 |
"'--cuda [cudaDeviceIndex]'" |
| 37 |
<< std::endl; |
| 38 |
std::cerr << "\tIf none selected, serial is used." << std::endl; |
| 39 |
std::cerr << "\t[Optional] --repeat :: how many times to repeat overall " |
| 40 |
"spadd (symbolic + repeated numeric)" |
| 41 |
<< std::endl; |
| 42 |
std::cerr << "\t[Optional] --layout :: matrix layout ('left' or 'right', " |
| 43 |
"default 'left')" |
| 44 |
<< std::endl; |
| 45 |
std::cerr << "\t[Optional] --m :: number of rows to generate" << std::endl; |
| 46 |
std::cerr << "\t[Optional] --n :: number of cols to generate" << std::endl; |
| 47 |
} |
| 48 |
|
| 49 |
int parse_inputs(Params& params, int argc, char** argv) { |
| 50 |
for (int i = 1; i < argc; ++i) { |
| 51 |
if (0 == Test::string_compare_no_case(argv[i], "--help") || 0 == Test::string_compare_no_case(argv[i], "-h")) { |
| 52 |
print_options(); |
| 53 |
exit(0); // note: this is before Kokkos::initialize |
| 54 |
} else if (0 == Test::string_compare_no_case(argv[i], "--threads")) { |
| 55 |
params.use_threads = atoi(argv[++i]); |
| 56 |
} else if (0 == Test::string_compare_no_case(argv[i], "--openmp")) { |
| 57 |
params.use_openmp = atoi(argv[++i]); |
| 58 |
} else if (0 == Test::string_compare_no_case(argv[i], "--cuda")) { |
| 59 |
params.use_cuda = atoi(argv[++i]) + 1; |
| 60 |
} else if (0 == Test::string_compare_no_case(argv[i], "--hip")) { |
| 61 |
params.use_hip = atoi(argv[++i]) + 1; |
| 62 |
} else if (0 == Test::string_compare_no_case(argv[i], "--layout")) { |
| 63 |
i++; |
| 64 |
if (0 == Test::string_compare_no_case(argv[i], "left")) |
| 65 |
params.layoutLeft = true; |
| 66 |
else if (0 == Test::string_compare_no_case(argv[i], "right")) |
| 67 |
params.layoutLeft = false; |
| 68 |
else { |
| 69 |
std::cerr << "Invalid layout: must be 'left' or 'right'.\n"; |
| 70 |
exit(1); |
| 71 |
} |
| 72 |
} else if (0 == Test::string_compare_no_case(argv[i], "--m")) { |
| 73 |
params.m = atoi(argv[++i]); |
| 74 |
} else if (0 == Test::string_compare_no_case(argv[i], "--n")) { |
| 75 |
params.n = atoi(argv[++i]); |
| 76 |
} else if (0 == Test::string_compare_no_case(argv[i], "--repeat")) { |
| 77 |
// if provided, C will be written to given file. |
| 78 |
// has to have ".bin", or ".crs" extension. |
| 79 |
params.repeat = atoi(argv[++i]); |
| 80 |
} else { |
| 81 |
std::cerr << "Unrecognized command line argument #" << i << ": " << argv[i] << std::endl; |
| 82 |
print_options(); |
| 83 |
return 1; |
| 84 |
} |
| 85 |
} |
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
template <typename ExecSpace, typename Layout> |
| 90 |
void run(int m, int n, int repeat) { |
| 91 |
// Declare type aliases |
| 92 |
using Scalar = double; |
| 93 |
using MemSpace = typename ExecSpace::memory_space; |
| 94 |
using Device = Kokkos::Device<ExecSpace, MemSpace>; |
| 95 |
|
| 96 |
std::cout << "Running GEMV experiment (" << ExecSpace::name() << ")\n"; |
| 97 |
|
| 98 |
// Create a View containing a 2D matrix; allocate KokkosView with template |
| 99 |
// args of Scalar**, a layout, and |
| 100 |
Kokkos::View<Scalar**, Layout, Device> A(Kokkos::view_alloc(Kokkos::WithoutInitializing, "A"), m, n); |
| 101 |
// Create Views containing 1D matrix; allocate (without) matrix "x" of size n |
| 102 |
Kokkos::View<Scalar*, Device> x(Kokkos::view_alloc(Kokkos::WithoutInitializing, "x"), n); |
| 103 |
// Create Views containing 1D matrix; allocate (without) matrix "y" of size m |
| 104 |
Kokkos::View<Scalar*, Device> y(Kokkos::view_alloc(Kokkos::WithoutInitializing, "y"), m); |
| 105 |
|
| 106 |
// Declaring variable pool w/ a number seed; |
| 107 |
// a parallel random number generator, so you |
| 108 |
// won't get the same number with a given seed each time |
| 109 |
Kokkos::Random_XorShift64_Pool<ExecSpace> pool(123); |
| 110 |
|
| 111 |
// Fill 2D Matrix "A" and 1D matrix (i.e., a vector) "x" with random values; |
| 112 |
// Here, 10 is the max value of the random generator between 1 and 10 |
| 113 |
// (uniform ) |
| 114 |
Kokkos::fill_random(A, pool, 10.0); |
| 115 |
Kokkos::fill_random(x, pool, 10.0); |
| 116 |
|
| 117 |
// Do a warm-up run |
| 118 |
KokkosBlas::gemv("N", 1.0, A, x, 0.0, y); |
| 119 |
|
| 120 |
// Start timing |
| 121 |
Kokkos::fence(); |
| 122 |
Kokkos::Timer timer; |
| 123 |
for (int i = 0; i < repeat; i++) { |
| 124 |
KokkosBlas::gemv("N", 1.0, A, x, 0.0, y); |
| 125 |
ExecSpace().fence(); |
| 126 |
} |
| 127 |
// Kokkos Timer set up |
| 128 |
double total = timer.seconds(); |
| 129 |
double avg = total / repeat; |
| 130 |
// Flops calculation |
| 131 |
size_t flopsPerRun = (size_t)2 * m * n; |
| 132 |
printf("Avg GEMV time: %f s.\n", avg); |
| 133 |
printf("Avg GEMV FLOP/s: %.3e\n", flopsPerRun / avg); |
| 134 |
} |
| 135 |
|
| 136 |
int main(int argc, char** argv) { |
| 137 |
// Create an instance of Params |
| 138 |
Params params; |
| 139 |
|
| 140 |
// Argument parsing: |
| 141 |
if (parse_inputs(params, argc, argv)) { |
| 142 |
return 1; |
| 143 |
} |
| 144 |
// const int num_threads = params.use_openmp; |
| 145 |
const int num_threads = std::max(params.use_openmp, params.use_threads); |
| 146 |
|
| 147 |
const int device_id = std::max(params.use_cuda, params.use_hip) - 1; |
| 148 |
Kokkos::initialize(Kokkos::InitializationSettings().set_num_threads(num_threads).set_device_id(device_id)); |
| 149 |
|
| 150 |
// Create booleans to handle pthreads, openmp and cuda params and initialize |
| 151 |
// to true; |
| 152 |
bool useThreads = params.use_threads != 0; |
| 153 |
bool useOMP = params.use_openmp != 0; |
| 154 |
bool useCUDA = params.use_cuda != 0; |
| 155 |
bool useHIP = params.use_hip != 0; |
| 156 |
|
| 157 |
// Create boolean to handle serial setting if not using open and cuda |
| 158 |
bool useSerial = !useThreads && !useOMP && !useCUDA && !useHIP; |
| 159 |
|
| 160 |
// Logic for runtime with PThreads |
| 161 |
if (useThreads) { |
| 162 |
#if defined(KOKKOS_ENABLE_THREADS) |
| 163 |
if (params.layoutLeft) |
| 164 |
run<Kokkos::Threads, Kokkos::LayoutLeft>(params.m, params.n, params.repeat); |
| 165 |
else |
| 166 |
run<Kokkos::Threads, Kokkos::LayoutRight>(params.m, params.n, params.repeat); |
| 167 |
#else |
| 168 |
std::cout << "ERROR: PThreads requested, but not available.\n"; |
| 169 |
return 1; |
| 170 |
#endif |
| 171 |
} |
| 172 |
|
| 173 |
// Logic for runtime with OpenMP |
| 174 |
if (useOMP) { |
| 175 |
#if defined(KOKKOS_ENABLE_OPENMP) |
| 176 |
if (params.layoutLeft) |
| 177 |
run<Kokkos::OpenMP, Kokkos::LayoutLeft>(params.m, params.n, params.repeat); |
| 178 |
else |
| 179 |
run<Kokkos::OpenMP, Kokkos::LayoutRight>(params.m, params.n, params.repeat); |
| 180 |
#else |
| 181 |
std::cout << "ERROR: OpenMP requested, but not available.\n"; |
| 182 |
return 1; |
| 183 |
#endif |
| 184 |
} |
| 185 |
|
| 186 |
// Logic for runtime with Cuda |
| 187 |
if (useCUDA) { |
| 188 |
#if defined(KOKKOS_ENABLE_CUDA) |
| 189 |
if (params.layoutLeft) |
| 190 |
run<Kokkos::Cuda, Kokkos::LayoutLeft>(params.m, params.n, params.repeat); |
| 191 |
else |
| 192 |
run<Kokkos::Cuda, Kokkos::LayoutRight>(params.m, params.n, params.repeat); |
| 193 |
#else |
| 194 |
std::cout << "ERROR: CUDA requested, but not available.\n"; |
| 195 |
return 1; |
| 196 |
#endif |
| 197 |
} |
| 198 |
if (useHIP) { |
| 199 |
#if defined(KOKKOS_ENABLE_HIP) |
| 200 |
if (params.layoutLeft) |
| 201 |
run<Kokkos::HIP, Kokkos::LayoutLeft>(params.m, params.n, params.repeat); |
| 202 |
else |
| 203 |
run<Kokkos::HIP, Kokkos::LayoutRight>(params.m, params.n, params.repeat); |
| 204 |
#else |
| 205 |
std::cout << "ERROR: HIP requested, but not available.\n"; |
| 206 |
return 1; |
| 207 |
#endif |
| 208 |
} |
| 209 |
// Logic for serial runtime |
| 210 |
if (useSerial) { |
| 211 |
#if defined(KOKKOS_ENABLE_SERIAL) |
| 212 |
if (params.layoutLeft) |
| 213 |
run<Kokkos::Serial, Kokkos::LayoutLeft>(params.m, params.n, params.repeat); |
| 214 |
else |
| 215 |
run<Kokkos::Serial, Kokkos::LayoutRight>(params.m, params.n, params.repeat); |
| 216 |
#else |
| 217 |
std::cout << "ERROR: Serial device requested, but not available.\n"; |
| 218 |
return 1; |
| 219 |
#endif |
| 220 |
} |
| 221 |
Kokkos::finalize(); |
| 222 |
return 0; |
| 223 |
} |