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 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
|
// MIT License
//
// Copyright (c) 2020-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 HIPCUB_BENCHMARK_UTILS_HPP_
#define HIPCUB_BENCHMARK_UTILS_HPP_
#ifndef BENCHMARK_UTILS_INCLUDE_GUARD
#error benchmark_utils.hpp must ONLY be included by common_benchmark_header.hpp. Please include common_benchmark_header.hpp instead.
#endif
// hipCUB API
#ifdef __HIP_PLATFORM_AMD__
#include "hipcub/backend/rocprim/util_ptx.hpp"
#elif defined(__HIP_PLATFORM_NVIDIA__)
#include "hipcub/config.hpp"
#include <cub/util_ptx.cuh>
#endif
#include "hipcub/tuple.hpp"
#ifndef HIPCUB_CUB_API
#define HIPCUB_WARP_THREADS_MACRO warpSize
#else
#define HIPCUB_WARP_THREADS_MACRO CUB_PTX_WARP_THREADS
#endif
namespace benchmark_utils
{
const size_t default_max_random_size = 1024 * 1024;
// get_random_data() generates only part of sequence and replicates it,
// because benchmarks usually do not need "true" random sequence.
template<class T>
inline auto
get_random_data(size_t size, T min, T max, size_t max_random_size = default_max_random_size) ->
typename std::enable_if<std::is_integral<T>::value, std::vector<T>>::type
{
std::random_device rd;
std::default_random_engine gen(rd());
using distribution_type = typename std::conditional<(sizeof(T) == 1), short, T>::type;
std::uniform_int_distribution<distribution_type> distribution(min, max);
std::vector<T> data(size);
std::generate(data.begin(),
data.begin() + std::min(size, max_random_size),
[&]() { return distribution(gen); });
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;
}
template<class T>
inline auto
get_random_data(size_t size, T min, T max, size_t max_random_size = default_max_random_size) ->
typename std::enable_if<std::is_floating_point<T>::value, std::vector<T>>::type
{
std::random_device rd;
std::default_random_engine gen(rd());
std::uniform_real_distribution<T> distribution(min, max);
std::vector<T> data(size);
std::generate(data.begin(),
data.begin() + std::min(size, max_random_size),
[&]() { return distribution(gen); });
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;
}
template<class T>
inline std::vector<T>
get_random_data01(size_t size, float p, size_t max_random_size = default_max_random_size)
{
std::random_device rd;
std::default_random_engine gen(rd());
std::bernoulli_distribution distribution(p);
std::vector<T> data(size);
std::generate(data.begin(),
data.begin() + std::min(size, max_random_size),
[&]() { return distribution(gen); });
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;
}
template<class T>
inline T get_random_value(T min, T max)
{
return get_random_data(1, min, max)[0];
}
// Can't use std::prefix_sum for inclusive/exclusive scan, because
// it does not handle short[] -> int(int a, int b) { a + b; } -> int[]
// they way we expect. That's because sum in std::prefix_sum's implementation
// is of type typename std::iterator_traits<InputIt>::value_type (short)
template<class InputIt, class OutputIt, class BinaryOperation>
OutputIt host_inclusive_scan(InputIt first, InputIt last, OutputIt d_first, BinaryOperation op)
{
using input_type = typename std::iterator_traits<InputIt>::value_type;
using output_type = typename std::iterator_traits<OutputIt>::value_type;
using result_type =
typename std::conditional<std::is_void<output_type>::value, input_type, output_type>::type;
if(first == last)
return d_first;
result_type sum = *first;
*d_first = sum;
while(++first != last)
{
sum = op(sum, static_cast<result_type>(*first));
*++d_first = sum;
}
return ++d_first;
}
template<class InputIt, class T, class OutputIt, class BinaryOperation>
OutputIt host_exclusive_scan(
InputIt first, InputIt last, T initial_value, OutputIt d_first, BinaryOperation op)
{
using input_type = typename std::iterator_traits<InputIt>::value_type;
using output_type = typename std::iterator_traits<OutputIt>::value_type;
using result_type =
typename std::conditional<std::is_void<output_type>::value, input_type, output_type>::type;
if(first == last)
return d_first;
result_type sum = initial_value;
*d_first = initial_value;
while((first + 1) != last)
{
sum = op(sum, static_cast<result_type>(*first));
*++d_first = sum;
first++;
}
return ++d_first;
}
template<class InputIt,
class KeyIt,
class T,
class OutputIt,
class BinaryOperation,
class KeyCompare>
OutputIt host_exclusive_scan_by_key(InputIt first,
InputIt last,
KeyIt k_first,
T initial_value,
OutputIt d_first,
BinaryOperation op,
KeyCompare key_compare_op)
{
using input_type = typename std::iterator_traits<InputIt>::value_type;
using output_type = typename std::iterator_traits<OutputIt>::value_type;
using result_type =
typename std::conditional<std::is_void<output_type>::value, input_type, output_type>::type;
if(first == last)
return d_first;
result_type sum = initial_value;
*d_first = initial_value;
while((first + 1) != last)
{
if(key_compare_op(*k_first, *++k_first))
{
sum = op(sum, static_cast<result_type>(*first));
} else
{
sum = initial_value;
}
*++d_first = sum;
first++;
}
return ++d_first;
}
template<class T, class U = T>
struct custom_type
{
using first_type = T;
using second_type = U;
T x;
U y;
HIPCUB_HOST_DEVICE inline constexpr custom_type() : x(T()), y(U()) {}
HIPCUB_HOST_DEVICE inline constexpr custom_type(T xx, U yy) : x(xx), y(yy) {}
HIPCUB_HOST_DEVICE inline constexpr custom_type(T xy) : x(xy), y(xy) {}
template<class V, class W = V>
HIPCUB_HOST_DEVICE inline custom_type(const custom_type<V, W>& other) : x(other.x), y(other.y)
{}
#ifndef HIPCUB_CUB_API
HIPCUB_HOST_DEVICE inline ~custom_type() = default;
#endif
HIPCUB_HOST_DEVICE inline custom_type& operator=(const custom_type& other)
{
x = other.x;
y = other.y;
return *this;
}
HIPCUB_HOST_DEVICE inline custom_type operator+(const custom_type& rhs) const
{
return custom_type(x + rhs.x, y + rhs.y);
}
HIPCUB_HOST_DEVICE inline custom_type operator-(const custom_type& other) const
{
return custom_type(x - other.x, y - other.y);
}
HIPCUB_HOST_DEVICE inline bool operator<(const custom_type& rhs) const
{
// intentionally suboptimal choice for short-circuting,
// required to generate more performant device code
return ((x == rhs.x && y < rhs.y) || x < rhs.x);
}
HIPCUB_HOST_DEVICE inline bool operator>(const custom_type& other) const
{
return (x > other.x || (x == other.x && y > other.y));
}
HIPCUB_HOST_DEVICE inline bool operator==(const custom_type& rhs) const
{
return x == rhs.x && y == rhs.y;
}
HIPCUB_HOST_DEVICE inline bool operator!=(const custom_type& other) const
{
return !(*this == other);
}
HIPCUB_HOST_DEVICE custom_type& operator+=(const custom_type& rhs)
{
this->x += rhs.x;
this->y += rhs.y;
return *this;
}
};
template<typename>
struct is_custom_type : std::false_type
{};
template<class T, class U>
struct is_custom_type<custom_type<T, U>> : std::true_type
{};
template<class CustomType>
struct custom_type_decomposer
{
static_assert(is_custom_type<CustomType>::value,
"custom_type_decomposer can only be used with instantiations "
"of custom_type");
using T = typename CustomType::first_type;
using U = typename CustomType::second_type;
HIPCUB_HOST_DEVICE ::hipcub::tuple<T&, U&> operator()(CustomType& key) const
{
return ::hipcub::tuple<T&, U&>{key.x, key.y};
}
};
template<class T, class enable = void>
struct generate_limits;
template<class T>
struct generate_limits<T, std::enable_if_t<std::is_integral<T>::value>>
{
static inline T min()
{
return std::numeric_limits<T>::min();
}
static inline T max()
{
return std::numeric_limits<T>::max();
}
};
template<class T>
struct generate_limits<T, std::enable_if_t<is_custom_type<T>::value>>
{
using F = typename T::first_type;
using S = typename T::second_type;
static inline T min()
{
return T(generate_limits<F>::min(), generate_limits<S>::min());
}
static inline T max()
{
return T(generate_limits<F>::max(), generate_limits<S>::max());
}
};
template<class T>
struct generate_limits<T, std::enable_if_t<std::is_floating_point<T>::value>>
{
static inline T min()
{
return T(-1000);
}
static inline T max()
{
return T(1000);
}
};
template<class T>
inline auto get_random_data(size_t size, T min, T max, size_t max_random_size = 1024 * 1024) ->
typename std::enable_if<is_custom_type<T>::value, std::vector<T>>::type
{
using first_type = typename T::first_type;
using second_type = typename T::second_type;
std::vector<T> data(size);
auto fdata = get_random_data<first_type>(size, min.x, max.x, max_random_size);
auto sdata = get_random_data<second_type>(size, min.y, max.y, max_random_size);
for(size_t i = 0; i < size; i++)
{
data[i] = T(fdata[i], sdata[i]);
}
return data;
}
template<class T>
inline auto get_random_data(size_t size, T min, T max, size_t max_random_size = 1024 * 1024) ->
typename std::enable_if<!is_custom_type<T>::value
&& !std::is_same<decltype(max.x), void>::value,
std::vector<T>>::type
{
using field_type = decltype(max.x);
std::vector<T> data(size);
auto field_data = get_random_data<field_type>(size, min.x, max.x, max_random_size);
for(size_t i = 0; i < size; i++)
{
data[i] = T(field_data[i]);
}
return data;
}
template<typename T>
std::vector<T>
get_random_segments(const size_t size, const size_t max_segment_length, const int seed_value)
{
static_assert(std::is_arithmetic<T>::value, "Key type must be arithmetic");
std::default_random_engine prng(seed_value);
std::uniform_int_distribution<size_t> segment_length_distribution(max_segment_length);
using key_distribution_type = std::conditional_t<std::is_integral<T>::value,
std::uniform_int_distribution<T>,
std::uniform_real_distribution<T>>;
key_distribution_type key_distribution(std::numeric_limits<T>::max());
std::vector<T> keys(size);
size_t keys_start_index = 0;
while(keys_start_index < size)
{
const size_t new_segment_length = segment_length_distribution(prng);
const size_t new_segment_end = std::min(size, keys_start_index + new_segment_length);
const T key = key_distribution(prng);
std::fill(std::next(keys.begin(), keys_start_index),
std::next(keys.begin(), new_segment_end),
key);
keys_start_index += new_segment_length;
}
return keys;
}
bool is_warp_size_supported(const unsigned required_warp_size)
{
return HIPCUB_HOST_WARP_THREADS >= required_warp_size;
}
template<unsigned int LogicalWarpSize>
__device__ constexpr bool device_test_enabled_for_warp_size_v
= HIPCUB_DEVICE_WARP_THREADS >= LogicalWarpSize;
template<typename Iterator>
using it_value_t = typename std::iterator_traits<Iterator>::value_type;
using engine_type = std::default_random_engine;
// generate_random_data_n() generates only part of sequence and replicates it,
// because benchmarks usually do not need "true" random sequence.
template<class OutputIter, class U, class V, class Generator>
inline auto generate_random_data_n(
OutputIter it, size_t size, U min, V max, Generator& gen, size_t max_random_size = 1024 * 1024)
-> typename std::enable_if_t<std::is_integral<it_value_t<OutputIter>>::value, OutputIter>
{
using T = it_value_t<OutputIter>;
using dis_type = typename std::conditional<(sizeof(T) == 1), short, T>::type;
std::uniform_int_distribution<dis_type> distribution((T)min, (T)max);
std::generate_n(it, std::min(size, max_random_size), [&]() { return distribution(gen); });
for(size_t i = max_random_size; i < size; i += max_random_size)
{
std::copy_n(it, std::min(size - i, max_random_size), it + i);
}
return it + size;
}
template<class OutputIterator, class U, class V, class Generator>
inline auto generate_random_data_n(OutputIterator it,
size_t size,
U min,
V max,
Generator& gen,
size_t max_random_size = 1024 * 1024)
-> std::enable_if_t<std::is_floating_point<it_value_t<OutputIterator>>::value, OutputIterator>
{
using T = typename std::iterator_traits<OutputIterator>::value_type;
std::uniform_real_distribution<T> distribution((T)min, (T)max);
std::generate_n(it, std::min(size, max_random_size), [&]() { return distribution(gen); });
for(size_t i = max_random_size; i < size; i += max_random_size)
{
std::copy_n(it, std::min(size - i, max_random_size), it + i);
}
return it + size;
}
template<std::size_t Size, std::size_t Alignment>
struct alignas(Alignment) custom_aligned_type
{
unsigned char data[Size];
};
template<typename T,
typename U,
std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<U>::value, int> = 0>
inline constexpr auto ceiling_div(const T a, const U b)
{
return a / b + (a % b > 0 ? 1 : 0);
}
} // namespace benchmark_utils
// Need for hipcub::DeviceReduce::Min/Max etc.
namespace std
{
template<>
class numeric_limits<benchmark_utils::custom_type<int>>
{
using T = typename benchmark_utils::custom_type<int>;
public:
static constexpr inline T min()
{
return std::numeric_limits<typename T::first_type>::min();
}
static constexpr inline T max()
{
return std::numeric_limits<typename T::first_type>::max();
}
static constexpr inline T lowest()
{
return std::numeric_limits<typename T::first_type>::lowest();
}
};
template<>
class numeric_limits<benchmark_utils::custom_type<float>>
{
using T = typename benchmark_utils::custom_type<float>;
public:
static constexpr inline T min()
{
return std::numeric_limits<typename T::first_type>::min();
}
static constexpr inline T max()
{
return std::numeric_limits<typename T::first_type>::max();
}
static constexpr inline T lowest()
{
return std::numeric_limits<typename T::first_type>::lowest();
}
};
} // namespace std
#endif // HIPCUB_BENCHMARK_UTILS_HPP_
|