-
Notifications
You must be signed in to change notification settings - Fork 396
[cudax] Add support for generic thread groups within warp and cluster #8792
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -116,46 +116,60 @@ __device__ cuda::std::optional<T> sum(cudax::this_cluster<Hierarchy> group, T (& | |
| return (cuda::gpu_thread.is_root_rank(group)) ? cuda::std::optional{result} : cuda::std::nullopt; | ||
| } | ||
|
|
||
| // todo(dabayer): Add support for warp and cluster levels. | ||
| template <class Group, class T, cuda::std::size_t N> | ||
| __device__ cuda::std::optional<T> sum(Group group, T (&array)[N]) | ||
| { | ||
| using Unit = typename Group::unit_type; | ||
| using MappingResult = typename Group::__mapping_result_type; | ||
|
|
||
| constexpr auto ngroups = MappingResult::static_group_count(); | ||
| static_assert(ngroups != cuda::std::dynamic_extent, "group count must be statically known"); | ||
|
|
||
| __shared__ T group_sums[ngroups]; | ||
| using Level = typename Group::level_type; | ||
|
|
||
| if (!Unit{}.is_part_of(group)) | ||
| if constexpr (cuda::std::is_same_v<Level, cuda::warp_level>) | ||
| { | ||
| return cuda::std::nullopt; | ||
| const auto result_unit = sum(cudax::this_thread{group.hierarchy()}, array); | ||
|
|
||
| // todo(dabayer): Implement fallback for cc < 80. | ||
| T result; | ||
| NV_IF_TARGET(NV_PROVIDES_SM_80, | ||
| ({ result = __reduce_add_sync(group.__synchronizer_instance().__lane_mask(), result_unit.value()); })) | ||
|
Contributor
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. Question: Cannot we use It should use the |
||
| return (cuda::gpu_thread.is_root_rank(group)) ? cuda::std::optional{result} : cuda::std::nullopt; | ||
|
Comment on lines
+128
to
+132
Contributor
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. Question: That comment suggests the code path is not valid for SM < 80, we should at least assert that to ensure we do not forget it once this goes out of experimentall |
||
| } | ||
| else | ||
| { | ||
| using Unit = typename Group::unit_type; | ||
| using MappingResult = typename Group::__mapping_result_type; | ||
|
|
||
| // todo(dabayer): Replace by group.rank(level) once this query is available. | ||
| const auto group_rank = group.__mapping_result().group_rank(); | ||
| constexpr auto ngroups = MappingResult::static_group_count(); | ||
| static_assert(ngroups != cuda::std::dynamic_extent, "group count must be statically known"); | ||
|
|
||
| if (cuda::gpu_thread.is_root_rank(group)) | ||
| { | ||
| group_sums[group_rank] = 0; | ||
| } | ||
| __shared__ T group_sums[ngroups]; | ||
|
Contributor
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. Hmm, I was thinking if in actual interface shared memory should come from the user instead, for example here if only one group calls this we waste shared memory
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. This is correct, but I would say that this is outside of this epic's scope, so I just went with statically shared memory allocations inside the algorithms |
||
|
|
||
| const auto unit_group = cudax::make_this_group(Unit{}, group.hierarchy()); | ||
| const auto result_unit = sum(unit_group, array); | ||
| if (!Unit{}.is_part_of(group)) | ||
| { | ||
| return cuda::std::nullopt; | ||
| } | ||
|
|
||
| // Wait until group_sums are are filled with 0. | ||
| group.sync_aligned(); | ||
| const auto group_rank = group.rank(Level{}); | ||
|
|
||
| if (cuda::gpu_thread.is_root_rank(unit_group)) | ||
| { | ||
| cuda::atomic_ref<T, cuda::thread_scope_block>{group_sums[group_rank]} += result_unit.value(); | ||
| } | ||
| if (cuda::gpu_thread.is_root_rank(group)) | ||
| { | ||
| group_sums[group_rank] = 0; | ||
| } | ||
|
|
||
| const auto unit_group = cudax::make_this_group(Unit{}, group.hierarchy()); | ||
| const auto result_unit = sum(unit_group, array); | ||
|
|
||
| // Wait until group_sums are are filled with 0. | ||
| group.sync_aligned(); | ||
|
|
||
| // Wait until all unit_group roots add the intermediate sum to the shared memory. | ||
| group.sync_aligned(); | ||
| if (cuda::gpu_thread.is_root_rank(unit_group)) | ||
| { | ||
| constexpr auto min_thread_scope = cudax::__minimum_required_scope_for<Level>(); | ||
| cuda::atomic_ref<T, min_thread_scope>{group_sums[group_rank]} += result_unit.value(); | ||
| } | ||
|
|
||
| return (cuda::gpu_thread.is_root_rank(group)) ? cuda::std::optional{group_sums[group_rank]} : cuda::std::nullopt; | ||
| // Wait until all unit_group roots add the intermediate sum to the shared memory. | ||
| group.sync_aligned(); | ||
|
|
||
| return (cuda::gpu_thread.is_root_rank(group)) ? cuda::std::optional{group_sums[group_rank]} : cuda::std::nullopt; | ||
| } | ||
| } | ||
|
|
||
| template <class Group> | ||
|
|
@@ -190,10 +204,24 @@ struct TestKernel | |
| test_cooperative_algorithm(cudax::this_block{config}); | ||
| test_cooperative_algorithm(cudax::this_cluster{config}); | ||
|
|
||
| // todo(dabayer): Enable these once cc < 75 fallback for __reduce_add is implemented. | ||
| NV_IF_TARGET( | ||
| NV_PROVIDES_SM_80, ({ | ||
| test_cooperative_algorithm( | ||
| cudax::group{cuda::gpu_thread, cudax::this_warp{config}, cudax::group_by<2>{}, cudax::lane_synchronizer{}}); | ||
| test_cooperative_algorithm( | ||
| cudax::group{cuda::gpu_thread, cudax::this_warp{config}, cudax::group_by<16>{}, cudax::lane_synchronizer{}}); | ||
| })) | ||
|
|
||
| test_cooperative_algorithm( | ||
| cudax::group{cuda::gpu_thread, cudax::this_block{config}, cudax::group_by<2>{}, cudax::lane_synchronizer{}}); | ||
| test_cooperative_algorithm( | ||
| cudax::group{cuda::gpu_thread, cudax::this_block{config}, cudax::group_by<16>{}, cudax::lane_synchronizer{}}); | ||
|
|
||
| test_cooperative_algorithm( | ||
| cudax::group{cuda::gpu_thread, cudax::this_cluster{config}, cudax::group_by<2>{}, cudax::lane_synchronizer{}}); | ||
| test_cooperative_algorithm( | ||
| cudax::group{cuda::gpu_thread, cudax::this_cluster{config}, cudax::group_by<16>{}, cudax::lane_synchronizer{}}); | ||
| } | ||
| }; | ||
| } // namespace | ||
|
|
||
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.
Question: Does this also handle cluster level fine or was the comment outdated