Skip to content

Commit

Permalink
move PartitionBuilder to separate file; build sycl tests with icpx (#27)
Browse files Browse the repository at this point in the history
Co-authored-by: Dmitry Razdoburdin <>
  • Loading branch information
razdoburdin authored Dec 20, 2023
1 parent e2a664c commit ca540cf
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 119 deletions.
115 changes: 115 additions & 0 deletions plugin/sycl/common/partition_builder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*!
* Copyright 2017-2023 XGBoost contributors
*/
#ifndef PLUGIN_SYCL_COMMON_PARTITION_BUILDER_H_
#define PLUGIN_SYCL_COMMON_PARTITION_BUILDER_H_

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wtautological-constant-compare"
#pragma GCC diagnostic ignored "-W#pragma-messages"
#include <xgboost/data.h>
#pragma GCC diagnostic pop
#include <algorithm>
#include <vector>
#include <utility>

#include "../data.h"

#include <CL/sycl.hpp>

namespace xgboost {
namespace sycl {
namespace common {
// The builder is required for samples partition to left and rights children for set of nodes
class PartitionBuilder {
public:
static constexpr size_t maxLocalSums = 256;
static constexpr size_t subgroupSize = 16;


template<typename Func>
void Init(::sycl::queue* qu, size_t n_nodes, Func funcNTaks) {
nodes_offsets_.resize(n_nodes+1);
result_rows_.resize(2 * n_nodes);
n_nodes_ = n_nodes;


nodes_offsets_[0] = 0;
for (size_t i = 1; i < n_nodes+1; ++i) {
nodes_offsets_[i] = nodes_offsets_[i-1] + funcNTaks(i-1);
}


if (data_.Size() < nodes_offsets_[n_nodes]) {
data_.Resize(qu, nodes_offsets_[n_nodes]);
}
prefix_sums_.Resize(qu, maxLocalSums);
}


xgboost::common::Span<size_t> GetData(int nid) {
return { data_.Data() + nodes_offsets_[nid], nodes_offsets_[nid + 1] - nodes_offsets_[nid] };
}


xgboost::common::Span<size_t> GetPrefixSums() {
return { prefix_sums_.Data(), prefix_sums_.Size() };
}


size_t GetLocalSize(const xgboost::common::Range1d& range) {
size_t range_size = range.end() - range.begin();
size_t local_subgroups = range_size / (maxLocalSums * subgroupSize) +
!!(range_size % (maxLocalSums * subgroupSize));
return subgroupSize * local_subgroups;
}


size_t GetSubgroupSize() {
return subgroupSize;
}

size_t* GetResultRowsPtr() {
return result_rows_.data();
}


size_t GetNLeftElems(int nid) const {
// return result_left_rows_[nid];
return result_rows_[2 * nid];
}


size_t GetNRightElems(int nid) const {
// return result_right_rows_[nid];
return result_rows_[2 * nid + 1];
}


::sycl::event MergeToArray(::sycl::queue* qu, size_t node_in_set,
size_t* data_result,
::sycl::event priv_event) {
size_t n_nodes_total = GetNLeftElems(node_in_set) + GetNRightElems(node_in_set);
if (n_nodes_total > 0) {
const size_t* data = data_.Data() + nodes_offsets_[node_in_set];
return qu->memcpy(data_result, data, sizeof(size_t) * n_nodes_total, priv_event);
} else {
return ::sycl::event();
}
}

protected:
std::vector<size_t> nodes_offsets_;
std::vector<size_t> result_rows_;
size_t n_nodes_;

USMVector<size_t, MemoryType::on_device> data_;
USMVector<size_t> prefix_sums_;
};

} // namespace common
} // namespace sycl
} // namespace xgboost


#endif // PLUGIN_SYCL_COMMON_PARTITION_BUILDER_H_
115 changes: 0 additions & 115 deletions plugin/sycl/common/row_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,121 +120,6 @@ class RowSetCollection {
std::vector<Elem> elem_of_each_node_;
};


// The builder is required for samples partition to left and rights children for set of nodes
class PartitionBuilder {
public:
static constexpr size_t maxLocalSums = 256;
static constexpr size_t subgroupSize = 16;


template<typename Func>
void Init(::sycl::queue qu, size_t n_nodes, Func funcNTaks) {
qu_ = qu;
nodes_offsets_.resize(n_nodes+1);
result_rows_.resize(2 * n_nodes);
n_nodes_ = n_nodes;


nodes_offsets_[0] = 0;
for (size_t i = 1; i < n_nodes+1; ++i) {
nodes_offsets_[i] = nodes_offsets_[i-1] + funcNTaks(i-1);
}


if (data_.Size() < nodes_offsets_[n_nodes]) {
data_.Resize(&qu_, nodes_offsets_[n_nodes]);
}
prefix_sums_.Resize(&qu, maxLocalSums);
}


xgboost::common::Span<size_t> GetData(int nid) {
return { data_.Data() + nodes_offsets_[nid], nodes_offsets_[nid + 1] - nodes_offsets_[nid] };
}


xgboost::common::Span<size_t> GetPrefixSums() {
return { prefix_sums_.Data(), prefix_sums_.Size() };
}


size_t GetLocalSize(const xgboost::common::Range1d& range) {
size_t range_size = range.end() - range.begin();
size_t local_subgroups = range_size / (maxLocalSums * subgroupSize) +
!!(range_size % (maxLocalSums * subgroupSize));
return subgroupSize * local_subgroups;
}


size_t GetSubgroupSize() {
return subgroupSize;
}

// void SetNLeftElems(int nid, size_t n_left) {
// result_left_rows_[nid] = n_left;
// }


// void SetNRightElems(int nid, size_t n_right) {
// result_right_rows_[nid] = n_right;
// }

// ::sycl::event SetNLeftRightElems(::sycl::queue& qu, const USMVector<size_t,
// MemoryType::on_device>& parts_size,
// const std::vector<::sycl::event>& priv_events) {
// auto event = qu.submit([&](::sycl::handler& cgh) {
// cgh.depends_on(priv_events);
// cgh.parallel_for<>(::sycl::range<1>(n_nodes_), [=](::sycl::item<1> nid) {
// const size_t node_in_set = nid.get_id(0);
// result_left_rows_[node_in_set] = parts_size[2 * node_in_set];
// result_right_rows_[node_in_set] = parts_size[2 * node_in_set + 1];
// });
// });
// return event;
// }


size_t* GetResultRowsPtr() {
return result_rows_.data();
}


size_t GetNLeftElems(int nid) const {
// return result_left_rows_[nid];
return result_rows_[2 * nid];
}


size_t GetNRightElems(int nid) const {
// return result_right_rows_[nid];
return result_rows_[2 * nid + 1];
}


::sycl::event MergeToArray(::sycl::queue* qu, size_t node_in_set,
size_t* data_result,
::sycl::event priv_event) {
size_t n_nodes_total = GetNLeftElems(node_in_set) + GetNRightElems(node_in_set);
if (n_nodes_total > 0) {
const size_t* data = data_.Data() + nodes_offsets_[node_in_set];
return qu->memcpy(data_result, data, sizeof(size_t) * n_nodes_total, priv_event);
} else {
return ::sycl::event();
}
}

protected:
std::vector<size_t> nodes_offsets_;
std::vector<size_t> result_rows_;
size_t n_nodes_;

USMVector<size_t, MemoryType::on_device> data_;
USMVector<size_t> prefix_sums_;
::sycl::queue qu_;
};


} // namespace common
} // namespace sycl
} // namespace xgboost
Expand Down
2 changes: 1 addition & 1 deletion plugin/sycl/tree/updater_quantile_hist.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1363,7 +1363,7 @@ void QuantileHistMaker::Builder<GradientSumT>::ApplySplit(
std::vector<int32_t> split_conditions;
FindSplitConditions(nodes, *p_tree, gmat, &split_conditions);

partition_builder_.Init(qu_, n_nodes, [&](size_t node_in_set) {
partition_builder_.Init(&qu_, n_nodes, [&](size_t node_in_set) {
const int32_t nid = nodes[node_in_set].nid;
return row_set_collection_[nid].Size();
});
Expand Down
1 change: 1 addition & 0 deletions plugin/sycl/tree/updater_quantile_hist.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "../common/hist_util.h"
#include "../common/row_set.h"
#include "../common/partition_builder.h"
#include "split_evaluator.h"
#include "../device_manager.h"

Expand Down
36 changes: 33 additions & 3 deletions tests/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,39 @@ if (USE_CUDA)
endif (USE_CUDA)

file(GLOB_RECURSE SYCL_TEST_SOURCES "plugin/test_sycl_*.cc")
if (NOT PLUGIN_SYCL)
list(REMOVE_ITEM TEST_SOURCES ${SYCL_TEST_SOURCES})
endif (NOT PLUGIN_SYCL)
list(REMOVE_ITEM TEST_SOURCES ${SYCL_TEST_SOURCES})

if(PLUGIN_SYCL)
set(CMAKE_CXX_COMPILER "icpx")
file(GLOB_RECURSE SYCL_TEST_SOURCES "plugin/test_sycl_*.cc")
add_library(plugin_sycl_test OBJECT ${SYCL_TEST_SOURCES})

target_include_directories(plugin_sycl_test
PRIVATE
${gtest_SOURCE_DIR}/include
${xgboost_SOURCE_DIR}/include
${xgboost_SOURCE_DIR}/dmlc-core/include
${xgboost_SOURCE_DIR}/rabit/include)

target_compile_definitions(plugin_sycl_test PUBLIC -DXGBOOST_USE_SYCL=1)

target_link_libraries(plugin_sycl_test PUBLIC -fsycl)

set_target_properties(plugin_sycl_test PROPERTIES
COMPILE_FLAGS -fsycl
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
POSITION_INDEPENDENT_CODE ON)
if(USE_OPENMP)
find_package(OpenMP REQUIRED)
set_target_properties(plugin_sycl_test PROPERTIES
COMPILE_FLAGS "-fsycl -qopenmp")
endif()
# Get compilation and link flags of plugin_sycl and propagate to testxgboost
target_link_libraries(testxgboost PUBLIC plugin_sycl_test)
# Add all objects of plugin_sycl to testxgboost
target_sources(testxgboost INTERFACE $<TARGET_OBJECTS:plugin_sycl_test>)
endif()

if (PLUGIN_FEDERATED)
target_include_directories(testxgboost PRIVATE ${xgboost_SOURCE_DIR}/plugin/federated)
Expand Down
4 changes: 4 additions & 0 deletions tests/cpp/plugin/test_sycl_multiclass_obj.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
* Copyright 2018-2019 XGBoost contributors
*/
#include <gtest/gtest.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wtautological-constant-compare"
#pragma GCC diagnostic ignored "-W#pragma-messages"
#include <xgboost/context.h>
#pragma GCC diagnostic pop

#include "../objective/test_multiclass_obj.h"

Expand Down
8 changes: 8 additions & 0 deletions tests/cpp/plugin/test_sycl_predictor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@
* Copyright 2017-2020 XGBoost contributors
*/
#include <gtest/gtest.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wtautological-constant-compare"
#pragma GCC diagnostic ignored "-W#pragma-messages"
#include <xgboost/predictor.h>
#pragma GCC diagnostic pop

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wtautological-constant-compare"
#include "../../../src/data/adapter.h"
#include "../../../src/gbm/gbtree.h"
#pragma GCC diagnostic pop
#include "../../../src/data/proxy_dmatrix.h"
#include "../../../src/gbm/gbtree_model.h"
#include "../filesystem.h" // dmlc::TemporaryDirectory
Expand Down
4 changes: 4 additions & 0 deletions tests/cpp/plugin/test_sycl_regression_obj.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
* Copyright 2017-2019 XGBoost contributors
*/
#include <gtest/gtest.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wtautological-constant-compare"
#pragma GCC diagnostic ignored "-W#pragma-messages"
#include <xgboost/objective.h>
#pragma GCC diagnostic pop
#include <xgboost/context.h>

#include "../helpers.h"
Expand Down

0 comments on commit ca540cf

Please sign in to comment.