Medial Code Documentation
Loading...
Searching...
No Matches
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 <algorithm> // for max
10#include <array> // for array
11#include <cmath> // for ceil
12#include <cstddef> // for size_t
13#include <cstdint> // for int32_t, int64_t
14#include <sstream> // for basic_istream, operator<<, istringstream
15#include <string> // for string, basic_string, getline, char_traits
16#include <tuple> // for make_tuple
17#include <utility> // for forward, index_sequence, make_index_sequence
18#include <vector> // for vector
19
20#include "xgboost/base.h" // for XGBOOST_DEVICE
21#include "xgboost/logging.h" // for LOG, LOG_FATAL, LogMessageFatal
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
35namespace dh {
36#if defined(__CUDACC__)
37/*
38 * Error handling functions
39 */
40#define safe_cuda(ans) ThrowOnCudaError((ans), __FILE__, __LINE__)
41
42inline 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
54namespace xgboost::common {
60inline 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
70void EscapeU8(std::string const &string, std::string *p_buffer);
71
72template <typename T>
73XGBOOST_DEVICE T Max(T a, T b) {
74 return a < b ? b : a;
75}
76
77template <typename T1, typename T2>
78XGBOOST_DEVICE T1 DivRoundUp(const T1 a, const T2 b) {
79 return static_cast<T1>(std::ceil(static_cast<double>(a) / b));
80}
81
82namespace detail {
83template <class T, std::size_t N, std::size_t... Idx>
84constexpr auto UnpackArr(std::array<T, N> &&arr, std::index_sequence<Idx...>) {
85 return std::make_tuple(std::forward<std::array<T, N>>(arr)[Idx]...);
86}
87} // namespace detail
88
89template <class T, std::size_t N>
90constexpr auto UnpackArr(std::array<T, N> &&arr) {
91 return detail::UnpackArr(std::forward<std::array<T, N>>(arr),
92 std::make_index_sequence<N>{});
93}
94
95/*
96 * Range iterator
97 */
98class Range {
99 public:
100 using DifferenceType = int64_t;
101
102 class Iterator {
103 friend class Range;
104
105 public:
106 XGBOOST_DEVICE DifferenceType operator*() const { return i_; }
107 XGBOOST_DEVICE const Iterator &operator++() {
108 i_ += step_;
109 return *this;
110 }
111 XGBOOST_DEVICE Iterator operator++(int) {
112 Iterator res {*this};
113 i_ += step_;
114 return res;
115 }
116
117 XGBOOST_DEVICE bool operator==(const Iterator &other) const {
118 return i_ >= other.i_;
119 }
120 XGBOOST_DEVICE bool operator!=(const Iterator &other) const {
121 return i_ < other.i_;
122 }
123
124 XGBOOST_DEVICE void Step(DifferenceType s) { step_ = s; }
125
126 protected:
127 XGBOOST_DEVICE explicit Iterator(DifferenceType start) : i_(start) {}
128 XGBOOST_DEVICE explicit Iterator(DifferenceType start, DifferenceType step) :
129 i_{start}, step_{step} {}
130
131 private:
132 int64_t i_;
133 DifferenceType step_ = 1;
134 };
135
136 XGBOOST_DEVICE Iterator begin() const { return begin_; } // NOLINT
137 XGBOOST_DEVICE Iterator end() const { return end_; } // NOLINT
138
139 XGBOOST_DEVICE Range(DifferenceType begin, DifferenceType end)
140 : begin_(begin), end_(end) {}
141 XGBOOST_DEVICE Range(DifferenceType begin, DifferenceType end,
142 DifferenceType step)
143 : begin_(begin, step), end_(end) {}
144
145 XGBOOST_DEVICE bool operator==(const Range& other) const {
146 return *begin_ == *other.begin_ && *end_ == *other.end_;
147 }
148 XGBOOST_DEVICE bool operator!=(const Range& other) const {
149 return !(*this == other);
150 }
151
152 XGBOOST_DEVICE void Step(DifferenceType s) { begin_.Step(s); }
153
154 private:
155 Iterator begin_;
156 Iterator end_;
157};
158
159int AllVisibleGPUs();
160
161inline void AssertGPUSupport() {
162#ifndef XGBOOST_USE_CUDA
163 LOG(FATAL) << "XGBoost version not compiled with GPU support.";
164#endif // XGBOOST_USE_CUDA
165}
166
167inline void AssertOneAPISupport() {
168#ifndef XGBOOST_USE_ONEAPI
169 LOG(FATAL) << "XGBoost version not compiled with OneAPI support.";
170#endif // XGBOOST_USE_ONEAPI
171}
172
173void SetDevice(std::int32_t device);
174
175#if !defined(XGBOOST_USE_CUDA)
176inline void SetDevice(std::int32_t device) {
177 if (device >= 0) {
178 AssertGPUSupport();
179 }
180}
181#endif
182
186template <typename Indexable>
187XGBOOST_DEVICE size_t LastOf(size_t group, Indexable const &indptr) {
188 return indptr[group + 1] - 1;
189}
190} // namespace xgboost::common
191#endif // XGBOOST_COMMON_COMMON_H_
@ Max
"max" - take max on conflict
Definition common.h:102
Definition common.h:98
Copyright 2015-2023 by XGBoost Contributors.
#define XGBOOST_DEVICE
Tag function as usable by device.
Definition base.h:64
defines console logging options for xgboost. Use to enforce unified print behavior.
detail namespace with internal helper functions
Definition json.hpp:249
int N
Simulate some binary data with a single categorical and single continuous predictor.
Definition logistic_regression.py:26
Copyright 2017-2023, XGBoost Contributors.
Definition span.h:77
XGBOOST_DEVICE size_t LastOf(size_t group, Indexable const &indptr)
Last index of a group in a CSR style of index pointer.
Definition common.h:187
std::vector< std::string > Split(const std::string &s, char delim)
Split a string by delimiter.
Definition common.h:60