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(), DeviceOrd device = DeviceOrd::CPU());
92  HostDeviceVector(std::initializer_list<T> init, DeviceOrd device = DeviceOrd::CPU());
93  explicit HostDeviceVector(const std::vector<T>& init, DeviceOrd device = DeviceOrd::CPU());
95 
98 
101 
102  [[nodiscard]] bool Empty() const { return Size() == 0; }
103  [[nodiscard]] std::size_t Size() const;
104  [[nodiscard]] DeviceOrd Device() 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  [[nodiscard]] bool HostCanRead() const;
131  [[nodiscard]] bool HostCanWrite() const;
132  [[nodiscard]] bool DeviceCanRead() const;
133  [[nodiscard]] bool DeviceCanWrite() const;
134  [[nodiscard]] GPUAccess DeviceAccess() const;
135 
136  void SetDevice(DeviceOrd device) const;
137 
138  void Resize(size_t new_size, T v = T());
139 
140  using value_type = T; // NOLINT
141 
142  private:
144 };
145 
146 } // namespace xgboost
147 
148 #endif // XGBOOST_HOST_DEVICE_VECTOR_H_
Definition: host_device_vector.h:87
const std::vector< T > & HostVector() const
Definition: host_device_vector.h:128
HostDeviceVector(size_t size=0, T v=T(), DeviceOrd device=DeviceOrd::CPU())
std::size_t Size() const
const T * ConstDevicePointer() const
void Extend(const HostDeviceVector< T > &other)
HostDeviceVector(const HostDeviceVector< T > &)=delete
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
void Copy(std::initializer_list< T > other)
HostDeviceVector< T > & operator=(const HostDeviceVector< T > &)=delete
HostDeviceVector(std::initializer_list< T > init, DeviceOrd device=DeviceOrd::CPU())
void Resize(size_t new_size, T v=T())
T value_type
Definition: host_device_vector.h:140
HostDeviceVector(HostDeviceVector< T > &&)
common::Span< const T > ConstDeviceSpan() const
const std::vector< T > & ConstHostVector() const
T * HostPointer()
Definition: host_device_vector.h:112
HostDeviceVector(const std::vector< T > &init, DeviceOrd device=DeviceOrd::CPU())
const T * HostPointer() const
Definition: host_device_vector.h:117
common::Span< T > DeviceSpan()
common::Span< T > HostSpan()
Definition: host_device_vector.h:113
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
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:422
Core data structure for multi-target trees.
Definition: base.h:87
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:34
constexpr static auto CPU()
Constructor for CPU.
Definition: context.h:64
Definition: host_device_vector.h:67