| 202 | |
| 203 | template <typename DT> |
| 204 | Result<DT*> ExportTensorImpl(const std::shared_ptr<Tensor>& t, bool copy) { |
| 205 | // Define DLDevice struct |
| 206 | ARROW_ASSIGN_OR_RAISE(auto device, ExportDevice(t)); |
| 207 | |
| 208 | // Define the DLDataType struct |
| 209 | const auto& type = *t->type(); |
| 210 | ARROW_ASSIGN_OR_RAISE(auto dtype, GetDLDataType(type)); |
| 211 | |
| 212 | // Compute strides |
| 213 | std::vector<int64_t> strides = {}; |
| 214 | strides.reserve(t->ndim()); |
| 215 | const auto byte_width = type.byte_width(); |
| 216 | for (auto i : t->strides()) { |
| 217 | strides.emplace_back(i / byte_width); |
| 218 | } |
| 219 | |
| 220 | auto params = ExportBufferParams<std::vector<int64_t>>{ |
| 221 | .size = t->size(), |
| 222 | .ndim = t->ndim(), |
| 223 | .strides = std::move(strides), |
| 224 | .shape = t->shape(), |
| 225 | .device = device, |
| 226 | .dtype = dtype, |
| 227 | }; |
| 228 | |
| 229 | if (copy) { |
| 230 | ARROW_ASSIGN_OR_RAISE(params.buffer, MemoryManager::CopyBuffer( |
| 231 | t->data(), default_cpu_memory_manager())); |
| 232 | // Since we make a copy only for the consumer, we do not need to mark it readonly. |
| 233 | params.flags = DLPACK_FLAG_BITMASK_IS_COPIED; |
| 234 | } else { |
| 235 | // Shared buffer with Arrow Tensor. |
| 236 | params.buffer = t->data(); |
| 237 | params.flags = t->is_mutable() ? 0 : DLPACK_FLAG_BITMASK_READ_ONLY; |
| 238 | } |
| 239 | |
| 240 | return ExportBuffer<DT>(std::move(params)); |
| 241 | } |
| 242 | |
| 243 | } // namespace |
| 244 |
nothing calls this directly
no test coverage detected