-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GH-39979: [Python] Low-level bindings for exporting/importing the C Device Interface #39980
Changes from 1 commit
4dfd0d6
3b28616
596175d
6ffc6b8
864a52c
7f78d83
d64f0e0
6e0870f
5e6c3d5
51064e3
2afbc63
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -652,3 +652,64 @@ def test_export_import_device_array(): | |
# Now released | ||
with assert_schema_released: | ||
pa.Array._import_from_c(ptr_array, ptr_schema) | ||
|
||
|
||
@needs_cffi | ||
def test_export_import_device_batch(): | ||
c_schema = ffi.new("struct ArrowSchema*") | ||
ptr_schema = int(ffi.cast("uintptr_t", c_schema)) | ||
c_array = ffi.new("struct ArrowDeviceArray*") | ||
ptr_array = int(ffi.cast("uintptr_t", c_array)) | ||
|
||
gc.collect() # Make sure no Arrow data dangles in a ref cycle | ||
old_allocated = pa.total_allocated_bytes() | ||
|
||
# Schema is known up front | ||
batch = make_batch() | ||
schema = batch.schema | ||
py_value = batch.to_pydict() | ||
batch._export_to_c_device(ptr_array) | ||
assert pa.total_allocated_bytes() > old_allocated | ||
|
||
# verify exported struct | ||
assert c_array.device_type == 1 # ARROW_DEVICE_CPU 1 | ||
assert c_array.device_id == -1 | ||
assert c_array.array.length == 2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could we add a test that uses the arrow cuda lib and verify the device etc.? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was planning to add actual cuda tests later in a separate PR (with proper roundtrip tests, not just export, but roundtrip doesn't work yet for non-cpu right now) |
||
|
||
# Delete and recreate C++ object from exported pointer | ||
del batch | ||
batch_new = pa.RecordBatch._import_from_c_device(ptr_array, schema) | ||
assert batch_new.to_pydict() == py_value | ||
assert batch_new.schema == schema | ||
assert pa.total_allocated_bytes() > old_allocated | ||
del batch_new, schema | ||
assert pa.total_allocated_bytes() == old_allocated | ||
# Now released | ||
with assert_array_released: | ||
pa.RecordBatch._import_from_c_device(ptr_array, make_schema()) | ||
|
||
# Type is exported and imported at the same time | ||
batch = make_batch() | ||
py_value = batch.to_pydict() | ||
batch._export_to_c_device(ptr_array, ptr_schema) | ||
# Delete and recreate C++ objects from exported pointers | ||
del batch | ||
batch_new = pa.RecordBatch._import_from_c_device(ptr_array, ptr_schema) | ||
assert batch_new.to_pydict() == py_value | ||
assert batch_new.schema == make_batch().schema | ||
assert pa.total_allocated_bytes() > old_allocated | ||
del batch_new | ||
assert pa.total_allocated_bytes() == old_allocated | ||
# Now released | ||
with assert_schema_released: | ||
pa.RecordBatch._import_from_c_device(ptr_array, ptr_schema) | ||
|
||
# Not a struct type | ||
pa.int32()._export_to_c(ptr_schema) | ||
make_batch()._export_to_c_device(ptr_array) | ||
with pytest.raises(ValueError, | ||
match="ArrowSchema describes non-struct type"): | ||
pa.RecordBatch._import_from_c_device(ptr_array, ptr_schema) | ||
# Now released | ||
with assert_schema_released: | ||
pa.RecordBatch._import_from_c_device(ptr_array, ptr_schema) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same comment as before, don't we want to allow using the pyarrow.cuda lib to provide a device mapper and hallow handling cuda-based gpu memory arrays?