Debian Codegraph @

src/include/xgboost/host_device_vector.h

raw symbols SCIP
1 /*!
2 * Copyright 2017-2019 XGBoost contributors
3 */
4
5 /**
6 * @file host_device_vector.h
7 * @brief A device-and-host vector abstraction layer.
8 *
9 * Why HostDeviceVector?<br/>
10 * With CUDA, one has to explicitly manage memory through 'cudaMemcpy' calls.
11 * This wrapper class hides this management from the users, thereby making it
12 * easy to integrate GPU/CPU usage under a single interface.
13 *
14 * Initialization/Allocation:<br/>
15 * One can choose to initialize the vector on CPU or GPU during constructor.
16 * (use the 'devices' argument) Or, can choose to use the 'Resize' method to
17 * allocate/resize memory explicitly, and use the 'SetDevice' method
18 * to specify the device.
19 *
20 * Accessing underlying data:<br/>
21 * Use 'HostVector' method to explicitly query for the underlying std::vector.
22 * If you need the raw device pointer, use the 'DevicePointer' method. For perf
23 * implications of these calls, see below.
24 *
25 * Accessing underling data and their perf implications:<br/>
26 * There are 4 scenarios to be considered here:
27 * HostVector and data on CPU --> no problems, std::vector returned immediately
28 * HostVector but data on GPU --> this causes a cudaMemcpy to be issued internally.
29 * subsequent calls to HostVector, will NOT incur this penalty.
30 * (assuming 'DevicePointer' is not called in between)
31 * DevicePointer but data on CPU --> this causes a cudaMemcpy to be issued internally.
32 * subsequent calls to DevicePointer, will NOT incur this penalty.
33 * (assuming 'HostVector' is not called in between)
34 * DevicePointer and data on GPU --> no problems, the device ptr
35 * will be returned immediately.
36 *
37 * What if xgboost is compiled without CUDA?<br/>
38 * In that case, there's a special implementation which always falls-back to
39 * working with std::vector. This logic can be found in host_device_vector.cc
40 *
41 * Why not consider CUDA unified memory?<br/>
42 * We did consider. However, it poses complications if we need to support both
43 * compiling with and without CUDA toolkit. It was easier to have
44 * 'HostDeviceVector' with a special-case implementation in host_device_vector.cc
45 *
46 * @note: Size and Devices methods are thread-safe.
47 */
48
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
69 /*!
70 * \brief Controls data access from the GPU.
71 *
72 * Since a `HostDeviceVector` can have data on both the host and device, access control needs to be
73 * maintained to keep the data consistent.
74 *
75 * There are 3 scenarios supported:
76 * - Data is being manipulated on device. GPU has write access, host doesn't have access.
77 * - Data is read-only on both the host and device.
78 * - Data is being manipulated on the host. Host has write access, device doesn't have access.
79 */
80 enum GPUAccess {
81 kNone, kRead,
82 // write implies read
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 /** @brief Resize and initialize the data if the new size is larger than the old size. */
141 void Resize(std::size_t new_size, T v);
142
143 using value_type = T; // NOLINT
144
145 private:
146 HostDeviceVectorImpl<T>* impl_;
147 };
148
149 } // namespace xgboost
150
151 #endif // XGBOOST_HOST_DEVICE_VECTOR_H_