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"; }
37 [[nodiscard]]
bool IsCPU()
const {
return device ==
kCPU; }
65 [[nodiscard]] std::string
Name()
const {
72 LOG(FATAL) <<
"Unknown device.";
79 static_assert(
sizeof(DeviceOrd) ==
sizeof(std::int32_t));
100 template <
typename Container>
103 this->SetDeviceOrdinal(kwargs);
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) {
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>();
194 DMLC_DECLARE_FIELD(
seed)
196 .describe(
"Random number seed during training.");
197 DMLC_DECLARE_ALIAS(
seed, random_state);
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);
206 .describe(
"Fail with error when gpu_id is invalid.");
209 .describe(
"Enable checking whether parameters are used or not.");
213 void SetDeviceOrdinal(
Args const& kwargs);
217 this->device = d.
Name();
224 mutable std::shared_ptr<CUDAContext> cuctx_;
226 std::int32_t cfs_cpu_count_;
Defines configuration macros and basic types for xgboost.
namespace of xgboost
Definition: base.h:90
std::vector< std::pair< std::string, std::string > > Args
Definition: base.h:316
std::int16_t bst_d_ordinal_t
Ordinal of a CUDA device.
Definition: base.h:130
macro for using C++11 enum class as DMLC parameter
Runtime context for XGBoost. Contains information like threads and device.
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
bool fail_on_invalid_gpu_id
Definition: context.h:115
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’.
bool seed_per_iteration
Definition: context.h:113
std::int32_t Threads() const
Returns the automatically chosen number of threads based on the nthread parameter and the system sett...
bool validate_parameters
Definition: context.h:116
std::int64_t seed
Definition: context.h:111
Context MakeCUDA(bst_d_ordinal_t ordinal=0) const
Make a CUDA context based on the current context.
Definition: context.h:160
static constexpr bst_d_ordinal_t InvalidOrdinal()
Definition: context.h:94
bool IsCPU() const
Is XGBoost running on CPU?
Definition: context.h:133
static constexpr bst_d_ordinal_t kCpuId
Definition: context.h:93
CUDAContext const * CUDACtx() const
Get a CUDA device context for allocator and stream.
std::int32_t gpu_id
Definition: context.h:107
static constexpr std::int64_t kDefaultSeed
Definition: context.h:95
bool IsCUDA() const
Is XGBoost running on a CUDA device?
Definition: context.h:137
std::int32_t nthread
Definition: context.h:109
Context MakeCPU() const
Make a CPU context based on the current context.
Definition: context.h:167
DMLC_DECLARE_PARAMETER(Context)
Definition: context.h:193
bst_d_ordinal_t Ordinal() const
Get the CUDA device ordinal. -1 if XGBoost is running on CPU.
Definition: context.h:145
Args UpdateAllowUnknown(Container const &kwargs)
Definition: context.h:101
A type for device ordinal. The type is packed into 32-bit for efficient use in viewing types like lin...
Definition: context.h:31
bool operator==(DeviceOrd const &that) const
Definition: context.h:58
bool IsCUDA() const
Definition: context.h:36
bool operator!=(DeviceOrd const &that) const
Definition: context.h:61
std::string Name() const
Get a string representation of the device and the ordinal.
Definition: context.h:65
DeviceOrd(DeviceOrd const &that)=default
enum xgboost::DeviceOrd::Type kCPU
DeviceOrd(DeviceOrd &&that)=default
DeviceOrd & operator=(DeviceOrd const &that)=default
Type
Definition: context.h:32
@ kCUDA
Definition: context.h:32
static auto CUDA(bst_d_ordinal_t ordinal)
Constructor for CUDA device.
Definition: context.h:56
bool IsCPU() const
Definition: context.h:37
DeviceOrd & operator=(DeviceOrd &&that)=default
constexpr DeviceOrd(Type type, bst_d_ordinal_t ord)
Definition: context.h:40
constexpr static auto CPU()
Constructor for CPU.
Definition: context.h:50
bst_d_ordinal_t ordinal
Definition: context.h:34
static constexpr auto CUDA()
Definition: context.h:24
static constexpr auto CPU()
Definition: context.h:23
Definition: parameter.h:84
Args UpdateAllowUnknown(Container const &kwargs)
Definition: parameter.h:90