[offload][OpenMP] Improve cross-team reduction grid selection #208253
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-codegen
@llvm/pr-subscribers-offload
Author: Robert Imschweiler (ro-i)
<details>
<summary>Changes</summary>
The default cross-team reduction algorithm benefits from larger and fewer teams. Implement that by using 2 x the default number of threads and 1/2 x the default number of teams for reduction kernels. This doesn't change the default total number of threads, it just redistributes them.
This is a first, rather simple heuristic, derived from (a subset of) what AOMP does.
The performance benefits I observed for the reduction tests in https://github.com/ro-i/xteam-test on a gfx942
(c71339705091500f731e2a39f247d2660bacbdce) are up to a few percent, with no regressions.
Claude assisted with this patch.
---
Full diff: https://github.com/llvm/llvm-project/pull/208253.diff
3 Files Affected:
````````````The server is unavailable at this time. Please wait a few minutes before you try again.
</details>
This is a first, rather simple heuristic, derived from (a subset of) what AOMP does.
I would say ROCm instead of AOMP.
Can you add a test?
Can you add a test?
yeah, this should now be possible without depending on specific hardware. I initially had no test because I was following a different selection strategy, but with the current redistribution, it's not a problem anymore. Thanks for the reminder!
Clamp it to GV_Max_WG_Size.
A more general concern is whether it makes sense to tie the reduction kernel wg_size to the default wg_size. If you think that reduction kernels should default to a different wg_size, perhaps use a different wg_size independently. Otherwise, what happens if in the future, the default_wg_size is changed? Would reduction kernels still benefit from the doubling?
Can you split the test so that target-agnostic checks work across targets and possibly have target-specific tests?
Re: The performance benefits I observed for the reduction tests in https://github.com/ro-i/xteam-test on a gfx942
What about other targets since they are impacted as well?
I actually previously followed the strategy to aim for 50% occupancy (using getHardwareParallelism()). But it seemed too random.
Being based on the default total number of threads means that the reductions don't arbitrarily aim for a different total number of threads. They just redistribute that total number by doubling the team size and cutting the number of teams in half.
The reason why this makes sense is that we know the algorithm that is executed by the cross-team reduction and that it benefits from a tendency towards larger and fewer teams due to less inter-team synchronization and less work in the final team.
This is far from perfect. It's just re-using the simple rule that we have for ROCm in a more portable and less arbitrary way. See e.g. https://github.com/ROCm/llvm-project/blob/7f90160e0c374525bbfd7d3485949f9130cb732b/llvm/include/llvm/Frontend/OpenMP/OMPConstants.h#L312. Unfortunately, ROCm has a lot of different aspects and tuning knobs that affect the actual grid layout. As I mentioned, I'm trying to get started with some conservative and simple rule.
Agreed, that's a reasonable choice. But clamp it to GV_Max_WG_Size.
Your change halves the number of blocks even if the blocksize could not be increased for some reason. To guard against that problem, guard it like below. Additionally, instead of a hardcoded 2, use a scale-factor.
if (isCrossTeamReduction() && EffectiveNumThreads > PreferredNumThreads) {
uint64_t Scaled = std::max<uint64_t>(
(uint64_t(DefaultNumBlocks) * PreferredNumThreads) / EffectiveNumThreads, 1);
uint64_t NumBlocks = std::min<uint64_t>(
Scaled, ((LoopTripCount - 1) / EffectiveNumThreads) + 1);
return std::min(NumBlocks, uint64_t(GenericDevice.getBlockLimit(EffectiveNumThreads)));
}
this has been superseded by my other PRs
dhruvachak
ro-i
The default cross-team reduction algorithm benefits from larger and fewer teams. Implement that by using 2 x the default number of threads and 1/2 x the default number of teams for reduction kernels. This doesn't change the default total number of threads, it just redistributes them.
This is a first, rather simple heuristic, derived from (a subset of) what ROCm does.
The performance benefits I observed for the reduction tests in https://github.com/ro-i/xteam-test on a gfx942
(c71339705091500f731e2a39f247d2660bacbdce) are up to a few percent, with no regressions.
Claude assisted with this patch.