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
82  kWrite
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 
97 
100 
101  bool Empty() const { return Size() == 0; }
102  size_t Size() const;
103  int DeviceIdx() const;
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;
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_
Definition: host_device_vector.h:86
const std::vector< T > & HostVector() const
Definition: host_device_vector.h:127
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:101
common::Span< T const > ConstHostSpan() const
Definition: host_device_vector.h:114
std::vector< T > & HostVector()
common::Span< T const > HostSpan() const
Definition: host_device_vector.h:113
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:139
HostDeviceVector(HostDeviceVector< T > &&)
common::Span< const T > ConstDeviceSpan() const
const std::vector< T > & ConstHostVector() const
T * HostPointer()
Definition: host_device_vector.h:111
void SetDevice(int device) const
const T * HostPointer() const
Definition: host_device_vector.h:116
common::Span< T > DeviceSpan()
common::Span< T > HostSpan()
Definition: host_device_vector.h:112
HostDeviceVector(const std::vector< T > &init, int device=-1)
common::Span< const T > DeviceSpan() const
Definition: host_device_vector.h:106
void Copy(const std::vector< T > &other)
GPUAccess DeviceAccess() const
HostDeviceVector< T > & operator=(HostDeviceVector< T > &&)
const T * DevicePointer() const
Definition: host_device_vector.h:109
const T * ConstHostPointer() const
Definition: host_device_vector.h:115
span class implementation, based on ISO++20 span<T>. The interface should be the same.
Definition: span.h:423
namespace of xgboost
Definition: base.h:110
GPUAccess
Controls data access from the GPU.
Definition: host_device_vector.h:79
@ kNone
Definition: host_device_vector.h:80
@ kRead
Definition: host_device_vector.h:80
@ kWrite
Definition: host_device_vector.h:82
Definition: host_device_vector.h:66