5#ifndef XGBOOST_CONTEXT_H_
6#define XGBOOST_CONTEXT_H_
23 static auto constexpr CPU() {
return "cpu"; }
24 static auto constexpr CUDA() {
return "cuda"; }
32 enum Type : std::int16_t { kCPU = 0, kCUDA = 1 } device{kCPU};
36 [[nodiscard]]
bool IsCUDA()
const {
return device == kCUDA; }
37 [[nodiscard]]
bool IsCPU()
const {
return device == kCPU; }
50 [[nodiscard]]
constexpr static auto CPU() {
return DeviceOrd{kCPU, -1}; }
58 [[nodiscard]]
bool operator==(
DeviceOrd const& that)
const {
59 return device == that.device && ordinal == that.ordinal;
61 [[nodiscard]]
bool operator!=(DeviceOrd
const& that)
const {
return !(*
this == that); }
65 [[nodiscard]] std::string
Name()
const {
68 return DeviceSym::CPU();
69 case DeviceOrd::kCUDA:
70 return DeviceSym::CUDA() + (
':' + std::to_string(ordinal));
72 LOG(FATAL) <<
"Unknown device.";
79static_assert(
sizeof(DeviceOrd) ==
sizeof(std::int32_t));
86 std::string device{DeviceSym::CPU()};
95 static std::int64_t
constexpr kDefaultSeed = 0;
100 template <
typename Container>
101 Args UpdateAllowUnknown(Container
const& kwargs) {
103 this->SetDeviceOrdinal(kwargs);
107 std::int32_t gpu_id{kCpuId};
109 std::int32_t nthread{0};
111 std::int64_t seed{kDefaultSeed};
113 bool seed_per_iteration{
false};
115 bool fail_on_invalid_gpu_id{
false};
116 bool validate_parameters{
false};
129 [[nodiscard]] std::int32_t
Threads()
const;
153 [[nodiscard]] CUDAContext
const*
CUDACtx()
const;
174 template <
typename CPUFn,
typename CUDAFn>
176 static_assert(std::is_same_v<std::invoke_result_t<CPUFn>, std::invoke_result_t<CUDAFn>>);
177 switch (this->
Device().device) {
178 case DeviceOrd::kCPU:
180 case DeviceOrd::kCUDA:
185 LOG(FATAL) <<
"Unknown device type:"
186 <<
static_cast<std::underlying_type_t<DeviceOrd::Type>
>(this->
Device().device);
189 return std::invoke_result_t<CPUFn>();
193 DMLC_DECLARE_PARAMETER(
Context) {
194 DMLC_DECLARE_FIELD(seed)
195 .set_default(kDefaultSeed)
196 .describe(
"Random number seed during training.");
197 DMLC_DECLARE_ALIAS(seed, random_state);
198 DMLC_DECLARE_FIELD(seed_per_iteration)
200 .describe(
"Seed PRNG determnisticly via iterator number.");
201 DMLC_DECLARE_FIELD(device).set_default(DeviceSym::CPU()).describe(
"Device ordinal.");
202 DMLC_DECLARE_FIELD(nthread).set_default(0).describe(
"Number of threads to use.");
203 DMLC_DECLARE_ALIAS(nthread, n_jobs);
204 DMLC_DECLARE_FIELD(fail_on_invalid_gpu_id)
206 .describe(
"Fail with error when gpu_id is invalid.");
207 DMLC_DECLARE_FIELD(validate_parameters)
209 .describe(
"Enable checking whether parameters are used or not.");
213 void SetDeviceOrdinal(Args
const& kwargs);
214 Context& SetDevice(DeviceOrd d) {
216 this->gpu_id = d.ordinal;
217 this->device = d.Name();
224 mutable std::shared_ptr<CUDAContext> cuctx_;
226 std::int32_t cfs_cpu_count_;
Copyright 2015-2023 by XGBoost Contributors.
defines console logging options for xgboost. Use to enforce unified print behavior.
macro for using C++11 enum class as DMLC parameter
namespace of xgboost
Definition base.h:90
std::int16_t bst_d_ordinal_t
Ordinal of a CUDA device.
Definition base.h:130
Runtime context for XGBoost.
Definition context.h:84
decltype(auto) DispatchDevice(CPUFn &&cpu_fn, CUDAFn &&cuda_fn) const
Call function based on the current device.
Definition context.h:175
DeviceOrd Device() const
Get the current device and ordinal.
Definition context.h:141
std::string DeviceName() const
Name of the current device.
Definition context.h:149
void ConfigureGpuId(bool require_gpu)
Configure the parameter ‘gpu_id’.
Definition context.cc:159
std::int32_t Threads() const
Returns the automatically chosen number of threads based on the nthread parameter and the system sett...
Definition context.cc:203
Context MakeCUDA(bst_d_ordinal_t ordinal=0) const
Make a CUDA context based on the current context.
Definition context.h:160
bool IsCPU() const
Is XGBoost running on CPU?
Definition context.h:133
CUDAContext const * CUDACtx() const
Get a CUDA device context for allocator and stream.
Definition context.cc:212
bool IsCUDA() const
Is XGBoost running on a CUDA device?
Definition context.h:137
Context MakeCPU() const
Make a CPU context based on the current context.
Definition context.h:167
bst_d_ordinal_t Ordinal() const
Get the CUDA device ordinal.
Definition context.h:145
A type for device ordinal.
Definition context.h:31
std::string Name() const
Get a string representation of the device and the ordinal.
Definition context.h:65
static auto CUDA(bst_d_ordinal_t ordinal)
Constructor for CUDA device.
Definition context.h:56
static constexpr auto CPU()
Constructor for CPU.
Definition context.h:50
Definition parameter.h:84