xgboost
base.h
Go to the documentation of this file.
1 
6 #ifndef XGBOOST_BASE_H_
7 #define XGBOOST_BASE_H_
8 
9 #include <dmlc/base.h>
10 #include <dmlc/omp.h>
11 #include <cmath>
12 #include <iostream>
13 #include <vector>
14 #include <string>
15 #include <utility>
16 
20 #ifndef XGBOOST_STRICT_R_MODE
21 #define XGBOOST_STRICT_R_MODE 0
22 #endif // XGBOOST_STRICT_R_MODE
23 
30 #ifndef XGBOOST_LOG_WITH_TIME
31 #define XGBOOST_LOG_WITH_TIME 1
32 #endif // XGBOOST_LOG_WITH_TIME
33 
37 #ifndef XGBOOST_CUSTOMIZE_LOGGER
38 #define XGBOOST_CUSTOMIZE_LOGGER XGBOOST_STRICT_R_MODE
39 #endif // XGBOOST_CUSTOMIZE_LOGGER
40 
44 #ifndef XGBOOST_CUSTOMIZE_GLOBAL_PRNG
45 #define XGBOOST_CUSTOMIZE_GLOBAL_PRNG XGBOOST_STRICT_R_MODE
46 #endif // XGBOOST_CUSTOMIZE_GLOBAL_PRNG
47 
51 #if defined(__GNUC__) && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || __GNUC__ > 4)
52 #define XGBOOST_ALIGNAS(X) alignas(X)
53 #else
54 #define XGBOOST_ALIGNAS(X)
55 #endif // defined(__GNUC__) && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || __GNUC__ > 4)
56 
57 #if defined(__GNUC__) && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || __GNUC__ > 4) && \
58  !defined(__CUDACC__) && !defined(__sun) && !defined(sun)
59 #include <parallel/algorithm>
60 #define XGBOOST_PARALLEL_SORT(X, Y, Z) __gnu_parallel::sort((X), (Y), (Z))
61 #define XGBOOST_PARALLEL_STABLE_SORT(X, Y, Z) \
62  __gnu_parallel::stable_sort((X), (Y), (Z))
63 #elif defined(_MSC_VER) && (!__INTEL_COMPILER)
64 #include <ppl.h>
65 #define XGBOOST_PARALLEL_SORT(X, Y, Z) concurrency::parallel_sort((X), (Y), (Z))
66 #define XGBOOST_PARALLEL_STABLE_SORT(X, Y, Z) std::stable_sort((X), (Y), (Z))
67 #else
68 #define XGBOOST_PARALLEL_SORT(X, Y, Z) std::sort((X), (Y), (Z))
69 #define XGBOOST_PARALLEL_STABLE_SORT(X, Y, Z) std::stable_sort((X), (Y), (Z))
70 #endif // GLIBC VERSION
71 
72 #if defined(__GNUC__)
73 #define XGBOOST_EXPECT(cond, ret) __builtin_expect((cond), (ret))
74 #else
75 #define XGBOOST_EXPECT(cond, ret) (cond)
76 #endif // defined(__GNUC__)
77 
81 #if defined (__CUDA__) || defined(__NVCC__)
82 #define XGBOOST_DEVICE __host__ __device__
83 #else
84 #define XGBOOST_DEVICE
85 #endif // defined (__CUDA__) || defined(__NVCC__)
86 
87 #if defined(__CUDA__) || defined(__CUDACC__)
88 #define XGBOOST_HOST_DEV_INLINE XGBOOST_DEVICE __forceinline__
89 #define XGBOOST_DEV_INLINE __device__ __forceinline__
90 #else
91 #define XGBOOST_HOST_DEV_INLINE
92 #define XGBOOST_DEV_INLINE
93 #endif // defined(__CUDA__) || defined(__CUDACC__)
94 
95 // These check are for Makefile.
96 #if !defined(XGBOOST_MM_PREFETCH_PRESENT) && !defined(XGBOOST_BUILTIN_PREFETCH_PRESENT)
97 /* default logic for software pre-fetching */
98 #if (defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64))) || defined(__INTEL_COMPILER)
99 // Enable _mm_prefetch for Intel compiler and MSVC+x86
100  #define XGBOOST_MM_PREFETCH_PRESENT
101  #define XGBOOST_BUILTIN_PREFETCH_PRESENT
102 #elif defined(__GNUC__)
103 // Enable __builtin_prefetch for GCC
104 #define XGBOOST_BUILTIN_PREFETCH_PRESENT
105 #endif // GUARDS
106 
107 #endif // !defined(XGBOOST_MM_PREFETCH_PRESENT) && !defined()
108 
110 namespace xgboost {
111 
113 using bst_uint = uint32_t; // NOLINT
115 using bst_int = int32_t; // NOLINT
117 using bst_ulong = uint64_t; // NOLINT
119 using bst_float = float; // NOLINT
121 using bst_cat_t = int32_t; // NOLINT
123 using bst_feature_t = uint32_t; // NOLINT
125 using bst_bin_t = int32_t; // NOLINT
132 using bst_row_t = std::size_t; // NOLINT
134 using bst_node_t = int32_t; // NOLINT
136 using bst_group_t = uint32_t; // NOLINT
137 
138 namespace detail {
142 template <typename T>
145  T grad_;
147  T hess_;
148 
149  XGBOOST_DEVICE void SetGrad(T g) { grad_ = g; }
150  XGBOOST_DEVICE void SetHess(T h) { hess_ = h; }
151 
152  public:
153  using ValueT = T;
154 
155  inline void Add(const ValueT& grad, const ValueT& hess) {
156  grad_ += grad;
157  hess_ += hess;
158  }
159 
160  inline static void Reduce(GradientPairInternal<T>& a, const GradientPairInternal<T>& b) { // NOLINT(*)
161  a += b;
162  }
163 
164  XGBOOST_DEVICE GradientPairInternal() : grad_(0), hess_(0) {}
165 
167  SetGrad(grad);
168  SetHess(hess);
169  }
170 
171  // Copy constructor if of same value type, marked as default to be trivially_copyable
173 
174  // Copy constructor if different value type - use getters and setters to
175  // perform conversion
176  template <typename T2>
178  SetGrad(g.GetGrad());
179  SetHess(g.GetHess());
180  }
181 
182  XGBOOST_DEVICE T GetGrad() const { return grad_; }
183  XGBOOST_DEVICE T GetHess() const { return hess_; }
184 
186  const GradientPairInternal<T> &rhs) {
187  grad_ += rhs.grad_;
188  hess_ += rhs.hess_;
189  return *this;
190  }
191 
193  const GradientPairInternal<T> &rhs) const {
195  g.grad_ = grad_ + rhs.grad_;
196  g.hess_ = hess_ + rhs.hess_;
197  return g;
198  }
199 
201  const GradientPairInternal<T> &rhs) {
202  grad_ -= rhs.grad_;
203  hess_ -= rhs.hess_;
204  return *this;
205  }
206 
208  const GradientPairInternal<T> &rhs) const {
210  g.grad_ = grad_ - rhs.grad_;
211  g.hess_ = hess_ - rhs.hess_;
212  return g;
213  }
214 
216  grad_ *= multiplier;
217  hess_ *= multiplier;
218  return *this;
219  }
220 
223  g.grad_ = grad_ * multiplier;
224  g.hess_ = hess_ * multiplier;
225  return g;
226  }
227 
229  grad_ /= divisor;
230  hess_ /= divisor;
231  return *this;
232  }
233 
236  g.grad_ = grad_ / divisor;
237  g.hess_ = hess_ / divisor;
238  return g;
239  }
240 
242  return grad_ == rhs.grad_ && hess_ == rhs.hess_;
243  }
244 
245  XGBOOST_DEVICE explicit GradientPairInternal(int value) {
246  *this = GradientPairInternal<T>(static_cast<float>(value),
247  static_cast<float>(value));
248  }
249 
250  friend std::ostream &operator<<(std::ostream &os,
251  const GradientPairInternal<T> &g) {
252  os << g.GetGrad() << "/" << g.GetHess();
253  return os;
254  }
255 };
256 } // namespace detail
257 
262 
266  using T = int64_t;
267  T grad_ = 0;
268  T hess_ = 0;
269 
270  public:
271  using ValueT = T;
272 
273  XGBOOST_DEVICE GradientPairInt64(T grad, T hess) : grad_(grad), hess_(hess) {}
274  GradientPairInt64() = default;
275 
276  // Copy constructor if of same value type, marked as default to be trivially_copyable
278 
279  XGBOOST_DEVICE T GetQuantisedGrad() const { return grad_; }
280  XGBOOST_DEVICE T GetQuantisedHess() const { return hess_; }
281 
283  grad_ += rhs.grad_;
284  hess_ += rhs.hess_;
285  return *this;
286  }
287 
290  g.grad_ = grad_ + rhs.grad_;
291  g.hess_ = hess_ + rhs.hess_;
292  return g;
293  }
294 
296  grad_ -= rhs.grad_;
297  hess_ -= rhs.hess_;
298  return *this;
299  }
300 
303  g.grad_ = grad_ - rhs.grad_;
304  g.hess_ = hess_ - rhs.hess_;
305  return g;
306  }
307 
308  XGBOOST_DEVICE bool operator==(const GradientPairInt64 &rhs) const {
309  return grad_ == rhs.grad_ && hess_ == rhs.hess_;
310  }
311  friend std::ostream &operator<<(std::ostream &os,
312  const GradientPairInt64 &g) {
313  os << g.GetQuantisedGrad() << "/" << g.GetQuantisedHess();
314  return os;
315  }
316 };
317 
318 using Args = std::vector<std::pair<std::string, std::string> >;
319 
321 constexpr bst_float kRtEps = 1e-6f;
322 
324 using omp_ulong = dmlc::omp_ulong; // NOLINT
326 using bst_omp_uint = dmlc::omp_uint; // NOLINT
328 using XGBoostVersionT = int32_t;
329 
334 #if DMLC_USE_CXX11 && defined(__GNUC__) && !defined(__clang_version__)
335 #if __GNUC__ == 4 && __GNUC_MINOR__ < 8
336 #define override
337 #define final
338 #endif // __GNUC__ == 4 && __GNUC_MINOR__ < 8
339 #endif // DMLC_USE_CXX11 && defined(__GNUC__) && !defined(__clang_version__)
340 } // namespace xgboost
341 
342 #endif // XGBOOST_BASE_H_
#define XGBOOST_DEVICE
Tag function as usable by device.
Definition: base.h:84
Fixed point representation for high precision gradient pair. Has a different interface so we don't ac...
Definition: base.h:265
T ValueT
Definition: base.h:271
GradientPairInt64(const GradientPairInt64 &g)=default
XGBOOST_DEVICE GradientPairInt64 operator+(const GradientPairInt64 &rhs) const
Definition: base.h:288
XGBOOST_DEVICE bool operator==(const GradientPairInt64 &rhs) const
Definition: base.h:308
XGBOOST_DEVICE T GetQuantisedHess() const
Definition: base.h:280
XGBOOST_DEVICE GradientPairInt64 & operator-=(const GradientPairInt64 &rhs)
Definition: base.h:295
friend std::ostream & operator<<(std::ostream &os, const GradientPairInt64 &g)
Definition: base.h:311
XGBOOST_DEVICE GradientPairInt64 & operator+=(const GradientPairInt64 &rhs)
Definition: base.h:282
XGBOOST_DEVICE T GetQuantisedGrad() const
Definition: base.h:279
XGBOOST_DEVICE GradientPairInt64(T grad, T hess)
Definition: base.h:273
XGBOOST_DEVICE GradientPairInt64 operator-(const GradientPairInt64 &rhs) const
Definition: base.h:301
Implementation of gradient statistics pair. Template specialisation may be used to overload different...
Definition: base.h:143
GradientPairInternal(const GradientPairInternal< T > &g)=default
XGBOOST_DEVICE GradientPairInternal< T > operator*(float multiplier) const
Definition: base.h:221
XGBOOST_DEVICE GradientPairInternal< T > & operator+=(const GradientPairInternal< T > &rhs)
Definition: base.h:185
void Add(const ValueT &grad, const ValueT &hess)
Definition: base.h:155
T ValueT
Definition: base.h:153
friend std::ostream & operator<<(std::ostream &os, const GradientPairInternal< T > &g)
Definition: base.h:250
XGBOOST_DEVICE GradientPairInternal(T grad, T hess)
Definition: base.h:166
XGBOOST_DEVICE GradientPairInternal< T > & operator*=(float multiplier)
Definition: base.h:215
XGBOOST_DEVICE GradientPairInternal()
Definition: base.h:164
XGBOOST_DEVICE GradientPairInternal(int value)
Definition: base.h:245
XGBOOST_DEVICE GradientPairInternal< T > operator+(const GradientPairInternal< T > &rhs) const
Definition: base.h:192
XGBOOST_DEVICE GradientPairInternal< T > & operator/=(float divisor)
Definition: base.h:228
XGBOOST_DEVICE GradientPairInternal< T > operator/(float divisor) const
Definition: base.h:234
XGBOOST_DEVICE T GetHess() const
Definition: base.h:183
XGBOOST_DEVICE GradientPairInternal< T > & operator-=(const GradientPairInternal< T > &rhs)
Definition: base.h:200
XGBOOST_DEVICE GradientPairInternal< T > operator-(const GradientPairInternal< T > &rhs) const
Definition: base.h:207
static void Reduce(GradientPairInternal< T > &a, const GradientPairInternal< T > &b)
Definition: base.h:160
XGBOOST_DEVICE T GetGrad() const
Definition: base.h:182
XGBOOST_DEVICE GradientPairInternal(const GradientPairInternal< T2 > &g)
Definition: base.h:177
XGBOOST_DEVICE bool operator==(const GradientPairInternal< T > &rhs) const
Definition: base.h:241
namespace of xgboost
Definition: base.h:110
int32_t XGBoostVersionT
Type used for representing version number in binary form.
Definition: base.h:328
std::vector< std::pair< std::string, std::string > > Args
Definition: base.h:318
uint32_t bst_feature_t
Type for data column (feature) index.
Definition: base.h:123
dmlc::omp_ulong omp_ulong
define unsigned long for openmp loop
Definition: base.h:324
dmlc::omp_uint bst_omp_uint
define unsigned int for openmp loop
Definition: base.h:326
int32_t bst_int
integer type.
Definition: base.h:115
uint64_t bst_ulong
unsigned long integers
Definition: base.h:117
std::size_t bst_row_t
Type for data row index.
Definition: base.h:132
uint32_t bst_uint
unsigned integer type used for feature index.
Definition: base.h:113
int32_t bst_cat_t
Categorical value type.
Definition: base.h:121
int32_t bst_node_t
Type for tree node index.
Definition: base.h:134
uint32_t bst_group_t
Type for ranking group index.
Definition: base.h:136
int32_t bst_bin_t
Type for histogram bin index.
Definition: base.h:125
float bst_float
float type, used for storing statistics
Definition: base.h:119
constexpr bst_float kRtEps
small eps gap for minimum split decision.
Definition: base.h:321