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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
|
// MIT License
//
// Copyright (c) 2023-2024 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef ROCPRIM_BENCHMARK_DEVICE_HISTOGRAM_PARALLEL_HPP_
#define ROCPRIM_BENCHMARK_DEVICE_HISTOGRAM_PARALLEL_HPP_
#include "benchmark_utils.hpp"
// Google Benchmark
#include <benchmark/benchmark.h>
// HIP API
#include <hip/hip_runtime_api.h>
// rocPRIM
#include <rocprim/device/detail/device_config_helper.hpp>
#include <rocprim/device/device_histogram.hpp>
#include <string>
#include <thread>
#include <vector>
#include <chrono>
#include <cstddef>
template<class T>
std::vector<T> generate(size_t size, int entropy_reduction, int lower_level, int upper_level)
{
if(entropy_reduction >= 5)
{
return std::vector<T>(size, static_cast<T>((lower_level + upper_level) / 2));
}
const size_t max_random_size = 1024 * 1024 + 4321;
const unsigned int seed = 123;
engine_type gen(seed);
std::vector<T> data(size);
std::generate(data.begin(),
data.begin() + std::min(size, max_random_size),
[&]()
{
// Reduce enthropy by applying bitwise AND to random bits
// "An Improved Supercomputer Sorting Benchmark", 1992
// Kurt Thearling & Stephen Smith
auto v = gen();
for(int e = 0; e < entropy_reduction; e++)
{
v &= gen();
}
return T(lower_level + v % (upper_level - lower_level));
});
for(size_t i = max_random_size; i < size; i += max_random_size)
{
std::copy_n(data.begin(), std::min(size - i, max_random_size), data.begin() + i);
}
return data;
}
// Cache for input data when multiple cases must be benchmarked with various configurations and
// same inputs can be used for consecutive benchmarks.
// It must be used as a singleton.
class input_cache
{
public:
~input_cache()
{
clear();
}
void clear()
{
for(auto& i : cache)
{
HIP_CHECK(hipFree(i.second));
}
cache.clear();
}
// The function returns an exisitng buffer if main_key matches and there is additional_key
// in the cache or generates a new buffer using gen().
// If main_key does not match, it frees all device buffers and resets the cache.
template<typename T, typename F>
T* get_or_generate(const std::string& main_key,
const std::string& additional_key,
size_t size,
F gen)
{
if(this->main_key != main_key)
{
// The main key (for example, data type) has been changed, clear the cache
clear();
this->main_key = main_key;
}
auto result = cache.find(additional_key);
if(result != cache.end())
{
return reinterpret_cast<T*>(result->second);
}
// Generate a new buffer
std::vector<T> data = gen();
T* d_buffer;
HIP_CHECK(hipMalloc(&d_buffer, size * sizeof(T)));
HIP_CHECK(hipMemcpy(d_buffer, data.data(), size * sizeof(T), hipMemcpyHostToDevice));
cache[additional_key] = d_buffer;
return d_buffer;
}
static input_cache& instance()
{
static input_cache instance;
return instance;
}
private:
std::string main_key;
std::map<std::string, void*> cache;
};
template<typename Config>
std::string config_name()
{
const rocprim::detail::histogram_config_params config = Config();
return "{bs:" + std::to_string(config.histogram_config.block_size)
+ ",ipt:" + std::to_string(config.histogram_config.items_per_thread)
+ ",max_grid_size:" + std::to_string(config.max_grid_size)
+ ",shared_impl_max_bins:" + std::to_string(config.shared_impl_max_bins)
+ ",shared_impl_histograms:" + std::to_string(config.shared_impl_histograms) + "}";
}
template<>
inline std::string config_name<rocprim::default_config>()
{
return "default_config";
}
template<typename T, unsigned int Channels, unsigned int ActiveChannels, typename Config>
struct device_histogram_benchmark : public config_autotune_interface
{
std::vector<unsigned int> cases;
device_histogram_benchmark(const std::vector<unsigned int>& cases) : cases(cases) {}
std::string name() const override
{
using namespace std::string_literals;
return bench_naming::format_name(
"{lvl:device,algo:histogram,value_type:" + std::string(Traits<T>::name()) + ",channels:"
+ std::to_string(Channels) + ",active_channels:" + std::to_string(ActiveChannels)
+ ",cfg:" + config_name<Config>() + "}");
}
static constexpr unsigned int batch_size = 3;
static constexpr unsigned int warmup_size = 5;
void run(benchmark::State& state,
size_t full_size,
const managed_seed&,
hipStream_t stream) const override
{
using counter_type = unsigned int;
using level_type = typename std::
conditional_t<std::is_integral<T>::value && sizeof(T) < sizeof(int), int, T>;
struct case_data
{
level_type lower_level[ActiveChannels];
level_type upper_level[ActiveChannels];
unsigned int num_levels[ActiveChannels];
T* d_input;
};
const std::size_t size = full_size / Channels;
size_t temporary_storage_bytes = 0;
void* d_temporary_storage = nullptr;
counter_type* d_histogram[ActiveChannels];
unsigned int max_bins = 0;
std::vector<case_data> cases_data;
for(auto& bins : cases)
{
for(int entropy_reduction : {0, 2, 4, 6})
{
case_data data;
// Reuse inputs for the same sample type. This autotune uses multipe inputs for all
// combinations of bins and entropy, but the inputs do not depend on autotuned
// params (bs, ipt, shared_impl_max_bins) and can be reused saving time needed for
// generating and copying to device.
data.d_input = input_cache::instance().get_or_generate<T>(
std::string(Traits<T>::name()),
std::to_string(bins) + "_" + std::to_string(entropy_reduction),
full_size,
[&]() { return generate<T>(full_size, entropy_reduction, 0, bins); });
for(unsigned int channel = 0; channel < ActiveChannels; channel++)
{
data.lower_level[channel] = 0;
data.upper_level[channel] = bins;
data.num_levels[channel] = bins + 1;
}
cases_data.push_back(data);
size_t current_temporary_storage_bytes = 0;
HIP_CHECK((rocprim::multi_histogram_even<Channels, ActiveChannels, Config>(
d_temporary_storage,
current_temporary_storage_bytes,
data.d_input,
size,
d_histogram,
data.num_levels,
data.lower_level,
data.upper_level,
stream,
false)));
temporary_storage_bytes
= std::max(temporary_storage_bytes, current_temporary_storage_bytes);
max_bins = std::max(max_bins, bins);
}
}
HIP_CHECK(hipMalloc(&d_temporary_storage, temporary_storage_bytes));
for(unsigned int channel = 0; channel < ActiveChannels; channel++)
{
HIP_CHECK(hipMalloc(&d_histogram[channel], max_bins * sizeof(counter_type)));
}
HIP_CHECK(hipDeviceSynchronize());
// Warm-up
for(size_t i = 0; i < warmup_size; i++)
{
for(auto& data : cases_data)
{
HIP_CHECK((rocprim::multi_histogram_even<Channels, ActiveChannels, Config>(
d_temporary_storage,
temporary_storage_bytes,
data.d_input,
size,
d_histogram,
data.num_levels,
data.lower_level,
data.upper_level,
stream,
false)));
}
}
HIP_CHECK(hipDeviceSynchronize());
// HIP events creation
hipEvent_t start, stop;
HIP_CHECK(hipEventCreate(&start));
HIP_CHECK(hipEventCreate(&stop));
for(auto _ : state)
{
// Record start event
HIP_CHECK(hipEventRecord(start, stream));
for(auto& data : cases_data)
{
for(size_t i = 0; i < batch_size; i++)
{
HIP_CHECK((rocprim::multi_histogram_even<Channels, ActiveChannels, Config>(
d_temporary_storage,
temporary_storage_bytes,
data.d_input,
size,
d_histogram,
data.num_levels,
data.lower_level,
data.upper_level,
stream,
false)));
}
}
// Record stop event and wait until it completes
HIP_CHECK(hipEventRecord(stop, stream));
HIP_CHECK(hipEventSynchronize(stop));
float elapsed_mseconds;
HIP_CHECK(hipEventElapsedTime(&elapsed_mseconds, start, stop));
state.SetIterationTime(elapsed_mseconds / 1000);
}
// Destroy HIP events
HIP_CHECK(hipEventDestroy(start));
HIP_CHECK(hipEventDestroy(stop));
state.SetBytesProcessed(state.iterations() * cases_data.size() * batch_size * size
* Channels * sizeof(T));
state.SetItemsProcessed(state.iterations() * cases_data.size() * batch_size * size
* Channels);
HIP_CHECK(hipFree(d_temporary_storage));
for(unsigned int channel = 0; channel < ActiveChannels; channel++)
{
HIP_CHECK(hipFree(d_histogram[channel]));
}
}
};
template<typename T, unsigned int BlockSize>
struct device_histogram_benchmark_generator
{
static constexpr unsigned int min_items_per_thread = 1;
static constexpr unsigned int max_items_per_thread = 16;
static constexpr unsigned int min_shared_impl_histograms = 2;
static constexpr unsigned int max_shared_impl_histograms = 4;
template<unsigned int ItemsPerThread>
struct create_ipt
{
template<unsigned int SharedImplHistograms>
struct create_shared_impl_histograms
{
using generated_config
= rocprim::histogram_config<rocprim::kernel_config<BlockSize, ItemsPerThread>,
2048,
2048,
SharedImplHistograms>;
template<unsigned int Channels,
unsigned int ActiveChannels,
unsigned int items_per_thread = ItemsPerThread>
auto create(std::vector<std::unique_ptr<config_autotune_interface>>& storage,
const std::vector<unsigned int>& cases) ->
typename std::enable_if<(items_per_thread * Channels <= max_items_per_thread),
void>::type
{
storage.emplace_back(
std::make_unique<
device_histogram_benchmark<T, Channels, ActiveChannels, generated_config>>(
cases));
}
template<unsigned int Channels,
unsigned int ActiveChannels,
unsigned int items_per_thread = ItemsPerThread>
auto create(std::vector<std::unique_ptr<config_autotune_interface>>& /*storage*/,
const std::vector<unsigned int>& /*cases*/) ->
typename std::enable_if<!(items_per_thread * Channels <= max_items_per_thread),
void>::type
{}
void operator()(std::vector<std::unique_ptr<config_autotune_interface>>& storage,
const std::vector<unsigned int>& cases)
{
// Tune histograms for single-channel data (histogram_even)
create<1, 1>(storage, cases);
// and some multi-channel configurations (multi_histogram_even)
create<2, 2>(storage, cases);
create<3, 3>(storage, cases);
create<4, 4>(storage, cases);
create<4, 3>(storage, cases);
}
};
void operator()(std::vector<std::unique_ptr<config_autotune_interface>>& storage,
const std::vector<unsigned int>& cases)
{
static_for_each<make_index_range<unsigned int,
min_shared_impl_histograms,
max_shared_impl_histograms>,
create_shared_impl_histograms>(storage, cases);
}
};
static void create(std::vector<std::unique_ptr<config_autotune_interface>>& storage)
{
// Benchmark multiple cases (with various sample distributions) and use sum of all cases
// as a measurement for autotuning
std::vector<unsigned int> cases;
if(std::is_same<T, int8_t>::value)
{
cases = {16, 127};
}
else
{
cases = {
10,
100,
1000,
10000 // Multiple bins to trigger a global memory implementation
};
}
static_for_each<make_index_range<unsigned int, min_items_per_thread, max_items_per_thread>,
create_ipt>(storage, cases);
}
};
#endif // ROCPRIM_BENCHMARK_DEVICE_HISTOGRAM_PARALLEL_HPP_
|