-
Notifications
You must be signed in to change notification settings - Fork 371
Refactor warpspeed scan tuning #8145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bernhardmgruber
wants to merge
8
commits into
NVIDIA:main
Choose a base branch
from
bernhardmgruber:scan_refactor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f2a950c
Use ptx_arch_id
bernhardmgruber df072c5
Refactor warpspeed scan tuning
bernhardmgruber cc526b4
Move some stuff from policy_selector to kernel source
bernhardmgruber af04c06
Update benchmark polict selector
bernhardmgruber 9fc4c3e
Work around clang
bernhardmgruber 4b23889
Fix warning
bernhardmgruber db1c4d7
Fix warning
bernhardmgruber db404fb
Review
bernhardmgruber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,11 +88,36 @@ struct DeviceScanKernelSource | |
| AccumT, | ||
| EnforceInclusive == ForceInclusive::Yes>) | ||
|
|
||
| CUB_RUNTIME_FUNCTION static constexpr ::cuda::std::size_t InputSize() | ||
| { | ||
| return sizeof(it_value_t<UnwrappedInputIteratorT>); | ||
| } | ||
|
|
||
| CUB_RUNTIME_FUNCTION static constexpr ::cuda::std::size_t InputAlign() | ||
| { | ||
| return alignof(it_value_t<UnwrappedInputIteratorT>); | ||
| } | ||
|
|
||
| CUB_RUNTIME_FUNCTION static constexpr ::cuda::std::size_t OutputSize() | ||
| { | ||
| return sizeof(it_value_t<UnwrappedOutputIteratorT>); | ||
| } | ||
|
|
||
| CUB_RUNTIME_FUNCTION static constexpr ::cuda::std::size_t OutputAlign() | ||
| { | ||
| return alignof(it_value_t<UnwrappedOutputIteratorT>); | ||
| } | ||
|
|
||
| CUB_RUNTIME_FUNCTION static constexpr ::cuda::std::size_t AccumSize() | ||
| { | ||
| return sizeof(AccumT); | ||
| } | ||
|
|
||
| CUB_RUNTIME_FUNCTION static constexpr ::cuda::std::size_t AccumAlign() | ||
| { | ||
| return alignof(AccumT); | ||
| } | ||
|
|
||
| CUB_RUNTIME_FUNCTION static ScanTileStateT TileState() | ||
| { | ||
| return {}; | ||
|
|
@@ -481,21 +506,19 @@ struct DispatchScan | |
| } | ||
|
|
||
| #if __cccl_ptx_isa >= 860 | ||
| template <typename PolicyGetter, typename PolicySelectorT> | ||
| CUB_RUNTIME_FUNCTION _CCCL_HOST _CCCL_FORCEINLINE cudaError_t | ||
| __invoke_lookahead_algorithm(PolicyGetter policy_getter, const PolicySelectorT& policy_selector) | ||
| template <typename PolicyGetter> | ||
| CUB_RUNTIME_FUNCTION _CCCL_HOST _CCCL_FORCEINLINE cudaError_t __invoke_lookahead_algorithm(PolicyGetter policy_getter) | ||
| { | ||
| if (num_items == 0) | ||
| { | ||
| temp_storage_bytes = 1; // just fulfill the contract that CUB always requires some temporary storage | ||
| return cudaSuccess; | ||
| } | ||
|
|
||
| CUB_DETAIL_CONSTEXPR_ISH auto active_policy = policy_getter(); | ||
| CUB_DETAIL_CONSTEXPR_ISH const auto warpspeed_policy = active_policy.warpspeed; | ||
| CUB_DETAIL_CONSTEXPR_ISH const detail::scan::scan_warpspeed_policy warpspeed_policy = policy_getter().warpspeed; | ||
|
|
||
| const int grid_dim = | ||
| static_cast<int>(::cuda::ceil_div(num_items, static_cast<OffsetT>(warpspeed_policy.tile_size))); | ||
| static_cast<int>(::cuda::ceil_div(num_items, static_cast<OffsetT>(warpspeed_policy.tile_size()))); | ||
|
|
||
| if (d_temp_storage == nullptr) | ||
| { | ||
|
|
@@ -524,37 +547,19 @@ struct DispatchScan | |
| // TODO(bgruber): we probably need to ensure alignment of d_temp_storage | ||
| _CCCL_ASSERT(::cuda::is_aligned(d_temp_storage, kernel_source.look_ahead_tile_state_alignment()), ""); | ||
|
|
||
| using selector_smem_info_t = detail::scan::selector_smem_info<PolicySelectorT>; | ||
|
|
||
| auto scan_kernel = kernel_source.ScanKernel(); | ||
| int smem_size_1_stage = 0; | ||
| if constexpr (selector_smem_info_t::has_static_layout) | ||
| { | ||
| CUB_DETAIL_CONSTEXPR_ISH int static_smem_size_1_stage = detail::scan::smem_for_stages( | ||
| warpspeed_policy, | ||
| 1, | ||
| selector_smem_info_t::input_value_size, | ||
| selector_smem_info_t::input_value_alignment, | ||
| selector_smem_info_t::output_value_size, | ||
| selector_smem_info_t::output_value_alignment, | ||
| selector_smem_info_t::accum_size, | ||
| selector_smem_info_t::accum_alignment); | ||
| CUB_DETAIL_STATIC_ISH_ASSERT(static_smem_size_1_stage <= detail::max_smem_per_block, | ||
| "Single-stage warpspeed scan exceeds architecture independent SMEM (48KiB)"); | ||
| smem_size_1_stage = static_smem_size_1_stage; | ||
| } | ||
| else | ||
| { | ||
| smem_size_1_stage = detail::scan::smem_for_stages( | ||
| warpspeed_policy, | ||
| 1, | ||
| policy_selector.input_value_size, | ||
| policy_selector.input_value_alignment, | ||
| policy_selector.output_value_size, | ||
| policy_selector.output_value_alignment, | ||
| policy_selector.accum_size, | ||
| policy_selector.accum_alignment); | ||
| } | ||
| auto scan_kernel = kernel_source.ScanKernel(); | ||
| [[maybe_unused]] auto kernel_src = kernel_source; // need to pull a copy to not access `this` during const. eval. | ||
| CUB_DETAIL_CONSTEXPR_ISH int smem_size_1_stage = detail::scan::smem_for_stages( | ||
| warpspeed_policy, | ||
| 1, | ||
| static_cast<int>(kernel_src.InputSize()), | ||
| static_cast<int>(kernel_src.InputAlign()), | ||
| static_cast<int>(kernel_src.OutputSize()), | ||
| static_cast<int>(kernel_src.OutputAlign()), | ||
| static_cast<int>(kernel_src.AccumSize()), | ||
| static_cast<int>(kernel_src.AccumAlign())); | ||
| CUB_DETAIL_STATIC_ISH_ASSERT(smem_size_1_stage <= detail::max_smem_per_block, | ||
| "Single-stage warpspeed scan exceeds architecture independent SMEM (48KiB)"); | ||
|
|
||
| int num_stages = 1; | ||
| int smem_size = smem_size_1_stage; | ||
|
|
@@ -565,36 +570,19 @@ struct DispatchScan | |
| // 1 CTA per SM +1 since it tends to improve performance | ||
| // TODO(bgruber): make the +1 a tuning parameter | ||
| const int max_stages_for_even_workload = static_cast<int>( | ||
| ::cuda::ceil_div(num_items, static_cast<OffsetT>(sm_count * warpspeed_policy.tile_size)) + 1); | ||
| ::cuda::ceil_div(num_items, static_cast<OffsetT>(sm_count * warpspeed_policy.tile_size())) + 1); | ||
|
|
||
| while (num_stages <= max_stages_for_even_workload) | ||
| { | ||
| const auto next_smem_size = [&] { | ||
| if constexpr (selector_smem_info_t::has_static_layout) | ||
| { | ||
| return detail::scan::smem_for_stages( | ||
| warpspeed_policy, | ||
| num_stages + 1, | ||
| selector_smem_info_t::input_value_size, | ||
| selector_smem_info_t::input_value_alignment, | ||
| selector_smem_info_t::output_value_size, | ||
| selector_smem_info_t::output_value_alignment, | ||
| selector_smem_info_t::accum_size, | ||
| selector_smem_info_t::accum_alignment); | ||
| } | ||
| else | ||
| { | ||
| return detail::scan::smem_for_stages( | ||
| warpspeed_policy, | ||
| num_stages + 1, | ||
| policy_selector.input_value_size, | ||
| policy_selector.input_value_alignment, | ||
| policy_selector.output_value_size, | ||
| policy_selector.output_value_alignment, | ||
| policy_selector.accum_size, | ||
| policy_selector.accum_alignment); | ||
| } | ||
| }(); | ||
| const int next_smem_size = detail::scan::smem_for_stages( | ||
| warpspeed_policy, | ||
| num_stages + 1, | ||
| static_cast<int>(kernel_source.InputSize()), | ||
| static_cast<int>(kernel_source.InputAlign()), | ||
| static_cast<int>(kernel_source.OutputSize()), | ||
| static_cast<int>(kernel_source.OutputAlign()), | ||
| static_cast<int>(kernel_source.AccumSize()), | ||
| static_cast<int>(kernel_source.AccumAlign())); | ||
| if (next_smem_size > max_dynamic_smem_size) | ||
| { | ||
| // This number of stages failed, so stay at the current settings | ||
|
|
@@ -647,7 +635,7 @@ struct DispatchScan | |
|
|
||
| // Invoke scan kernel | ||
| { | ||
| const int block_dim = warpspeed_policy.num_total_threads; | ||
| const int block_dim = detail::scan::num_total_threads(warpspeed_policy); | ||
|
|
||
| # ifdef CUB_DEBUG_LOG | ||
| _CubLog("Invoking DeviceScanKernel<<<%d, %d, %d, %lld>>>()\n", grid_dim, block_dim, smem_size, (long long) stream); | ||
|
|
@@ -689,9 +677,8 @@ struct DispatchScan | |
| _CCCL_DIAG_PUSH | ||
| _CCCL_DIAG_SUPPRESS_MSVC(4702) | ||
|
|
||
| template <typename PolicyGetter, typename PolicySelectorT> | ||
| CUB_RUNTIME_FUNCTION _CCCL_FORCEINLINE cudaError_t | ||
| __invoke(PolicyGetter policy_getter, [[maybe_unused]] const PolicySelectorT& policy_selector) | ||
| template <typename PolicyGetter> | ||
| CUB_RUNTIME_FUNCTION _CCCL_FORCEINLINE cudaError_t __invoke(PolicyGetter policy_getter) | ||
| { | ||
| CUB_DETAIL_CONSTEXPR_ISH auto active_policy = policy_getter(); | ||
|
|
||
|
|
@@ -702,12 +689,12 @@ struct DispatchScan | |
| # if defined(CUB_DEFINE_RUNTIME_POLICIES) | ||
| if (kernel_source.use_warpspeed(active_policy)) | ||
| { | ||
| return __invoke_lookahead_algorithm(policy_getter, policy_selector); | ||
| return __invoke_lookahead_algorithm(policy_getter); | ||
| } | ||
| # else | ||
| if CUB_DETAIL_CONSTEXPR_ISH (KernelSource::use_warpspeed(active_policy)) | ||
| { | ||
| return __invoke_lookahead_algorithm(policy_getter, policy_selector); | ||
| return __invoke_lookahead_algorithm(policy_getter); | ||
| } | ||
| # endif | ||
| #endif // __cccl_ptx_isa >= 860 | ||
|
|
@@ -851,13 +838,7 @@ struct DispatchScan | |
| } | ||
| }; | ||
|
|
||
| using policy_selector_t = detail::scan::policy_selector_from_types< | ||
| detail::it_value_t<InputIteratorT>, | ||
| detail::it_value_t<OutputIteratorT>, | ||
| AccumT, | ||
| OffsetT, | ||
| ScanOpT>; | ||
|
Comment on lines
-854
to
-859
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: This was incorrect, since it ignored the user provided policy hub. |
||
| return __invoke(policy_getter{}, policy_selector_t{}); | ||
| return __invoke(policy_getter{}); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -1027,7 +1008,7 @@ CUB_RUNTIME_FUNCTION _CCCL_FORCEINLINE auto dispatch( | |
| -1 /* ptx_version, not used actually */, | ||
| kernel_source, | ||
| launcher_factory} | ||
| .__invoke(policy_getter, policy_selector); | ||
| .__invoke(policy_getter); | ||
| }); | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my understanding is that everything here is at compile-time
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's only
constexprwhen called through the CUB API. It's justconstwhen called through CCCL.C.