5 #ifndef XGBOOST_CONTEXT_H_
6 #define XGBOOST_CONTEXT_H_
9 #include <xgboost/logging.h>
15 #include <type_traits>
23 static auto constexpr
CPU() {
return "cpu"; }
24 static auto constexpr
CUDA() {
return "cuda"; }
26 static auto constexpr
SyclCPU() {
return "sycl:cpu"; }
27 static auto constexpr
SyclGPU() {
return "sycl:gpu"; }
45 [[nodiscard]]
bool IsCPU()
const {
return device ==
kCPU; }
106 [[nodiscard]] std::string
Name()
const {
119 LOG(FATAL) <<
"未知设备。";
126 static_assert(
sizeof(DeviceOrd) ==
sizeof(std::int32_t));
148 template <
typename Container>
151 this->SetDeviceOrdinal(kwargs);
220 [[nodiscard]] CUDAContext
const*
CUDACtx()
const;
242 template <
typename CPUFn,
typename CUDAFn>
244 static_assert(std::is_same_v<std::invoke_result_t<CPUFn>, std::invoke_result_t<CUDAFn>>);
245 switch (this->
Device().device) {
254 LOG(WARNING) <<
"请求的功能目前还没有 SYCL 特定实现。"
258 LOG(FATAL) <<
"未知设备类型:"
259 <<
static_cast<std::underlying_type_t<DeviceOrd::Type>
>(this->
Device().device);
263 return std::invoke_result_t<CPUFn>();
269 template <
typename CPUFn,
typename CUDAFn,
typename SYCLFn>
270 decltype(
auto)
DispatchDevice(CPUFn&& cpu_fn, CUDAFn&& cuda_fn, SYCLFn&& sycl_fn)
const {
271 static_assert(std::is_same_v<std::invoke_result_t<CPUFn>, std::invoke_result_t<SYCLFn>>);
281 DMLC_DECLARE_FIELD(
seed)
283 .describe(
"训练期间的随机数种子。");
284 DMLC_DECLARE_ALIAS(
seed, random_state);
287 .describe(
"通过迭代器编号确定性地为 PRNG 设置种子。");
288 DMLC_DECLARE_FIELD(device).set_default(
DeviceSym::CPU()).describe(
"设备序号。");
289 DMLC_DECLARE_FIELD(
nthread).set_default(0).describe(
"要使用的线程数。");
290 DMLC_DECLARE_ALIAS(
nthread, n_jobs);
293 .describe(
"当 gpu_id 无效时报错并失败。");
296 .describe(
"启用检查参数是否被使用。");
300 void SetDeviceOrdinal(
Args const& kwargs);
302 this->device = (this->device_ = d).Name();
309 mutable std::shared_ptr<CUDAContext> cuctx_;
311 std::int32_t cfs_cpu_count_;
多目标树的核心数据结构。
定义: base.h:89
std::vector< std::pair< std::string, std::string > > Args
定义: base.h:316
std::ostream & operator<<(std::ostream &os, DeviceOrd ord)
std::int16_t bst_d_ordinal_t
CUDA 设备的序号。
定义: base.h:131
使用 C++11 enum class 作为 DMLC 参数的宏
XGBoost 的运行时上下文。包含线程和设备等信息。
定义: context.h:133
decltype(auto) DispatchDevice(CPUFn &&cpu_fn, CUDAFn &&cuda_fn) const
根据当前设备调用函数。
定义: context.h:243
bool fail_on_invalid_gpu_id
定义: context.h:162
DeviceOrd Device() const
获取当前设备和序号。
定义: context.h:208
bool IsSycl() const
XGBoost 是否在任何 SYCL 设备上运行?
定义: context.h:201
std::string DeviceName() const
当前设备的名称。
定义: context.h:216
void ConfigureGpuId(bool require_gpu)
配置参数“device”。已弃用,gpu_id 移除后将删除。
bool seed_per_iteration
定义: context.h:160
std::int32_t Threads() const
根据 nthread 参数和系统设置返回自动选择的线程数。
bool validate_parameters
定义: context.h:163
std::int64_t seed
定义: context.h:158
Context MakeCUDA(bst_d_ordinal_t ordinal=0) const
根据当前上下文创建一个 CUDA 上下文。
定义: context.h:227
bool IsCPU() const
XGBoost 是否在 CPU 上运行?
定义: context.h:181
CUDAContext const * CUDACtx() const
获取用于分配器和流的 CUDA 设备上下文。
static constexpr std::int64_t kDefaultSeed
定义: context.h:141
bool IsCUDA() const
XGBoost 是否在 CUDA 设备上运行?
定义: context.h:185
bool IsSyclCPU() const
XGBoost 是否在 SYCL CPU 上运行?
定义: context.h:193
std::int32_t nthread
定义: context.h:156
Context MakeCPU() const
根据当前上下文创建一个 CPU 上下文。
定义: context.h:234
bool IsSyclGPU() const
XGBoost 是否在 SYCL GPU 上运行?
定义: context.h:197
bool IsSyclDefault() const
XGBoost 是否在默认 SYCL 设备上运行?
定义: context.h:189
DMLC_DECLARE_PARAMETER(Context)
定义: context.h:280
bst_d_ordinal_t Ordinal() const
获取 CUDA 设备序号。如果在 CPU 上运行则为 -1。
定义: context.h:212
void Init(Args const &kwargs)
Args UpdateAllowUnknown(Container const &kwargs)
定义: context.h:149
一种用于设备序号的类型。该类型被打包成 32 位,以便在诸如行之类的查看类型中高效使用。
定义: context.h:34
constexpr static auto SyclGPU(bst_d_ordinal_t ordinal=-1)
SYCL GPU 的构造函数。
定义: context.h:95
bool operator==(DeviceOrd const &that) const
定义: context.h:99
bool IsCUDA() const
定义: context.h:44
bool operator!=(DeviceOrd const &that) const
定义: context.h:102
bool IsSyclCPU() const
定义: context.h:47
std::string Name() const
获取设备和序号的字符串表示。
定义: context.h:106
enum xgboost::DeviceOrd::Type kCPU
bool IsSyclDefault() const
定义: context.h:46
constexpr DeviceOrd & operator=(DeviceOrd &&that)=default
constexpr DeviceOrd(DeviceOrd &&that)=default
constexpr DeviceOrd & operator=(DeviceOrd const &that)=default
@ kSyclGPU
定义: context.h:40
@ kSyclDefault
定义: context.h:40
@ kSyclCPU
定义: context.h:40
bool IsSyclGPU() const
定义: context.h:48
bool IsCPU() const
定义: context.h:45
static constexpr bst_d_ordinal_t InvalidOrdinal()
定义: context.h:37
static constexpr bst_d_ordinal_t CPUOrdinal()
定义: context.h:36
constexpr DeviceOrd(Type type, bst_d_ordinal_t ord)
定义: context.h:54
constexpr static auto SyclCPU(bst_d_ordinal_t ordinal=-1)
SYCL CPU 的构造函数。
定义: context.h:86
constexpr DeviceOrd()=default
bool IsSycl() const
定义: context.h:49
static constexpr auto CUDA(bst_d_ordinal_t ordinal)
CUDA 设备的构造函数。
定义: context.h:70
constexpr DeviceOrd(DeviceOrd const &that)=default
constexpr static auto CPU()
CPU 的构造函数。
定义: context.h:64
constexpr static auto SyclDefault(bst_d_ordinal_t ordinal=-1)
SYCL 的构造函数。
定义: context.h:78
bst_d_ordinal_t ordinal
定义: context.h:42
static constexpr auto CUDA()
定义: context.h:24
static constexpr auto CPU()
定义: context.h:23
static constexpr auto SyclDefault()
定义: context.h:25
static constexpr auto SyclGPU()
定义: context.h:27
static constexpr auto SyclCPU()
定义: context.h:26
Args UpdateAllowUnknown(Container const &kwargs)
定义: parameter.h:90