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  // 写操作意味着读操作
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]] std::size_t SizeBytes() const { return this->Size() * sizeof(T); }
105  [[nodiscard]] DeviceOrd Device() const;
110  const T* ConstDevicePointer() const;
111  const T* DevicePointer() const { return ConstDevicePointer(); }
112 
113  T* HostPointer() { return HostVector().data(); }
117  const T* ConstHostPointer() const { return ConstHostVector().data(); }
118  const T* HostPointer() const { return ConstHostPointer(); }
119 
120  void Fill(T v);
121  void Copy(const HostDeviceVector<T>& other);
122  void Copy(const std::vector<T>& other);
123  void Copy(std::initializer_list<T> other);
124 
125  void Extend(const HostDeviceVector<T>& other);
126 
127  std::vector<T>& HostVector();
128  const std::vector<T>& ConstHostVector() const;
129  const std::vector<T>& HostVector() const {return ConstHostVector(); }
130 
131  [[nodiscard]] bool HostCanRead() const;
132  [[nodiscard]] bool HostCanWrite() const;
133  [[nodiscard]] bool DeviceCanRead() const;
134  [[nodiscard]] bool DeviceCanWrite() const;
135  [[nodiscard]] GPUAccess DeviceAccess() const;
136 
137  void SetDevice(DeviceOrd device) const;
138 
139  void Resize(std::size_t new_size);
141  void Resize(std::size_t new_size, T v);
142 
143  using value_type = T; // NOLINT
144 
145  private
147 };
148 
149 } // namespace xgboost
150 
151 #endif // XGBOOST_HOST_DEVICE_VECTOR_H_
定义: host_device_vector.h:87
const std::vector< T > & HostVector() const
定义: host_device_vector.h:129
std::size_t SizeBytes() const
定义: host_device_vector.h:104
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
Definition: host_device_vector.h:116
void Resize(std::size_t new_size, T v)
如果新大小大于旧大小,则调整大小并初始化数据。
std::vector< T > & HostVector()
common::Span< T const > HostSpan() const
定义: host_device_vector.h:115
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:143
HostDeviceVector(HostDeviceVector< T > &&)
common::Span< const T > ConstDeviceSpan() const
const std::vector< T > & ConstHostVector() const
T * HostPointer()
定义: host_device_vector.h:113
HostDeviceVector(const std::vector< T > &init, DeviceOrd device=DeviceOrd::CPU())
const T * HostPointer() const
定义: host_device_vector.h:118
common::Span< T > DeviceSpan()
common::Span< T > HostSpan()
定义: host_device_vector.h:114
common::Span< const T > DeviceSpan() const
定义: host_device_vector.h:108
void Copy(const std::vector< T > &other)
GPUAccess DeviceAccess() const
HostDeviceVector< T > & operator=(HostDeviceVector< T > &&)
const T * DevicePointer() const
定义: host_device_vector.h:111
void SetDevice(DeviceOrd device) const
DeviceOrd Device() const
void Resize(std::size_t new_size)
const T * ConstHostPointer() const
定义: host_device_vector.h:117
span类实现,基于ISO++20 span<T>。接口应相同。
Definition: span.h:431
集成目标、gbm和评估的学习器接口。这是用户面临的XGB...
Definition: base.h:97
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...)时高效使用
Definition: context.h:34
constexpr static auto CPU()
CPU 的构造函数。
定义: context.h:64
定义: host_device_vector.h:67