| 937 | } // namespace |
| 938 | |
| 939 | Status WriteTensor(const Tensor& tensor, io::OutputStream* dst, int32_t* metadata_length, |
| 940 | int64_t* body_length) { |
| 941 | const int elem_size = tensor.type()->byte_width(); |
| 942 | |
| 943 | *body_length = tensor.size() * elem_size; |
| 944 | |
| 945 | // Tensor metadata accounts for padding |
| 946 | if (tensor.is_contiguous()) { |
| 947 | RETURN_NOT_OK(WriteTensorHeader(tensor, dst, metadata_length)); |
| 948 | auto data = tensor.data(); |
| 949 | if (data && data->data()) { |
| 950 | RETURN_NOT_OK(dst->Write(data->data(), *body_length)); |
| 951 | } else { |
| 952 | *body_length = 0; |
| 953 | } |
| 954 | } else { |
| 955 | // The tensor written is made contiguous |
| 956 | Tensor dummy(tensor.type(), nullptr, tensor.shape()); |
| 957 | RETURN_NOT_OK(WriteTensorHeader(dummy, dst, metadata_length)); |
| 958 | |
| 959 | // TODO: Do we care enough about this temporary allocation to pass in a |
| 960 | // MemoryPool to this function? |
| 961 | ARROW_ASSIGN_OR_RAISE(auto scratch_space, |
| 962 | AllocateBuffer(tensor.shape()[tensor.ndim() - 1] * elem_size)); |
| 963 | |
| 964 | RETURN_NOT_OK(WriteStridedTensorData(0, 0, elem_size, tensor, |
| 965 | scratch_space->mutable_data(), dst)); |
| 966 | } |
| 967 | |
| 968 | return Status::OK(); |
| 969 | } |
| 970 | |
| 971 | Result<std::unique_ptr<Message>> GetTensorMessage(const Tensor& tensor, |
| 972 | MemoryPool* pool) { |