| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
#ifndef XGBOOST_HOST_DEVICE_VECTOR_H_ |
| 50 |
#define XGBOOST_HOST_DEVICE_VECTOR_H_ |
| 51 |
|
| 52 |
#include <xgboost/context.h> |
| 53 |
#include <xgboost/span.h> |
| 54 |
|
| 55 |
#include <initializer_list> |
| 56 |
#include <type_traits> |
| 57 |
#include <vector> |
| 58 |
|
| 59 |
namespace xgboost { |
| 60 |
|
| 61 |
#ifdef __CUDACC__ |
| 62 |
|
| 63 |
|
| 64 |
void SetCudaSetDeviceHandler(void (*handler)(int)); |
| 65 |
#endif |
| 66 |
|
| 67 |
template <typename T> struct HostDeviceVectorImpl; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
enum GPUAccess { |
| 81 |
kNone, kRead, |
| 82 |
|
| 83 |
kWrite |
| 84 |
}; |
| 85 |
|
| 86 |
template <typename T> |
| 87 |
class HostDeviceVector { |
| 88 |
static_assert(std::is_standard_layout_v<T>, "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()); |
| 94 |
~HostDeviceVector(); |
| 95 |
|
| 96 |
HostDeviceVector(const HostDeviceVector<T>&) = delete; |
| 97 |
HostDeviceVector(HostDeviceVector<T>&&); |
| 98 |
|
| 99 |
HostDeviceVector<T>& operator=(const HostDeviceVector<T>&) = delete; |
| 100 |
HostDeviceVector<T>& operator=(HostDeviceVector<T>&&); |
| 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; |
| 106 |
common::Span<T> DeviceSpan(); |
| 107 |
common::Span<const T> ConstDeviceSpan() const; |
| 108 |
common::Span<const T> DeviceSpan() const { return ConstDeviceSpan(); } |
| 109 |
T* DevicePointer(); |
| 110 |
const T* ConstDevicePointer() const; |
| 111 |
const T* DevicePointer() const { return ConstDevicePointer(); } |
| 112 |
|
| 113 |
T* HostPointer() { return HostVector().data(); } |
| 114 |
common::Span<T> HostSpan() { return common::Span<T>{HostVector()}; } |
| 115 |
common::Span<T const> HostSpan() const { return common::Span<T const>{HostVector()}; } |
| 116 |
common::Span<T const> ConstHostSpan() const { return HostSpan(); } |
| 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); |
| 140 |
|
| 141 |
void Resize(std::size_t new_size, T v); |
| 142 |
|
| 143 |
using value_type = T; |
| 144 |
|
| 145 |
private: |
| 146 |
HostDeviceVectorImpl<T>* impl_; |
| 147 |
}; |
| 148 |
|
| 149 |
} |
| 150 |
|
| 151 |
#endif |