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

Update is_sorted to use experimental::row::lexicographic #12752

Merged
Merged
Show file tree
Hide file tree
Changes from 6 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
41 changes: 18 additions & 23 deletions cpp/src/sort/is_sorted.cu
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2022, NVIDIA CORPORATION.
* Copyright (c) 2019-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 All @@ -17,7 +17,7 @@
#include <cudf/detail/nvtx/ranges.hpp>
#include <cudf/detail/structs/utilities.hpp>
#include <cudf/detail/utilities/vector_factories.hpp>
#include <cudf/table/row_operators.cuh>
#include <cudf/table/experimental/row_operators.cuh>
#include <cudf/table/table_device_view.cuh>
#include <cudf/table/table_view.hpp>
#include <cudf/types.hpp>
Expand All @@ -36,31 +36,27 @@ namespace detail {

auto is_sorted(cudf::table_view const& in,
std::vector<order> const& column_order,
bool has_nulls,
std::vector<null_order> const& null_precedence,
rmm::cuda_stream_view stream)
{
// 0-table_view, 1-column_order, 2-null_precedence, 3-validity_columns
auto flattened = structs::detail::flatten_nested_columns(in, column_order, null_precedence);
auto const comparator =
experimental::row::lexicographic::self_comparator{in, column_order, null_precedence, stream};

auto const d_input = table_device_view::create(flattened, stream);
auto const d_column_order = make_device_uvector_async(flattened.orders(), stream);
auto const d_null_precedence = has_nulls
? make_device_uvector_async(flattened.null_orders(), stream)
: rmm::device_uvector<null_order>(0, stream);
if (cudf::detail::has_nested_columns(in)) {
auto const device_comparator = comparator.less<true>(has_nested_nulls(in));
Comment on lines +45 to +46
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it's been discussed before, I feel it makes more sense to move this branching (whether has nested cols or not) into the comparator detail.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the way it was implemented for lexicographic::*_comparator, so I followed the convention. That said, there are some algorithms that dispatch different code-paths for nested and non-nested types already, so you want the granular level of control where those algorithms can select a particular version of the comparator

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood. It's definitely out of the scope of this PR. But if we always dispatch different code-paths, the fine control of those algorithms seems unnecessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. All of these if-statements scattered throughout the code seems like an anti-pattern we should try to factor out somehow. We should take a look once these are done. I'm also worried about compile time (surprise) and so that would be a reason to consider refactoring before these are complete perhaps.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer any refactor for design patterns to be a follow up to this #12593


auto comparator = row_lexicographic_comparator(nullate::DYNAMIC{has_nulls},
*d_input,
*d_input,
d_column_order.data(),
d_null_precedence.data());
return thrust::is_sorted(rmm::exec_policy(stream),
thrust::make_counting_iterator(0),
thrust::make_counting_iterator(in.num_rows()),
device_comparator);
} else {
auto const device_comparator = comparator.less<false>(has_nested_nulls(in));

auto sorted = thrust::is_sorted(rmm::exec_policy(stream),
thrust::make_counting_iterator(0),
thrust::make_counting_iterator(in.num_rows()),
comparator);

return sorted;
return thrust::is_sorted(rmm::exec_policy(stream),
thrust::make_counting_iterator(0),
thrust::make_counting_iterator(in.num_rows()),
device_comparator);
}
}

} // namespace detail
Expand All @@ -83,8 +79,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, has_nulls(in), null_precedence, cudf::get_default_stream());
return detail::is_sorted(in, column_order, null_precedence, cudf::get_default_stream());
}

} // namespace cudf
59 changes: 56 additions & 3 deletions cpp/tests/sort/is_sorted_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2022, NVIDIA CORPORATION.
* Copyright (c) 2019-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 All @@ -17,6 +17,7 @@
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_list_utilities.hpp>
#include <cudf_test/type_lists.hpp>

Expand Down Expand Up @@ -223,6 +224,58 @@ auto nulls_before<cudf::struct_view>()
return cudf::test::structs_column_wrapper{{col1, col2}, {0, 1}};
}

using lcw = cudf::test::lists_column_wrapper<int32_t>;
using cudf::test::iterators::null_at;
/*
List<List<List<int>
[
[[[0]], [[0]], [[0]]], 0
[[[0], [0], [0]]], 1
[[[0, 0]], [[0, 0, 0, 0, 0, 0, 0, 0]], [[0]]] 2
[[[0, 0, 0]]], 3
[[[0, 0, 0]], [[0]], [[0]]], 4
]
*/

template <typename T>
std::enable_if_t<std::is_same_v<T, cudf::list_view>, lcw> ascending()
{
return lcw{lcw{lcw{lcw{0}}, lcw{lcw{0}}, lcw{lcw{0}}},
lcw{lcw{lcw{0}, lcw{0}, lcw{0}}},
lcw{lcw{lcw{0, 0}}, lcw{lcw{0, 0, 0, 0, 0, 0, 0, 0}}, lcw{lcw{0}}},
lcw{lcw{lcw{0, 0, 0}}},
lcw{lcw{lcw{0, 0, 0}}, lcw{lcw{0}}, lcw{lcw{0}}}};
}

template <typename T>
std::enable_if_t<std::is_same_v<T, cudf::list_view>, lcw> descending()
{
return lcw{lcw{lcw{lcw{0, 0, 0}}, lcw{lcw{0}}, lcw{lcw{0}}},
lcw{lcw{lcw{0, 0, 0}}},
lcw{lcw{lcw{0, 0}}, lcw{lcw{0, 0, 0, 0, 0, 0, 0, 0}}, lcw{lcw{0}}},

lcw{lcw{lcw{0}, lcw{0}, lcw{0}}},
lcw{lcw{lcw{0}}, lcw{lcw{0}}, lcw{lcw{0}}}};
}

template <>
auto empty<cudf::list_view>()
{
return lcw{};
}

template <>
auto nulls_after<cudf::list_view>()
{
return lcw{{{1}, {2, 2}, {0}}, null_at(2)};
}

template <>
auto nulls_before<cudf::list_view>()
{
return lcw{{{0}, {1}, {2, 2}}, null_at(0)};
}

} // namespace testdata

// =============================================================================
Expand All @@ -232,8 +285,8 @@ template <typename T>
struct IsSortedTest : public cudf::test::BaseFixture {
};

using SupportedTypes =
cudf::test::Concat<cudf::test::ComparableTypes, cudf::test::Types<cudf::struct_view>>;
using SupportedTypes = cudf::test::
Concat<cudf::test::ComparableTypes, cudf::test::Types<cudf::struct_view>, cudf::test::ListTypes>;
TYPED_TEST_SUITE(IsSortedTest, SupportedTypes);

TYPED_TEST(IsSortedTest, NoColumns)
Expand Down