| 688 | */ |
| 689 | template <typename T, std::int32_t D> |
| 690 | Json ArrayInterface(TensorView<T const, D> const &t) { |
| 691 | Json array_interface{Object{}}; |
| 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()) { |
| 696 | // Change this once we have different CUDA stream. |
| 697 | array_interface["stream"] = Integer{2}; |
| 698 | } |
| 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) { |
| 702 | shape[i] = Integer(t.Shape(i)); |
| 703 | stride[i] = Integer(t.Stride(i) * sizeof(T)); |
| 704 | } |
| 705 | array_interface["shape"] = Array{shape}; |
| 706 | array_interface["strides"] = Array{stride}; |
| 707 | array_interface["version"] = 3; |
| 708 | |
| 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)))}; |
| 713 | } else { |
| 714 | array_interface["typestr"] = String{">" + (kT + std::to_string(sizeof(T)))}; |
| 715 | } |
| 716 | return array_interface; |
| 717 | } |
| 718 | |
| 719 | /** |
| 720 | * \brief Same as const version, but returns non-readonly data pointer. |