xgboost
linalg.h
Go to the documentation of this file.
1 
6 #ifndef XGBOOST_LINALG_H_
7 #define XGBOOST_LINALG_H_
8 
9 #include <xgboost/span.h>
12 
13 #include <array>
14 #include <algorithm>
15 #include <utility>
16 #include <vector>
17 
18 namespace xgboost {
24 template <typename T> class MatrixView {
25  int32_t device_;
26  common::Span<T> values_;
27  size_t strides_[2];
28  size_t shape_[2];
29 
30  template <typename Vec> static auto InferValues(Vec *vec, int32_t device) {
31  return device == GenericParameter::kCpuId ? vec->HostSpan()
32  : vec->DeviceSpan();
33  }
34 
35  public:
42  MatrixView(HostDeviceVector<T> *vec, std::array<size_t, 2> strides,
43  std::array<size_t, 2> shape, int32_t device)
44  : device_{device}, values_{InferValues(vec, device)} {
45  std::copy(strides.cbegin(), strides.cend(), strides_);
46  std::copy(shape.cbegin(), shape.cend(), shape_);
47  }
48  MatrixView(HostDeviceVector<std::remove_const_t<T>> const *vec,
49  std::array<size_t, 2> strides, std::array<size_t, 2> shape,
50  int32_t device)
51  : device_{device}, values_{InferValues(vec, device)} {
52  std::copy(strides.cbegin(), strides.cend(), strides_);
53  std::copy(shape.cbegin(), shape.cend(), shape_);
54  }
56  MatrixView(HostDeviceVector<T> *vec, std::array<size_t, 2> shape,
57  int32_t device)
58  : device_{device}, values_{InferValues(vec, device)} {
59  std::copy(shape.cbegin(), shape.cend(), shape_);
60  strides_[0] = shape[1];
61  strides_[1] = 1;
62  }
63  MatrixView(std::vector<T> *vec, std::array<size_t, 2> shape)
64  : device_{GenericParameter::kCpuId}, values_{*vec} {
65  CHECK_EQ(vec->size(), shape[0] * shape[1]);
66  std::copy(shape.cbegin(), shape.cend(), shape_);
67  strides_[0] = shape[1];
68  strides_[1] = 1;
69  }
70  MatrixView(HostDeviceVector<std::remove_const_t<T>> const *vec,
71  std::array<size_t, 2> shape, int32_t device)
72  : device_{device}, values_{InferValues(vec, device)} {
73  std::copy(shape.cbegin(), shape.cend(), shape_);
74  strides_[0] = shape[1];
75  strides_[1] = 1;
76  }
77 
78  XGBOOST_DEVICE T const &operator()(size_t r, size_t c) const {
79  return values_[strides_[0] * r + strides_[1] * c];
80  }
81  XGBOOST_DEVICE T &operator()(size_t r, size_t c) {
82  return values_[strides_[0] * r + strides_[1] * c];
83  }
84 
85  auto Strides() const { return strides_; }
86  auto Shape() const { return shape_; }
87  auto Values() const { return values_; }
88  auto Size() const { return shape_[0] * shape_[1]; }
89  auto DeviceIdx() const { return device_; }
90 };
91 
93 template <typename T> class VectorView {
94  MatrixView<T> matrix_;
95  size_t column_;
96 
97  public:
98  explicit VectorView(MatrixView<T> matrix, size_t column)
99  : matrix_{std::move(matrix)}, column_{column} {}
100 
101  XGBOOST_DEVICE T &operator[](size_t i) {
102  return matrix_(i, column_);
103  }
104 
105  XGBOOST_DEVICE T const &operator[](size_t i) const {
106  return matrix_(i, column_);
107  }
108 
109  size_t Size() { return matrix_.Shape()[0]; }
110  int32_t DeviceIdx() const { return matrix_.DeviceIdx(); }
111 };
112 } // namespace xgboost
113 #endif // XGBOOST_LINALG_H_
xgboost::MatrixView::operator()
XGBOOST_DEVICE T & operator()(size_t r, size_t c)
Definition: linalg.h:81
xgboost::MatrixView::Strides
auto Strides() const
Definition: linalg.h:85
xgboost::HostDeviceVector
Definition: host_device_vector.h:86
host_device_vector.h
A device-and-host vector abstraction layer.
xgboost::MatrixView::MatrixView
MatrixView(HostDeviceVector< std::remove_const_t< T >> const *vec, std::array< size_t, 2 > strides, std::array< size_t, 2 > shape, int32_t device)
Definition: linalg.h:48
xgboost::MatrixView::operator()
XGBOOST_DEVICE const T & operator()(size_t r, size_t c) const
Definition: linalg.h:78
xgboost::VectorView::Size
size_t Size()
Definition: linalg.h:109
span.h
xgboost::VectorView::DeviceIdx
int32_t DeviceIdx() const
Definition: linalg.h:110
xgboost::VectorView::operator[]
XGBOOST_DEVICE T & operator[](size_t i)
Definition: linalg.h:101
xgboost::MatrixView::MatrixView
MatrixView(std::vector< T > *vec, std::array< size_t, 2 > shape)
Definition: linalg.h:63
xgboost::MatrixView::DeviceIdx
auto DeviceIdx() const
Definition: linalg.h:89
xgboost::MatrixView::Values
auto Values() const
Definition: linalg.h:87
xgboost::MatrixView::MatrixView
MatrixView(HostDeviceVector< T > *vec, std::array< size_t, 2 > shape, int32_t device)
Row major constructor.
Definition: linalg.h:56
generic_parameters.h
xgboost::MatrixView::MatrixView
MatrixView(HostDeviceVector< std::remove_const_t< T >> const *vec, std::array< size_t, 2 > shape, int32_t device)
Definition: linalg.h:70
xgboost::VectorView::VectorView
VectorView(MatrixView< T > matrix, size_t column)
Definition: linalg.h:98
xgboost::common::Span< T >
xgboost::MatrixView::Size
auto Size() const
Definition: linalg.h:88
xgboost::MatrixView::Shape
auto Shape() const
Definition: linalg.h:86
xgboost::MatrixView
A view over a matrix on contiguous storage.
Definition: linalg.h:24
xgboost::MatrixView::MatrixView
MatrixView(HostDeviceVector< T > *vec, std::array< size_t, 2 > strides, std::array< size_t, 2 > shape, int32_t device)
Definition: linalg.h:42
xgboost::VectorView
A slice for 1 column of MatrixView. Can be extended to row if needed.
Definition: linalg.h:93
XGBOOST_DEVICE
#define XGBOOST_DEVICE
Tag function as usable by device.
Definition: base.h:84
xgboost::GenericParameter::kCpuId
static constexpr int32_t kCpuId
Definition: generic_parameters.h:17
xgboost::VectorView::operator[]
XGBOOST_DEVICE const T & operator[](size_t i) const
Definition: linalg.h:105
xgboost
namespace of xgboost
Definition: base.h:110