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 // simple routine to convert any data to string
68 template<typename T>
69 inline std::string ToString(const T& data) {
70  std::ostringstream os;
71  os << data;
72  return os.str();
73 }
74 
75 template <typename T1, typename T2>
76 XGBOOST_DEVICE T1 DivRoundUp(const T1 a, const T2 b) {
77  return static_cast<T1>(std::ceil(static_cast<double>(a) / b));
78 }
79 
80 /*
81  * Range iterator
82  */
83 class Range {
84  public:
85  using DifferenceType = int64_t;
86 
87  class Iterator {
88  friend class Range;
89 
90  public:
91  XGBOOST_DEVICE DifferenceType operator*() const { return i_; }
93  i_ += step_;
94  return *this;
95  }
97  Iterator res {*this};
98  i_ += step_;
99  return res;
100  }
101 
102  XGBOOST_DEVICE bool operator==(const Iterator &other) const {
103  return i_ >= other.i_;
104  }
105  XGBOOST_DEVICE bool operator!=(const Iterator &other) const {
106  return i_ < other.i_;
107  }
108 
109  XGBOOST_DEVICE void Step(DifferenceType s) { step_ = s; }
110 
111  protected:
112  XGBOOST_DEVICE explicit Iterator(DifferenceType start) : i_(start) {}
114  i_{start}, step_{step} {}
115 
116  public:
117  int64_t i_;
118  DifferenceType step_ = 1;
119  };
120 
121  XGBOOST_DEVICE Iterator begin() const { return begin_; } // NOLINT
122  XGBOOST_DEVICE Iterator end() const { return end_; } // NOLINT
123 
125  : begin_(begin), end_(end) {}
127  DifferenceType step)
128  : begin_(begin, step), end_(end) {}
129 
130  XGBOOST_DEVICE bool operator==(const Range& other) const {
131  return *begin_ == *other.begin_ && *end_ == *other.end_;
132  }
133  XGBOOST_DEVICE bool operator!=(const Range& other) const {
134  return !(*this == other);
135  }
136 
137  XGBOOST_DEVICE void Step(DifferenceType s) { begin_.Step(s); }
138 
139  private:
140  Iterator begin_;
141  Iterator end_;
142 };
143 
144 int AllVisibleGPUs();
145 } // namespace common
146 } // namespace xgboost
147 #endif // XGBOOST_COMMON_COMMON_H_
XGBOOST_DEVICE Iterator begin() const
Definition: common.h:121
XGBOOST_DEVICE Iterator(DifferenceType start)
Definition: common.h:112
XGBOOST_DEVICE bool operator!=(const Iterator &other) const
Definition: common.h:105
XGBOOST_DEVICE DifferenceType operator*() const
Definition: common.h:91
XGBOOST_DEVICE void Step(DifferenceType s)
Definition: common.h:137
XGBOOST_DEVICE Iterator operator++(int)
Definition: common.h:96
XGBOOST_DEVICE bool operator!=(const Range &other) const
Definition: common.h:133
Definition: common.h:83
int64_t DifferenceType
Definition: common.h:85
XGBOOST_DEVICE Iterator end() const
Definition: common.h:122
XGBOOST_DEVICE Iterator(DifferenceType start, DifferenceType step)
Definition: common.h:113
XGBOOST_DEVICE T1 DivRoundUp(const T1 a, const T2 b)
Definition: common.h:76
std::vector< std::string > Split(const std::string &s, char delim)
Split a string by delimiter.
Definition: common.h:57
Definition: common.h:87
#define XGBOOST_DEVICE
Tag function as usable by device.
Definition: base.h:84
int64_t i_
Definition: common.h:117
namespace of xgboost
Definition: base.h:102
defines configuration macros of xgboost.
XGBOOST_DEVICE Range(DifferenceType begin, DifferenceType end)
Definition: common.h:124
XGBOOST_DEVICE bool operator==(const Iterator &other) const
Definition: common.h:102
XGBOOST_DEVICE const Iterator & operator++()
Definition: common.h:92
XGBOOST_DEVICE Range(DifferenceType begin, DifferenceType end, DifferenceType step)
Definition: common.h:126
XGBOOST_DEVICE bool operator==(const Range &other) const
Definition: common.h:130
std::string ToString(const T &data)
Definition: common.h:69
XGBOOST_DEVICE void Step(DifferenceType s)
Definition: common.h:109
Definition: common.h:31