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 {
80  // write implies read
82 };
83 
84 template <typename T>
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);
91 
92  HostDeviceVector(const HostDeviceVector<T>&) = delete;
94 
97 
98  size_t Size() const;
99  int DeviceIdx() const;
103  T* DevicePointer();
104  const T* ConstDevicePointer() const;
105  const T* DevicePointer() const { return ConstDevicePointer(); }
106 
107  T* HostPointer() { return HostVector().data(); }
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:
137 };
138 
139 } // namespace xgboost
140 
141 #endif // XGBOOST_HOST_DEVICE_VECTOR_H_
void Copy(const HostDeviceVector< T > &other)
bst_float value_type
Definition: host_device_vector.h:133
HostDeviceVector< T > & operator=(const HostDeviceVector< T > &)=delete
const T * DevicePointer() const
Definition: host_device_vector.h:105
Definition: host_device_vector.h:85
GPUAccess DeviceAccess() const
T * HostPointer()
Definition: host_device_vector.h:107
const T * HostPointer() const
Definition: host_device_vector.h:112
common::Span< const T > ConstDeviceSpan() const
const T * ConstHostPointer() const
Definition: host_device_vector.h:111
const std::vector< T > & HostVector() const
Definition: host_device_vector.h:121
span class implementation, based on ISO++20 span<T>. The interface should be the same.
Definition: span.h:126
Definition: host_device_vector.h:79
Definition: host_device_vector.h:65
Definition: host_device_vector.h:81
common::Span< T const > HostSpan() const
Definition: host_device_vector.h:109
const T * ConstDevicePointer() const
common::Span< const T > DeviceSpan() const
Definition: host_device_vector.h:102
Definition: host_device_vector.h:79
common::Span< T > DeviceSpan()
HostDeviceVector(size_t size=0, T v=T(), int device=-1)
void SetDevice(int device) const
std::vector< T > & HostVector()
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:78
void Resize(size_t new_size, T v=T())
common::Span< T > HostSpan()
Definition: host_device_vector.h:108
common::Span< T const > ConstHostSpan() const
Definition: host_device_vector.h:110