felipecrv commented on PR #41477:
URL: https://github.com/apache/arrow/pull/41477#issuecomment-2116480668
The goal is to have a definitive function that can copy bytes from any
device buffer to CPU by delegating to `MemoryManager` who should know the best
way to do it. Having to write that code that @zeroshade wrote in `bridge.cc` to
copy 4 or 8 bytes from a `Buffer` was very annoying. Without getting into the
detail of what is fast and what is slow, I think there should be a helper for
doing this:
```cpp
if (device_type_ == DeviceAllocationType::kCPU) {
auto offsets = data_->GetValues<OffsetType>(offsets_buffer_id);
// Compute visible size of buffer
int64_t buffer_size =
(c_struct_->length > 0) ? byte_width * offsets[c_struct_->length]
: 0;
return ImportBuffer(buffer_id, buffer_size);
}
// we only need the value of the last offset so let's just copy that
// one value from device to host.
auto single_value_buf =
SliceBuffer(data_->buffers[offsets_buffer_id],
c_struct_->length * sizeof(OffsetType),
sizeof(OffsetType));
ARROW_ASSIGN_OR_RAISE(
auto cpubuf, Buffer::ViewOrCopy(single_value_buf,
default_cpu_memory_manager()));
auto offsets = cpubuf->data_as<OffsetType>();
// Compute visible size of buffer
int64_t buffer_size = (c_struct_->length > 0) ? byte_width * offsets[0]
: 0;
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]