File size: 39,754 Bytes
0dc1b04 | 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 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 | /******************************************************************************
* Copyright (c) 2011, Duane Merrill. All rights reserved.
* Copyright (c) 2011-2018, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the NVIDIA CORPORATION nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************/
/**
* \file
* Callback operator types for supplying BlockScan prefixes
*/
#pragma once
#include <iterator>
#include <cub/config.cuh>
#include <cub/detail/strong_load.cuh>
#include <cub/detail/strong_store.cuh>
#include <cub/detail/uninitialized_copy.cuh>
#include <cub/thread/thread_load.cuh>
#include <cub/thread/thread_store.cuh>
#include <cub/util_device.cuh>
#include <cub/warp/warp_reduce.cuh>
#include <nv/target>
CUB_NAMESPACE_BEGIN
/******************************************************************************
* Prefix functor type for maintaining a running prefix while scanning a
* region independent of other thread blocks
******************************************************************************/
/**
* Stateful callback operator type for supplying BlockScan prefixes.
* Maintains a running prefix that can be applied to consecutive
* BlockScan operations.
*/
template <
typename T, ///< BlockScan value type
typename ScanOpT> ///< Wrapped scan operator type
struct BlockScanRunningPrefixOp
{
ScanOpT op; ///< Wrapped scan operator
T running_total; ///< Running block-wide prefix
/// Constructor
__device__ __forceinline__ BlockScanRunningPrefixOp(ScanOpT op)
:
op(op)
{}
/// Constructor
__device__ __forceinline__ BlockScanRunningPrefixOp(
T starting_prefix,
ScanOpT op)
:
op(op),
running_total(starting_prefix)
{}
/**
* Prefix callback operator. Returns the block-wide running_total in thread-0.
*/
__device__ __forceinline__ T operator()(
const T &block_aggregate) ///< The aggregate sum of the BlockScan inputs
{
T retval = running_total;
running_total = op(running_total, block_aggregate);
return retval;
}
};
/******************************************************************************
* Generic tile status interface types for block-cooperative scans
******************************************************************************/
/**
* Enumerations of tile status
*/
enum ScanTileStatus
{
SCAN_TILE_OOB, // Out-of-bounds (e.g., padding)
SCAN_TILE_INVALID = 99, // Not yet processed
SCAN_TILE_PARTIAL, // Tile aggregate is available
SCAN_TILE_INCLUSIVE, // Inclusive tile prefix is available
};
namespace detail
{
template <int Delay, unsigned int GridThreshold = 500>
__device__ __forceinline__ void delay()
{
NV_IF_TARGET(NV_PROVIDES_SM_70,
(if (Delay > 0)
{
if (gridDim.x < GridThreshold)
{
__threadfence_block();
}
else
{
__nanosleep(Delay);
}
}));
}
template <unsigned int GridThreshold = 500>
__device__ __forceinline__ void delay(int ns)
{
NV_IF_TARGET(NV_PROVIDES_SM_70,
(if (ns > 0)
{
if (gridDim.x < GridThreshold)
{
__threadfence_block();
}
else
{
__nanosleep(ns);
}
}));
}
template <int Delay>
__device__ __forceinline__ void always_delay()
{
NV_IF_TARGET(NV_PROVIDES_SM_70, (__nanosleep(Delay);));
}
__device__ __forceinline__ void always_delay(int ns)
{
NV_IF_TARGET(NV_PROVIDES_SM_70, (__nanosleep(ns);), ((void)ns;));
}
template <unsigned int Delay = 350, unsigned int GridThreshold = 500>
__device__ __forceinline__ void delay_or_prevent_hoisting()
{
NV_IF_TARGET(NV_PROVIDES_SM_70,
(delay<Delay, GridThreshold>();),
(__threadfence_block();));
}
template <unsigned int GridThreshold = 500>
__device__ __forceinline__ void delay_or_prevent_hoisting(int ns)
{
NV_IF_TARGET(NV_PROVIDES_SM_70,
(delay<GridThreshold>(ns);),
((void)ns; __threadfence_block();));
}
template <unsigned int Delay = 350>
__device__ __forceinline__ void always_delay_or_prevent_hoisting()
{
NV_IF_TARGET(NV_PROVIDES_SM_70,
(always_delay(Delay);),
(__threadfence_block();));
}
__device__ __forceinline__ void always_delay_or_prevent_hoisting(int ns)
{
NV_IF_TARGET(NV_PROVIDES_SM_70,
(always_delay(ns);),
((void)ns; __threadfence_block();));
}
template <unsigned int L2WriteLatency>
struct no_delay_constructor_t
{
struct delay_t
{
__device__ __forceinline__ void operator()()
{
NV_IF_TARGET(NV_PROVIDES_SM_70,
(),
(__threadfence_block();));
}
};
__device__ __forceinline__ no_delay_constructor_t(unsigned int /* seed */)
{
delay<L2WriteLatency>();
}
__device__ __forceinline__ delay_t operator()() { return {}; }
};
template <unsigned int Delay, unsigned int L2WriteLatency, unsigned int GridThreshold = 500>
struct reduce_by_key_delay_constructor_t
{
struct delay_t
{
__device__ __forceinline__ void operator()()
{
NV_DISPATCH_TARGET(
NV_IS_EXACTLY_SM_80, (delay<Delay, GridThreshold>();),
NV_PROVIDES_SM_70, (delay< 0, GridThreshold>();),
NV_IS_DEVICE, (__threadfence_block();));
}
};
__device__ __forceinline__ reduce_by_key_delay_constructor_t(unsigned int /* seed */)
{
delay<L2WriteLatency>();
}
__device__ __forceinline__ delay_t operator()() { return {}; }
};
template <unsigned int Delay, unsigned int L2WriteLatency>
struct fixed_delay_constructor_t
{
struct delay_t
{
__device__ __forceinline__ void operator()() { delay_or_prevent_hoisting<Delay>(); }
};
__device__ __forceinline__ fixed_delay_constructor_t(unsigned int /* seed */)
{
delay<L2WriteLatency>();
}
__device__ __forceinline__ delay_t operator()() { return {}; }
};
template <unsigned int InitialDelay, unsigned int L2WriteLatency>
struct exponential_backoff_constructor_t
{
struct delay_t
{
int delay;
__device__ __forceinline__ void operator()()
{
always_delay_or_prevent_hoisting(delay);
delay <<= 1;
}
};
__device__ __forceinline__ exponential_backoff_constructor_t(unsigned int /* seed */)
{
always_delay<L2WriteLatency>();
}
__device__ __forceinline__ delay_t operator()() { return {InitialDelay}; }
};
template <unsigned int InitialDelay, unsigned int L2WriteLatency>
struct exponential_backoff_jitter_constructor_t
{
struct delay_t
{
static constexpr unsigned int a = 16807;
static constexpr unsigned int c = 0;
static constexpr unsigned int m = 1u << 31;
unsigned int max_delay;
unsigned int &seed;
__device__ __forceinline__ unsigned int next(unsigned int min, unsigned int max)
{
return (seed = (a * seed + c) % m) % (max + 1 - min) + min;
}
__device__ __forceinline__ void operator()()
{
always_delay_or_prevent_hoisting(next(0, max_delay));
max_delay <<= 1;
}
};
unsigned int seed;
__device__ __forceinline__ exponential_backoff_jitter_constructor_t(unsigned int seed)
: seed(seed)
{
always_delay<L2WriteLatency>();
}
__device__ __forceinline__ delay_t operator()() { return {InitialDelay, seed}; }
};
template <unsigned int InitialDelay, unsigned int L2WriteLatency>
struct exponential_backoff_jitter_window_constructor_t
{
struct delay_t
{
static constexpr unsigned int a = 16807;
static constexpr unsigned int c = 0;
static constexpr unsigned int m = 1u << 31;
unsigned int max_delay;
unsigned int &seed;
__device__ __forceinline__ unsigned int next(unsigned int min, unsigned int max)
{
return (seed = (a * seed + c) % m) % (max + 1 - min) + min;
}
__device__ __forceinline__ void operator()()
{
unsigned int next_max_delay = max_delay << 1;
always_delay_or_prevent_hoisting(next(max_delay, next_max_delay));
max_delay = next_max_delay;
}
};
unsigned int seed;
__device__ __forceinline__ exponential_backoff_jitter_window_constructor_t(unsigned int seed)
: seed(seed)
{
always_delay<L2WriteLatency>();
}
__device__ __forceinline__ delay_t operator()() { return {InitialDelay, seed}; }
};
template <unsigned int InitialDelay, unsigned int L2WriteLatency>
struct exponential_backon_jitter_window_constructor_t
{
struct delay_t
{
static constexpr unsigned int a = 16807;
static constexpr unsigned int c = 0;
static constexpr unsigned int m = 1u << 31;
unsigned int max_delay;
unsigned int &seed;
__device__ __forceinline__ unsigned int next(unsigned int min, unsigned int max)
{
return (seed = (a * seed + c) % m) % (max + 1 - min) + min;
}
__device__ __forceinline__ void operator()()
{
int prev_delay = max_delay >> 1;
always_delay_or_prevent_hoisting(next(prev_delay, max_delay));
max_delay = prev_delay;
}
};
unsigned int seed;
unsigned int max_delay = InitialDelay;
__device__ __forceinline__ exponential_backon_jitter_window_constructor_t(unsigned int seed)
: seed(seed)
{
always_delay<L2WriteLatency>();
}
__device__ __forceinline__ delay_t operator()()
{
max_delay >>= 1;
return {max_delay, seed};
}
};
template <unsigned int InitialDelay, unsigned int L2WriteLatency>
struct exponential_backon_jitter_constructor_t
{
struct delay_t
{
static constexpr unsigned int a = 16807;
static constexpr unsigned int c = 0;
static constexpr unsigned int m = 1u << 31;
unsigned int max_delay;
unsigned int &seed;
__device__ __forceinline__ unsigned int next(unsigned int min, unsigned int max)
{
return (seed = (a * seed + c) % m) % (max + 1 - min) + min;
}
__device__ __forceinline__ void operator()()
{
always_delay_or_prevent_hoisting(next(0, max_delay));
max_delay >>= 1;
}
};
unsigned int seed;
unsigned int max_delay = InitialDelay;
__device__ __forceinline__ exponential_backon_jitter_constructor_t(unsigned int seed)
: seed(seed)
{
always_delay<L2WriteLatency>();
}
__device__ __forceinline__ delay_t operator()()
{
max_delay >>= 1;
return {max_delay, seed};
}
};
template <unsigned int InitialDelay, unsigned int L2WriteLatency>
struct exponential_backon_constructor_t
{
struct delay_t
{
unsigned int delay;
__device__ __forceinline__ void operator()()
{
always_delay_or_prevent_hoisting(delay);
delay >>= 1;
}
};
unsigned int max_delay = InitialDelay;
__device__ __forceinline__ exponential_backon_constructor_t(unsigned int /* seed */)
{
always_delay<L2WriteLatency>();
}
__device__ __forceinline__ delay_t operator()()
{
max_delay >>= 1;
return {max_delay};
}
};
using default_no_delay_constructor_t = no_delay_constructor_t<450>;
using default_no_delay_t = default_no_delay_constructor_t::delay_t;
template <class T>
using default_delay_constructor_t = cub::detail::conditional_t<Traits<T>::PRIMITIVE,
fixed_delay_constructor_t<350, 450>,
default_no_delay_constructor_t>;
template <class T>
using default_delay_t = typename default_delay_constructor_t<T>::delay_t;
template <class KeyT, class ValueT>
using default_reduce_by_key_delay_constructor_t =
detail::conditional_t<(Traits<ValueT>::PRIMITIVE) && (sizeof(ValueT) + sizeof(KeyT) < 16),
reduce_by_key_delay_constructor_t<350, 450>,
default_delay_constructor_t<KeyValuePair<KeyT, ValueT>>>;
}
/**
* Tile status interface.
*/
template <
typename T,
bool SINGLE_WORD = Traits<T>::PRIMITIVE>
struct ScanTileState;
/**
* Tile status interface specialized for scan status and value types
* that can be combined into one machine word that can be
* read/written coherently in a single access.
*/
template <typename T>
struct ScanTileState<T, true>
{
// Status word type
using StatusWord = cub::detail::conditional_t<
sizeof(T) == 8,
unsigned long long,
cub::detail::conditional_t<
sizeof(T) == 4,
unsigned int,
cub::detail::conditional_t<sizeof(T) == 2, unsigned short, unsigned char>>>;
// Unit word type
using TxnWord = cub::detail::conditional_t<
sizeof(T) == 8,
ulonglong2,
cub::detail::conditional_t<
sizeof(T) == 4,
uint2,
unsigned int>>;
// Device word type
struct TileDescriptor
{
StatusWord status;
T value;
};
// Constants
enum
{
TILE_STATUS_PADDING = CUB_PTX_WARP_THREADS,
};
// Device storage
TxnWord *d_tile_descriptors;
/// Constructor
__host__ __device__ __forceinline__
ScanTileState()
:
d_tile_descriptors(NULL)
{}
/// Initializer
__host__ __device__ __forceinline__
cudaError_t Init(
int /*num_tiles*/, ///< [in] Number of tiles
void *d_temp_storage, ///< [in] Device-accessible allocation of temporary storage. When NULL, the required allocation size is written to \p temp_storage_bytes and no work is done.
size_t /*temp_storage_bytes*/) ///< [in] Size in bytes of \t d_temp_storage allocation
{
d_tile_descriptors = reinterpret_cast<TxnWord*>(d_temp_storage);
return cudaSuccess;
}
/**
* Compute device memory needed for tile status
*/
__host__ __device__ __forceinline__
static cudaError_t AllocationSize(
int num_tiles, ///< [in] Number of tiles
size_t &temp_storage_bytes) ///< [out] Size in bytes of \t d_temp_storage allocation
{
temp_storage_bytes = (num_tiles + TILE_STATUS_PADDING) * sizeof(TxnWord); // bytes needed for tile status descriptors
return cudaSuccess;
}
/**
* Initialize (from device)
*/
__device__ __forceinline__ void InitializeStatus(int num_tiles)
{
int tile_idx = (blockIdx.x * blockDim.x) + threadIdx.x;
TxnWord val = TxnWord();
TileDescriptor *descriptor = reinterpret_cast<TileDescriptor*>(&val);
if (tile_idx < num_tiles)
{
// Not-yet-set
descriptor->status = StatusWord(SCAN_TILE_INVALID);
d_tile_descriptors[TILE_STATUS_PADDING + tile_idx] = val;
}
if ((blockIdx.x == 0) && (threadIdx.x < TILE_STATUS_PADDING))
{
// Padding
descriptor->status = StatusWord(SCAN_TILE_OOB);
d_tile_descriptors[threadIdx.x] = val;
}
}
/**
* Update the specified tile's inclusive value and corresponding status
*/
__device__ __forceinline__ void SetInclusive(int tile_idx, T tile_inclusive)
{
TileDescriptor tile_descriptor;
tile_descriptor.status = SCAN_TILE_INCLUSIVE;
tile_descriptor.value = tile_inclusive;
TxnWord alias;
*reinterpret_cast<TileDescriptor*>(&alias) = tile_descriptor;
detail::store_relaxed(d_tile_descriptors + TILE_STATUS_PADDING + tile_idx, alias);
}
/**
* Update the specified tile's partial value and corresponding status
*/
__device__ __forceinline__ void SetPartial(int tile_idx, T tile_partial)
{
TileDescriptor tile_descriptor;
tile_descriptor.status = SCAN_TILE_PARTIAL;
tile_descriptor.value = tile_partial;
TxnWord alias;
*reinterpret_cast<TileDescriptor*>(&alias) = tile_descriptor;
detail::store_relaxed(d_tile_descriptors + TILE_STATUS_PADDING + tile_idx, alias);
}
/**
* Wait for the corresponding tile to become non-invalid
*/
template <class DelayT = detail::default_delay_t<T>>
__device__ __forceinline__ void WaitForValid(
int tile_idx,
StatusWord &status,
T &value,
DelayT delay_or_prevent_hoisting = {})
{
TileDescriptor tile_descriptor;
{
TxnWord alias = detail::load_relaxed(d_tile_descriptors + TILE_STATUS_PADDING + tile_idx);
tile_descriptor = reinterpret_cast<TileDescriptor&>(alias);
}
while (WARP_ANY((tile_descriptor.status == SCAN_TILE_INVALID), 0xffffffff))
{
delay_or_prevent_hoisting();
TxnWord alias = detail::load_relaxed(d_tile_descriptors + TILE_STATUS_PADDING + tile_idx);
tile_descriptor = reinterpret_cast<TileDescriptor&>(alias);
}
status = tile_descriptor.status;
value = tile_descriptor.value;
}
/**
* Loads and returns the tile's value. The returned value is undefined if either (a) the tile's status is invalid or
* (b) there is no memory fence between reading a non-invalid status and the call to LoadValid.
*/
__device__ __forceinline__ T LoadValid(int tile_idx)
{
TxnWord alias = d_tile_descriptors[TILE_STATUS_PADDING + tile_idx];
TileDescriptor tile_descriptor = reinterpret_cast<TileDescriptor&>(alias);
return tile_descriptor.value;
}
};
/**
* Tile status interface specialized for scan status and value types that
* cannot be combined into one machine word.
*/
template <typename T>
struct ScanTileState<T, false>
{
// Status word type
using StatusWord = unsigned int;
// Constants
enum
{
TILE_STATUS_PADDING = CUB_PTX_WARP_THREADS,
};
// Device storage
StatusWord *d_tile_status;
T *d_tile_partial;
T *d_tile_inclusive;
/// Constructor
__host__ __device__ __forceinline__
ScanTileState()
:
d_tile_status(NULL),
d_tile_partial(NULL),
d_tile_inclusive(NULL)
{}
/// Initializer
__host__ __device__ __forceinline__
cudaError_t Init(
int num_tiles, ///< [in] Number of tiles
void *d_temp_storage, ///< [in] Device-accessible allocation of temporary storage. When NULL, the required allocation size is written to \p temp_storage_bytes and no work is done.
size_t temp_storage_bytes) ///< [in] Size in bytes of \t d_temp_storage allocation
{
cudaError_t error = cudaSuccess;
do
{
void* allocations[3] = {};
size_t allocation_sizes[3];
allocation_sizes[0] = (num_tiles + TILE_STATUS_PADDING) * sizeof(StatusWord); // bytes needed for tile status descriptors
allocation_sizes[1] = (num_tiles + TILE_STATUS_PADDING) * sizeof(Uninitialized<T>); // bytes needed for partials
allocation_sizes[2] = (num_tiles + TILE_STATUS_PADDING) * sizeof(Uninitialized<T>); // bytes needed for inclusives
// Compute allocation pointers into the single storage blob
if (CubDebug(error = AliasTemporaries(d_temp_storage, temp_storage_bytes, allocations, allocation_sizes))) break;
// Alias the offsets
d_tile_status = reinterpret_cast<StatusWord*>(allocations[0]);
d_tile_partial = reinterpret_cast<T*>(allocations[1]);
d_tile_inclusive = reinterpret_cast<T*>(allocations[2]);
}
while (0);
return error;
}
/**
* Compute device memory needed for tile status
*/
__host__ __device__ __forceinline__
static cudaError_t AllocationSize(
int num_tiles, ///< [in] Number of tiles
size_t &temp_storage_bytes) ///< [out] Size in bytes of \t d_temp_storage allocation
{
// Specify storage allocation requirements
size_t allocation_sizes[3];
allocation_sizes[0] = (num_tiles + TILE_STATUS_PADDING) * sizeof(StatusWord); // bytes needed for tile status descriptors
allocation_sizes[1] = (num_tiles + TILE_STATUS_PADDING) * sizeof(Uninitialized<T>); // bytes needed for partials
allocation_sizes[2] = (num_tiles + TILE_STATUS_PADDING) * sizeof(Uninitialized<T>); // bytes needed for inclusives
// Set the necessary size of the blob
void* allocations[3] = {};
return CubDebug(AliasTemporaries(NULL, temp_storage_bytes, allocations, allocation_sizes));
}
/**
* Initialize (from device)
*/
__device__ __forceinline__ void InitializeStatus(int num_tiles)
{
int tile_idx = (blockIdx.x * blockDim.x) + threadIdx.x;
if (tile_idx < num_tiles)
{
// Not-yet-set
d_tile_status[TILE_STATUS_PADDING + tile_idx] = StatusWord(SCAN_TILE_INVALID);
}
if ((blockIdx.x == 0) && (threadIdx.x < TILE_STATUS_PADDING))
{
// Padding
d_tile_status[threadIdx.x] = StatusWord(SCAN_TILE_OOB);
}
}
/**
* Update the specified tile's inclusive value and corresponding status
*/
__device__ __forceinline__ void SetInclusive(int tile_idx, T tile_inclusive)
{
// Update tile inclusive value
ThreadStore<STORE_CG>(d_tile_inclusive + TILE_STATUS_PADDING + tile_idx, tile_inclusive);
detail::store_release(d_tile_status + TILE_STATUS_PADDING + tile_idx, StatusWord(SCAN_TILE_INCLUSIVE));
}
/**
* Update the specified tile's partial value and corresponding status
*/
__device__ __forceinline__ void SetPartial(int tile_idx, T tile_partial)
{
// Update tile partial value
ThreadStore<STORE_CG>(d_tile_partial + TILE_STATUS_PADDING + tile_idx, tile_partial);
detail::store_release(d_tile_status + TILE_STATUS_PADDING + tile_idx, StatusWord(SCAN_TILE_PARTIAL));
}
/**
* Wait for the corresponding tile to become non-invalid
*/
template <class DelayT = detail::default_no_delay_t>
__device__ __forceinline__ void WaitForValid(
int tile_idx,
StatusWord &status,
T &value,
DelayT delay = {})
{
do
{
delay();
status = detail::load_relaxed(d_tile_status + TILE_STATUS_PADDING + tile_idx);
__threadfence();
} while (WARP_ANY((status == SCAN_TILE_INVALID), 0xffffffff));
if (status == StatusWord(SCAN_TILE_PARTIAL))
{
value = ThreadLoad<LOAD_CG>(d_tile_partial + TILE_STATUS_PADDING + tile_idx);
}
else
{
value = ThreadLoad<LOAD_CG>(d_tile_inclusive + TILE_STATUS_PADDING + tile_idx);
}
}
/**
* Loads and returns the tile's value. The returned value is undefined if either (a) the tile's status is invalid or
* (b) there is no memory fence between reading a non-invalid status and the call to LoadValid.
*/
__device__ __forceinline__ T LoadValid(int tile_idx)
{
return d_tile_inclusive[TILE_STATUS_PADDING + tile_idx];
}
};
/******************************************************************************
* ReduceByKey tile status interface types for block-cooperative scans
******************************************************************************/
/**
* Tile status interface for reduction by key.
*
*/
template <
typename ValueT,
typename KeyT,
bool SINGLE_WORD = (Traits<ValueT>::PRIMITIVE) && (sizeof(ValueT) + sizeof(KeyT) < 16)>
struct ReduceByKeyScanTileState;
/**
* Tile status interface for reduction by key, specialized for scan status and value types that
* cannot be combined into one machine word.
*/
template <
typename ValueT,
typename KeyT>
struct ReduceByKeyScanTileState<ValueT, KeyT, false> :
ScanTileState<KeyValuePair<KeyT, ValueT> >
{
typedef ScanTileState<KeyValuePair<KeyT, ValueT> > SuperClass;
/// Constructor
__host__ __device__ __forceinline__
ReduceByKeyScanTileState() : SuperClass() {}
};
/**
* Tile status interface for reduction by key, specialized for scan status and value types that
* can be combined into one machine word that can be read/written coherently in a single access.
*/
template <
typename ValueT,
typename KeyT>
struct ReduceByKeyScanTileState<ValueT, KeyT, true>
{
using KeyValuePairT = KeyValuePair<KeyT, ValueT>;
// Constants
enum
{
PAIR_SIZE = static_cast<int>(sizeof(ValueT) + sizeof(KeyT)),
TXN_WORD_SIZE = 1 << Log2<PAIR_SIZE + 1>::VALUE,
STATUS_WORD_SIZE = TXN_WORD_SIZE - PAIR_SIZE,
TILE_STATUS_PADDING = CUB_PTX_WARP_THREADS,
};
// Status word type
using StatusWord = cub::detail::conditional_t<
STATUS_WORD_SIZE == 8,
unsigned long long,
cub::detail::conditional_t<
STATUS_WORD_SIZE == 4,
unsigned int,
cub::detail::conditional_t<STATUS_WORD_SIZE == 2, unsigned short, unsigned char>>>;
// Status word type
using TxnWord = cub::detail::conditional_t<
TXN_WORD_SIZE == 16,
ulonglong2,
cub::detail::conditional_t<TXN_WORD_SIZE == 8, unsigned long long, unsigned int>>;
// Device word type (for when sizeof(ValueT) == sizeof(KeyT))
struct TileDescriptorBigStatus
{
KeyT key;
ValueT value;
StatusWord status;
};
// Device word type (for when sizeof(ValueT) != sizeof(KeyT))
struct TileDescriptorLittleStatus
{
ValueT value;
StatusWord status;
KeyT key;
};
// Device word type
using TileDescriptor =
cub::detail::conditional_t<sizeof(ValueT) == sizeof(KeyT),
TileDescriptorBigStatus,
TileDescriptorLittleStatus>;
// Device storage
TxnWord *d_tile_descriptors;
/// Constructor
__host__ __device__ __forceinline__
ReduceByKeyScanTileState()
:
d_tile_descriptors(NULL)
{}
/// Initializer
__host__ __device__ __forceinline__
cudaError_t Init(
int /*num_tiles*/, ///< [in] Number of tiles
void *d_temp_storage, ///< [in] Device-accessible allocation of temporary storage. When NULL, the required allocation size is written to \p temp_storage_bytes and no work is done.
size_t /*temp_storage_bytes*/) ///< [in] Size in bytes of \t d_temp_storage allocation
{
d_tile_descriptors = reinterpret_cast<TxnWord*>(d_temp_storage);
return cudaSuccess;
}
/**
* Compute device memory needed for tile status
*/
__host__ __device__ __forceinline__
static cudaError_t AllocationSize(
int num_tiles, ///< [in] Number of tiles
size_t &temp_storage_bytes) ///< [out] Size in bytes of \t d_temp_storage allocation
{
temp_storage_bytes = (num_tiles + TILE_STATUS_PADDING) * sizeof(TxnWord); // bytes needed for tile status descriptors
return cudaSuccess;
}
/**
* Initialize (from device)
*/
__device__ __forceinline__ void InitializeStatus(int num_tiles)
{
int tile_idx = (blockIdx.x * blockDim.x) + threadIdx.x;
TxnWord val = TxnWord();
TileDescriptor *descriptor = reinterpret_cast<TileDescriptor*>(&val);
if (tile_idx < num_tiles)
{
// Not-yet-set
descriptor->status = StatusWord(SCAN_TILE_INVALID);
d_tile_descriptors[TILE_STATUS_PADDING + tile_idx] = val;
}
if ((blockIdx.x == 0) && (threadIdx.x < TILE_STATUS_PADDING))
{
// Padding
descriptor->status = StatusWord(SCAN_TILE_OOB);
d_tile_descriptors[threadIdx.x] = val;
}
}
/**
* Update the specified tile's inclusive value and corresponding status
*/
__device__ __forceinline__ void SetInclusive(int tile_idx, KeyValuePairT tile_inclusive)
{
TileDescriptor tile_descriptor;
tile_descriptor.status = SCAN_TILE_INCLUSIVE;
tile_descriptor.value = tile_inclusive.value;
tile_descriptor.key = tile_inclusive.key;
TxnWord alias;
*reinterpret_cast<TileDescriptor*>(&alias) = tile_descriptor;
detail::store_relaxed(d_tile_descriptors + TILE_STATUS_PADDING + tile_idx, alias);
}
/**
* Update the specified tile's partial value and corresponding status
*/
__device__ __forceinline__ void SetPartial(int tile_idx, KeyValuePairT tile_partial)
{
TileDescriptor tile_descriptor;
tile_descriptor.status = SCAN_TILE_PARTIAL;
tile_descriptor.value = tile_partial.value;
tile_descriptor.key = tile_partial.key;
TxnWord alias;
*reinterpret_cast<TileDescriptor*>(&alias) = tile_descriptor;
detail::store_relaxed(d_tile_descriptors + TILE_STATUS_PADDING + tile_idx, alias);
}
/**
* Wait for the corresponding tile to become non-invalid
*/
template <class DelayT = detail::fixed_delay_constructor_t<350, 450>::delay_t>
__device__ __forceinline__ void WaitForValid(
int tile_idx,
StatusWord &status,
KeyValuePairT &value,
DelayT delay_or_prevent_hoisting = {})
{
// TxnWord alias = ThreadLoad<LOAD_CG>(d_tile_descriptors + TILE_STATUS_PADDING + tile_idx);
// TileDescriptor tile_descriptor = reinterpret_cast<TileDescriptor&>(alias);
//
// while (tile_descriptor.status == SCAN_TILE_INVALID)
// {
// __threadfence_block(); // prevent hoisting loads from loop
//
// alias = ThreadLoad<LOAD_CG>(d_tile_descriptors + TILE_STATUS_PADDING + tile_idx);
// tile_descriptor = reinterpret_cast<TileDescriptor&>(alias);
// }
//
// status = tile_descriptor.status;
// value.value = tile_descriptor.value;
// value.key = tile_descriptor.key;
TileDescriptor tile_descriptor;
do
{
delay_or_prevent_hoisting();
TxnWord alias = detail::load_relaxed(d_tile_descriptors + TILE_STATUS_PADDING + tile_idx);
tile_descriptor = reinterpret_cast<TileDescriptor&>(alias);
} while (WARP_ANY((tile_descriptor.status == SCAN_TILE_INVALID), 0xffffffff));
status = tile_descriptor.status;
value.value = tile_descriptor.value;
value.key = tile_descriptor.key;
}
};
/******************************************************************************
* Prefix call-back operator for coupling local block scan within a
* block-cooperative scan
******************************************************************************/
/**
* Stateful block-scan prefix functor. Provides the the running prefix for
* the current tile by using the call-back warp to wait on on
* aggregates/prefixes from predecessor tiles to become available.
*
* @tparam DelayConstructorT
* Implementation detail, do not specify directly, requirements on the
* content of this type are subject to breaking change.
*/
template <
typename T,
typename ScanOpT,
typename ScanTileStateT,
int LEGACY_PTX_ARCH = 0,
typename DelayConstructorT = detail::default_delay_constructor_t<T>>
struct TilePrefixCallbackOp
{
// Parameterized warp reduce
typedef WarpReduce<T, CUB_PTX_WARP_THREADS> WarpReduceT;
// Temporary storage type
struct _TempStorage
{
typename WarpReduceT::TempStorage warp_reduce;
T exclusive_prefix;
T inclusive_prefix;
T block_aggregate;
};
// Alias wrapper allowing temporary storage to be unioned
struct TempStorage : Uninitialized<_TempStorage> {};
// Type of status word
typedef typename ScanTileStateT::StatusWord StatusWord;
// Fields
_TempStorage& temp_storage; ///< Reference to a warp-reduction instance
ScanTileStateT& tile_status; ///< Interface to tile status
ScanOpT scan_op; ///< Binary scan operator
int tile_idx; ///< The current tile index
T exclusive_prefix; ///< Exclusive prefix for the tile
T inclusive_prefix; ///< Inclusive prefix for the tile
// Constructs prefix functor for a given tile index.
// Precondition: thread blocks processing all of the predecessor tiles were scheduled.
__device__ __forceinline__ TilePrefixCallbackOp(ScanTileStateT &tile_status,
TempStorage &temp_storage,
ScanOpT scan_op,
int tile_idx)
: temp_storage(temp_storage.Alias())
, tile_status(tile_status)
, scan_op(scan_op)
, tile_idx(tile_idx)
{}
// Computes the tile index and constructs prefix functor with it.
// Precondition: thread block per tile assignment.
__device__ __forceinline__ TilePrefixCallbackOp(ScanTileStateT &tile_status,
TempStorage &temp_storage,
ScanOpT scan_op)
: TilePrefixCallbackOp(tile_status, temp_storage, scan_op, blockIdx.x)
{}
// Block until all predecessors within the warp-wide window have non-invalid status
template <class DelayT = detail::default_delay_t<T>>
__device__ __forceinline__
void ProcessWindow(
int predecessor_idx, ///< Preceding tile index to inspect
StatusWord &predecessor_status, ///< [out] Preceding tile status
T &window_aggregate, ///< [out] Relevant partial reduction from this window of preceding tiles
DelayT delay = {})
{
T value;
tile_status.WaitForValid(predecessor_idx, predecessor_status, value, delay);
// Perform a segmented reduction to get the prefix for the current window.
// Use the swizzled scan operator because we are now scanning *down* towards thread0.
int tail_flag = (predecessor_status == StatusWord(SCAN_TILE_INCLUSIVE));
window_aggregate = WarpReduceT(temp_storage.warp_reduce).TailSegmentedReduce(
value,
tail_flag,
SwizzleScanOp<ScanOpT>(scan_op));
}
// BlockScan prefix callback functor (called by the first warp)
__device__ __forceinline__
T operator()(T block_aggregate)
{
// Update our status with our tile-aggregate
if (threadIdx.x == 0)
{
detail::uninitialized_copy(&temp_storage.block_aggregate,
block_aggregate);
tile_status.SetPartial(tile_idx, block_aggregate);
}
int predecessor_idx = tile_idx - threadIdx.x - 1;
StatusWord predecessor_status;
T window_aggregate;
// Wait for the warp-wide window of predecessor tiles to become valid
DelayConstructorT construct_delay(tile_idx);
ProcessWindow(predecessor_idx, predecessor_status, window_aggregate, construct_delay());
// The exclusive tile prefix starts out as the current window aggregate
exclusive_prefix = window_aggregate;
// Keep sliding the window back until we come across a tile whose inclusive prefix is known
while (WARP_ALL((predecessor_status != StatusWord(SCAN_TILE_INCLUSIVE)), 0xffffffff))
{
predecessor_idx -= CUB_PTX_WARP_THREADS;
// Update exclusive tile prefix with the window prefix
ProcessWindow(predecessor_idx, predecessor_status, window_aggregate, construct_delay());
exclusive_prefix = scan_op(window_aggregate, exclusive_prefix);
}
// Compute the inclusive tile prefix and update the status for this tile
if (threadIdx.x == 0)
{
inclusive_prefix = scan_op(exclusive_prefix, block_aggregate);
tile_status.SetInclusive(tile_idx, inclusive_prefix);
detail::uninitialized_copy(&temp_storage.exclusive_prefix,
exclusive_prefix);
detail::uninitialized_copy(&temp_storage.inclusive_prefix,
inclusive_prefix);
}
// Return exclusive_prefix
return exclusive_prefix;
}
// Get the exclusive prefix stored in temporary storage
__device__ __forceinline__
T GetExclusivePrefix()
{
return temp_storage.exclusive_prefix;
}
// Get the inclusive prefix stored in temporary storage
__device__ __forceinline__
T GetInclusivePrefix()
{
return temp_storage.inclusive_prefix;
}
// Get the block aggregate stored in temporary storage
__device__ __forceinline__
T GetBlockAggregate()
{
return temp_storage.block_aggregate;
}
__device__ __forceinline__
int GetTileIdx() const
{
return tile_idx;
}
};
CUB_NAMESPACE_END
|