Skip to content

Commit

Permalink
Update is_sorted to use experimental::row::lexicographic (#12752)
Browse files Browse the repository at this point in the history
This PR is a part of #11844.

Authors:
  - Divye Gala (https://github.com/divyegala)

Approvers:
  - Nghia Truong (https://github.com/ttnghia)
  - Yunsong Wang (https://github.com/PointKernel)

URL: #12752
  • Loading branch information
divyegala committed Feb 16, 2023
1 parent 506a479 commit 7190e33
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 26 deletions.
41 changes: 18 additions & 23 deletions cpp/src/sort/is_sorted.cu
@@ -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));

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
@@ -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

0 comments on commit 7190e33

Please sign in to comment.