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

Split hash-based groupby into multiple smaller files to reduce build time #17089

Merged
merged 26 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
7041944
Add groupby helper
PointKernel Oct 15, 2024
b34fa35
Splitting files to reduce build time
PointKernel Oct 15, 2024
9373be2
Remove redundant bitmask calculation
PointKernel Oct 15, 2024
d1abcdc
Move sparse_to_dense_results to its own TU
PointKernel Oct 15, 2024
daaa332
Move compute_single_pass_aggs to its own TU
PointKernel Oct 15, 2024
9261052
Update copyright years
PointKernel Oct 15, 2024
0bfcd9f
Rename single pass functor + fix typos
PointKernel Oct 15, 2024
5ba40b2
Merge remote-tracking branch 'upstream/branch-24.12' into dispatch-gr…
PointKernel Oct 17, 2024
238508f
Move local utility to anonymous namespace
PointKernel Oct 17, 2024
bc9b669
Make compute_groupby a CUDA header
PointKernel Oct 17, 2024
c8bddd2
Fix
PointKernel Oct 17, 2024
d5ce67a
Minor improvement with reserve
PointKernel Oct 17, 2024
cea2d68
Fix typo and cleanups
PointKernel Oct 17, 2024
8f5d48b
Use async policy
PointKernel Oct 17, 2024
45d0bab
Remove outdated doc
PointKernel Oct 17, 2024
f4ea8da
Update docs
PointKernel Oct 17, 2024
461c672
Merge remote-tracking branch 'upstream/branch-24.12' into dispatch-gr…
PointKernel Oct 17, 2024
a008b09
Make compute_groupby a C++ header
PointKernel Oct 17, 2024
444316d
Expose extract_populated_keys in header
PointKernel Oct 17, 2024
7d06617
Update docs
PointKernel Oct 17, 2024
9f786ec
Add missing doc
PointKernel Oct 17, 2024
2aa5385
Minor doc fix
PointKernel Oct 17, 2024
ca516ef
Move create_sparse_results_table to anonymous namespace
PointKernel Oct 17, 2024
0e4b5a6
Merge remote-tracking branch 'upstream/branch-24.12' into dispatch-gr…
PointKernel Oct 18, 2024
1cc0acd
Move create_sparse_results_table to a separate file
PointKernel Oct 18, 2024
84eead9
Add missing header
PointKernel Oct 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,12 @@ add_library(
src/filling/repeat.cu
src/filling/sequence.cu
src/groupby/groupby.cu
src/groupby/hash/compute_groupby.cu
src/groupby/hash/compute_single_pass_aggs.cu
src/groupby/hash/flatten_single_pass_aggs.cpp
src/groupby/hash/groupby.cu
src/groupby/hash/hash_compound_agg_finalizer.cu
src/groupby/hash/sparse_to_dense_results.cu
src/groupby/sort/aggregate.cpp
src/groupby/sort/group_argmax.cu
src/groupby/sort/group_argmin.cu
Expand Down
174 changes: 174 additions & 0 deletions cpp/src/groupby/hash/compute_groupby.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* Copyright (c) 2024, 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 "compute_groupby.cuh"
#include "compute_single_pass_aggs.hpp"
#include "helpers.cuh"
#include "sparse_to_dense_results.hpp"

#include <cudf/detail/aggregation/aggregation.cuh>
#include <cudf/detail/aggregation/result_cache.hpp>
#include <cudf/detail/cuco_helpers.hpp>
#include <cudf/detail/gather.hpp>
#include <cudf/groupby.hpp>
#include <cudf/null_mask.hpp>
#include <cudf/types.hpp>
#include <cudf/utilities/span.hpp>

#include <rmm/cuda_stream_view.hpp>
#include <rmm/device_uvector.hpp>
#include <rmm/mr/device/device_memory_resource.hpp>

#include <cuco/static_set.cuh>

#include <iterator>
#include <memory>

namespace cudf::groupby::detail::hash {
namespace {
/**
* @brief Computes and returns a device vector containing all populated keys in
* `map`.
*/
template <typename SetType>
rmm::device_uvector<size_type> extract_populated_keys(SetType const& key_set,
mhaseeb123 marked this conversation as resolved.
Show resolved Hide resolved
size_type num_keys,
rmm::cuda_stream_view stream)
{
rmm::device_uvector<size_type> populated_keys(num_keys, stream);
auto const keys_end = key_set.retrieve_all(populated_keys.begin(), stream.value());

populated_keys.resize(std::distance(populated_keys.begin(), keys_end), stream);
return populated_keys;
}

template rmm::device_uvector<size_type> extract_populated_keys<global_set_t>(
global_set_t const& key_set, size_type num_keys, rmm::cuda_stream_view stream);

template rmm::device_uvector<size_type> extract_populated_keys<nullable_global_set_t>(
nullable_global_set_t const& key_set, size_type num_keys, rmm::cuda_stream_view stream);
} // namespace

/**
* @brief Computes groupby using hash table.
*
* First, we create a hash table that stores the indices of unique rows in
* `keys`. The upper limit on the number of values in this map is the number
* of rows in `keys`.
*
* To store the results of aggregations, we create temporary sparse columns
* which have the same size as input value columns. Using the hash map, we
* determine the location within the sparse column to write the result of the
* aggregation into.
*
* The sparse column results of all aggregations are stored into the cache
* `sparse_results`. This enables the use of previously calculated results in
ttnghia marked this conversation as resolved.
Show resolved Hide resolved
* other aggregations.
*
* All the aggregations which can be computed in a single pass are computed
* first, in a combined kernel. Then using these results, aggregations that
* require multiple passes, will be computed.
*
* Finally, using the hash map, we generate a vector of indices of populated
* values in sparse result columns. Then, for each aggregation originally
* requested in `requests`, we gather sparse results into a column of dense
* results using the aforementioned index vector. Dense results are stored into
* the in/out parameter `cache`.
*/
template <typename Equal>
std::unique_ptr<table> compute_groupby(table_view const& keys,
host_span<aggregation_request const> requests,
bool skip_rows_with_nulls,
Equal const& d_row_equal,
row_hash_t const& d_row_hash,
cudf::detail::result_cache* cache,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr)
{
// convert to int64_t to avoid potential overflow with large `keys`
auto const num_keys = static_cast<int64_t>(keys.num_rows());

// Cache of sparse results where the location of aggregate value in each
// column is indexed by the hash set
cudf::detail::result_cache sparse_results(requests.size());

auto const set = cuco::static_set{
num_keys,
cudf::detail::CUCO_DESIRED_LOAD_FACTOR, // 50% load factor
cuco::empty_key{cudf::detail::CUDF_SIZE_TYPE_SENTINEL},
d_row_equal,
probing_scheme_t{d_row_hash},
cuco::thread_scope_device,
cuco::storage<GROUPBY_WINDOW_SIZE>{},
cudf::detail::cuco_allocator<char>{rmm::mr::polymorphic_allocator<char>{}, stream},
stream.value()};

auto row_bitmask =
ttnghia marked this conversation as resolved.
Show resolved Hide resolved
skip_rows_with_nulls
? cudf::bitmask_and(keys, stream, cudf::get_current_device_resource_ref()).first
: rmm::device_buffer{};

// Compute all single pass aggs first
compute_single_pass_aggs(num_keys,
skip_rows_with_nulls,
static_cast<bitmask_type*>(row_bitmask.data()),
set.ref(cuco::insert_and_find),
requests,
&sparse_results,
stream);

// Extract the populated indices from the hash set and create a gather map.
// Gathering using this map from sparse results will give dense results.
auto gather_map = extract_populated_keys(set, keys.num_rows(), stream);

// Compact all results from sparse_results and insert into cache
sparse_to_dense_results(requests,
&sparse_results,
cache,
gather_map,
set.ref(cuco::find),
static_cast<bitmask_type*>(row_bitmask.data()),
stream,
mr);

return cudf::detail::gather(keys,
gather_map,
out_of_bounds_policy::DONT_CHECK,
cudf::detail::negative_index_policy::NOT_ALLOWED,
stream,
mr);
}

template std::unique_ptr<table> compute_groupby<row_comparator_t>(
table_view const& keys,
host_span<aggregation_request const> requests,
bool skip_rows_with_nulls,
row_comparator_t const& d_row_equal,
row_hash_t const& d_row_hash,
cudf::detail::result_cache* cache,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr);

template std::unique_ptr<table> compute_groupby<nullable_row_comparator_t>(
table_view const& keys,
host_span<aggregation_request const> requests,
bool skip_rows_with_nulls,
nullable_row_comparator_t const& d_row_equal,
row_hash_t const& d_row_hash,
cudf::detail::result_cache* cache,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr);
} // namespace cudf::groupby::detail::hash
79 changes: 79 additions & 0 deletions cpp/src/groupby/hash/compute_groupby.cuh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (c) 2024, 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.
*/
#pragma once

#include "helpers.cuh"
PointKernel marked this conversation as resolved.
Show resolved Hide resolved

#include <cudf/detail/aggregation/result_cache.hpp>
#include <cudf/groupby.hpp>
#include <cudf/table/table_view.hpp>
#include <cudf/types.hpp>
#include <cudf/utilities/span.hpp>

#include <rmm/cuda_stream_view.hpp>
#include <rmm/mr/device/device_memory_resource.hpp>

#include <memory>

namespace cudf::groupby::detail::hash {
/**
* @brief Computes groupby using hash table.
*
* First, we create a hash table that stores the indices of unique rows in
* `keys`. The upper limit on the number of values in this map is the number
* of rows in `keys`.
*
* To store the results of aggregations, we create temporary sparse columns
* which have the same size as input value columns. Using the hash map, we
* determine the location within the sparse column to write the result of the
* aggregation into.
*
* The sparse column results of all aggregations are stored into the cache
* `sparse_results`. This enables the use of previously calculated results in
* other aggregations.
*
* All the aggregations which can be computed in a single pass are computed
* first, in a combined kernel. Then using these results, aggregations that
* require multiple passes, will be computed.
*
* Finally, using the hash map, we generate a vector of indices of populated
* values in sparse result columns. Then, for each aggregation originally
* requested in `requests`, we gather sparse results into a column of dense
* results using the aforementioned index vector. Dense results are stored into
* the in/out parameter `cache`.
*
* @tparam Equal Device row comparator type
*
* @param keys Table whose rows act as the groupby keys
* @param requests The set of columns to aggregate and the aggregations to perform
* @param skip_rows_with_nulls Flag indicating whether to ignore nulls or not
* @param d_row_equal Device row comparator
* @param d_row_hash Device row hasher
* @param cache Dense aggregation results
* @param stream CUDA stream used for device memory operations and kernel launches
* @param mr Device memory resource used to allocate the returned table
* @return Table of unique keys
*/
template <typename Equal>
std::unique_ptr<cudf::table> compute_groupby(table_view const& keys,
host_span<aggregation_request const> requests,
bool skip_rows_with_nulls,
Equal const& d_row_equal,
row_hash_t const& d_row_hash,
PointKernel marked this conversation as resolved.
Show resolved Hide resolved
cudf::detail::result_cache* cache,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr);
} // namespace cudf::groupby::detail::hash
Loading
Loading