-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc] Add utility functions for warp-level scan and reduction #84866
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
Merged
Merged
Changes from all commits
Commits
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
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 |
---|---|---|
@@ -1 +1,4 @@ | ||
add_subdirectory(threads) | ||
if(LIBC_TARGET_OS_IS_GPU) | ||
add_subdirectory(GPU) | ||
endif() |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
add_custom_target(libc-support-gpu-tests) | ||
add_dependencies(libc-integration-tests libc-support-gpu-tests) | ||
|
||
add_integration_test( | ||
scan_reduce_test | ||
SUITE libc-support-gpu-tests | ||
SRCS | ||
scan_reduce.cpp | ||
LOADER_ARGS | ||
--threads 64 | ||
) |
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//===-- Test for the parallel scan and reduction operations on the GPU ----===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/__support/CPP/bit.h" | ||
#include "src/__support/GPU/utils.h" | ||
#include "test/IntegrationTest/test.h" | ||
|
||
using namespace LIBC_NAMESPACE; | ||
|
||
static uint32_t sum(uint32_t n) { return n * (n + 1) / 2; } | ||
|
||
// Tests a reduction within a convergant warp or wavefront using some known | ||
// values. For example, if every element in the lane is one, then the sum should | ||
// be the size of the warp or wavefront, i.e. 1 + 1 + 1 ... + 1. | ||
static void test_reduce() { | ||
uint64_t mask = gpu::get_lane_mask(); | ||
uint32_t x = gpu::reduce(mask, 1); | ||
EXPECT_EQ(x, gpu::get_lane_size()); | ||
|
||
uint32_t y = gpu::reduce(mask, gpu::get_lane_id()); | ||
EXPECT_EQ(y, sum(gpu::get_lane_size() - 1)); | ||
|
||
uint32_t z = 0; | ||
if (gpu::get_lane_id() % 2) | ||
z = gpu::reduce(gpu::get_lane_mask(), 1); | ||
gpu::sync_lane(mask); | ||
|
||
EXPECT_EQ(z, gpu::get_lane_id() % 2 ? gpu::get_lane_size() / 2 : 0); | ||
} | ||
|
||
// Tests an accumulation scan within a convergent warp or wavefront using some | ||
// known values. For example, if every element in the lane is one, then the scan | ||
// should have each element be equivalent to its ID, i.e. 1, 1 + 1, ... | ||
static void test_scan() { | ||
uint64_t mask = gpu::get_lane_mask(); | ||
|
||
uint32_t x = gpu::scan(mask, 1); | ||
EXPECT_EQ(x, gpu::get_lane_id() + 1); | ||
|
||
uint32_t y = gpu::scan(mask, gpu::get_lane_id()); | ||
EXPECT_EQ(y, sum(gpu::get_lane_id())); | ||
|
||
uint32_t z = 0; | ||
if (gpu::get_lane_id() % 2) | ||
z = gpu::scan(gpu::get_lane_mask(), 1); | ||
gpu::sync_lane(mask); | ||
|
||
EXPECT_EQ(z, gpu::get_lane_id() % 2 ? gpu::get_lane_id() / 2 + 1 : 0); | ||
} | ||
|
||
TEST_MAIN(int argc, char **argv, char **envp) { | ||
test_reduce(); | ||
|
||
test_scan(); | ||
|
||
return 0; | ||
} |
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.
We have llvm.amdgcn.wave.reduce.umin/umax. The intent was to expand those intrinsics to cover more operations , since the expansion has different strategies depending on the available instructions
Uh oh!
There was an error while loading. Please reload this page.
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.
I'll need this for the NVPTX build in any case, and I think it's sufficient for now to have this in source the way I need it until someone wants to implement the suite more correctly. However I don't think it will change too much considering that this is just convergent addition.
So, can we keep this implementation and replace it with a better builtin one 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.
Also looks like the alternative implementations were never implemented. currently looks like we don't have the bpermute or DPP paths
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.
I definitely see the appeal of having these as intrinsic functions. Specifically because right now I need to use
__builtin_amdgcn_wavefrontsize
which prevents these loops from being fully unrolled. However, I think it will be easy to drop those intrinsic functions in later.