6 #ifndef XGBOOST_COMMON_COMMON_H_ 7 #define XGBOOST_COMMON_COMMON_H_ 10 #include <xgboost/logging.h> 14 #include <type_traits> 19 #if defined(__CUDACC__) 20 #include <thrust/system/cuda/error.h> 21 #include <thrust/system_error.h> 23 #define WITH_CUDA() true 27 #define WITH_CUDA() false 29 #endif // defined(__CUDACC__) 32 #if defined(__CUDACC__) 36 #define safe_cuda(ans) ThrowOnCudaError((ans), __FILE__, __LINE__) 38 inline cudaError_t ThrowOnCudaError(cudaError_t code,
const char *file,
40 if (code != cudaSuccess) {
41 LOG(FATAL) << thrust::system_error(code, thrust::cuda_category(),
42 std::string{file} +
": " +
43 std::to_string(line)).what();
47 #endif // defined(__CUDACC__) 57 inline std::vector<std::string>
Split(
const std::string& s,
char delim) {
59 std::istringstream is(s);
60 std::vector<std::string> ret;
61 while (std::getline(is, item, delim)) {
70 std::ostringstream os;
98 return i_ >= other.
i_;
101 return i_ < other.
i_;
109 i_{start}, step_{step} {}
120 : begin_(begin), end_(end) {}
123 : begin_(begin, step), end_(end) {}
126 return *begin_ == *other.begin_ && *end_ == *other.end_;
129 return !(*
this == other);
141 static int AllVisible();
153 explicit GPUSet(
int start = 0,
int ndevices = 0)
154 : devices_(start, start + ndevices) {}
159 return n_gpus <= 0 ? Empty() :
GPUSet{start, n_gpus};
163 GpuIdType num_rows = std::numeric_limits<GpuIdType>::max()) {
164 CHECK_GE(gpu_id, 0) <<
"gpu_id must be >= 0.";
165 CHECK_GE(n_gpus, -1) <<
"n_gpus must be >= -1.";
168 if (n_devices_visible == 0 || n_gpus == 0) {
return Empty(); }
170 GpuIdType const n_available_devices = n_devices_visible - gpu_id;
172 if (n_gpus == kAll) {
173 CHECK(gpu_id < n_devices_visible)
174 <<
"\ngpu_id should be less than number of visible devices.\ngpu_id: " 176 <<
", number of visible devices: " 177 << n_devices_visible;
179 n_available_devices < num_rows ? n_available_devices : num_rows;
180 return Range(gpu_id, n_devices);
182 CHECK_LE(n_gpus, n_available_devices)
183 <<
"Starting from gpu id: " << gpu_id <<
", there are only " 184 << n_available_devices <<
" available devices, while n_gpus is set to: " 186 GpuIdType n_devices = n_gpus < num_rows ? n_gpus : num_rows;
187 return Range(gpu_id, n_devices);
192 GpuIdType n = AllVisibleImpl::AllVisible();
197 GpuIdType size = *devices_.end() - *devices_.begin();
199 return static_cast<size_t>(res);
213 CHECK(Contains(result)) <<
"\nDevice " << result <<
" is not in GPUSet." 214 <<
"\nIndex: " << index
215 <<
"\nGPUSet: (" << *begin() <<
", " << *end() <<
")" 220 CHECK(Contains(device)) <<
"\nDevice " << device <<
" is not in GPUSet." 221 <<
"\nGPUSet: (" << *begin() <<
", " << *end() <<
")" 223 size_t result =
static_cast<size_t>(device - *devices_.begin());
230 return *devices_.begin() <= device && device < *devices_.end();
237 return lhs.devices_ == rhs.devices_;
240 return !(lhs == rhs);
248 #endif // XGBOOST_COMMON_COMMON_H_ static GPUSet AllVisible()
Definition: common.h:191
GpuIdType DeviceId(size_t index) const
Definition: common.h:211
bool Contains(GpuIdType device) const
Definition: common.h:229
XGBOOST_DEVICE Iterator begin() const
Definition: common.h:116
friend bool operator!=(const GPUSet &lhs, const GPUSet &rhs)
Definition: common.h:239
XGBOOST_DEVICE Iterator(DifferenceType start)
Definition: common.h:107
XGBOOST_DEVICE bool operator!=(const Iterator &other) const
Definition: common.h:100
XGBOOST_DEVICE DifferenceType operator*() const
Definition: common.h:86
XGBOOST_DEVICE void Step(DifferenceType s)
Definition: common.h:132
XGBOOST_DEVICE Iterator operator++(int)
Definition: common.h:91
XGBOOST_DEVICE bool operator!=(const Range &other) const
Definition: common.h:128
GPUSet(int start=0, int ndevices=0)
Definition: common.h:153
int GpuIdType
Definition: common.h:150
int64_t DifferenceType
Definition: common.h:80
XGBOOST_DEVICE Iterator end() const
Definition: common.h:117
size_t Size() const
Definition: common.h:196
static GPUSet All(GpuIdType gpu_id, GpuIdType n_gpus, GpuIdType num_rows=std::numeric_limits< GpuIdType >::max())
n_gpus and num_rows both are upper bounds.
Definition: common.h:162
XGBOOST_DEVICE Iterator(DifferenceType start, DifferenceType step)
Definition: common.h:108
common::Range::Iterator end() const
Definition: common.h:234
std::vector< std::string > Split(const std::string &s, char delim)
Split a string by delimiter.
Definition: common.h:57
common::Range::Iterator begin() const
Definition: common.h:233
#define XGBOOST_DEVICE
Tag function as usable by device.
Definition: base.h:75
int64_t i_
Definition: common.h:112
namespace of xgboost
Definition: base.h:79
static GPUSet Range(GpuIdType start, GpuIdType n_gpus)
Definition: common.h:158
defines configuration macros of xgboost.
size_t Index(GpuIdType device) const
Definition: common.h:219
XGBOOST_DEVICE Range(DifferenceType begin, DifferenceType end)
Definition: common.h:119
XGBOOST_DEVICE bool operator==(const Iterator &other) const
Definition: common.h:97
XGBOOST_DEVICE const Iterator & operator++()
Definition: common.h:87
XGBOOST_DEVICE Range(DifferenceType begin, DifferenceType end, DifferenceType step)
Definition: common.h:121
XGBOOST_DEVICE bool operator==(const Range &other) const
Definition: common.h:125
friend bool operator==(const GPUSet &lhs, const GPUSet &rhs)
Definition: common.h:236
bool IsEmpty() const
Definition: common.h:227
std::string ToString(const T &data)
Definition: common.h:69
static GPUSet Empty()
Definition: common.h:156
XGBOOST_DEVICE void Step(DifferenceType s)
Definition: common.h:104