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,39 @@ 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)
using _IndexType = std::make_signed_t<decltype(__end)>;

_IndexType __n = __end - __start;
dmitriy-sobolev marked this conversation as resolved.
Show resolved Hide resolved

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:
case 1:
break;
case 2:
dmitriy-sobolev marked this conversation as resolved.
Show resolved Hide resolved
{
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))
std::swap(__first_item, __second_item);
dmitriy-sobolev marked this conversation as resolved.
Show resolved Hide resolved
}
break;
default:
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.

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