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 {
81  // write implies read
83 };
84 
85 template <typename T>
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);
94 
95  HostDeviceVector(const HostDeviceVector<T>&) = delete;
97 
100 
101  bool Empty() const { return Size() == 0; }
102  size_t Size() const;
103  int DeviceIdx() const;
107  T* DevicePointer();
108  const T* ConstDevicePointer() const;
109  const T* DevicePointer() const { return ConstDevicePointer(); }
110 
111  T* HostPointer() { return HostVector().data(); }
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:
143 };
144 
145 } // namespace xgboost
146 
147 #endif // XGBOOST_HOST_DEVICE_VECTOR_H_
void Copy(const HostDeviceVector< T > &other)
bst_float value_type
Definition: host_device_vector.h:139
HostDeviceVector< T > & operator=(const HostDeviceVector< T > &)=delete
const T * DevicePointer() const
Definition: host_device_vector.h:109
Definition: host_device_vector.h:86
GPUAccess DeviceAccess() const
bool Empty() const
Definition: host_device_vector.h:101
T * HostPointer()
Definition: host_device_vector.h:111
const T * HostPointer() const
Definition: host_device_vector.h:116
common::Span< const T > ConstDeviceSpan() const
const T * ConstHostPointer() const
Definition: host_device_vector.h:115
const std::vector< T > & HostVector() const
Definition: host_device_vector.h:127
span class implementation, based on ISO++20 span<T>. The interface should be the same.
Definition: span.h:126
Definition: host_device_vector.h:80
Definition: host_device_vector.h:66
Definition: host_device_vector.h:82
common::Span< T const > HostSpan() const
Definition: host_device_vector.h:113
const T * ConstDevicePointer() const
common::Span< const T > DeviceSpan() const
Definition: host_device_vector.h:106
Definition: host_device_vector.h:80
common::Span< T > DeviceSpan()
HostDeviceVector(size_t size=0, T v=T(), int device=-1)
void SetDevice(int device) const
std::vector< T > & HostVector()
void Extend(const HostDeviceVector< T > &other)
namespace of xgboost
Definition: base.h:102
const std::vector< T > & ConstHostVector() const
GPUAccess
Controls data access from the GPU.
Definition: host_device_vector.h:79
void Resize(size_t new_size, T v=T())
common::Span< T > HostSpan()
Definition: host_device_vector.h:112
common::Span< T const > ConstHostSpan() const
Definition: host_device_vector.h:114