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>
69 
81 enum GPUAccess {
84  // write implies read
85  kWrite
86 };
87 
88 template <typename T>
90  static_assert(std::is_standard_layout_v<T>, "HostDeviceVector admits only POD types");
91 
92  public:
93  explicit HostDeviceVector(size_t size = 0, T v = T(), DeviceOrd device = DeviceOrd::CPU());
94  HostDeviceVector(std::initializer_list<T> init, DeviceOrd device = DeviceOrd::CPU());
95  explicit HostDeviceVector(const std::vector<T>& init, DeviceOrd device = DeviceOrd::CPU());
97 
100 
103 
104  [[nodiscard]] bool Empty() const { return Size() == 0; }
105  [[nodiscard]] std::size_t Size() const;
106  [[nodiscard]] std::size_t SizeBytes() const { return this->Size() * sizeof(T); }
107  [[nodiscard]] DeviceOrd Device() const;
112  const T* ConstDevicePointer() const;
113  const T* DevicePointer() const { return ConstDevicePointer(); }
114 
115  T* HostPointer() { return HostVector().data(); }
119  const T* ConstHostPointer() const { return ConstHostVector().data(); }
120  const T* HostPointer() const { return ConstHostPointer(); }
121 
122  void Fill(T v);
123  void Copy(const HostDeviceVector<T>& other);
124  void Copy(const std::vector<T>& other);
125  void Copy(std::initializer_list<T> other);
126 
127  void Extend(const HostDeviceVector<T>& other);
128 
129  std::vector<T>& HostVector();
130  const std::vector<T>& ConstHostVector() const;
131  const std::vector<T>& HostVector() const { return ConstHostVector(); }
132 
133  [[nodiscard]] bool HostCanRead() const;
134  [[nodiscard]] bool HostCanWrite() const;
135  [[nodiscard]] bool DeviceCanRead() const;
136  [[nodiscard]] bool DeviceCanWrite() const;
137  [[nodiscard]] GPUAccess DeviceAccess() const;
138 
139  void SetDevice(DeviceOrd device) const;
140 
141  void Resize(std::size_t new_size);
143  void Resize(std::size_t new_size, T v);
144 
145  using value_type = T; // NOLINT
146 
147  private:
149 };
150 
151 } // namespace xgboost
152 
153 #endif // XGBOOST_HOST_DEVICE_VECTOR_H_
Definition: host_device_vector.h:68
Definition: host_device_vector.h:89
const std::vector< T > & HostVector() const
Definition: host_device_vector.h:131
std::size_t SizeBytes() const
Definition: host_device_vector.h:106
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:104
common::Span< T const > ConstHostSpan() const
Definition: host_device_vector.h:118
void Resize(std::size_t new_size, T v)
Resize and initialize the data if the new size is larger than the old size.
std::vector< T > & HostVector()
common::Span< T const > HostSpan() const
Definition: host_device_vector.h:117
void Copy(std::initializer_list< T > other)
HostDeviceVector< T > & operator=(const HostDeviceVector< T > &)=delete
HostDeviceVector(std::initializer_list< T > init, DeviceOrd device=DeviceOrd::CPU())
T value_type
Definition: host_device_vector.h:145
HostDeviceVector(HostDeviceVector< T > &&)
common::Span< const T > ConstDeviceSpan() const
const std::vector< T > & ConstHostVector() const
T * HostPointer()
Definition: host_device_vector.h:115
HostDeviceVector(const std::vector< T > &init, DeviceOrd device=DeviceOrd::CPU())
const T * HostPointer() const
Definition: host_device_vector.h:120
common::Span< T > DeviceSpan()
common::Span< T > HostSpan()
Definition: host_device_vector.h:116
common::Span< const T > DeviceSpan() const
Definition: host_device_vector.h:110
void Copy(const std::vector< T > &other)
GPUAccess DeviceAccess() const
HostDeviceVector< T > & operator=(HostDeviceVector< T > &&)
const T * DevicePointer() const
Definition: host_device_vector.h:113
void SetDevice(DeviceOrd device) const
DeviceOrd Device() const
void Resize(std::size_t new_size)
const T * ConstHostPointer() const
Definition: host_device_vector.h:119
span class implementation, based on ISO++20 span<T>. The interface should be the same.
Definition: span.h:435
Learner interface that integrates objective, gbm and evaluation together. This is the user facing XGB...
Definition: base.h:89
GPUAccess
Controls data access from the GPU.
Definition: host_device_vector.h:81
@ kNone
Definition: host_device_vector.h:82
@ kRead
Definition: host_device_vector.h:83
@ kWrite
Definition: host_device_vector.h:85
A type for device ordinal. The type is packed into 32-bit for efficient use in viewing types like lin...
Definition: context.h:40
constexpr static auto CPU()
Constructor for CPU.
Definition: context.h:73