xgboost

host_device_vector.h
Go to the documentation of this file.
1
49 #ifndef XGBOOST_HOST_DEVICE_VECTOR_H_
50 #define XGBOOST_HOST_DEVICE_VECTOR_H_
51
52 #include <initializer_list>
53 #include <vector>
54 #include <type_traits>
55
56 #include "span.h"
57
58 namespace xgboost {
59
60 #ifdef __CUDACC__
61 // Sets a function to call instead of cudaSetDevice();
62 // only added for testing
63 void SetCudaSetDeviceHandler(void (*handler)(int));
64 #endif // __CUDACC__
65
66 template <typename T> struct HostDeviceVectorImpl;
67
79 enum GPUAccess {
80 kNone, kRead,
81 // write implies read
82 kWrite
83 };
84
85 template <typename T>
86 class HostDeviceVector {
87 static_assert(std::is_standard_layout<T>::value, "HostDeviceVector admits only POD types");
88
89 public:
90 explicit HostDeviceVector(size_t size = 0, T v = T(), int device = -1);
91 HostDeviceVector(std::initializer_list<T> init, int device = -1);
92 explicit HostDeviceVector(const std::vector<T>& init, int device = -1);
93 ~HostDeviceVector();
94
95 HostDeviceVector(const HostDeviceVector<T>&) = delete;
96 HostDeviceVector(HostDeviceVector<T>&&);
97
98 HostDeviceVector<T>& operator=(const HostDeviceVector<T>&) = delete;
99 HostDeviceVector<T>& operator=(HostDeviceVector<T>&&);
100
101 bool Empty() const { return Size() == 0; }
102 size_t Size() const;
103 int DeviceIdx() const;
104 common::Span<T> DeviceSpan();
105 common::Span<const T> ConstDeviceSpan() const;
106 common::Span<const T> DeviceSpan() const { return ConstDeviceSpan(); }
107 T* DevicePointer();
108 const T* ConstDevicePointer() const;
109 const T* DevicePointer() const { return ConstDevicePointer(); }
110
111 T* HostPointer() { return HostVector().data(); }
112 common::Span<T> HostSpan() { return common::Span<T>{HostVector()}; }
113 common::Span<T const> HostSpan() const { return common::Span<T const>{HostVector()}; }
114 common::Span<T const> ConstHostSpan() const { return HostSpan(); }
115 const T* ConstHostPointer() const { return ConstHostVector().data(); }
116 const T* HostPointer() const { return ConstHostPointer(); }
117
118 void Fill(T v);
119 void Copy(const HostDeviceVector<T>& other);
120 void Copy(const std::vector<T>& other);
121 void Copy(std::initializer_list<T> other);
122
123 void Extend(const HostDeviceVector<T>& other);
124
125 std::vector<T>& HostVector();
126 const std::vector<T>& ConstHostVector() const;
127 const std::vector<T>& HostVector() const {return ConstHostVector(); }
128
129 bool HostCanRead() const;
130 bool HostCanWrite() const;
131 bool DeviceCanRead() const;
132 bool DeviceCanWrite() const;
133 GPUAccess DeviceAccess() const;
134
135 void SetDevice(int device) const;
136
137 void Resize(size_t new_size, T v = T());
138
139 using value_type = T; // NOLINT
140
141 private:
142 HostDeviceVectorImpl<T>* impl_;
143 };
144
145 } // namespace xgboost
146
147 #endif // XGBOOST_HOST_DEVICE_VECTOR_H_
xgboost::HostDeviceVector::HostVector
const std::vector< T > & HostVector() const
Definition: host_device_vector.h:127
xgboost::kWrite
@ kWrite
Definition: host_device_vector.h:82
xgboost::HostDeviceVector::Extend
void Extend(const HostDeviceVector< T > &other)
xgboost::HostDeviceVector::HostSpan
common::Span< T const > HostSpan() const
Definition: host_device_vector.h:113
xgboost::HostDeviceVector::DeviceSpan
common::Span< const T > DeviceSpan() const
Definition: host_device_vector.h:106
xgboost::HostDeviceVector
Definition: host_device_vector.h:86
xgboost::HostDeviceVector::DeviceSpan
common::Span< T > DeviceSpan()
xgboost::HostDeviceVector::DeviceAccess
GPUAccess DeviceAccess() const
xgboost::HostDeviceVector::HostCanRead
bool HostCanRead() const
xgboost::HostDeviceVector::DevicePointer
T * DevicePointer()
xgboost::HostDeviceVector::Empty
bool Empty() const
Definition: host_device_vector.h:101
xgboost::HostDeviceVector::ConstHostSpan
common::Span< T const > ConstHostSpan() const
Definition: host_device_vector.h:114
xgboost::HostDeviceVector::DeviceCanRead
bool DeviceCanRead() const
xgboost::HostDeviceVector::DeviceCanWrite
bool DeviceCanWrite() const
xgboost::HostDeviceVector::~HostDeviceVector
~HostDeviceVector()
xgboost::HostDeviceVector::SetDevice
void SetDevice(int device) const
xgboost::HostDeviceVector::HostVector
std::vector< T > & HostVector()
xgboost::HostDeviceVector::DevicePointer
const T * DevicePointer() const
Definition: host_device_vector.h:109
span.h
xgboost::HostDeviceVector::HostSpan
common::Span< T > HostSpan()
Definition: host_device_vector.h:112
xgboost::GPUAccess
GPUAccess
Controls data access from the GPU.
Definition: host_device_vector.h:79
xgboost::kRead
@ kRead
Definition: host_device_vector.h:80
xgboost::HostDeviceVector::Copy
void Copy(const HostDeviceVector< T > &other)
xgboost::HostDeviceVector::Size
size_t Size() const
xgboost::HostDeviceVector::HostPointer
T * HostPointer()
Definition: host_device_vector.h:111
xgboost::HostDeviceVector::Resize
void Resize(size_t new_size, T v=T())
xgboost::HostDeviceVector::ConstDevicePointer
const T * ConstDevicePointer() const
xgboost::common::Span
span class implementation, based on ISO++20 span<T>. The interface should be the same.
Definition: span.h:142
xgboost::kNone
@ kNone
Definition: host_device_vector.h:80
xgboost::HostDeviceVector< bst_float >::value_type
bst_float value_type
Definition: host_device_vector.h:139
xgboost::HostDeviceVector::ConstHostPointer
const T * ConstHostPointer() const
Definition: host_device_vector.h:115
xgboost::HostDeviceVector::HostDeviceVector
HostDeviceVector(size_t size=0, T v=T(), int device=-1)
xgboost::HostDeviceVector::ConstHostVector
const std::vector< T > & ConstHostVector() const
xgboost::HostDeviceVector::DeviceIdx
int DeviceIdx() const
xgboost::HostDeviceVectorImpl
Definition: host_device_vector.h:66
xgboost::HostDeviceVector::HostPointer
const T * HostPointer() const
Definition: host_device_vector.h:116
xgboost::HostDeviceVector::ConstDeviceSpan
common::Span< const T > ConstDeviceSpan() const
xgboost::HostDeviceVector::Fill
void Fill(T v)
xgboost::HostDeviceVector::HostCanWrite
bool HostCanWrite() const
xgboost
namespace of xgboost
Definition: base.h:110
xgboost::HostDeviceVector::operator=
HostDeviceVector< T > & operator=(const HostDeviceVector< T > &)=delete