xgboost

bitfield.h
Go to the documentation of this file.
1
5 #ifndef XGBOOST_COMMON_BITFIELD_H_
6 #define XGBOOST_COMMON_BITFIELD_H_
7
8 #include <algorithm>
9 #include <bitset>
10 #include <cinttypes>
11 #include <iostream>
12 #include <sstream>
13 #include <string>
14 #include <vector>
15
16 #if defined(__CUDACC__)
17 #include <thrust/copy.h>
18 #include <thrust/device_ptr.h>
19 #include "device_helpers.cuh"
20 #endif // defined(__CUDACC__)
21
22 #include "xgboost/span.h"
23 #include "common.h"
24
25 namespace xgboost {
26
27 #if defined(__CUDACC__)
28 using BitFieldAtomicType = unsigned long long; // NOLINT
29
30 __forceinline__ __device__ BitFieldAtomicType AtomicOr(BitFieldAtomicType* address,
31 BitFieldAtomicType val) {
32 BitFieldAtomicType old = *address, assumed; // NOLINT
33 do {
34 assumed = old;
35 old = atomicCAS(address, assumed, val | assumed);
36 } while (assumed != old);
37
38 return old;
39 }
40
41 __forceinline__ __device__ BitFieldAtomicType AtomicAnd(BitFieldAtomicType* address,
42 BitFieldAtomicType val) {
43 BitFieldAtomicType old = *address, assumed; // NOLINT
44 do {
45 assumed = old;
46 old = atomicCAS(address, assumed, val & assumed);
47 } while (assumed != old);
48
49 return old;
50 }
51 #endif // defined(__CUDACC__)
52
58 template <typename VT, typename Direction, bool IsConst = false>
59 struct BitFieldContainer {
60 using value_type = std::conditional_t<IsConst, VT const, VT>; // NOLINT
61 using pointer = value_type*; // NOLINT
62
63 static value_type constexpr kValueSize = sizeof(value_type) * 8;
64 static value_type constexpr kOne = 1; // force correct type.
65
66 struct Pos {
67 std::remove_const_t<value_type> int_pos {0};
68 std::remove_const_t<value_type> bit_pos {0};
69 };
70
71 private:
72 common::Span<value_type> bits_;
73 static_assert(!std::is_signed<VT>::value, "Must use unsiged type as underlying storage.");
74
75 public:
76 XGBOOST_DEVICE static Pos ToBitPos(value_type pos) {
77 Pos pos_v;
78 if (pos == 0) {
79 return pos_v;
80 }
81 pos_v.int_pos = pos / kValueSize;
82 pos_v.bit_pos = pos % kValueSize;
83 return pos_v;
84 }
85
86 public:
87 BitFieldContainer() = default;
88 XGBOOST_DEVICE explicit BitFieldContainer(common::Span<value_type> bits) : bits_{bits} {}
89 XGBOOST_DEVICE BitFieldContainer(BitFieldContainer const& other) : bits_{other.bits_} {}
90
91 common::Span<value_type> Bits() { return bits_; }
92 common::Span<value_type const> Bits() const { return bits_; }
93
94 /*\brief Compute the size of needed memory allocation. The returned value is in terms
95 * of number of elements with `BitFieldContainer::value_type'.
96 */
97 XGBOOST_DEVICE static size_t ComputeStorageSize(size_t size) {
98 return common::DivRoundUp(size, kValueSize);
99 }
100 #if defined(__CUDA_ARCH__)
101 __device__ BitFieldContainer& operator|=(BitFieldContainer const& rhs) {
102 auto tid = blockIdx.x * blockDim.x + threadIdx.x;
103 size_t min_size = min(bits_.size(), rhs.bits_.size());
104 if (tid < min_size) {
105 bits_[tid] |= rhs.bits_[tid];
106 }
107 return *this;
108 }
109 #else
110 BitFieldContainer& operator|=(BitFieldContainer const& rhs) {
111 size_t min_size = std::min(bits_.size(), rhs.bits_.size());
112 for (size_t i = 0; i < min_size; ++i) {
113 bits_[i] |= rhs.bits_[i];
114 }
115 return *this;
116 }
117 #endif // #if defined(__CUDA_ARCH__)
118
119 #if defined(__CUDA_ARCH__)
120 __device__ BitFieldContainer& operator&=(BitFieldContainer const& rhs) {
121 size_t min_size = min(bits_.size(), rhs.bits_.size());
122 auto tid = blockIdx.x * blockDim.x + threadIdx.x;
123 if (tid < min_size) {
124 bits_[tid] &= rhs.bits_[tid];
125 }
126 return *this;
127 }
128 #else
129 BitFieldContainer& operator&=(BitFieldContainer const& rhs) {
130 size_t min_size = std::min(bits_.size(), rhs.bits_.size());
131 for (size_t i = 0; i < min_size; ++i) {
132 bits_[i] &= rhs.bits_[i];
133 }
134 return *this;
135 }
136 #endif // defined(__CUDA_ARCH__)
137
138 #if defined(__CUDA_ARCH__)
139 __device__ auto Set(value_type pos) {
140 Pos pos_v = Direction::Shift(ToBitPos(pos));
141 value_type& value = bits_[pos_v.int_pos];
142 value_type set_bit = kOne << pos_v.bit_pos;
143 using Type = typename dh::detail::AtomicDispatcher<sizeof(value_type)>::Type;
144 atomicOr(reinterpret_cast<Type *>(&value), set_bit);
145 }
146 __device__ void Clear(value_type pos) {
147 Pos pos_v = Direction::Shift(ToBitPos(pos));
148 value_type& value = bits_[pos_v.int_pos];
149 value_type clear_bit = ~(kOne << pos_v.bit_pos);
150 using Type = typename dh::detail::AtomicDispatcher<sizeof(value_type)>::Type;
151 atomicAnd(reinterpret_cast<Type *>(&value), clear_bit);
152 }
153 #else
154 void Set(value_type pos) {
155 Pos pos_v = Direction::Shift(ToBitPos(pos));
156 value_type& value = bits_[pos_v.int_pos];
157 value_type set_bit = kOne << pos_v.bit_pos;
158 value |= set_bit;
159 }
160 void Clear(value_type pos) {
161 Pos pos_v = Direction::Shift(ToBitPos(pos));
162 value_type& value = bits_[pos_v.int_pos];
163 value_type clear_bit = ~(kOne << pos_v.bit_pos);
164 value &= clear_bit;
165 }
166 #endif // defined(__CUDA_ARCH__)
167
168 XGBOOST_DEVICE bool Check(Pos pos_v) const {
169 pos_v = Direction::Shift(pos_v);
170 SPAN_LT(pos_v.int_pos, bits_.size());
171 value_type const value = bits_[pos_v.int_pos];
172 value_type const test_bit = kOne << pos_v.bit_pos;
173 value_type result = test_bit & value;
174 return static_cast<bool>(result);
175 }
176 XGBOOST_DEVICE bool Check(value_type pos) const {
177 Pos pos_v = ToBitPos(pos);
178 return Check(pos_v);
179 }
180
181 XGBOOST_DEVICE size_t Size() const { return kValueSize * bits_.size(); }
182
183 XGBOOST_DEVICE pointer Data() const { return bits_.data(); }
184
185 inline friend std::ostream &
186 operator<<(std::ostream &os, BitFieldContainer<VT, Direction, IsConst> field) {
187 os << "Bits " << "storage size: " << field.bits_.size() << "\n";
188 for (typename common::Span<value_type>::index_type i = 0; i < field.bits_.size(); ++i) {
189 std::bitset<BitFieldContainer<VT, Direction, IsConst>::kValueSize> bset(field.bits_[i]);
190 os << bset << "\n";
191 }
192 return os;
193 }
194 };
195
196 // Bits start from left most bits (most significant bit).
197 template <typename VT, bool IsConst = false>
198 struct LBitsPolicy : public BitFieldContainer<VT, LBitsPolicy<VT, IsConst>, IsConst> {
199 using Container = BitFieldContainer<VT, LBitsPolicy<VT, IsConst>, IsConst>;
200 using Pos = typename Container::Pos;
201 using value_type = typename Container::value_type; // NOLINT
202
203 XGBOOST_DEVICE static Pos Shift(Pos pos) {
204 pos.bit_pos = Container::kValueSize - pos.bit_pos - Container::kOne;
205 return pos;
206 }
207 };
208
209 // Bits start from right most bit (least significant bit) of each entry, but integer index
210 // is from left to right.
211 template <typename VT>
212 struct RBitsPolicy : public BitFieldContainer<VT, RBitsPolicy<VT>> {
213 using Container = BitFieldContainer<VT, RBitsPolicy<VT>>;
214 using Pos = typename Container::Pos;
215 using value_type = typename Container::value_type; // NOLINT
216
217 XGBOOST_DEVICE static Pos Shift(Pos pos) {
218 return pos;
219 }
220 };
221
222 // Format: <Const><Direction>BitField<size of underlying type in bits>, underlying type
223 // must be unsigned.
224 using LBitField64 = BitFieldContainer<uint64_t, LBitsPolicy<uint64_t>>;
225 using RBitField8 = BitFieldContainer<uint8_t, RBitsPolicy<unsigned char>>;
226
227 using LBitField32 = BitFieldContainer<uint32_t, LBitsPolicy<uint32_t>>;
228 using CLBitField32 = BitFieldContainer<uint32_t, LBitsPolicy<uint32_t, true>, true>;
229 } // namespace xgboost
230
231 #endif // XGBOOST_COMMON_BITFIELD_H_
xgboost::BitFieldContainer::kOne
static constexpr value_type kOne
Definition: bitfield.h:64
xgboost::BitFieldContainer::Data
XGBOOST_DEVICE pointer Data() const
Definition: bitfield.h:183
xgboost::BitFieldContainer::BitFieldContainer
BitFieldContainer()=default
xgboost::LBitsPolicy::value_type
typename Container::value_type value_type
Definition: bitfield.h:201
xgboost::BitFieldContainer< VT, LBitsPolicy< VT, false >, false >::pointer
value_type * pointer
Definition: bitfield.h:61
xgboost::BitFieldContainer::Clear
void Clear(value_type pos)
Definition: bitfield.h:160
xgboost::BitFieldContainer::ComputeStorageSize
static XGBOOST_DEVICE size_t ComputeStorageSize(size_t size)
Definition: bitfield.h:97
xgboost::BitFieldContainer
A non-owning type with auxiliary methods defined for manipulating bits.
Definition: bitfield.h:59
xgboost::common::Span::index_type
std::size_t index_type
Definition: span.h:416
xgboost::BitFieldContainer::Bits
common::Span< value_type > Bits()
Definition: bitfield.h:91
xgboost::BitFieldContainer::value_type
std::conditional_t< IsConst, VT const, VT > value_type
Definition: bitfield.h:60
xgboost::BitFieldContainer::Pos::int_pos
std::remove_const_t< value_type > int_pos
Definition: bitfield.h:67
xgboost::RBitsPolicy
Definition: bitfield.h:212
xgboost::BitFieldContainer::Bits
common::Span< value_type const > Bits() const
Definition: bitfield.h:92
xgboost::BitFieldContainer::operator&=
BitFieldContainer & operator&=(BitFieldContainer const &rhs)
Definition: bitfield.h:129
span.h
xgboost::RBitsPolicy::Pos
typename Container::Pos Pos
Definition: bitfield.h:214
xgboost::BitFieldContainer::Check
XGBOOST_DEVICE bool Check(Pos pos_v) const
Definition: bitfield.h:168
xgboost::RBitsPolicy::Shift
static XGBOOST_DEVICE Pos Shift(Pos pos)
Definition: bitfield.h:217
xgboost::LBitsPolicy::Pos
typename Container::Pos Pos
Definition: bitfield.h:200
xgboost::LBitsPolicy
Definition: bitfield.h:198
xgboost::common::DivRoundUp
XGBOOST_DEVICE T1 DivRoundUp(const T1 a, const T2 b)
Definition: common.h:84
xgboost::LBitsPolicy::Shift
static XGBOOST_DEVICE Pos Shift(Pos pos)
Definition: bitfield.h:203
xgboost::BitFieldContainer::Pos
Definition: bitfield.h:66
xgboost::BitFieldContainer::kValueSize
static constexpr value_type kValueSize
Definition: bitfield.h:63
SPAN_LT
#define SPAN_LT(lhs, rhs)
Definition: span.h:112
xgboost::BitFieldContainer::ToBitPos
static XGBOOST_DEVICE Pos ToBitPos(value_type pos)
Definition: bitfield.h:76
xgboost::BitFieldContainer::Set
void Set(value_type pos)
Definition: bitfield.h:154
common.h
Common utilities.
xgboost::RBitsPolicy::value_type
typename Container::value_type value_type
Definition: bitfield.h:215
xgboost::common::Span< value_type >
xgboost::BitFieldContainer::Pos::bit_pos
std::remove_const_t< value_type > bit_pos
Definition: bitfield.h:68
xgboost::BitFieldContainer::BitFieldContainer
XGBOOST_DEVICE BitFieldContainer(BitFieldContainer const &other)
Definition: bitfield.h:89
xgboost::common::Span::size
constexpr XGBOOST_DEVICE index_type size() const __span_noexcept
Definition: span.h:542
xgboost::common::Span::data
constexpr XGBOOST_DEVICE pointer data() const __span_noexcept
Definition: span.h:537
xgboost::BitFieldContainer::operator|=
BitFieldContainer & operator|=(BitFieldContainer const &rhs)
Definition: bitfield.h:110
xgboost::BitFieldContainer::Size
XGBOOST_DEVICE size_t Size() const
Definition: bitfield.h:181
xgboost::BitFieldContainer::BitFieldContainer
XGBOOST_DEVICE BitFieldContainer(common::Span< value_type > bits)
Definition: bitfield.h:88
xgboost::BitFieldContainer::operator<<
friend std::ostream & operator<<(std::ostream &os, BitFieldContainer< VT, Direction, IsConst > field)
Definition: bitfield.h:186
XGBOOST_DEVICE
#define XGBOOST_DEVICE
Tag function as usable by device.
Definition: base.h:84
xgboost::BitFieldContainer::Check
XGBOOST_DEVICE bool Check(value_type pos) const
Definition: bitfield.h:176
xgboost
namespace of xgboost
Definition: base.h:110