| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#ifndef XGBOOST_CONTEXT_H_ |
| 6 |
#define XGBOOST_CONTEXT_H_ |
| 7 |
|
| 8 |
#include <xgboost/base.h> |
| 9 |
#include <xgboost/logging.h> |
| 10 |
#include <xgboost/parameter.h> |
| 11 |
|
| 12 |
#include <cstdint> |
| 13 |
#include <memory> |
| 14 |
#include <string> |
| 15 |
#include <type_traits> |
| 16 |
|
| 17 |
namespace xgboost { |
| 18 |
|
| 19 |
struct CUDAContext; |
| 20 |
|
| 21 |
|
| 22 |
struct DeviceSym { |
| 23 |
static auto constexpr CPU() { return "cpu"; } |
| 24 |
static auto constexpr CUDA() { return "cuda"; } |
| 25 |
static auto constexpr SyclDefault() { return "sycl"; } |
| 26 |
static auto constexpr SyclCPU() { return "sycl:cpu"; } |
| 27 |
static auto constexpr SyclGPU() { return "sycl:gpu"; } |
| 28 |
}; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
struct DeviceOrd { |
| 35 |
|
| 36 |
static bst_d_ordinal_t constexpr CPUOrdinal() { return -1; } |
| 37 |
static bst_d_ordinal_t constexpr InvalidOrdinal() { return -2; } |
| 38 |
|
| 39 |
enum Type : std::int16_t { kCPU = 0, kCUDA = 1, |
| 40 |
kSyclDefault = 2, kSyclCPU = 3, kSyclGPU = 4} device{kCPU}; |
| 41 |
|
| 42 |
bst_d_ordinal_t ordinal{CPUOrdinal()}; |
| 43 |
|
| 44 |
[[nodiscard]] bool IsCUDA() const { return device == kCUDA; } |
| 45 |
[[nodiscard]] bool IsCPU() const { return device == kCPU; } |
| 46 |
[[nodiscard]] bool IsSyclDefault() const { return device == kSyclDefault; } |
| 47 |
[[nodiscard]] bool IsSyclCPU() const { return device == kSyclCPU; } |
| 48 |
[[nodiscard]] bool IsSyclGPU() const { return device == kSyclGPU; } |
| 49 |
[[nodiscard]] bool IsSycl() const { return (IsSyclDefault() || |
| 50 |
IsSyclCPU() || |
| 51 |
IsSyclGPU()); } |
| 52 |
|
| 53 |
constexpr DeviceOrd() = default; |
| 54 |
constexpr DeviceOrd(Type type, bst_d_ordinal_t ord) : device{type}, ordinal{ord} {} |
| 55 |
|
| 56 |
constexpr DeviceOrd(DeviceOrd const& that) = default; |
| 57 |
constexpr DeviceOrd& operator=(DeviceOrd const& that) = default; |
| 58 |
constexpr DeviceOrd(DeviceOrd&& that) = default; |
| 59 |
constexpr DeviceOrd& operator=(DeviceOrd&& that) = default; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
[[nodiscard]] constexpr static auto CPU() { return DeviceOrd{kCPU, CPUOrdinal()}; } |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
[[nodiscard]] static constexpr auto CUDA(bst_d_ordinal_t ordinal) { |
| 71 |
return DeviceOrd{kCUDA, ordinal}; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
[[nodiscard]] constexpr static auto SyclDefault(bst_d_ordinal_t ordinal = -1) { |
| 79 |
return DeviceOrd{kSyclDefault, ordinal}; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
[[nodiscard]] constexpr static auto SyclCPU(bst_d_ordinal_t ordinal = -1) { |
| 87 |
return DeviceOrd{kSyclCPU, ordinal}; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
[[nodiscard]] constexpr static auto SyclGPU(bst_d_ordinal_t ordinal = -1) { |
| 96 |
return DeviceOrd{kSyclGPU, ordinal}; |
| 97 |
} |
| 98 |
|
| 99 |
[[nodiscard]] bool operator==(DeviceOrd const& that) const { |
| 100 |
return device == that.device && ordinal == that.ordinal; |
| 101 |
} |
| 102 |
[[nodiscard]] bool operator!=(DeviceOrd const& that) const { return !(*this == that); } |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
[[nodiscard]] std::string Name() const { |
| 107 |
switch (device) { |
| 108 |
case DeviceOrd::kCPU: |
| 109 |
return DeviceSym::CPU(); |
| 110 |
case DeviceOrd::kCUDA: |
| 111 |
return DeviceSym::CUDA() + (':' + std::to_string(ordinal)); |
| 112 |
case DeviceOrd::kSyclDefault: |
| 113 |
return DeviceSym::SyclDefault() + (':' + std::to_string(ordinal)); |
| 114 |
case DeviceOrd::kSyclCPU: |
| 115 |
return DeviceSym::SyclCPU() + (':' + std::to_string(ordinal)); |
| 116 |
case DeviceOrd::kSyclGPU: |
| 117 |
return DeviceSym::SyclGPU() + (':' + std::to_string(ordinal)); |
| 118 |
default: { |
| 119 |
LOG(FATAL) << "Unknown device."; |
| 120 |
return ""; |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
}; |
| 125 |
|
| 126 |
static_assert(sizeof(DeviceOrd) == sizeof(std::int32_t)); |
| 127 |
|
| 128 |
std::ostream& operator<<(std::ostream& os, DeviceOrd ord); |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
struct Context : public XGBoostParameter<Context> { |
| 134 |
private: |
| 135 |
|
| 136 |
std::string device{DeviceSym::CPU()}; |
| 137 |
|
| 138 |
DeviceOrd device_{DeviceOrd::CPU()}; |
| 139 |
|
| 140 |
public: |
| 141 |
static std::int64_t constexpr kDefaultSeed = 0; |
| 142 |
|
| 143 |
public: |
| 144 |
Context(); |
| 145 |
|
| 146 |
void Init(Args const& kwargs); |
| 147 |
|
| 148 |
template <typename Container> |
| 149 |
Args UpdateAllowUnknown(Container const& kwargs) { |
| 150 |
auto args = XGBoostParameter<Context>::UpdateAllowUnknown(kwargs); |
| 151 |
this->SetDeviceOrdinal(kwargs); |
| 152 |
return args; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
std::int32_t nthread{0}; |
| 157 |
|
| 158 |
std::int64_t seed{kDefaultSeed}; |
| 159 |
|
| 160 |
bool seed_per_iteration{false}; |
| 161 |
|
| 162 |
bool fail_on_invalid_gpu_id{false}; |
| 163 |
bool validate_parameters{false}; |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
[[nodiscard]] std::int32_t Threads() const; |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
[[nodiscard]] bool IsCPU() const { return Device().IsCPU(); } |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
[[nodiscard]] bool IsCUDA() const { return Device().IsCUDA(); } |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
[[nodiscard]] bool IsSyclDefault() const { return Device().IsSyclDefault(); } |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
[[nodiscard]] bool IsSyclCPU() const { return Device().IsSyclCPU(); } |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
[[nodiscard]] bool IsSyclGPU() const { return Device().IsSyclGPU(); } |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
[[nodiscard]] bool IsSycl() const { return IsSyclDefault() |
| 194 |
|| IsSyclCPU() |
| 195 |
|| IsSyclGPU(); } |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
[[nodiscard]] DeviceOrd Device() const { return device_; } |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
[[nodiscard]] DeviceOrd DeviceFP64() const; |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
[[nodiscard]] bst_d_ordinal_t Ordinal() const { return Device().ordinal; } |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
[[nodiscard]] std::string DeviceName() const { return Device().Name(); } |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
[[nodiscard]] CUDAContext const* CUDACtx() const; |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
[[nodiscard]] Context MakeCUDA(bst_d_ordinal_t ordinal = 0) const { |
| 227 |
Context ctx = *this; |
| 228 |
return ctx.SetDevice(DeviceOrd::CUDA(ordinal)); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
[[nodiscard]] Context MakeCPU() const { |
| 234 |
Context ctx = *this; |
| 235 |
return ctx.SetDevice(DeviceOrd::CPU()); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
template <typename CPUFn, typename CUDAFn> |
| 242 |
decltype(auto) DispatchDevice(CPUFn&& cpu_fn, CUDAFn&& cuda_fn) const { |
| 243 |
static_assert(std::is_same_v<std::invoke_result_t<CPUFn>, std::invoke_result_t<CUDAFn>>); |
| 244 |
switch (this->Device().device) { |
| 245 |
case DeviceOrd::kCPU: |
| 246 |
return cpu_fn(); |
| 247 |
case DeviceOrd::kCUDA: |
| 248 |
return cuda_fn(); |
| 249 |
default: |
| 250 |
|
| 251 |
|
| 252 |
if (this->Device().IsSycl()) { |
| 253 |
LOG(WARNING) << "The requested feature doesn't have SYCL specific implementation yet. " |
| 254 |
<< "CPU implementation is used"; |
| 255 |
return cpu_fn(); |
| 256 |
} else { |
| 257 |
LOG(FATAL) << "Unknown device type:" |
| 258 |
<< static_cast<std::underlying_type_t<DeviceOrd::Type>>(this->Device().device); |
| 259 |
break; |
| 260 |
} |
| 261 |
} |
| 262 |
return std::invoke_result_t<CPUFn>(); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
template <typename CPUFn, typename CUDAFn, typename SYCLFn> |
| 269 |
decltype(auto) DispatchDevice(CPUFn&& cpu_fn, CUDAFn&& cuda_fn, SYCLFn&& sycl_fn) const { |
| 270 |
static_assert(std::is_same_v<std::invoke_result_t<CPUFn>, std::invoke_result_t<SYCLFn>>); |
| 271 |
if (this->Device().IsSycl()) { |
| 272 |
return sycl_fn(); |
| 273 |
} else { |
| 274 |
return DispatchDevice(cpu_fn, cuda_fn); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
DMLC_DECLARE_PARAMETER(Context) { |
| 280 |
DMLC_DECLARE_FIELD(seed) |
| 281 |
.set_default(kDefaultSeed) |
| 282 |
.describe("Random number seed during training."); |
| 283 |
DMLC_DECLARE_ALIAS(seed, random_state); |
| 284 |
DMLC_DECLARE_FIELD(seed_per_iteration) |
| 285 |
.set_default(false) |
| 286 |
.describe("Seed PRNG determnisticly via iterator number."); |
| 287 |
DMLC_DECLARE_FIELD(device).set_default(DeviceSym::CPU()).describe("Device ordinal."); |
| 288 |
DMLC_DECLARE_FIELD(nthread).set_default(0).describe("Number of threads to use."); |
| 289 |
DMLC_DECLARE_ALIAS(nthread, n_jobs); |
| 290 |
DMLC_DECLARE_FIELD(fail_on_invalid_gpu_id) |
| 291 |
.set_default(false) |
| 292 |
.describe("Fail with error when gpu_id is invalid."); |
| 293 |
DMLC_DECLARE_FIELD(validate_parameters) |
| 294 |
.set_default(false) |
| 295 |
.describe("Enable checking whether parameters are used or not."); |
| 296 |
} |
| 297 |
|
| 298 |
private: |
| 299 |
void SetDeviceOrdinal(Args const& kwargs); |
| 300 |
Context& SetDevice(DeviceOrd d) { |
| 301 |
this->device = (this->device_ = d).Name(); |
| 302 |
return *this; |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
mutable std::shared_ptr<CUDAContext> cuctx_; |
| 309 |
|
| 310 |
std::int32_t cfs_cpu_count_; |
| 311 |
}; |
| 312 |
} |
| 313 |
|
| 314 |
#endif |