Skip to content

Commit

Permalink
Modified the cmake logic
Browse files Browse the repository at this point in the history
Signed-off-by: divya2108 <divya.kotadiya@fujitsu.com>
  • Loading branch information
divya2108 committed Oct 8, 2024
1 parent c8717c1 commit 8f5a9c6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 46 deletions.
13 changes: 6 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
endif()
endif()

include(${xgboost_SOURCE_DIR}/cmake/CheckSVEsupport.cmake)
check_xgboost_sve_support()
if(XGBOOST_COMPILER_HAS_ARM_SVE)
add_compile_definitions(XGBOOST_SVE_COMPILER_SUPPORT)
endif()

include(${xgboost_SOURCE_DIR}/cmake/PrefetchIntrinsics.cmake)
find_prefetch_intrinsics()
include(${xgboost_SOURCE_DIR}/cmake/Version.cmake)
Expand Down Expand Up @@ -348,13 +354,6 @@ target_include_directories(xgboost
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>)
#-- End shared library

include(${xgboost_SOURCE_DIR}/cmake/CheckSVEsupport.cmake)
check_xgboost_sve_support()
if(XGBOOST_ARM_SVE_HARDWARE_SUPPORT)
target_compile_definitions(objxgboost PUBLIC XGBOOST_SVE_SUPPORT_DETECTED)
target_compile_options(objxgboost PRIVATE ${XGBOOST_SVE_FLAGS})
endif()

#-- CLI for xgboost
if(BUILD_DEPRECATED_CLI)
add_executable(runxgboost ${xgboost_SOURCE_DIR}/src/cli_main.cc)
Expand Down
24 changes: 0 additions & 24 deletions cmake/CheckSVEsupport.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,6 @@ if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")

if(XGBOOST_COMPILER_HAS_ARM_SVE)
message(STATUS "ARM SVE compiler support detected")

# Check for hardware support
set(SOURCE_CODE "
#include <sys/prctl.h>
int main() {
int ret = prctl(PR_SVE_GET_VL);
return ret >= 0 ? 0 : 1;
}
")
file(WRITE ${CMAKE_BINARY_DIR}/check_sve_support.c "${SOURCE_CODE}")
try_run(RUN_RESULT COMPILE_RESULT
${CMAKE_BINARY_DIR}/check_sve_support_output
${CMAKE_BINARY_DIR}/check_sve_support.c
)

if(RUN_RESULT EQUAL 0)
message(STATUS "ARM SVE hardware support detected")
# Apply the SVE flags and definitions specifically to the xgboost target
set(XGBOOST_ARM_SVE_HARDWARE_SUPPORT TRUE PARENT_SCOPE)
set(XGBOOST_SVE_FLAGS "-march=armv8-a+sve" PARENT_SCOPE)
set(XGBOOST_SVE_DEFINITIONS "-DXGBOOST_SVE_SUPPORT_DETECTED" PARENT_SCOPE)
else()
message(STATUS "ARM SVE hardware support not detected")
endif()
else()
message(STATUS "ARM SVE compiler support not detected")
endif()
Expand Down
43 changes: 28 additions & 15 deletions src/common/hist_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
#include "xgboost/context.h" // for Context
#include "xgboost/data.h" // for SparsePage, SortedCSCPage

#ifdef XGBOOST_SVE_SUPPORT_DETECTED
#ifdef __linux__
#include <sys/prctl.h>
#endif

#ifdef XGBOOST_SVE_COMPILER_SUPPORT
#include <arm_sve.h> // to leverage sve intrinsics
#endif

Expand Down Expand Up @@ -183,8 +187,9 @@ class GHistBuildingManager {
}
};

#ifdef XGBOOST_SVE_SUPPORT_DETECTED
#ifdef XGBOOST_SVE_COMPILER_SUPPORT
template <typename BinIdxType>
__attribute__((target("arch=armv8-a+sve")))
inline void UpdateHistogramWithSVE(size_t row_size, const BinIdxType *gr_index_local,
const std::uint32_t *offsets, double *hist_data,
const float *p_gpair, size_t idx_gh, const uint32_t two,
Expand Down Expand Up @@ -241,6 +246,12 @@ inline void UpdateHistogramWithSVE(size_t row_size, const BinIdxType *gr_index_l
}
#endif

// Returns true if SVE ISA is available on the current CPU
bool check_sve_hw_support() {
int ret = prctl(PR_SVE_GET_VL);
return ret >= 0 ? 1 : 0;
}

template <bool do_prefetch, class BuildingManager>
void RowsWiseBuildHistKernel(Span<GradientPair const> gpair, Span<bst_idx_t const> row_indices,
const GHistIndexMatrix &gmat, GHistRow hist) {
Expand Down Expand Up @@ -302,20 +313,22 @@ void RowsWiseBuildHistKernel(Span<GradientPair const> gpair, Span<bst_idx_t cons
}
const BinIdxType *gr_index_local = gradient_index + icol_start;

#ifdef XGBOOST_SVE_SUPPORT_DETECTED
UpdateHistogramWithSVE(row_size, gr_index_local, offsets, hist_data, p_gpair, idx_gh, two,
kAnyMissing);
#else
// The trick with pgh_t buffer helps the compiler to generate faster binary.
const float pgh_t[] = {p_gpair[idx_gh], p_gpair[idx_gh + 1]};
for (size_t j = 0; j < row_size; ++j) {
const uint32_t idx_bin =
two * (static_cast<uint32_t>(gr_index_local[j]) + (kAnyMissing ? 0 : offsets[j]));
auto hist_local = hist_data + idx_bin;
*(hist_local) += pgh_t[0];
*(hist_local + 1) += pgh_t[1];
if (check_sve_hw_support()) {
#ifdef XGBOOST_SVE_COMPILER_SUPPORT
UpdateHistogramWithSVE(row_size, gr_index_local, offsets, hist_data, p_gpair, idx_gh, two,
kAnyMissing);
#endif
} else {
// The trick with pgh_t buffer helps the compiler to generate faster binary.
const float pgh_t[] = {p_gpair[idx_gh], p_gpair[idx_gh + 1]};
for (size_t j = 0; j < row_size; ++j) {
const uint32_t idx_bin =
two * (static_cast<uint32_t>(gr_index_local[j]) + (kAnyMissing ? 0 : offsets[j]));
auto hist_local = hist_data + idx_bin;
*(hist_local) += pgh_t[0];
*(hist_local + 1) += pgh_t[1];
}
}
#endif
}
}

Expand Down

0 comments on commit 8f5a9c6

Please sign in to comment.