7 #ifndef XGBOOST_LINALG_H_
8 #define XGBOOST_LINALG_H_
10 #include <dmlc/endian.h>
24 #include <type_traits>
34 #if defined(__CUDA__) || defined(__NVCC__)
35 #define LINALG_HD __host__ __device__
47 return (std::is_floating_point_v<T>
49 : (std::is_integral_v<T> ? (std::is_signed_v<T> ?
'i' :
'u') :
'\0'));
53 template <
size_t dim,
typename S,
typename Head,
size_t D>
54 constexpr
size_t Offset(S (&strides)[D],
size_t n, Head head) {
55 static_assert(dim < D);
56 return n + head * strides[dim];
59 template <
size_t dim,
typename S,
size_t D,
typename Head,
typename... Tail>
60 constexpr std::enable_if_t<
sizeof...(Tail) != 0,
size_t>
Offset(S (&strides)[D],
size_t n,
61 Head head, Tail &&...rest) {
62 static_assert(dim < D);
63 return Offset<dim + 1>(strides, n + (head * strides[dim]), std::forward<Tail>(rest)...);
66 template <
int32_t D,
bool f_array = false>
67 constexpr
void CalcStride(
size_t const (&shape)[D],
size_t (&stride)[D]) {
70 for (int32_t s = 1; s < D; ++s) {
71 stride[s] = shape[s - 1] * stride[s - 1];
75 for (int32_t s = D - 2; s >= 0; --s) {
76 stride[s] = shape[s + 1] * stride[s + 1];
89 [[nodiscard]] constexpr
size_t Size()
const {
return end -
beg; }
97 return std::is_same_v<T, IntTag> ? 0 : 1;
100 template <
typename T,
typename... S>
101 constexpr std::enable_if_t<
sizeof...(S) != 0, int32_t>
CalcSliceDim() {
108 for (
auto d : shape) {
114 template <
typename S>
117 template <
typename S>
120 template <
int32_t n,
typename Fn>
122 #if defined __CUDA_ARCH__
125 for (int32_t i = 0; i < n; ++i) {
130 template <
typename T>
133 for (; v != 0; v &= v - 1) c++;
138 #if defined(__CUDA_ARCH__)
140 #elif defined(__GNUC__) || defined(__clang__)
141 return __builtin_popcount(v);
142 #elif defined(_MSC_VER)
150 #if defined(__CUDA_ARCH__)
152 #elif defined(__GNUC__) || defined(__clang__)
153 return __builtin_popcountll(v);
154 #elif defined(_MSC_VER) && defined(_M_X64)
155 return __popcnt64(v);
161 template <std::
size_t D,
typename Head>
163 static_assert(std::is_integral_v<std::remove_reference_t<Head>>,
"Invalid index type.");
170 template <std::size_t D,
typename Head,
typename... Rest>
172 static_assert(
sizeof...(Rest) < D,
"Index overflow.");
173 static_assert(std::is_integral_v<std::remove_reference_t<Head>>,
"Invalid index type.");
174 arr[D -
sizeof...(Rest) - 1] = head;
175 IndexToArr(arr, std::forward<Rest>(index)...);
178 template <
class T, std::size_t N, std::size_t... Idx>
179 constexpr
auto ArrToTuple(T (&arr)[N], std::index_sequence<Idx...>) {
180 return std::make_tuple(arr[Idx]...);
186 template <
class T, std::
size_t N>
188 return ArrToTuple(arr, std::make_index_sequence<N>{});
194 template <
typename I, std::
int32_t D>
196 std::size_t index[D]{0};
197 static_assert(std::is_signed_v<decltype(D)>,
198 "Don't change the type without changing the for loop.");
199 auto const sptr = shape.
data();
200 for (int32_t dim = D; --dim > 0;) {
201 auto s =
static_cast<std::remove_const_t<std::remove_reference_t<I>
>>(sptr[dim]);
204 index[dim] = idx - t * s;
207 index[dim] = idx & (s - 1);
215 template <
size_t dim,
typename I,
int32_t D>
217 static_assert(dim < D);
221 template <
size_t dim, int32_t D,
typename... S,
typename I,
222 std::enable_if_t<
sizeof...(S) != 0> * =
nullptr>
224 static_assert(dim < D);
226 ReshapeImpl<dim + 1>(out_shape, std::forward<S>(rest)...);
236 template <
class B1,
class... Bn>
238 : std::conditional_t<static_cast<bool>(B1::value), Conjunction<Bn...>, B1> {};
240 template <
typename... Index>
243 template <
typename... Index>
254 template <
typename I>
277 template <
typename T, std::
int32_t kDim>
305 template <
size_t old_dim,
size_t new_dim, std::
int32_t D,
typename I>
306 LINALG_HD SizeType MakeSliceDim(std::size_t new_shape[D], std::size_t new_stride[D],
307 detail::RangeTag<I> &&range)
const {
308 static_assert(new_dim < D);
309 static_assert(old_dim < kDim);
310 new_stride[new_dim] = stride_[old_dim];
311 new_shape[new_dim] = range.Size();
312 assert(
static_cast<decltype(shape_[old_dim])
>(range.end) <= shape_[old_dim]);
314 auto offset = stride_[old_dim] * range.beg;
320 template <
size_t old_dim,
size_t new_dim, int32_t D,
typename I,
typename... S>
322 detail::RangeTag<I> &&range, S &&...slices)
const {
323 static_assert(new_dim < D);
324 static_assert(old_dim < kDim);
325 new_stride[new_dim] = stride_[old_dim];
326 new_shape[new_dim] = range.Size();
327 assert(
static_cast<decltype(shape_[old_dim])
>(range.end) <= shape_[old_dim]);
329 auto offset = stride_[old_dim] * range.beg;
330 return MakeSliceDim<old_dim + 1, new_dim + 1, D>(new_shape, new_stride,
331 std::forward<S>(slices)...) +
335 template <
size_t old_dim,
size_t new_dim,
int32_t D>
336 LINALG_HD SizeType MakeSliceDim(
size_t new_shape[D],
size_t new_stride[D], detail::AllTag)
const {
337 static_assert(new_dim < D);
338 static_assert(old_dim < kDim);
339 new_stride[new_dim] = stride_[old_dim];
340 new_shape[new_dim] = shape_[old_dim];
346 template <
size_t old_dim,
size_t new_dim, int32_t D,
typename... S>
347 LINALG_HD SizeType MakeSliceDim(
size_t new_shape[D],
size_t new_stride[D], detail::AllTag,
348 S &&...slices)
const {
349 static_assert(new_dim < D);
350 static_assert(old_dim < kDim);
351 new_stride[new_dim] = stride_[old_dim];
352 new_shape[new_dim] = shape_[old_dim];
353 return MakeSliceDim<old_dim + 1, new_dim + 1, D>(new_shape, new_stride,
354 std::forward<S>(slices)...);
357 template <
size_t old_dim,
size_t new_dim,
int32_t D,
typename Index>
359 [[maybe_unused]]
size_t new_stride[D], Index i)
const {
360 static_assert(old_dim < kDim);
361 return stride_[old_dim] * i;
366 template <
size_t old_dim,
size_t new_dim, int32_t D,
typename Index,
typename... S>
367 LINALG_HD std::enable_if_t<std::is_integral_v<Index>,
size_t> MakeSliceDim(
size_t new_shape[D],
368 size_t new_stride[D],
370 S &&...slices)
const {
371 static_assert(old_dim < kDim);
372 auto offset = stride_[old_dim] * i;
374 MakeSliceDim<old_dim + 1, new_dim, D>(new_shape, new_stride, std::forward<S>(slices)...);
394 template <
typename I, std::
int32_t D>
398 template <
typename I,
int32_t D>
400 : data_{data}, ptr_{data_.data()}, device_{device} {
401 static_assert(D > 0 && D <= kDim,
"Invalid shape.");
403 detail::UnrollLoop<D>([&](
auto i) { shape_[i] = shape[i]; });
404 for (
auto i = D; i < kDim; ++i) {
414 detail::CalcStride<kDim, true>(shape_, stride_);
429 template <
typename I, std::
int32_t D>
432 : data_{data}, ptr_{data_.data()}, device_{device} {
433 static_assert(D == kDim,
"Invalid shape & stride.");
434 detail::UnrollLoop<D>([&](
auto i) {
435 shape_[i] = shape[i];
436 stride_[i] = stride[i];
443 std::enable_if_t<common::detail::IsAllowedElementTypeConversion<U, T>::value> * =
nullptr>
445 : data_{that.
Values()}, ptr_{data_.data()}, size_{that.
Size()}, device_{that.
Device()} {
446 detail::UnrollLoop<kDim>([&](
auto i) {
447 stride_[i] = that.
Stride(i);
448 shape_[i] = that.
Shape(i);
467 static_assert(
sizeof...(index) <= kDim,
"Invalid index.");
468 size_t offset = detail::Offset<0ul>(stride_, 0ul, std::forward<Index>(index)...);
469 assert(offset < data_.
size() &&
"Out of bound access.");
477 static_assert(
sizeof...(index) <= kDim,
"Invalid index.");
478 size_t offset = detail::Offset<0ul>(stride_, 0ul, std::forward<Index>(index)...);
479 assert(offset < data_.
size() &&
"Out of bound access.");
496 template <
typename... S>
498 static_assert(
sizeof...(slices) <= kDim,
"Invalid slice.");
499 int32_t constexpr kNewDim{detail::CalcSliceDim<detail::IndexToTag<S>...>()};
500 size_t new_shape[kNewDim];
501 size_t new_stride[kNewDim];
502 auto offset = MakeSliceDim<0, 0, kNewDim>(new_shape, new_stride, std::forward<S>(slices)...);
525 [[nodiscard]]
bool Empty()
const {
return Size() == 0; }
537 static_assert(std::is_same_v<decltype(stride), decltype(stride_)>);
547 static_assert(std::is_same_v<decltype(stride), decltype(stride_)>);
549 detail::CalcStride<kDim, true>(shape_, stride);
565 template <
typename Container,
typename... S,
566 std::enable_if_t<!common::detail::IsSpan<Container>::value &&
567 !std::is_pointer_v<Container>> * =
nullptr>
569 using T = std::conditional_t<std::is_const_v<Container>,
570 std::add_const_t<typename Container::value_type>,
571 typename Container::value_type>;
572 std::size_t in_shape[
sizeof...(S)];
579 std::size_t in_shape[
sizeof...(S)];
581 return TensorView<T,
sizeof...(S)>{data, in_shape, device};
591 std::size_t in_shape[
sizeof...(S)];
596 template <
typename T,
typename... S>
602 template <
typename T,
typename... S>
613 if (idx > std::numeric_limits<uint32_t>::max()) {
614 return detail::UnravelImpl<uint64_t, D>(
static_cast<uint64_t
>(idx), shape);
616 return detail::UnravelImpl<uint32_t, D>(
static_cast<uint32_t
>(idx), shape);
625 template <
typename... S>
627 std::size_t s[
sizeof...(S)];
637 template <
typename T>
647 template <
typename T>
652 template <
typename T>
657 template <
typename T>
663 template <
typename T>
669 template <
typename T>
680 template <
typename T>
689 template <
typename T, std::
int32_t D>
692 array_interface[
"data"] = std::vector<Json>(2);
693 array_interface[
"data"][0] =
Integer{
reinterpret_cast<int64_t
>(t.
Values().data())};
694 array_interface[
"data"][1] =
Boolean{
true};
695 if (t.
Device().IsCUDA()) {
697 array_interface[
"stream"] =
Integer{2};
699 std::vector<Json> shape(t.
Shape().size());
700 std::vector<Json> stride(t.
Stride().size());
701 for (
size_t i = 0; i < t.
Shape().size(); ++i) {
705 array_interface[
"shape"] =
Array{shape};
706 array_interface[
"strides"] =
Array{stride};
707 array_interface[
"version"] = 3;
709 char constexpr kT = detail::ArrayInterfaceHandler::TypeChar<T>();
710 static_assert(kT !=
'\0');
711 if (DMLC_LITTLE_ENDIAN) {
712 array_interface[
"typestr"] =
String{
"<" + (kT + std::to_string(
sizeof(T)))};
714 array_interface[
"typestr"] =
String{
">" + (kT + std::to_string(
sizeof(T)))};
716 return array_interface;
722 template <
typename T,
int32_t D>
726 res[
"data"][1] =
Boolean{
false};
733 template <
typename T,
int32_t D>
740 template <
typename T,
int32_t D>
747 template <
typename T>
759 template <
typename T,
int32_t kDim = 5>
770 template <
typename I, std::
int32_t D>
771 void Initialize(I
const (&shape)[D],
DeviceOrd device) {
772 static_assert(D <= kDim,
"Invalid shape.");
773 std::copy(shape, shape + D, shape_);
774 for (
auto i = D; i < kDim; ++i) {
777 if (!device.
IsCPU()) {
793 template <
typename I,
int32_t D>
795 :
Tensor{common::
Span<I const, D>{shape}, device, order} {}
797 template <
typename I,
size_t D>
801 std::copy(shape.
data(), shape.
data() + D, shape_);
802 for (
auto i = D; i < kDim; ++i) {
806 if (!device.
IsCPU()) {
810 if (!device.
IsCPU()) {
817 template <
typename It,
typename I,
int32_t D>
821 h_vec.insert(h_vec.begin(), begin, end);
823 this->Initialize(shape, device);
826 template <
typename I,
int32_t D>
833 this->Initialize(shape, device);
839 template <
typename... Index>
841 return this->HostView()(std::forward<Index>(idx)...);
847 template <
typename... Index>
849 return this->HostView()(std::forward<Index>(idx)...);
856 if (device.
IsCPU()) {
866 if (device.
IsCPU()) {
879 [[nodiscard]] std::size_t
Size()
const {
return data_.
Size(); }
880 [[nodiscard]]
bool Empty()
const {
return Size() == 0; }
883 auto Shape(
size_t i)
const {
return shape_[i]; }
894 template <
typename Fn>
898 <<
"Inconsistent size after modification.";
908 static_assert(
sizeof...(S) <= kDim,
"Invalid shape.");
909 detail::ReshapeImpl<0>(shape_, std::forward<S>(s)...);
910 auto constexpr kEnd =
sizeof...(S);
911 static_assert(kEnd <= kDim,
"Invalid shape.");
912 std::fill(shape_ + kEnd, shape_ + kDim, 1);
924 static_assert(D <= kDim,
"Invalid shape.");
925 std::copy(shape.
data(), shape.
data() + D, this->shape_);
926 std::fill(shape_ + D, shape_ + kDim, 1);
938 template <
typename... S>
940 return this->HostView().Slice(std::forward<S>(slices)...);
945 template <
typename... S>
947 return this->HostView().Slice(std::forward<S>(slices)...);
957 template <
typename T>
960 template <
typename T>
966 template <
typename T,
typename... Index>
968 Tensor<T,
sizeof...(Index)> t;
977 template <
typename T, std::
int32_t kDim>
988 template <
typename T,
typename... Index>
990 Tensor<T,
sizeof...(Index)> t;
993 t.Data()->Fill(std::move(v));
1000 template <
typename T,
typename... Index>
1002 return Constant(ctx,
static_cast<T
>(0), index...);
1006 template <
typename T,
int32_t D>
1012 for (
size_t i = 1; i < D; ++i) {
1013 if (shape[i] == 0) {
1014 shape[i] = r.
Shape(i);
1016 CHECK_EQ(shape[i], r.
Shape(i));
1027 template <
typename T>
1029 std::size_t shape[2]{x.
Shape(0), 1};
1030 std::size_t stride[2]{x.
Stride(0), 1};
1035 #if defined(LINALG_HD)
Defines configuration macros and basic types for xgboost.
Definition: host_device_vector.h:89
const T * ConstDevicePointer() const
void Extend(const HostDeviceVector< T > &other)
common::Span< T const > ConstHostSpan() const
Definition: host_device_vector.h:118
std::vector< T > & HostVector()
common::Span< const T > ConstDeviceSpan() const
T * HostPointer()
Definition: host_device_vector.h:115
common::Span< T > DeviceSpan()
common::Span< T > HostSpan()
Definition: host_device_vector.h:116
void SetDevice(DeviceOrd device) const
void Resize(std::size_t new_size)
const T * ConstHostPointer() const
Definition: host_device_vector.h:119
Describes both true and false.
Definition: json.h:354
Data structure representing JSON format.
Definition: json.h:396
static void Dump(Json json, std::string *out, std::ios::openmode mode=std::ios::out)
Encode the JSON object. Optional parameter mode for choosing between text and binary (ubjson) output.
span class implementation, based on ISO++20 span<T>. The interface should be the same.
Definition: span.h:435
constexpr XGBOOST_DEVICE pointer data() const __span_noexcept
Definition: span.h:554
XGBOOST_DEVICE auto subspan() const -> Span< element_type, detail::ExtentValue< Extent, Offset, Count >::value >
Definition: span.h:601
constexpr XGBOOST_DEVICE index_type size() const __span_noexcept
Definition: span.h:559
constexpr XGBOOST_DEVICE bool empty() const __span_noexcept
Definition: span.h:566
A tensor view with static type and dimension. It implements indexing and slicing.
Definition: linalg.h:278
LINALG_HD std::size_t Size() const
Number of items in the tensor.
Definition: linalg.h:524
LINALG_HD TensorView(common::Span< T > data, I const (&shape)[D], DeviceOrd device)
Create a tensor with data and shape.
Definition: linalg.h:395
std::remove_cv_t< T > value_type
Definition: linalg.h:285
T element_type
Definition: linalg.h:284
LINALG_HD bool CContiguous() const
Whether it's a c-contiguous array.
Definition: linalg.h:535
LINALG_HD auto Stride(size_t i) const
Definition: linalg.h:519
LINALG_HD TensorView(common::Span< T > data, I const (&shape)[D], DeviceOrd device, Order order)
Definition: linalg.h:399
LINALG_HD auto Shape() const
Definition: linalg.h:510
ShapeT StrideT
Definition: linalg.h:281
constexpr static size_t kDimension
Definition: linalg.h:380
LINALG_HD auto Stride() const
Definition: linalg.h:515
LINALG_HD auto Slice(S &&...slices) const
Slice the tensor. The returned tensor has inferred dim and shape. Scalar result is not supported.
Definition: linalg.h:497
LINALG_HD auto Values() const -> decltype(data_) const &
Obtain a reference to the raw data.
Definition: linalg.h:555
LINALG_HD bool Contiguous() const
Whether this is a contiguous array, both C and F contiguous returns true.
Definition: linalg.h:529
bool Empty() const
Definition: linalg.h:525
LINALG_HD T const & operator()(Index &&...index) const
Index the tensor to obtain a scalar value.
Definition: linalg.h:476
std::size_t SizeType
Definition: linalg.h:282
LINALG_HD TensorView(TensorView< U, kDim > const &that)
Definition: linalg.h:444
LINALG_HD T & operator()(Index &&...index)
Index the tensor to obtain a scalar value.
Definition: linalg.h:466
constexpr static size_t kValueSize
Definition: linalg.h:379
LINALG_HD bool FContiguous() const
Whether it's a f-contiguous array.
Definition: linalg.h:545
LINALG_HD auto Shape(size_t i) const
Definition: linalg.h:514
LINALG_HD TensorView(common::Span< T > data, I const (&shape)[D], I const (&stride)[D], DeviceOrd device)
Create a tensor with data, shape and strides. Don't use this constructor if stride can be calculated ...
Definition: linalg.h:430
std::size_t[kDim] ShapeT
Definition: linalg.h:280
LINALG_HD auto Device() const
Obtain the CUDA device ordinal.
Definition: linalg.h:559
A tensor storage. To use it for other functionality like slicing one needs to obtain a view first....
Definition: linalg.h:760
auto Slice(S &&...slices)
Get a host view on the slice.
Definition: linalg.h:946
bool Empty() const
Definition: linalg.h:880
Tensor(It begin, It end, I const (&shape)[D], DeviceOrd device, Order order=kC)
Definition: linalg.h:818
auto Slice(S &&...slices) const
Get a host view on the slice.
Definition: linalg.h:939
HostDeviceVector< T > const * Data() const
Definition: linalg.h:886
void Reshape(size_t(&shape)[D])
Definition: linalg.h:932
auto View(DeviceOrd device) const
Definition: linalg.h:865
auto HostView()
Definition: linalg.h:876
auto Shape(size_t i) const
Definition: linalg.h:883
HostDeviceVector< T > * Data()
Definition: linalg.h:885
T & operator()(Index &&...idx)
Index operator. Not thread safe, should not be used in performance critical region....
Definition: linalg.h:840
auto View(DeviceOrd device)
Get a TensorView for this tensor.
Definition: linalg.h:855
Tensor(common::Span< I const, D > shape, DeviceOrd device, Order order=kC)
Definition: linalg.h:798
auto Shape() const
Definition: linalg.h:882
void ModifyInplace(Fn &&fn)
Visitor function for modification that changes shape and data.
Definition: linalg.h:895
void SetDevice(DeviceOrd device) const
Set device ordinal for this tensor.
Definition: linalg.h:953
Tensor(std::initializer_list< T > data, I const (&shape)[D], DeviceOrd device, Order order=kC)
Definition: linalg.h:827
void Reshape(common::Span< size_t const, D > shape)
Reshape the tensor.
Definition: linalg.h:923
DeviceOrd Device() const
Definition: linalg.h:954
auto HostView() const
Definition: linalg.h:877
Tensor(I const (&shape)[D], DeviceOrd device, Order order=kC)
Create a tensor with shape and device ordinal. The storage is initialized automatically.
Definition: linalg.h:794
T const & operator()(Index &&...idx) const
Index operator. Not thread safe, should not be used in performance critical region....
Definition: linalg.h:848
std::size_t[kDim] ShapeT
Definition: linalg.h:762
void Reshape(S &&...s)
Reshape the tensor.
Definition: linalg.h:907
ShapeT StrideT
Definition: linalg.h:763
std::size_t Size() const
Definition: linalg.h:879
A device-and-host vector abstraction layer.
#define LINALG_HD
Definition: linalg.h:37
constexpr std::size_t dynamic_extent
Definition: span.h:150
Span(std::vector< T > const &) -> Span< T const >
std::conditional_t< std::is_integral_v< RemoveCRType< S > >, IntTag, S > IndexToTag
Definition: linalg.h:118
LINALG_HD auto UnravelImpl(I idx, common::Span< size_t const, D > shape)
Definition: linalg.h:195
void ReshapeImpl(size_t(&out_shape)[D], I s)
Definition: linalg.h:216
LINALG_HD int Popc(uint32_t v)
Definition: linalg.h:137
std::remove_const_t< std::remove_reference_t< S > > RemoveCRType
Definition: linalg.h:115
constexpr int32_t CalcSliceDim()
Calculate the dimension of sliced tensor.
Definition: linalg.h:96
constexpr LINALG_HD auto UnrollLoop(Fn fn)
Definition: linalg.h:121
constexpr size_t Offset(S(&strides)[D], size_t n, Head head)
Definition: linalg.h:54
LINALG_HD void IndexToArr(std::size_t(&arr)[D], Head head)
Definition: linalg.h:162
constexpr void CalcStride(size_t const (&shape)[D], size_t(&stride)[D])
Definition: linalg.h:67
constexpr auto ArrToTuple(T(&arr)[N], std::index_sequence< Idx... >)
Definition: linalg.h:179
int32_t NativePopc(T v)
Definition: linalg.h:131
std::enable_if_t< IsAllIntegral< Index... >::value > EnableIfIntegral
Definition: linalg.h:244
constexpr size_t CalcSize(size_t(&shape)[D])
Definition: linalg.h:106
constexpr detail::RangeTag< I > Range(I beg, I end)
Specify a range of elements in the axis for slicing.
Definition: linalg.h:255
auto Make1dInterface(T const *vec, std::size_t len)
Definition: linalg.h:748
auto EmptyLike(Context const *ctx, Tensor< T, kDim > const &in)
Create an array with the same shape and dtype as the input.
Definition: linalg.h:978
MatrixView< T > ExpandDim(VectorView< T > x)
Push an extra dim to the end.
Definition: linalg.h:1028
auto MakeTensorView(Context const *ctx, Container &data, S &&...shape)
Constructor for automatic type deduction.
Definition: linalg.h:568
auto ArrayInterfaceStr(TensorView< T const, D > const &t)
Return string representation of array interface.
Definition: linalg.h:734
auto MakeVec(T *ptr, size_t s, DeviceOrd device=DeviceOrd::CPU())
Create a vector view from contigious memory.
Definition: linalg.h:648
LINALG_HD auto UnravelIndex(size_t idx, common::Span< size_t const, D > shape)
Turns linear index into multi-dimension index. Similar to numpy unravel.
Definition: linalg.h:612
void Stack(Tensor< T, D > *l, Tensor< T, D > const &r)
Definition: linalg.h:1007
auto Constant(Context const *ctx, T v, Index &&...index)
Create an array with value v.
Definition: linalg.h:989
auto Zeros(Context const *ctx, Index &&...index)
Like np.zeros, return a new array of given shape and type, filled with zeros.
Definition: linalg.h:1001
auto Empty(Context const *ctx, Index &&...index)
Create an array without initialization.
Definition: linalg.h:967
constexpr detail::AllTag All()
Specify all elements in the axis for slicing.
Definition: linalg.h:250
Json ArrayInterface(TensorView< T const, D > const &t)
Array Interface defined by numpy.
Definition: linalg.h:690
Order
Definition: linalg.h:259
@ kC
Definition: linalg.h:260
@ kF
Definition: linalg.h:261
JsonInteger Integer
Definition: json.h:625
#define SPAN_CHECK(cond)
Definition: span.h:127
Runtime context for XGBoost. Contains information like threads and device.
Definition: context.h:142
DeviceOrd Device() const
Get the current device and ordinal.
Definition: context.h:207
bool IsCPU() const
Is XGBoost running on CPU?
Definition: context.h:182
A type for device ordinal. The type is packed into 32-bit for efficient use in viewing types like lin...
Definition: context.h:40
bool IsCUDA() const
Definition: context.h:55
bool IsCPU() const
Definition: context.h:56
constexpr static auto CPU()
Constructor for CPU.
Definition: context.h:73
static constexpr char TypeChar()
Definition: linalg.h:46
constexpr size_t Size() const
Definition: linalg.h:89
I end
Definition: linalg.h:88
I beg
Definition: linalg.h:87