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