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

Avoid extra loops on sorted data in the __subgroup_bubble_sorter::sort #1874

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,41 @@ struct __subgroup_bubble_sorter
void
sort(const _StorageAcc& __storage_acc, _Compare __comp, std::uint32_t __start, std::uint32_t __end) const
{
for (std::uint32_t i = __start; i < __end; ++i)
std::uint32_t __n = __end - __start;

switch (__n)
Copy link
Contributor

Choose a reason for hiding this comment

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

There are no other occurrences of switch in the library. What about using if-else? I guess switch is not necessary here due to having only 4 options.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe switch / case should works faster.

Copy link
Contributor

Choose a reason for hiding this comment

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

For large number of elements switch should perform better although I do not know in device code. However, with just 4 cases there's probably no difference.

In my opinion, unless there is a measurable performance difference between switch and if-else, then we should use if-else.

mmichel11 marked this conversation as resolved.
Show resolved Hide resolved
{
for (std::uint32_t j = __start + 1; j < __start + __end - i; ++j)
case 0: // The case when __end == __start no source data means no sorting required
case 1: // The case when __end == __start + 1 one source data item means no sorting required
break;
case 2: // The case when __end == __start + 2 two source data items required only one comparison (and swap) without any loops and etc.
{
auto& __first_item = __storage_acc[j - 1];
auto& __second_item = __storage_acc[j];
auto& __first_item = __storage_acc[__start];
auto& __second_item = __storage_acc[__start + 1];
if (__comp(__second_item, __first_item))
{
using std::swap;
swap(__first_item, __second_item);
}
}
break;
default: // The case when __end > __start + 2 three or more source data items require full bubble sort
do
Copy link
Contributor Author

Choose a reason for hiding this comment

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

{
std::uint32_t __new_n = 0;
for (std::uint32_t __i = 1; __i < __n; ++__i)
{
auto& __first_item = __storage_acc[__start + __i - 1];
auto& __second_item = __storage_acc[__start + __i];
if (__comp(__second_item, __first_item))
{
using std::swap;
swap(__first_item, __second_item);
__new_n = __i;
}
}
__n = __new_n;
} while (__n > 1);
}
}
};
Expand Down
Loading