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