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

sorting an empty view should exit early and not fail #6130

Merged
merged 10 commits into from
May 16, 2023

Conversation

fnrizzi
Copy link
Contributor

@fnrizzi fnrizzi commented May 12, 2023

sort

The current impl of sort does not handle nor test the special case of an empty view.
Passing an empty view to sort should not fail.
Currently, the code fails because if we look here:

sort(const ExecutionSpace& exec,
const Kokkos::View<DataType, Properties...>& view) {
using ViewType = Kokkos::View<DataType, Properties...>;
using CompType = BinOp1D<ViewType>;
Kokkos::MinMaxScalar<typename ViewType::non_const_value_type> result;
Kokkos::MinMax<typename ViewType::non_const_value_type> reducer(result);
parallel_reduce("Kokkos::Sort::FindExtent",
Kokkos::RangePolicy<typename ViewType::execution_space>(
exec, 0, view.extent(0)),
Impl::min_max_functor<ViewType>(view), reducer);
if (result.min_val == result.max_val) return;
// For integral types the number of bins may be larger than the range
// in which case we can exactly have one unique value per bin
// and then don't need to sort bins.
bool sort_in_bins = true;
// TODO: figure out better max_bins then this ...
int64_t max_bins = view.extent(0) / 2;
if (std::is_integral<typename ViewType::non_const_value_type>::value) {
// Cast to double to avoid possible overflow when using integer
auto const max_val = static_cast<double>(result.max_val);
auto const min_val = static_cast<double>(result.min_val);
// using 10M as the cutoff for special behavior (roughly 40MB for the count
// array)
if ((max_val - min_val) < 10000000) {
max_bins = max_val - min_val + 1;
sort_in_bins = false;
}
}
if (std::is_floating_point<typename ViewType::non_const_value_type>::value) {
KOKKOS_ASSERT(std::isfinite(static_cast<double>(result.max_val) -
static_cast<double>(result.min_val)));
}

when view is empty, the reduction does not happen and so result contains the reduction_identity values.
Since those values are not equal, the following if is not satisfied:

if (result.min_val == result.max_val) return;

and the code fails because it hits this:

if (std::is_floating_point<typename ViewType::non_const_value_type>::value) {
    KOKKOS_ASSERT(std::isfinite(static_cast<double>(result.max_val) -
                                static_cast<double>(result.min_val)));
  }

BinSort

For BinSort there is a similar problem here:

// Sort a subset of a view with respect to the first dimension using the
// permutation array
template <class ExecutionSpace, class ValuesViewType>
void sort(const ExecutionSpace& exec, ValuesViewType const& values,
int values_range_begin, int values_range_end) const {

Comments

Note that an empty view is not a precondition violation.
Also, the std lib behaves similarly: if we try to call std::sort on an empty vector the code does not crash.

When merging, need to be careful with #6081

@fnrizzi fnrizzi changed the title sorting an empty view should not fail sorting an empty view should exit early and not fail May 12, 2023
@fnrizzi fnrizzi added the Bug Broken / incorrect code; it could be Kokkos' responsibility, or others’ (e.g., Trilinos) label May 12, 2023
Copy link
Contributor

@masterleinad masterleinad left a comment

Choose a reason for hiding this comment

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

What about BinSort::sort?

@fnrizzi
Copy link
Contributor Author

fnrizzi commented May 12, 2023

What about BinSort::sort?

yes that too, i am working on it... have not pushed yet. I was unsure whether to make a separate PR just for that. I will jsut add it here

@fnrizzi fnrizzi changed the title sorting an empty view should exit early and not fail calling sort or BinSort::sort on an empty view should exit early and not fail May 12, 2023
@fnrizzi fnrizzi marked this pull request as draft May 12, 2023 20:23
@fnrizzi fnrizzi marked this pull request as ready for review May 12, 2023 20:32
@fnrizzi fnrizzi added this to In progress in Developer: FRANCESCO RIZZI May 12, 2023
@fnrizzi fnrizzi added this to the Release 4.1 milestone May 12, 2023
@fnrizzi fnrizzi marked this pull request as draft May 12, 2023 20:57
@fnrizzi fnrizzi marked this pull request as ready for review May 12, 2023 21:43
algorithms/unit_tests/TestSort.hpp Outdated Show resolved Hide resolved
algorithms/unit_tests/TestSort.hpp Outdated Show resolved Hide resolved
algorithms/unit_tests/TestSort.hpp Outdated Show resolved Hide resolved
algorithms/src/Kokkos_Sort.hpp Show resolved Hide resolved
algorithms/src/Kokkos_Sort.hpp Show resolved Hide resolved
algorithms/unit_tests/TestSort.hpp Outdated Show resolved Hide resolved
algorithms/unit_tests/TestSort.hpp Outdated Show resolved Hide resolved
@fnrizzi fnrizzi changed the title calling sort or BinSort::sort on an empty view should exit early and not fail calling sort or BinSort::sort or BinSort::create_permute_vector on an empty view should exit early and not fail May 15, 2023
@fnrizzi fnrizzi changed the title calling sort or BinSort::sort or BinSort::create_permute_vector on an empty view should exit early and not fail sorting an empty view should exit early and not fail May 15, 2023
@dalg24
Copy link
Member

dalg24 commented May 16, 2023

What did you change?

@fnrizzi
Copy link
Contributor Author

fnrizzi commented May 16, 2023

What did you change?

change what? I only rebased on develop

@dalg24
Copy link
Member

dalg24 commented May 16, 2023

What did you change?

change what? I only rebased on develop

Why bother? Anyway too late. I plan on merging after the CI completes

@fnrizzi
Copy link
Contributor Author

fnrizzi commented May 16, 2023

What did you change?

change what? I only rebased on develop

Why bother? Anyway too late. I plan on merging after the CI completes

habit of keeping things updated so that no surprises come up after merging (maybe in this case does not matter, but in general does)

@dalg24
Copy link
Member

dalg24 commented May 16, 2023

So you know we test your PR merged into develop unless there is conflict in which we can't merge anyway.

@fnrizzi
Copy link
Contributor Author

fnrizzi commented May 16, 2023

So you know we test your PR merged into develop unless there is conflict in which we can't merge anyway.

Now I know :) thanks!

@dalg24 dalg24 merged commit b86d73a into kokkos:develop May 16, 2023
28 checks passed
@nmm0 nmm0 mentioned this pull request Jun 16, 2023
nliber pushed a commit to nliber/kokkos that referenced this pull request Jun 22, 2023
* sorting an empty view should not fail

* fix binsort too

* fix unused

* fix test

* add comment

* address review comments

* add fences

* address comments

* address comments

* remove unused
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Broken / incorrect code; it could be Kokkos' responsibility, or others’ (e.g., Trilinos) Kokkos-Algorithms
Projects
Development

Successfully merging this pull request may close these issues.

None yet

4 participants