xgboost
host_device_vector.h
前往此文件的文档。
1 
49 #ifndef XGBOOST_HOST_DEVICE_VECTOR_H_
50 #define XGBOOST_HOST_DEVICE_VECTOR_H_
51 
52 #include <xgboost/context.h> // 用于 DeviceOrd
53 #include <xgboost/span.h> // 用于 Span
54 
55 #include <initializer_list>
56 #include <type_traits>
57 #include <vector>
58 
59 namespace xgboost {
60 
61 #ifdef __CUDACC__
62 // 设置一个函数来替代调用 cudaSetDevice();
63 // 仅为测试而添加
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_v<T>, "HostDeviceVector 只接受 POD 类型");
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(std::size_t new_size);
140  void Resize(std::size_t new_size, T v);
141 
142  using value_type = T; // NOLINT
143 
144  private
146 };
147 
148 } // namespace xgboost
149 
150 #endif // XGBOOST_HOST_DEVICE_VECTOR_H_
定义: host_device_vector.h:87
const std::vector< T > & HostVector() const
定义: 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
定义: host_device_vector.h:102
common::Span< T const > ConstHostSpan() const
定义: host_device_vector.h:115
void Resize(std::size_t new_size, T v)
如果新大小大于旧大小时,调整大小并初始化数据。
std::vector< T > & HostVector()
common::Span< T const > HostSpan() const
定义: 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())
T value_type
定义: host_device_vector.h:142
HostDeviceVector(HostDeviceVector< T > &&)
common::Span< const T > ConstDeviceSpan() const
const std::vector< T > & ConstHostVector() const
T * HostPointer()
定义: host_device_vector.h:112
HostDeviceVector(const std::vector< T > &init, DeviceOrd device=DeviceOrd::CPU())
const T * HostPointer() const
定义: host_device_vector.h:117
common::Span< T > DeviceSpan()
common::Span< T > HostSpan()
定义: host_device_vector.h:113
common::Span< const T > DeviceSpan() const
定义: host_device_vector.h:107
void Copy(const std::vector< T > &other)
GPUAccess DeviceAccess() const
HostDeviceVector< T > & operator=(HostDeviceVector< T > &&)
const T * DevicePointer() const
定义: host_device_vector.h:110
void SetDevice(DeviceOrd device) const
DeviceOrd Device() const
void Resize(std::size_t new_size)
const T * ConstHostPointer() const
定义: host_device_vector.h:116
span 类的实现,基于 ISO C++20 span。接口应保持一致。
定义: span.h:431
多目标树的核心数据结构。
定义: base.h:89
GPUAccess
控制 GPU 的数据访问。
定义: host_device_vector.h:80
@ kNone
定义: host_device_vector.h:81
@ kRead
定义: host_device_vector.h:81
@ kWrite
定义: host_device_vector.h:83
一种设备序号类型。该类型打包成 32 位,以便在查看类似 lin... 的类型时高效使用。
定义: context.h:34
constexpr static auto CPU()
CPU 的构造函数。
定义: context.h:64
定义: host_device_vector.h:67