Skip to content

Commit

Permalink
Merge 7da04ab into c435525
Browse files Browse the repository at this point in the history
  • Loading branch information
Johan511 committed Aug 29, 2023
2 parents c435525 + 7da04ab commit 0c93a3b
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 0 deletions.
31 changes: 31 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,37 @@ hpx_option(
ADVANCED
)

hpx_option(
HPX_WITH_FETCH_SIMD_SORT
BOOL
"Use FetchContent to fetch x86-simd-sort. By default an installed x86-simd-sort will be used. (default: On). It has support only for AVX-512 registers. If the architecture has no AVX-512 registers, simd-sort won't be used"
ON
CATEGORY "Build Targets"
ADVANCED
)
hpx_option(
HPX_WITH_SIMD_SORT_TAG STRING "x86-simd-sort repository tag or branch" "v2.0"
CATEGORY "Build Targets"
ADVANCED
)

# simd sort supports only avx-512 registers
if(__AVX512F__)
set(HPX_WITH_SIMD_SORT ON)
if(HPX_WITH_FETCH_SIMD_SORT)
include(HPX_SetupSimdSort)
else()
find_package(SimdSort)
endif()
else()
set(HPX_WITH_SIMD_SORT OFF)
set(HPX_WITH_FETCH_SIMD_SORT OFF)
endif()

# debug message to be removed before merge
message(WARNING "HPX_WITH_SIMD_SORT : ${HPX_WITH_SIMD_SORT}")
message(WARNING "HPX_WITH_FETCH_SIMD_SORT : ${HPX_WITH_FETCH_SIMD_SORT}")

# ##############################################################################
# HPX CUDA configuration
# ##############################################################################
Expand Down
42 changes: 42 additions & 0 deletions cmake/FindSimdSort.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright (c) 2023 Hari Hara Naveen S
#
# SPDX-License-Identifier: BSL-1.0
# Distributed under the Boost Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

if(NOT TARGET SimdSort::simdsort)
find_path(SIMD_SORT_INCLUDE_DIR avx512-16bit-qsort.hpp
HINTS "${SIMD_SORT_ROOT}" ENV SIMD_SORT_ROOT
"${HPX_SIMD_SORT_ROOT}"
)

if(NOT SIMD_SORT_INCLUDE_DIR)
hpx_error("Simd Sort not found")
endif()

if(SIMD_SORT)
# The call to file is for compatibility with windows paths
file(TO_CMAKE_PATH ${SIMD_SORT} SIMD_SORT)
elseif("$ENV{SIMD_SORT}")
file(TO_CMAKE_PATH $ENV{SIMD_SORT} SIMD_SORT)
else()
file(TO_CMAKE_PATH "${SIMD_SORT_INCLUDE_DIR}" SIMD_SORT_INCLUDE_DIR)
string(REPLACE "/src" "" SIMD_SORT_ROOT "${SIMD_SORT_INCLUDE_DIR}")
endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
SimdSort
REQUIRED_VARS SIMD_SORT_INCLUDE_DIR
VERSION_VAR SIMD_SORT_VERSION_STRING
)

add_library(SimdSort::simdsort INTERFACE IMPORTED)
target_include_directories(
SimdSort::simdsort SYSTEM INTERFACE ${SIMD_SORT_INCLUDE_DIR}
)

mark_as_advanced(
SIMD_SORT_ROOT SIMD_SORT_INCLUDE_DIR SIMD_SORT_VERSION_STRING
)
endif()
67 changes: 67 additions & 0 deletions cmake/HPX_SetupSimdSort.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright (c) 2023 Hari Hara Naveen S
#
# SPDX-License-Identifier: BSL-1.0
# Distributed under the Boost Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

if(HPX_WITH_FETCH_SIMD_SORT)
if(FETCHCONTENT_SOURCE_DIR_SIMD_SORT)
hpx_info(
"HPX_WITH_FETCH_SIMD_SORT=${HPX_WITH_FETCH_SIMD_SORT}, x86-simd-sort will be used through CMake's FetchContent and installed alongside HPX (FETCHCONTENT_SOURCE_DIR_SIMD_SORT=${FETCHCONTENT_SOURCE_DIR_SIMD_SORT})"
)
else()
hpx_info(
"HPX_WITH_FETCH_SIMD_SORT=${HPX_WITH_FETCH_SIMD_SORT}, x86-simd-sort will be fetched using CMake's FetchContent and installed alongside HPX (HPX_WITH_SIMD_SORT_TAG=${HPX_WITH_SIMD_SORT_TAG})"
)
endif()

include(FetchContent)
fetchcontent_declare(
simdsort
GIT_REPOSITORY https://github.com/intel/x86-simd-sort
GIT_TAG ${HPX_WITH_SIMD_SORT_TAG}
)

fetchcontent_getproperties(simdsort)
if(NOT simdsort_POPULATED)
fetchcontent_populate(simdsort)
endif()
set(SIMD_SORT_ROOT ${simdsort_SOURCE_DIR})

add_library(simdsort INTERFACE)
target_include_directories(
simdsort SYSTEM INTERFACE $<BUILD_INTERFACE:${SIMD_SORT_ROOT}/src/>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

install(
TARGETS simdsort
EXPORT HPXSimdSortTarget
COMPONENT core
)

install(
DIRECTORY ${SIMD_SORT_ROOT}/src/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
COMPONENT core
FILES_MATCHING
PATTERN "*.hpp"
PATTERN "*.h"
)

export(
TARGETS simdsort
NAMESPACE SimdSort::
FILE "${CMAKE_CURRENT_BINARY_DIR}/lib/cmake/${HPX_PACKAGE_NAME}/HPXSimdSortTarget.cmake"
)

install(
EXPORT HPXSimdSortTarget
NAMESPACE SimdSort::
FILE HPXSimdSortTarget.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${HPX_PACKAGE_NAME}
)

add_library(SimdSort::simdsort ALIAS simdsort)

endif()
2 changes: 2 additions & 0 deletions cmake/templates/HPXConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ if("${HPX_WITH_DATAPAR_BACKEND}" STREQUAL "SVE")
endif()
endif()

include("${CMAKE_CURRENT_LIST_DIR}/HPXSimdSortTarget.cmake")

include("${CMAKE_CURRENT_LIST_DIR}/HPXInternalTargets.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/HPXTargets.cmake")

Expand Down
4 changes: 4 additions & 0 deletions libs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,10 @@ if("${HPX_WITH_DATAPAR_BACKEND}" STREQUAL "SVE")
target_link_libraries(hpx_core PUBLIC SVE::sve)
endif()

if(HPX_WITH_SIMD_SORT)
target_link_libraries(hpx_core PUBLIC SimdSort::simdsort)
endif()

if(HPX_WITH_ITTNOTIFY)
target_link_libraries(hpx_core PUBLIC Amplifier::amplifier)
endif()
Expand Down
2 changes: 2 additions & 0 deletions libs/core/algorithms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ set(algorithms_headers
hpx/parallel/algorithms/detail/dispatch.hpp
hpx/parallel/algorithms/detail/distance.hpp
hpx/parallel/algorithms/detail/equal.hpp
hpx/parallel/algorithms/detail/simd_sort.hpp
hpx/parallel/algorithms/detail/fill.hpp
hpx/parallel/algorithms/detail/find.hpp
hpx/parallel/algorithms/detail/generate.hpp
Expand All @@ -41,6 +42,7 @@ set(algorithms_headers
hpx/parallel/algorithms/detail/sample_sort.hpp
hpx/parallel/algorithms/detail/search.hpp
hpx/parallel/algorithms/detail/set_operation.hpp
hpx/parallel/algorithms/detail/simd_sort.hpp
hpx/parallel/algorithms/detail/spin_sort.hpp
hpx/parallel/algorithms/detail/transfer.hpp
hpx/parallel/algorithms/detail/upper_lower_bound.hpp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) 2023 Hari Hara Naveen S
//
// SPDX-License-Identifier: BSL-1.0
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <type_traits>

#pragma once

namespace hpx::parallel::util {
// TODO : add support for _Float16
// need compile time test as _Float16 is not always supported
template <class T>
struct is_simd_sortable
: std::integral_constant<bool,
std::is_same<uint16_t,
typename std::remove_volatile<T>::type>::value ||
std::is_same<int16_t,
typename std::remove_volatile<T>::type>::value ||
std::is_same<uint32_t,
typename std::remove_volatile<T>::type>::value ||
std::is_same<int32_t,
typename std::remove_volatile<T>::type>::value ||
std::is_same<float,
typename std::remove_volatile<T>::type>::value ||
std::is_same<uint64_t,
typename std::remove_volatile<T>::type>::value ||
std::is_same<int64_t,
typename std::remove_volatile<T>::type>::value ||
std::is_same<double,
typename std::remove_volatile<T>::type>::value>
{
};

template <class T>
constexpr bool is_simd_sortable_v = is_simd_sortable<T>::value;
} // namespace hpx::parallel::util

#if (HPX_WITH_SIMD_SORT)

namespace hpx::parallel::util {
template <typename T>
void simd_quicksort(T* arr, int64_t arrsize)
{
static_assert(hpx::parallel::util::is_simd_sortable_v<T>);
return avx512_qsort(arr, arrsize);
}
} // namespace hpx::parallel::util
#endif
17 changes: 17 additions & 0 deletions libs/core/algorithms/include/hpx/parallel/algorithms/sort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ namespace hpx {
#include <hpx/parallel/algorithms/detail/advance_to_sentinel.hpp>
#include <hpx/parallel/algorithms/detail/dispatch.hpp>
#include <hpx/parallel/algorithms/detail/is_sorted.hpp>
#include <hpx/parallel/algorithms/detail/simd_sort.hpp>
#include <hpx/parallel/algorithms/detail/pivot.hpp>
#include <hpx/parallel/util/compare_projected.hpp>
#include <hpx/parallel/util/detail/algorithm_result.hpp>
Expand All @@ -172,6 +173,7 @@ namespace hpx {
#include <exception>
#include <iterator>
#include <list>
#include <memory>
#include <type_traits>
#include <utility>

Expand Down Expand Up @@ -464,6 +466,21 @@ namespace hpx {
HPX_MOVE(comp), hpx::identity_v);
}
} sort{};

#if (HPX_WITH_SIMD_SORT)
template <typename RandomIt, typename Comp = hpx::parallel::detail::less,
HPX_CONCEPT_REQUIRES_(hpx::traits::is_random_iterator_v<RandomIt>&&
hpx::is_invocable_v<Comp, hpx::traits::iter_value_t<RandomIt>,
hpx::traits::iter_value_t<RandomIt>>)>
parallel::util::detail::algorithm_result_t<ExPolicy> tag_invoke(hpx::sort_t,
hpx::execution::unsequenced_policy, RandomIt first, RandomIt last,
Comp comp = Comp())
{
return hpx::parallel::util::simd_quicksort(
std::addressof(*first), std::distance(first, last));
}
#endif

} // namespace hpx

#endif // DOXYGEN

0 comments on commit 0c93a3b

Please sign in to comment.