1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright Contributors to the Kokkos project
/**
* @file PerfTest_Stream.cpp
* @brief Implementation of STREAM benchmark operations for Kokkos.
*
* @details This file provides a set of memory bandwidth benchmarks based on the
* STREAM benchmark suite. It implements the five core STREAM operations (Set,
* Copy, Scale, Add, and Triad) using Kokkos parallel primitives. It includes
* validation.
*
* The implementation strives to use as few Kokkos features as possible, thus
* validation is performed on the host rather than via parallel_reduce.
*/
#include <Kokkos_Core.hpp>
#include <benchmark/benchmark.h>
#include "Benchmark_Context.hpp"
namespace {
using StreamType = double;
constexpr static StreamType A_INIT = 1.0;
constexpr static StreamType B_INIT = 2.0;
constexpr static StreamType C_INIT = 3.0;
constexpr static StreamType SCALAR = 4.0;
template <unsigned MemTraits>
using StreamView = Kokkos::View<StreamType*, Kokkos::MemoryTraits<MemTraits>>;
// different than benchmarks/stream, which uses int
// wide index types are common as GPU memory grows
using StreamIndex = int64_t;
using Policy = Kokkos::RangePolicy<Kokkos::IndexType<StreamIndex>>;
template <typename V>
void perform_set(const V& a, typename V::const_value_type scalar) {
Kokkos::parallel_for(
"set", Policy(0, a.extent(0)),
KOKKOS_LAMBDA(const StreamIndex i) { a[i] = scalar; });
Kokkos::fence();
}
template <typename V>
void perform_copy(const V& a, const V& b) {
Kokkos::parallel_for(
"copy", Policy(0, a.extent(0)),
KOKKOS_LAMBDA(const StreamIndex i) { b[i] = a[i]; });
Kokkos::fence();
}
template <typename V>
void perform_scale(const V& b, const V& c,
typename V::const_value_type scalar) {
Kokkos::parallel_for(
"scale", Policy(0, b.extent(0)),
KOKKOS_LAMBDA(const StreamIndex i) { b[i] = scalar * c[i]; });
Kokkos::fence();
}
template <typename V>
void perform_add(const V& a, const V& b, const V& c) {
Kokkos::parallel_for(
"add", Policy(0, a.extent(0)),
KOKKOS_LAMBDA(const StreamIndex i) { c[i] = a[i] + b[i]; });
Kokkos::fence();
}
template <typename V>
void perform_triad(const V& a, const V& b, const V& c,
typename V::const_value_type scalar) {
Kokkos::parallel_for(
"triad", Policy(0, a.extent(0)),
KOKKOS_LAMBDA(const StreamIndex i) { a[i] = b[i] + scalar * c[i]; });
Kokkos::fence();
}
template <typename V>
int validate_array(V& a_dev, typename V::const_value_type expected) {
using scalar_type = typename V::non_const_value_type;
const auto a =
Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, a_dev);
scalar_type error = 0.0;
for (size_t i = 0; i < a.size(); ++i) {
error += std::abs(a[i] - expected);
}
const scalar_type avgError = error / (scalar_type)a.size();
// all values here are pretty easy for float types to represent
// so let's make the tolerances very tight.
return std::abs(avgError / expected) >
Kokkos::Experimental::epsilon_v<scalar_type>;
}
template <unsigned MemTraits>
static void StreamSet(benchmark::State& state) {
const size_t N8 = std::pow(state.range(0), 8);
static constexpr int DATA_RATIO = 1;
StreamView<MemTraits> a(Kokkos::view_alloc(Kokkos::WithoutInitializing, "a"),
N8);
for (auto _ : state) {
Kokkos::Timer timer;
perform_set(a, SCALAR);
KokkosBenchmark::report_results(state, a, DATA_RATIO, timer.seconds());
}
if (validate_array(a, SCALAR)) {
state.SkipWithError("validation failure");
}
}
template <unsigned MemTraits>
static void StreamCopy(benchmark::State& state) {
const size_t N8 = std::pow(state.range(0), 8);
static constexpr int DATA_RATIO = 2;
StreamView<MemTraits> a("a", N8), b("b", N8);
perform_set(a, A_INIT);
for (auto _ : state) {
Kokkos::Timer timer;
perform_copy(a, b);
KokkosBenchmark::report_results(state, a, DATA_RATIO, timer.seconds());
}
if (validate_array(b, A_INIT)) {
state.SkipWithError("validation failure");
}
}
template <unsigned MemTraits>
static void StreamScale(benchmark::State& state) {
const size_t N8 = std::pow(state.range(0), 8);
static constexpr int DATA_RATIO = 2;
StreamView<MemTraits> a("a", N8), b("b", N8);
perform_set(b, B_INIT);
for (auto _ : state) {
Kokkos::Timer timer;
perform_scale(a, b, SCALAR);
KokkosBenchmark::report_results(state, b, DATA_RATIO, timer.seconds());
}
if (validate_array(a, B_INIT * SCALAR)) {
state.SkipWithError("validation failure");
}
}
template <unsigned MemTraits>
static void StreamAdd(benchmark::State& state) {
const size_t N8 = std::pow(state.range(0), 8);
static constexpr int DATA_RATIO = 3;
StreamView<MemTraits> a("a", N8), b("b", N8), c("c", N8);
perform_set(a, A_INIT);
perform_set(b, B_INIT);
perform_set(c, C_INIT);
for (auto _ : state) {
Kokkos::Timer timer;
perform_add(a, b, c);
KokkosBenchmark::report_results(state, c, DATA_RATIO, timer.seconds());
}
if (validate_array(c, A_INIT + B_INIT)) {
state.SkipWithError("validation failure");
}
}
template <unsigned MemTraits>
static void StreamTriad(benchmark::State& state) {
const size_t N8 = std::pow(state.range(0), 8);
static constexpr int DATA_RATIO = 3;
StreamView<MemTraits> a("a", N8), b("b", N8), c("c", N8);
perform_set(a, A_INIT);
perform_set(b, B_INIT);
perform_set(c, C_INIT);
for (auto _ : state) {
Kokkos::Timer timer;
perform_triad(a, b, c, SCALAR);
KokkosBenchmark::report_results(state, a, DATA_RATIO, timer.seconds());
}
if (validate_array(a, B_INIT + SCALAR * C_INIT)) {
state.SkipWithError("validation failure");
}
}
// skips a benchmark with an error from thrown exceptions
template <void (*bm)(benchmark::State&)>
static void or_skip(benchmark::State& state) {
try {
bm(state);
} catch (const std::runtime_error& e) {
state.SkipWithError(e.what());
}
}
// As of May 2025, 10^8 doubles is larger than caches, but not so large as
// to be inconvenient. Also run 11^8 for a quick check of convergence.
#define STREAM_ARGS(label) \
Name(label) \
->ArgName("N") \
->Arg(10) \
->Arg(11) \
->Unit(benchmark::kMillisecond) \
->UseManualTime()
// clang-format off
// clang-format formatted these lines inconsistently, making it hard to
// see the common pattern
BENCHMARK(or_skip<StreamSet<0>>)
->STREAM_ARGS("StreamSet");
BENCHMARK(or_skip<StreamSet<Kokkos::Restrict>>)
->STREAM_ARGS("StreamSet<Restrict>");
BENCHMARK(or_skip<StreamCopy<0>>)
->STREAM_ARGS("StreamCopy");
BENCHMARK(or_skip<StreamCopy<Kokkos::Restrict>>)
->STREAM_ARGS("StreamCopy<Restrict>");
BENCHMARK(or_skip<StreamScale<0>>)
->STREAM_ARGS("StreamScale");
BENCHMARK(or_skip<StreamScale<Kokkos::Restrict>>)
->STREAM_ARGS("StreamScale<Restrict>");
BENCHMARK(or_skip<StreamAdd<0>>)
->STREAM_ARGS("StreamAdd");
BENCHMARK(or_skip<StreamAdd<Kokkos::Restrict>>)
->STREAM_ARGS("StreamAdd<Restrict>");
BENCHMARK(or_skip<StreamTriad<0>>)
->STREAM_ARGS("StreamTriad");
BENCHMARK(or_skip<StreamTriad<Kokkos::Restrict>>)
->STREAM_ARGS("StreamTriad<Restrict>");
// clang-format on
#undef STREAM_ARGS
} // namespace
|