| 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 |
/*! \brief file gups.cpp |
| 18 |
|
| 19 |
An implementation of something like HPCC RandomAccess. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "Kokkos_Core.hpp" |
| 23 |
#include <cstdio> |
| 24 |
#include <cstdlib> |
| 25 |
#include <cmath> |
| 26 |
#include <chrono> |
| 27 |
#include <numeric> |
| 28 |
#include <algorithm> |
| 29 |
#include <random> |
| 30 |
|
| 31 |
#define HLINE "-------------------------------------------------------------\n" |
| 32 |
|
| 33 |
using Index = int; |
| 34 |
using Datum = int64_t; |
| 35 |
|
| 36 |
using IndexView = Kokkos::View<Index*>; |
| 37 |
using DataView = Kokkos::View<Datum*>; |
| 38 |
|
| 39 |
using Clock = std::chrono::steady_clock; |
| 40 |
using Duration = std::chrono::duration<double>; |
| 41 |
|
| 42 |
using RandomDevice = std::random_device; |
| 43 |
using RNG = std::mt19937; |
| 44 |
|
| 45 |
IndexView randomized_indices(const Index indicesCount, const Index dataCount, |
| 46 |
RNG& rng) { |
| 47 |
// generate random indices 0..dataCount |
| 48 |
std::uniform_int_distribution<Index> uid(0, dataCount); |
| 49 |
std::vector<Index> indices(indicesCount); |
| 50 |
std::generate(indices.begin(), indices.end(), [&]() { return uid(rng); }); |
| 51 |
|
| 52 |
// Copy to the default space and return |
| 53 |
Kokkos::View<Index*, Kokkos::HostSpace, Kokkos::MemoryUnmanaged> |
| 54 |
unmanaged_indices(indices.data(), indices.size()); |
| 55 |
IndexView dev_indices("dev_indices", indicesCount); |
| 56 |
Kokkos::deep_copy(dev_indices, unmanaged_indices); |
| 57 |
return dev_indices; |
| 58 |
} |
| 59 |
|
| 60 |
IndexView permuted_indices(const Index indicesCount, const Index dataCount, |
| 61 |
RNG& rng) { |
| 62 |
// create a permutation array of offsets into the data |
| 63 |
std::vector<Index> perm(dataCount); |
| 64 |
std::iota(perm.begin(), perm.end(), 0); |
| 65 |
std::shuffle(perm.begin(), perm.end(), rng); |
| 66 |
|
| 67 |
// indices is repeated copies of the permutation array |
| 68 |
// (or the first entries of the permutation array if there |
| 69 |
// are fewer indices than data elements) |
| 70 |
IndexView dev_indices("dev_indices", indicesCount); |
| 71 |
auto indices = Kokkos::create_mirror_view(dev_indices); |
| 72 |
for (Index i = 0; i < Index(indices.extent(0)); ++i) { |
| 73 |
indices(i) = perm[i % perm.size()]; |
| 74 |
} |
| 75 |
|
| 76 |
// Copy to the default space and return |
| 77 |
|
| 78 |
Kokkos::deep_copy(dev_indices, indices); |
| 79 |
return dev_indices; |
| 80 |
} |
| 81 |
|
| 82 |
void run_gups(IndexView& indices, DataView& data, const Datum datum, |
| 83 |
const bool performAtomics) { |
| 84 |
if (performAtomics) { |
| 85 |
Kokkos::parallel_for( |
| 86 |
"bench-gups-atomic", indices.extent(0), KOKKOS_LAMBDA(const Index i) { |
| 87 |
Kokkos::atomic_fetch_xor(&data[indices[i]], datum); |
| 88 |
}); |
| 89 |
} else { |
| 90 |
Kokkos::parallel_for( |
| 91 |
"bench-gups-non-atomic", indices.extent(0), |
| 92 |
KOKKOS_LAMBDA(const Index i) { data[indices[i]] ^= datum; }); |
| 93 |
} |
| 94 |
|
| 95 |
Kokkos::fence(); |
| 96 |
} |
| 97 |
|
| 98 |
enum class AccessPattern { random, permutation }; |
| 99 |
|
| 100 |
int run_benchmark(const Index indicesCount, const Index dataCount, |
| 101 |
const int repeats, const bool useAtomics, |
| 102 |
const AccessPattern pattern) { |
| 103 |
constexpr auto arbitrary_seed = 20230913; |
| 104 |
RNG rng(arbitrary_seed); |
| 105 |
|
| 106 |
printf("Reports fastest timing per kernel\n"); |
| 107 |
printf("Creating Views...\n"); |
| 108 |
|
| 109 |
printf("Memory Sizes:\n"); |
| 110 |
printf("- Elements: %15" PRIu64 " (%12.4f MB)\n", |
| 111 |
static_cast<uint64_t>(dataCount), |
| 112 |
1.0e-6 * ((double)dataCount * (double)sizeof(Datum))); |
| 113 |
printf("- Indices: %15" PRIu64 " (%12.4f MB)\n", |
| 114 |
static_cast<uint64_t>(indicesCount), |
| 115 |
1.0e-6 * ((double)indicesCount * (double)sizeof(Index))); |
| 116 |
printf(" - Atomics: %15s\n", (useAtomics ? "Yes" : "No")); |
| 117 |
printf("Benchmark kernels will be performed for %d iterations.\n", repeats); |
| 118 |
|
| 119 |
printf(HLINE); |
| 120 |
|
| 121 |
printf("Initializing Data...\n"); |
| 122 |
DataView data("data", dataCount); |
| 123 |
Kokkos::parallel_for( |
| 124 |
"init-data", |
| 125 |
Kokkos::RangePolicy<Kokkos::DefaultExecutionSpace>(0, dataCount), |
| 126 |
KOKKOS_LAMBDA(const int i) { data[i] = 10101010101; }); |
| 127 |
|
| 128 |
printf("Starting benchmarking...\n"); |
| 129 |
double gupsTime = 0.0; |
| 130 |
constexpr Datum datum = -1; |
| 131 |
for (Index k = 0; k < repeats; ++k) { |
| 132 |
IndexView indices; |
| 133 |
switch (pattern) { |
| 134 |
case AccessPattern::random: { |
| 135 |
indices = randomized_indices(indicesCount, dataCount, rng); |
| 136 |
break; |
| 137 |
} |
| 138 |
case AccessPattern::permutation: { |
| 139 |
indices = permuted_indices(indicesCount, dataCount, rng); |
| 140 |
break; |
| 141 |
} |
| 142 |
default: { |
| 143 |
Kokkos::abort("unexpected mode"); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
auto start = Clock::now(); |
| 148 |
run_gups(indices, data, datum, useAtomics); |
| 149 |
gupsTime += Duration(Clock::now() - start).count(); |
| 150 |
} |
| 151 |
|
| 152 |
printf(HLINE); |
| 153 |
printf("GUP/s Random: %18.6f\n", |
| 154 |
(1.0e-9 * ((double)repeats) * (double)indicesCount) / gupsTime); |
| 155 |
printf(HLINE); |
| 156 |
|
| 157 |
return 0; |
| 158 |
} |
| 159 |
|
| 160 |
int main(int argc, char* argv[]) { |
| 161 |
printf(HLINE); |
| 162 |
printf("Kokkos GUPS Benchmark\n"); |
| 163 |
printf(HLINE); |
| 164 |
|
| 165 |
Kokkos::initialize(argc, argv); |
| 166 |
|
| 167 |
int64_t indices = 8192; |
| 168 |
int64_t data = 33554432; |
| 169 |
int64_t repeats = 10; |
| 170 |
bool useAtomics = false; |
| 171 |
AccessPattern pattern = AccessPattern::random; |
| 172 |
|
| 173 |
for (int i = 1; i < argc; ++i) { |
| 174 |
if (strcmp(argv[i], "--indices") == 0) { |
| 175 |
indices = std::atoll(argv[i + 1]); |
| 176 |
++i; |
| 177 |
} else if (strcmp(argv[i], "--data") == 0) { |
| 178 |
data = std::atoll(argv[i + 1]); |
| 179 |
++i; |
| 180 |
} else if (strcmp(argv[i], "--repeats") == 0) { |
| 181 |
repeats = std::atoll(argv[i + 1]); |
| 182 |
++i; |
| 183 |
} else if (strcmp(argv[i], "--atomics") == 0) { |
| 184 |
useAtomics = true; |
| 185 |
} else if (strcmp(argv[i], "--pattern-permutation") == 0) { |
| 186 |
pattern = AccessPattern::permutation; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
const int rc = run_benchmark(indices, data, repeats, useAtomics, pattern); |
| 191 |
|
| 192 |
Kokkos::finalize(); |
| 193 |
|
| 194 |
return rc; |
| 195 |
} |