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

Extend and simplify API for calculation of range-based rolling window offsets #17807

Open
wants to merge 7 commits into
base: branch-25.04
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
Refactor to try and reduce/parallelise compile times
  • Loading branch information
wence- committed Jan 31, 2025
commit b4f1ba03b8ade286e701518ae3b233629c4aaa20
2 changes: 2 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,8 @@ add_library(
src/reshape/interleave_columns.cu
src/reshape/tile.cu
src/rolling/detail/optimized_unbounded_window.cpp
src/rolling/detail/range_following.cu
src/rolling/detail/range_preceding.cu
src/rolling/detail/rolling_collect_list.cu
src/rolling/detail/rolling_fixed_window.cu
src/rolling/detail/rolling_variable_window.cu
Expand Down
72 changes: 71 additions & 1 deletion cpp/include/cudf/detail/rolling.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2024, NVIDIA CORPORATION.
* Copyright (c) 2021-2025, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,6 +29,26 @@
namespace CUDF_EXPORT cudf {
namespace detail {

namespace rolling {
/**
* @brief Direction tag for a range-based rolling window.
*/
enum class direction : bool {
PRECEDING, ///< A preceding window.
FOLLOWING, ///< A following window.
};
/**
* @brief Wrapper for preprocessed group information from sorted group keys.
*/
struct preprocessed_group_info {
rmm::device_uvector<size_type> const& labels; ///< Mapping from row index to group label
rmm::device_uvector<size_type> const& offsets; ///< Mapping from group label to row offsets
rmm::device_uvector<size_type> const&
nulls_per_group; ///< Mapping from group label to null count in the group
};

} // namespace rolling

/**
* @copydoc std::unique_ptr<column> rolling_window(
* column_view const& input,
Expand All @@ -48,5 +68,55 @@ std::unique_ptr<column> rolling_window(column_view const& input,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr);

/**
* @brief Make a column representing the window offsets for a range-based window
*
* @tparam Direction Is this a preceding window or a following one.
*
* @param group_keys Table defining grouping of the windows. May be empty. If
* non-empty, group keys must be sorted.
* @param orderby Column use to define window ranges. If @p group_keys is empty,
* must be sorted. If
* @p group_keys is non-empty, must be sorted within each group. As well as
* being sorted, must be sorted consistently with the @p order and @p null_order
* parameters.
* @param order The sort order of the @p orderby column.
* @param null_order The sort order of nulls in the @p orderby column.
* @param row_delta Pointer to scalar providing the delta for the window range.
* May be null, but only if the @p window_type is @p CURRENT_ROW or @p
* UNBOUNDED. Note that @p row_delta is always added to the current row value.
* @param window_type The type of window we are computing bounds for.
* @param stream CUDA stream used for device memory operations and kernel
* launches.
* @param mr Device memory resource used for allocations.
*/
template <rolling::direction Direction>
[[nodiscard]] std::unique_ptr<column> make_range_window_bound(
column_view const& orderby,
std::optional<rolling::preprocessed_group_info> const& grouping,
order order,
null_order null_order,
range_window_type window,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr);

[[nodiscard]] std::unique_ptr<column> make_preceding_range_window_bound(
column_view const& orderby,
std::optional<rolling::preprocessed_group_info> const& grouping,
order order,
null_order null_order,
range_window_type window,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr);

[[nodiscard]] std::unique_ptr<column> make_following_range_window_bound(
column_view const& orderby,
std::optional<rolling::preprocessed_group_info> const& grouping,
order order,
null_order null_order,
range_window_type window,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr);

} // namespace detail
} // namespace CUDF_EXPORT cudf
47 changes: 47 additions & 0 deletions cpp/src/rolling/detail/range_following.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2025, 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 "range_utils.cuh"

#include <cudf/column/column.hpp>
#include <cudf/column/column_view.hpp>
#include <cudf/detail/rolling.hpp>
#include <cudf/rolling.hpp>
#include <cudf/types.hpp>

#include <rmm/cuda_stream_view.hpp>
#include <rmm/resource_ref.hpp>

#include <optional>

namespace CUDF_EXPORT cudf {
namespace detail {

[[nodiscard]] std::unique_ptr<column> make_following_range_window_bound(
column_view const& orderby,
std::optional<rolling::preprocessed_group_info> const& grouping,
order order,
null_order null_order,
range_window_type window,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr)
{
return make_range_window_bound<rolling::direction::FOLLOWING>(
orderby, grouping, order, null_order, window, stream, mr);
}

} // namespace detail
} // namespace CUDF_EXPORT cudf
47 changes: 47 additions & 0 deletions cpp/src/rolling/detail/range_preceding.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2025, 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 "range_utils.cuh"

#include <cudf/column/column.hpp>
#include <cudf/column/column_view.hpp>
#include <cudf/detail/rolling.hpp>
#include <cudf/rolling.hpp>
#include <cudf/types.hpp>

#include <rmm/cuda_stream_view.hpp>
#include <rmm/resource_ref.hpp>

#include <optional>

namespace CUDF_EXPORT cudf {
namespace detail {

[[nodiscard]] std::unique_ptr<column> make_preceding_range_window_bound(
column_view const& orderby,
std::optional<rolling::preprocessed_group_info> const& grouping,
order order,
null_order null_order,
range_window_type window,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr)
{
return make_range_window_bound<rolling::direction::PRECEDING>(
orderby, grouping, order, null_order, window, stream, mr);
}

} // namespace detail
} // namespace CUDF_EXPORT cudf
Loading