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