zeroshade commented on code in PR #36489:
URL: https://github.com/apache/arrow/pull/36489#discussion_r1254578881
##########
cpp/src/arrow/c/bridge.cc:
##########
@@ -663,6 +672,115 @@ Status ExportRecordBatch(const RecordBatch& batch, struct
ArrowArray* out,
return Status::OK();
}
+//////////////////////////////////////////////////////////////////////////
+// C device arrays
+
+Result<std::pair<DeviceType, int64_t>> validate_device_info(const ArrayData&
data) {
+ DeviceType device_type = DeviceType::UNKNOWN;
+ int64_t device_id = -1;
+
+ for (const auto& buf : data.buffers) {
+ if (!buf) {
+ // some buffers might be null
+ // for example, the null bitmap is optional if there's no nulls
+ continue;
+ }
+
+ if (device_type == DeviceType::UNKNOWN) {
+ device_type = buf->device_type();
+ device_id = buf->device()->device_id();
+ continue;
+ }
+
+ if (buf->device_type() != device_type) {
+ return Status::Invalid(
+ "exporting device array with buffers on more than one device.");
+ }
+
+ if (buf->device()->device_id() != device_id) {
+ return Status::Invalid(
+ "exporting device array with buffers on multiple device ids.");
+ }
+ }
+
+ // recursively check the children
+ auto info = std::make_pair(device_type, device_id);
Review Comment:
hmm, I was trying to simplify the logic by not passing the device type and
id through the recursion like that. Technically though, we could probably avoid
the scenario you're suggesting by simply doing the recursive children check
*first* before I check `data.buffers`.
The only cases where `data.buffers` could be empty always have children or
the entire array has a length of 0. In both cases checking the children first
to either get `Unknown` (no children or length is 0) solves the problem without
having to pass the device type and id through the recursion, allowing me to
keep the logic simple. Thoughts?
--
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]