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

Expose streams in all public sorting APIs #14146

Merged
merged 7 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
28 changes: 24 additions & 4 deletions cpp/include/cudf/sorting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <cudf/aggregation.hpp>
#include <cudf/types.hpp>
#include <cudf/utilities/default_stream.hpp>

#include <rmm/mr/device/per_device_resource.hpp>

Expand All @@ -43,6 +44,7 @@ namespace cudf {
* @param null_precedence The desired order of null compared to other elements
* for each column. Size must be equal to `input.num_columns()` or empty.
* If empty, all columns will be sorted in `null_order::BEFORE`.
* @param stream CUDA stream used for device memory operations and kernel launches
* @param mr Device memory resource used to allocate the returned column's device memory
* @return A non-nullable column of elements containing the permuted row indices of
* `input` if it were sorted
Expand All @@ -51,6 +53,7 @@ std::unique_ptr<column> sorted_order(
table_view const& input,
std::vector<order> const& column_order = {},
std::vector<null_order> const& null_precedence = {},
rmm::cuda_stream_view stream = cudf::get_default_stream(),
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
Expand All @@ -65,27 +68,30 @@ std::unique_ptr<column> stable_sorted_order(
table_view const& input,
std::vector<order> const& column_order = {},
std::vector<null_order> const& null_precedence = {},
rmm::cuda_stream_view stream = cudf::get_default_stream(),
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @brief Checks whether the rows of a `table` are sorted in a lexicographical
* order.
*
* @param[in] table Table whose rows need to be compared for ordering
* @param[in] column_order The expected sort order for each column. Size
* @param table Table whose rows need to be compared for ordering
* @param column_order The expected sort order for each column. Size
* must be equal to `in.num_columns()` or empty. If
* empty, it is expected all columns are in
* ascending order.
vyasr marked this conversation as resolved.
Show resolved Hide resolved
* @param[in] null_precedence The desired order of null compared to other
* @param null_precedence The desired order of null compared to other
* elements for each column. Size must be equal to
* `input.num_columns()` or empty. If empty,
* `null_order::BEFORE` is assumed for all columns.
*
* @param stream CUDA stream used for device memory operations and kernel launches
* @returns bool true if sorted as expected, false if not
vyasr marked this conversation as resolved.
Show resolved Hide resolved
*/
bool is_sorted(cudf::table_view const& table,
std::vector<order> const& column_order,
std::vector<null_order> const& null_precedence);
std::vector<null_order> const& null_precedence,
rmm::cuda_stream_view stream = cudf::get_default_stream());

/**
* @brief Performs a lexicographic sort of the rows of a table
Expand All @@ -98,13 +104,15 @@ bool is_sorted(cudf::table_view const& table,
* elements for each column in `input`. Size must be equal to
* `input.num_columns()` or empty. If empty, all columns will be sorted with
* `null_order::BEFORE`.
* @param stream CUDA stream used for device memory operations and kernel launches
* @param mr Device memory resource used to allocate the returned table's device memory
* @return New table containing the desired sorted order of `input`
*/
std::unique_ptr<table> sort(
table_view const& input,
std::vector<order> const& column_order = {},
std::vector<null_order> const& null_precedence = {},
rmm::cuda_stream_view stream = cudf::get_default_stream(),
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
Expand All @@ -124,6 +132,7 @@ std::unique_ptr<table> sort(
* elements for each column in `keys`. Size must be equal to
* `keys.num_columns()` or empty. If empty, all columns will be sorted with
* `null_order::BEFORE`.
* @param stream CUDA stream used for device memory operations and kernel launches
* @param mr Device memory resource used to allocate the returned table's device memory
* @return The reordering of `values` determined by the lexicographic order of
* the rows of `keys`.
Expand All @@ -133,6 +142,7 @@ std::unique_ptr<table> sort_by_key(
table_view const& keys,
std::vector<order> const& column_order = {},
std::vector<null_order> const& null_precedence = {},
rmm::cuda_stream_view stream = cudf::get_default_stream(),
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
Expand All @@ -154,6 +164,7 @@ std::unique_ptr<table> sort_by_key(
* elements for each column in `keys`. Size must be equal to
* `keys.num_columns()` or empty. If empty, all columns will be sorted with
* `null_order::BEFORE`.
* @param stream CUDA stream used for device memory operations and kernel launches
* @param mr Device memory resource used to allocate the returned table's device memory
* @return The reordering of `values` determined by the lexicographic order of
* the rows of `keys`.
Expand All @@ -163,6 +174,7 @@ std::unique_ptr<table> stable_sort_by_key(
table_view const& keys,
std::vector<order> const& column_order = {},
std::vector<null_order> const& null_precedence = {},
rmm::cuda_stream_view stream = cudf::get_default_stream(),
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
Expand All @@ -189,6 +201,7 @@ std::unique_ptr<table> stable_sort_by_key(
* @param null_precedence The desired order of null compared to other elements
* for column
* @param percentage flag to convert ranks to percentage in range (0,1]
* @param stream CUDA stream used for device memory operations and kernel launches
* @param mr Device memory resource used to allocate the returned column's device memory
* @return A column of containing the rank of the each element of the column of `input`. The output
* column type will be `size_type`column by default or else `double` when
Expand All @@ -201,6 +214,7 @@ std::unique_ptr<column> rank(
null_policy null_handling,
null_order null_precedence,
bool percentage,
rmm::cuda_stream_view stream = cudf::get_default_stream(),
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
Expand Down Expand Up @@ -241,6 +255,7 @@ std::unique_ptr<column> rank(
* elements for each column in `keys`. Size must be equal to
* `keys.num_columns()` or empty. If empty, all columns will be sorted with
* `null_order::BEFORE`.
* @param stream CUDA stream used for device memory operations and kernel launches
* @param mr Device memory resource to allocate any returned objects
* @return sorted order of the segment sorted table
*
Expand All @@ -250,6 +265,7 @@ std::unique_ptr<column> segmented_sorted_order(
column_view const& segment_offsets,
std::vector<order> const& column_order = {},
std::vector<null_order> const& null_precedence = {},
rmm::cuda_stream_view stream = cudf::get_default_stream(),
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
Expand All @@ -262,6 +278,7 @@ std::unique_ptr<column> stable_segmented_sorted_order(
column_view const& segment_offsets,
std::vector<order> const& column_order = {},
std::vector<null_order> const& null_precedence = {},
rmm::cuda_stream_view stream = cudf::get_default_stream(),
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
Expand Down Expand Up @@ -306,6 +323,7 @@ std::unique_ptr<column> stable_segmented_sorted_order(
* elements for each column in `keys`. Size must be equal to
* `keys.num_columns()` or empty. If empty, all columns will be sorted with
* `null_order::BEFORE`.
* @param stream CUDA stream used for device memory operations and kernel launches
* @param mr Device memory resource to allocate any returned objects
* @return table with elements in each segment sorted
*
Expand All @@ -316,6 +334,7 @@ std::unique_ptr<table> segmented_sort_by_key(
column_view const& segment_offsets,
std::vector<order> const& column_order = {},
std::vector<null_order> const& null_precedence = {},
rmm::cuda_stream_view stream = cudf::get_default_stream(),
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
Expand All @@ -329,6 +348,7 @@ std::unique_ptr<table> stable_segmented_sort_by_key(
column_view const& segment_offsets,
std::vector<order> const& column_order = {},
std::vector<null_order> const& null_precedence = {},
rmm::cuda_stream_view stream = cudf::get_default_stream(),
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/** @} */ // end of group
Expand Down
30 changes: 15 additions & 15 deletions cpp/src/lists/segmented_sort.cu
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2022, NVIDIA CORPORATION.
* Copyright (c) 2021-2023, 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 @@ -70,13 +70,13 @@ std::unique_ptr<column> sort_lists(lists_column_view const& input,
auto output_offset = build_output_offsets(input, stream, mr);
auto const child = input.get_sliced_child(stream);

auto const sorted_child_table = segmented_sort_by_key(table_view{{child}},
table_view{{child}},
output_offset->view(),
{column_order},
{null_precedence},
stream,
mr);
auto const sorted_child_table = cudf::detail::segmented_sort_by_key(table_view{{child}},
table_view{{child}},
output_offset->view(),
{column_order},
{null_precedence},
stream,
mr);

return make_lists_column(input.size(),
std::move(output_offset),
Expand All @@ -98,13 +98,13 @@ std::unique_ptr<column> stable_sort_lists(lists_column_view const& input,
auto output_offset = build_output_offsets(input, stream, mr);
auto const child = input.get_sliced_child(stream);

auto const sorted_child_table = stable_segmented_sort_by_key(table_view{{child}},
table_view{{child}},
output_offset->view(),
{column_order},
{null_precedence},
stream,
mr);
auto const sorted_child_table = cudf::detail::stable_segmented_sort_by_key(table_view{{child}},
table_view{{child}},
output_offset->view(),
{column_order},
{null_precedence},
stream,
mr);

return make_lists_column(input.size(),
std::move(output_offset),
Expand Down
5 changes: 3 additions & 2 deletions cpp/src/sort/is_sorted.cu
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ bool is_sorted(cudf::table_view const& in,

bool is_sorted(cudf::table_view const& in,
std::vector<order> const& column_order,
std::vector<null_order> const& null_precedence)
std::vector<null_order> const& null_precedence,
rmm::cuda_stream_view stream)
{
CUDF_FUNC_RANGE();
if (in.num_columns() == 0 || in.num_rows() == 0) { return true; }
Expand All @@ -89,7 +90,7 @@ bool is_sorted(cudf::table_view const& in,
"Number of columns in the table doesn't match the vector null_precedence's size .\n");
}

return detail::is_sorted(in, column_order, null_precedence, cudf::get_default_stream());
return detail::is_sorted(in, column_order, null_precedence, stream);
}

} // namespace cudf
11 changes: 3 additions & 8 deletions cpp/src/sort/rank.cu
Original file line number Diff line number Diff line change
Expand Up @@ -366,16 +366,11 @@ std::unique_ptr<column> rank(column_view const& input,
null_policy null_handling,
null_order null_precedence,
bool percentage,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::rank(input,
method,
column_order,
null_handling,
null_precedence,
percentage,
cudf::get_default_stream(),
mr);
return detail::rank(
input, method, column_order, null_handling, null_precedence, percentage, stream, mr);
}
} // namespace cudf
8 changes: 5 additions & 3 deletions cpp/src/sort/segmented_sort.cu
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2022, NVIDIA CORPORATION.
* Copyright (c) 2021-2023, 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 @@ -81,23 +81,25 @@ std::unique_ptr<column> segmented_sorted_order(table_view const& keys,
column_view const& segment_offsets,
std::vector<order> const& column_order,
std::vector<null_order> const& null_precedence,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::segmented_sorted_order(
keys, segment_offsets, column_order, null_precedence, cudf::get_default_stream(), mr);
keys, segment_offsets, column_order, null_precedence, stream, mr);
}

std::unique_ptr<table> segmented_sort_by_key(table_view const& values,
table_view const& keys,
column_view const& segment_offsets,
std::vector<order> const& column_order,
std::vector<null_order> const& null_precedence,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::segmented_sort_by_key(
values, keys, segment_offsets, column_order, null_precedence, cudf::get_default_stream(), mr);
values, keys, segment_offsets, column_order, null_precedence, stream, mr);
}

} // namespace cudf
2 changes: 1 addition & 1 deletion cpp/src/sort/segmented_sort_impl.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ std::unique_ptr<column> fast_segmented_sorted_order(column_view const& input,
// Unfortunately, CUB's segmented sort functions cannot accept iterators.
// We have to build a pre-filled sequence of indices as input.
auto sorted_indices =
cudf::detail::sequence(input.size(), numeric_scalar<size_type>{0}, stream, mr);
cudf::detail::sequence(input.size(), numeric_scalar<size_type>{0, true, stream}, stream, mr);
auto indices_view = sorted_indices->mutable_view();

cudf::type_dispatcher<dispatch_storage_type>(input.type(),
Expand Down
10 changes: 6 additions & 4 deletions cpp/src/sort/sort.cu
Original file line number Diff line number Diff line change
Expand Up @@ -109,30 +109,32 @@ std::unique_ptr<table> sort(table_view const& input,
std::unique_ptr<column> sorted_order(table_view const& input,
std::vector<order> const& column_order,
std::vector<null_order> const& null_precedence,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::sorted_order(input, column_order, null_precedence, cudf::get_default_stream(), mr);
return detail::sorted_order(input, column_order, null_precedence, stream, mr);
}

std::unique_ptr<table> sort(table_view const& input,
std::vector<order> const& column_order,
std::vector<null_order> const& null_precedence,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::sort(input, column_order, null_precedence, cudf::get_default_stream(), mr);
return detail::sort(input, column_order, null_precedence, stream, mr);
}

std::unique_ptr<table> sort_by_key(table_view const& values,
table_view const& keys,
std::vector<order> const& column_order,
std::vector<null_order> const& null_precedence,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::sort_by_key(
values, keys, column_order, null_precedence, cudf::get_default_stream(), mr);
return detail::sort_by_key(values, keys, column_order, null_precedence, stream, mr);
}

} // namespace cudf
8 changes: 5 additions & 3 deletions cpp/src/sort/stable_segmented_sort.cu
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2022, NVIDIA CORPORATION.
* Copyright (c) 2021-2023, 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 @@ -55,23 +55,25 @@ std::unique_ptr<column> stable_segmented_sorted_order(
column_view const& segment_offsets,
std::vector<order> const& column_order,
std::vector<null_order> const& null_precedence,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::stable_segmented_sorted_order(
keys, segment_offsets, column_order, null_precedence, cudf::get_default_stream(), mr);
keys, segment_offsets, column_order, null_precedence, stream, mr);
}

std::unique_ptr<table> stable_segmented_sort_by_key(table_view const& values,
table_view const& keys,
column_view const& segment_offsets,
std::vector<order> const& column_order,
std::vector<null_order> const& null_precedence,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::stable_segmented_sort_by_key(
values, keys, segment_offsets, column_order, null_precedence, cudf::get_default_stream(), mr);
values, keys, segment_offsets, column_order, null_precedence, stream, mr);
}

} // namespace cudf
Loading