From 57f9caf578b5617cdaf2ec16006bc7b83ce8f36c Mon Sep 17 00:00:00 2001 From: Sergey Kopienko Date: Tue, 24 Sep 2024 13:51:42 +0200 Subject: [PATCH] include/oneapi/dpl/pstl/hetero/dpcpp/parallel_backend_sycl_merge_sort.h - avoid loops and condition checks for small data sizes in __subgroup_bubble_sorter::sort Signed-off-by: Sergey Kopienko --- .../dpcpp/parallel_backend_sycl_merge_sort.h | 37 ++++++++++++++----- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/include/oneapi/dpl/pstl/hetero/dpcpp/parallel_backend_sycl_merge_sort.h b/include/oneapi/dpl/pstl/hetero/dpcpp/parallel_backend_sycl_merge_sort.h index bf249b882d..5c80362ba6 100644 --- a/include/oneapi/dpl/pstl/hetero/dpcpp/parallel_backend_sycl_merge_sort.h +++ b/include/oneapi/dpl/pstl/hetero/dpcpp/parallel_backend_sycl_merge_sort.h @@ -40,22 +40,39 @@ struct __subgroup_bubble_sorter void sort(const _StorageAcc& __storage_acc, _Compare __comp, std::uint32_t __start, std::uint32_t __end) const { - bool __changed = true; + using _IndexType = std::make_signed_t; - for (std::uint32_t i = __start; i < __end && __changed; ++i) - { - __changed = false; + _IndexType __n = __end - __start; - for (std::uint32_t j = __start + 1; j < __start + __end - i; ++j) + switch (__n) + { + case 0: + case 1: + break; + case 2: { - 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); - __changed = true; - } } + break; + default: + do + { + _IndexType __new_n = 0; + for (_IndexType __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)) + { + std::swap(__first_item, __second_item); + __new_n = __i; + } + } + __n = __new_n; + } while (__n > 1); } } };