Medial Code Documentation
Loading...
Searching...
No Matches
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
59namespace xgboost {
60
61#ifdef __CUDACC__
62// Sets a function to call instead of cudaSetDevice();
63// only added for testing
64void SetCudaSetDeviceHandler(void (*handler)(int));
65#endif // __CUDACC__
66
67template <typename T> struct HostDeviceVectorImpl;
68
81 kNone, kRead,
82 // write implies read
83 kWrite
84};
85
86template <typename T>
88 static_assert(std::is_standard_layout<T>::value, "HostDeviceVector admits only POD types");
89
90 public:
91 explicit HostDeviceVector(size_t size = 0, T v = T(), int device = -1);
92 HostDeviceVector(std::initializer_list<T> init, int device = -1);
93 explicit HostDeviceVector(const std::vector<T>& init, int device = -1);
95
98
99 HostDeviceVector<T>& operator=(const HostDeviceVector<T>&) = delete;
101
102 bool Empty() const { return Size() == 0; }
103 size_t Size() const;
104 int DeviceIdx() const;
105 common::Span<T> DeviceSpan();
106 common::Span<const T> ConstDeviceSpan() const;
107 common::Span<const T> DeviceSpan() const { return ConstDeviceSpan(); }
108 T* DevicePointer();
109 const T* ConstDevicePointer() const;
110 const T* DevicePointer() const { return ConstDevicePointer(); }
111
112 T* HostPointer() { return HostVector().data(); }
113 common::Span<T> HostSpan() { return common::Span<T>{HostVector()}; }
114 common::Span<T const> HostSpan() const { return common::Span<T const>{HostVector()}; }
115 common::Span<T const> ConstHostSpan() const { return HostSpan(); }
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 bool HostCanRead() const;
131 bool HostCanWrite() const;
132 bool DeviceCanRead() const;
133 bool DeviceCanWrite() const;
134 GPUAccess DeviceAccess() const;
135
136 void SetDevice(int device) const;
137 void SetDevice(DeviceOrd device) const;
138
139 void Resize(size_t new_size, T v = T());
140
141 using value_type = T; // NOLINT
142
143 private:
145};
146
147} // namespace xgboost
148
149#endif // XGBOOST_HOST_DEVICE_VECTOR_H_
Definition host_device_vector.h:87
span class implementation, based on ISO++20 span<T>. The interface should be the same.
Definition span.h:424
Copyright 2014-2023, XGBoost Contributors.
namespace of xgboost
Definition base.h:90
GPUAccess
Controls data access from the GPU.
Definition host_device_vector.h:80
A type for device ordinal.
Definition context.h:31
Definition host_device_vector.cc:19