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