-
Notifications
You must be signed in to change notification settings - Fork 917
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
Generate group labels from offsets #10945
Merged
Merged
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
41a7baa
Switch to use `generate_list_labels`
ttnghia 1204e9a
Remove comments
ttnghia 165f752
Switch to use 0-based list labels
ttnghia 3f385eb
Implement `fill_segmented_labels`
ttnghia 2684625
Move file and change file name
ttnghia 0cfe856
Use `fill_segmented_labels` in groupby
ttnghia 1007176
Add comment
ttnghia 9b5a88d
Add example
ttnghia 9903007
Rename and move file
ttnghia 7bd714d
Rename variable
ttnghia 031302b
Add a benchmark
ttnghia 28e1463
Rewrite `label_segments`
ttnghia d3708a5
Fix compile error
ttnghia 60259c9
Hack to test
ttnghia 97b9a57
Revert "Add a benchmark"
ttnghia a094dab
Revert "Hack to test"
ttnghia 8315764
Merge branch 'branch-22.08' into list_label
ttnghia f5a5520
Add comment
ttnghia a060e3d
Add comment clarifying bound check
ttnghia b8cb363
Rewrite example
ttnghia ab1e25a
Reverse comments. They will be removed completely later on so don't c…
ttnghia 8e1f01a
Merge branch 'branch-22.08' into list_label
ttnghia cc0dfc1
Merge branch 'branch-22.08' into list_label
ttnghia ba91075
Cleanup headers
ttnghia de2f197
Cleanup headers
ttnghia becb593
Use offsets iterator directly
ttnghia e461814
Initialize output at first
ttnghia b0d5122
Merge branch 'branch-22.08' into list_label
ttnghia b7e6d9a
Fix loop, excluding the last offset value
ttnghia bfe0bf0
Add comment
ttnghia 74a33d4
Merge branch 'branch-22.08' into list_label
ttnghia 15d036a
Try to reverse `sort_helper.cu`
ttnghia 26aed34
Revert "Try to reverse `sort_helper.cu`"
ttnghia a9930b1
Handle the special case when the output array is empty
ttnghia 10812bb
Reorganize code
ttnghia 847311b
Add a test
ttnghia 1e7b843
Modify test
ttnghia ba58d6f
Merge branch 'branch-22.08' into list_label
ttnghia 6e098a2
Rewrite comment
ttnghia 8dd7f2d
Change termination condition
ttnghia 64a107c
Add comment
ttnghia 77002ef
Fix comment
ttnghia 136511d
Rename `out_` iterators into `label_`
ttnghia 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 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,99 @@ | ||
/* | ||
* Copyright (c) 2022, NVIDIA CORPORATION. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <cudf/types.hpp> | ||
|
||
#include <rmm/cuda_stream_view.hpp> | ||
#include <rmm/exec_policy.hpp> | ||
|
||
#include <thrust/distance.h> | ||
#include <thrust/for_each.h> | ||
#include <thrust/scan.h> | ||
#include <thrust/uninitialized_fill.h> | ||
|
||
namespace cudf::detail { | ||
|
||
/** | ||
* @brief Fill label values for segments defined by a given offsets array. | ||
* | ||
* Given a pair of iterators accessing to an offset array, generate label values for segments | ||
* defined by the offset values. The output will be an array containing consecutive groups of | ||
* identical labels, the number of elements in each group `i` is defined by | ||
* `offsets[i+1] - offsets[i]`. | ||
* | ||
* The labels always start from `0` regardless of the offset values. | ||
* In case there are empty segments, their corresponding label values will be skipped in the output. | ||
* | ||
* Note that the caller is responsible to make sure the output range have the correct size, which is | ||
* the total segment sizes (i.e., `size = *(offsets_end - 1) - *offsets_begin`). Otherwise, the | ||
* result is undefined. | ||
* | ||
* @code{.pseudo} | ||
* Examples: | ||
* | ||
* offsets = [ 0, 4, 6, 6, 6, 10 ] | ||
* output = [ 0, 0, 0, 0, 1, 1, 4, 4, 4, 4 ] | ||
* | ||
* offsets = [ 5, 10, 12 ] | ||
* output = [ 0, 0, 0, 0, 0, 1, 1 ] | ||
* @endcode | ||
* | ||
* @param offsets_begin The beginning of the offsets that define segments. | ||
* @param offsets_end The end of the offsets that define segments. | ||
* @param label_begin The beginning of the output label range. | ||
* @param label_end The end of the output label range. | ||
* @param stream CUDA stream used for device memory operations and kernel launches. | ||
*/ | ||
template <typename InputIterator, typename OutputIterator> | ||
void label_segments(InputIterator offsets_begin, | ||
InputIterator offsets_end, | ||
OutputIterator label_begin, | ||
OutputIterator label_end, | ||
rmm::cuda_stream_view stream) | ||
{ | ||
// If the output array is empty, that means we have all empty segments. | ||
// In such cases, we must terminate immediately. Otherwise, the `for_each` loop below may try to | ||
// access memory of the output array, resulting in "illegal memory access" error. | ||
if (thrust::distance(label_begin, label_end) == 0) { return; } | ||
|
||
// When the output array is not empty, always fill it with `0` value first. | ||
using OutputType = typename thrust::iterator_value<OutputIterator>::type; | ||
thrust::uninitialized_fill(rmm::exec_policy(stream), label_begin, label_end, OutputType{0}); | ||
|
||
// If the offsets array has no more than 2 offset values, there will be at max 1 segment. | ||
// In such cases, the output will just be an array of all `0` values (which we already filled). | ||
// We should terminate here, otherwise the `inclusive_scan` call below still do its entire | ||
// computation. That is unnecessary and may be expensive if we have the input offsets defining a | ||
// very large segment. | ||
if (thrust::distance(offsets_begin, offsets_end) <= 2) { return; } | ||
|
||
thrust::for_each(rmm::exec_policy(stream), | ||
offsets_begin + 1, // exclude the first offset value | ||
offsets_end - 1, // exclude the last offset value | ||
[offsets = offsets_begin, output = label_begin] __device__(auto const idx) { | ||
// Zero-normalized offsets. | ||
auto const dst_idx = idx - (*offsets); | ||
|
||
// Scatter value `1` to the index at (idx - offsets[0]). | ||
// In case we have repeated offsets (i.e., we have empty segments), this | ||
// `atomicAdd` call will make sure the label values corresponding to these | ||
// empty segments will be skipped in the output. | ||
atomicAdd(&output[dst_idx], OutputType{1}); | ||
}); | ||
thrust::inclusive_scan(rmm::exec_policy(stream), label_begin, label_end, label_begin); | ||
} | ||
|
||
} // namespace cudf::detail |
This file contains 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 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 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
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.
This change is due to switching to using 0-starting labels from 1-starting labels.