Skip to content
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

Workaround groupby aggregate thrust::copy_if overflow #12079

Merged
merged 7 commits into from
Nov 10, 2022
Merged
35 changes: 24 additions & 11 deletions cpp/src/groupby/hash/groupby.cu
Original file line number Diff line number Diff line change
Expand Up @@ -512,18 +512,31 @@ rmm::device_uvector<size_type> extract_populated_keys(map_type const& map,
{
rmm::device_uvector<size_type> populated_keys(num_keys, stream);

auto get_key = [] __device__(auto const& element) { return element.first; }; // first = key
auto get_key_it = thrust::make_transform_iterator(map.data(), get_key);
auto key_used = [unused = map.get_unused_key()] __device__(auto key) { return key != unused; };

auto end_it = thrust::copy_if(rmm::exec_policy(stream),
get_key_it,
get_key_it + map.capacity(),
populated_keys.begin(),
key_used);

populated_keys.resize(std::distance(populated_keys.begin(), end_it), stream);
auto get_key = [] __device__(auto const& element) { return element.first; }; // first = key
auto key_itr = thrust::make_transform_iterator(map.data(), get_key);
auto key_used = [unused = map.get_unused_key()] __device__(auto key) { return key != unused; };
davidwendt marked this conversation as resolved.
Show resolved Hide resolved

// thrust::copy_if has a bug where it cannot iterate over int-max values
// so if map.capacity() > int-max we'll call thrust::copy_if in chunks instead
auto const map_size =
davidwendt marked this conversation as resolved.
Show resolved Hide resolved
std::min(map.capacity(), static_cast<std::size_t>(std::numeric_limits<size_type>::max()));
davidwendt marked this conversation as resolved.
Show resolved Hide resolved
auto const key_end = key_itr + map.capacity();
auto pop_keys_itr = populated_keys.begin();

std::size_t output_size = 0;
while (key_itr != key_end) {
auto const copy_end = static_cast<std::size_t>(std::distance(key_itr, key_end)) <= map_size
davidwendt marked this conversation as resolved.
Show resolved Hide resolved
? key_end
: key_itr + map_size;
auto const end_it =
thrust::copy_if(rmm::exec_policy(stream), key_itr, copy_end, pop_keys_itr, key_used);
auto const copied = std::distance(pop_keys_itr, end_it);
pop_keys_itr += copied;
output_size += copied;
key_itr = copy_end;
}

populated_keys.resize(output_size, stream);
return populated_keys;
}

Expand Down