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 
17 #ifndef XGBOOST_STRICT_R_MODE
18 #define XGBOOST_STRICT_R_MODE 0
19 #endif // XGBOOST_STRICT_R_MODE
20 
27 #ifndef XGBOOST_LOG_WITH_TIME
28 #define XGBOOST_LOG_WITH_TIME 1
29 #endif // XGBOOST_LOG_WITH_TIME
30 
34 #ifndef XGBOOST_CUSTOMIZE_LOGGER
35 #define XGBOOST_CUSTOMIZE_LOGGER XGBOOST_STRICT_R_MODE
36 #endif // XGBOOST_CUSTOMIZE_LOGGER
37 
41 #ifndef XGBOOST_CUSTOMIZE_GLOBAL_PRNG
42 #define XGBOOST_CUSTOMIZE_GLOBAL_PRNG XGBOOST_STRICT_R_MODE
43 #endif // XGBOOST_CUSTOMIZE_GLOBAL_PRNG
44 
48 #if defined(__GNUC__) && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || __GNUC__ > 4)
49 #define XGBOOST_ALIGNAS(X) alignas(X)
50 #else
51 #define XGBOOST_ALIGNAS(X)
52 #endif // defined(__GNUC__) && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || __GNUC__ > 4)
53 
54 #if defined(__GNUC__) && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || __GNUC__ > 4) && \
55  !defined(__CUDACC__)
56 #include <parallel/algorithm>
57 #define XGBOOST_PARALLEL_SORT(X, Y, Z) __gnu_parallel::sort((X), (Y), (Z))
58 #define XGBOOST_PARALLEL_STABLE_SORT(X, Y, Z) \
59  __gnu_parallel::stable_sort((X), (Y), (Z))
60 #elif defined(_MSC_VER) && (!__INTEL_COMPILER)
61 #include <ppl.h>
62 #define XGBOOST_PARALLEL_SORT(X, Y, Z) concurrency::parallel_sort((X), (Y), (Z))
63 #define XGBOOST_PARALLEL_STABLE_SORT(X, Y, Z) std::stable_sort((X), (Y), (Z))
64 #else
65 #define XGBOOST_PARALLEL_SORT(X, Y, Z) std::sort((X), (Y), (Z))
66 #define XGBOOST_PARALLEL_STABLE_SORT(X, Y, Z) std::stable_sort((X), (Y), (Z))
67 #endif // GLIBC VERSION
68 
72 #if defined (__CUDA__) || defined(__NVCC__)
73 #define XGBOOST_DEVICE __host__ __device__
74 #else
75 #define XGBOOST_DEVICE
76 #endif // defined (__CUDA__) || defined(__NVCC__)
77 
79 namespace xgboost {
84 using bst_uint = uint32_t; // NOLINT
85 using bst_int = int32_t; // NOLINT
87 typedef uint64_t bst_ulong; // NOLINT(*)
89 using bst_float = float; // NOLINT
90 
91 namespace detail {
95 template <typename T>
98  T grad_;
100  T hess_;
101 
102  XGBOOST_DEVICE void SetGrad(float g) { grad_ = g; }
103  XGBOOST_DEVICE void SetHess(float h) { hess_ = h; }
104 
105  public:
106  using ValueT = T;
107 
108  XGBOOST_DEVICE GradientPairInternal() : grad_(0), hess_(0) {}
109 
110  XGBOOST_DEVICE GradientPairInternal(float grad, float hess) {
111  SetGrad(grad);
112  SetHess(hess);
113  }
114 
115  // Copy constructor if of same value type
117  : grad_(g.grad_), hess_(g.hess_) {} // NOLINT
118 
119  // Copy constructor if different value type - use getters and setters to
120  // perform conversion
121  template <typename T2>
123  SetGrad(g.GetGrad());
124  SetHess(g.GetHess());
125  }
126 
127  XGBOOST_DEVICE float GetGrad() const { return grad_; }
128  XGBOOST_DEVICE float GetHess() const { return hess_; }
129 
131  const GradientPairInternal<T> &rhs) {
132  grad_ += rhs.grad_;
133  hess_ += rhs.hess_;
134  return *this;
135  }
136 
138  const GradientPairInternal<T> &rhs) const {
140  g.grad_ = grad_ + rhs.grad_;
141  g.hess_ = hess_ + rhs.hess_;
142  return g;
143  }
144 
146  const GradientPairInternal<T> &rhs) {
147  grad_ -= rhs.grad_;
148  hess_ -= rhs.hess_;
149  return *this;
150  }
151 
153  const GradientPairInternal<T> &rhs) const {
155  g.grad_ = grad_ - rhs.grad_;
156  g.hess_ = hess_ - rhs.hess_;
157  return g;
158  }
159 
160  XGBOOST_DEVICE explicit GradientPairInternal(int value) {
161  *this = GradientPairInternal<T>(static_cast<float>(value),
162  static_cast<float>(value));
163  }
164 
165  friend std::ostream &operator<<(std::ostream &os,
166  const GradientPairInternal<T> &g) {
167  os << g.GetGrad() << "/" << g.GetHess();
168  return os;
169  }
170 };
171 
172 template<>
174  return grad_ * 1e-4f;
175 }
176 template<>
178  return hess_ * 1e-4f;
179 }
180 template<>
182  grad_ = static_cast<int64_t>(std::round(g * 1e4));
183 }
184 template<>
186  hess_ = static_cast<int64_t>(std::round(h * 1e4));
187 }
188 
189 } // namespace detail
190 
193 
196 
201 
203 const bst_float kRtEps = 1e-6f;
204 
206 using omp_ulong = dmlc::omp_ulong; // NOLINT
208 using bst_omp_uint = dmlc::omp_uint; // NOLINT
209 
214 #if DMLC_USE_CXX11 && defined(__GNUC__) && !defined(__clang_version__)
215 #if __GNUC__ == 4 && __GNUC_MINOR__ < 8
216 #define override
217 #define final
218 #endif // __GNUC__ == 4 && __GNUC_MINOR__ < 8
219 #endif // DMLC_USE_CXX11 && defined(__GNUC__) && !defined(__clang_version__)
220 } // namespace xgboost
221 
222 /* Always keep this #include at the bottom of xgboost/base.h */
223 #include <xgboost/build_config.h>
224 
225 #endif // XGBOOST_BASE_H_
XGBOOST_DEVICE GradientPairInternal(const GradientPairInternal< T2 > &g)
Definition: base.h:122
float bst_float
float type, used for storing statistics
Definition: base.h:89
XGBOOST_DEVICE GradientPairInternal< T > & operator-=(const GradientPairInternal< T > &rhs)
Definition: base.h:145
int32_t bst_int
Definition: base.h:85
XGBOOST_DEVICE GradientPairInternal(int value)
Definition: base.h:160
dmlc::omp_ulong omp_ulong
define unsigned long for openmp loop
Definition: base.h:206
dmlc::omp_uint bst_omp_uint
define unsigned int for openmp loop
Definition: base.h:208
XGBOOST_DEVICE GradientPairInternal()
Definition: base.h:108
XGBOOST_DEVICE float GetGrad() const
Definition: base.h:127
Implementation of gradient statistics pair. Template specialisation may be used to overload different...
Definition: base.h:96
XGBOOST_DEVICE GradientPairInternal(float grad, float hess)
Definition: base.h:110
const bst_float kRtEps
small eps gap for minimum split decision.
Definition: base.h:203
uint64_t bst_ulong
long integers
Definition: base.h:87
XGBOOST_DEVICE GradientPairInternal< T > & operator+=(const GradientPairInternal< T > &rhs)
Definition: base.h:130
#define XGBOOST_DEVICE
Tag function as usable by device.
Definition: base.h:75
namespace of xgboost
Definition: base.h:79
XGBOOST_DEVICE GradientPairInternal(const GradientPairInternal< T > &g)
Definition: base.h:116
friend std::ostream & operator<<(std::ostream &os, const GradientPairInternal< T > &g)
Definition: base.h:165
XGBOOST_DEVICE GradientPairInternal< T > operator-(const GradientPairInternal< T > &rhs) const
Definition: base.h:152
XGBOOST_DEVICE GradientPairInternal< T > operator+(const GradientPairInternal< T > &rhs) const
Definition: base.h:137
uint32_t bst_uint
unsigned integer type used in boost, used for feature index and row index.
Definition: base.h:84
XGBOOST_DEVICE float GetHess() const
Definition: base.h:128
T ValueT
Definition: base.h:106