xgboost
common.h
Go to the documentation of this file.
1 
6 #ifndef XGBOOST_COMMON_COMMON_H_
7 #define XGBOOST_COMMON_COMMON_H_
8 
9 #include <xgboost/base.h>
10 #include <xgboost/logging.h>
11 
12 #include <exception>
13 #include <limits>
14 #include <type_traits>
15 #include <vector>
16 #include <string>
17 #include <sstream>
18 
19 #if defined(__CUDACC__)
20 #include <thrust/system/cuda/error.h>
21 #include <thrust/system_error.h>
22 
23 #define WITH_CUDA() true
24 
25 #else
26 
27 #define WITH_CUDA() false
28 
29 #endif // defined(__CUDACC__)
30 
31 namespace dh {
32 #if defined(__CUDACC__)
33 /*
34  * Error handling functions
35  */
36 #define safe_cuda(ans) ThrowOnCudaError((ans), __FILE__, __LINE__)
37 
38 inline cudaError_t ThrowOnCudaError(cudaError_t code, const char *file,
39  int line) {
40  if (code != cudaSuccess) {
41  LOG(FATAL) << thrust::system_error(code, thrust::cuda_category(),
42  std::string{file} + ": " + // NOLINT
43  std::to_string(line)).what();
44  }
45  return code;
46 }
47 #endif // defined(__CUDACC__)
48 } // namespace dh
49 
50 namespace xgboost {
51 namespace common {
57 inline std::vector<std::string> Split(const std::string& s, char delim) {
58  std::string item;
59  std::istringstream is(s);
60  std::vector<std::string> ret;
61  while (std::getline(is, item, delim)) {
62  ret.push_back(item);
63  }
64  return ret;
65 }
66 
67 template <typename T>
68 XGBOOST_DEVICE T Max(T a, T b) {
69  return a < b ? b : a;
70 }
71 
72 // simple routine to convert any data to string
73 template<typename T>
74 inline std::string ToString(const T& data) {
75  std::ostringstream os;
76  os << data;
77  return os.str();
78 }
79 
80 template <typename T1, typename T2>
81 XGBOOST_DEVICE T1 DivRoundUp(const T1 a, const T2 b) {
82  return static_cast<T1>(std::ceil(static_cast<double>(a) / b));
83 }
84 
85 /*
86  * Range iterator
87  */
88 class Range {
89  public:
90  using DifferenceType = int64_t;
91 
92  class Iterator {
93  friend class Range;
94 
95  public:
96  XGBOOST_DEVICE DifferenceType operator*() const { return i_; }
98  i_ += step_;
99  return *this;
100  }
102  Iterator res {*this};
103  i_ += step_;
104  return res;
105  }
106 
107  XGBOOST_DEVICE bool operator==(const Iterator &other) const {
108  return i_ >= other.i_;
109  }
110  XGBOOST_DEVICE bool operator!=(const Iterator &other) const {
111  return i_ < other.i_;
112  }
113 
114  XGBOOST_DEVICE void Step(DifferenceType s) { step_ = s; }
115 
116  protected:
117  XGBOOST_DEVICE explicit Iterator(DifferenceType start) : i_(start) {}
119  i_{start}, step_{step} {}
120 
121  private:
122  int64_t i_;
123  DifferenceType step_ = 1;
124  };
125 
126  XGBOOST_DEVICE Iterator begin() const { return begin_; } // NOLINT
127  XGBOOST_DEVICE Iterator end() const { return end_; } // NOLINT
128 
130  : begin_(begin), end_(end) {}
132  DifferenceType step)
133  : begin_(begin, step), end_(end) {}
134 
135  XGBOOST_DEVICE bool operator==(const Range& other) const {
136  return *begin_ == *other.begin_ && *end_ == *other.end_;
137  }
138  XGBOOST_DEVICE bool operator!=(const Range& other) const {
139  return !(*this == other);
140  }
141 
142  XGBOOST_DEVICE void Step(DifferenceType s) { begin_.Step(s); }
143 
144  private:
145  Iterator begin_;
146  Iterator end_;
147 };
148 
149 int AllVisibleGPUs();
150 
151 inline void AssertGPUSupport() {
152 #ifndef XGBOOST_USE_CUDA
153  LOG(FATAL) << "XGBoost version not compiled with GPU support.";
154 #endif // XGBOOST_USE_CUDA
155 }
156 
157 inline void AssertOneAPISupport() {
158 #ifndef XGBOOST_USE_ONEAPI
159  LOG(FATAL) << "XGBoost version not compiled with OneAPI support.";
160 #endif // XGBOOST_USE_ONEAPI
161 }
162 
163 } // namespace common
164 } // namespace xgboost
165 #endif // XGBOOST_COMMON_COMMON_H_
XGBOOST_DEVICE Iterator begin() const
Definition: common.h:126
XGBOOST_DEVICE Iterator(DifferenceType start)
Definition: common.h:117
XGBOOST_DEVICE bool operator!=(const Iterator &other) const
Definition: common.h:110
XGBOOST_DEVICE DifferenceType operator*() const
Definition: common.h:96
XGBOOST_DEVICE void Step(DifferenceType s)
Definition: common.h:142
XGBOOST_DEVICE Iterator operator++(int)
Definition: common.h:101
XGBOOST_DEVICE bool operator!=(const Range &other) const
Definition: common.h:138
Definition: common.h:88
int64_t DifferenceType
Definition: common.h:90
XGBOOST_DEVICE Iterator end() const
Definition: common.h:127
void AssertGPUSupport()
Definition: common.h:151
void AssertOneAPISupport()
Definition: common.h:157
XGBOOST_DEVICE Iterator(DifferenceType start, DifferenceType step)
Definition: common.h:118
XGBOOST_DEVICE T1 DivRoundUp(const T1 a, const T2 b)
Definition: common.h:81
std::vector< std::string > Split(const std::string &s, char delim)
Split a string by delimiter.
Definition: common.h:57
Definition: common.h:92
#define XGBOOST_DEVICE
Tag function as usable by device.
Definition: base.h:84
namespace of xgboost
Definition: base.h:102
defines configuration macros of xgboost.
XGBOOST_DEVICE Range(DifferenceType begin, DifferenceType end)
Definition: common.h:129
XGBOOST_DEVICE bool operator==(const Iterator &other) const
Definition: common.h:107
XGBOOST_DEVICE const Iterator & operator++()
Definition: common.h:97
XGBOOST_DEVICE Range(DifferenceType begin, DifferenceType end, DifferenceType step)
Definition: common.h:131
XGBOOST_DEVICE bool operator==(const Range &other) const
Definition: common.h:135
XGBOOST_DEVICE T Max(T a, T b)
Definition: common.h:68
std::string ToString(const T &data)
Definition: common.h:74
XGBOOST_DEVICE void Step(DifferenceType s)
Definition: common.h:114
Definition: common.h:31