Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4.3k
GH-39979: [Python] Low-level bindings for exporting/importing the C Device Interface#39980
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
4dfd0d63b28616596175d6ffc6b8864a52c7f78d83d64f0e06e0870f5e6c3d551064e32afbc63File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1778,6 +1778,70 @@ cdef class Array(_PandasConvertible): | ||
| return pyarrow_wrap_array(array) | ||
| def _export_to_c_device(self, out_ptr, out_schema_ptr=0): | ||
Member 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.
MemberAuthor 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 would propose to leave this as is, to keep it consistent with the other | ||
| """ | ||
| Export to a C ArrowDeviceArray struct, given its pointer. | ||
| If a C ArrowSchema struct pointer is also given, the array type | ||
| is exported to it at the same time. | ||
| Parameters | ||
| ---------- | ||
| out_ptr: int | ||
| The raw pointer to a C ArrowDeviceArray struct. | ||
| out_schema_ptr: int (optional) | ||
| The raw pointer to a C ArrowSchema struct. | ||
| Be careful: if you don't pass the ArrowDeviceArray struct to a consumer, | ||
| array memory will leak. This is a low-level function intended for | ||
| expert users. | ||
Comment on lines
+1795
to
+1797
Member 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. Should this explicitly mention the release callback on the struct? MemberAuthor 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 copied this from the existing docstrings. We could mention the release callback explicitly, but essentially then you are a "consumer". This functions returns an integer, you can't call the release callback on the return value as such. Only when you actually interpret it as an ArrowArray struct, you can do that (and at that point, you are a consumer who should be aware of those details?) Member 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 agree with @jorisvandenbossche that the release callback need not be mentioned here. This is all in the spec. | ||
| """ | ||
| cdef: | ||
| void* c_ptr = _as_c_pointer(out_ptr) | ||
| void* c_schema_ptr = _as_c_pointer(out_schema_ptr, | ||
| allow_null=True) | ||
| with nogil: | ||
| check_status(ExportDeviceArray( | ||
| deref(self.sp_array), <shared_ptr[CSyncEvent]>NULL, | ||
| <ArrowDeviceArray*> c_ptr, <ArrowSchema*> c_schema_ptr)) | ||
| @staticmethod | ||
| def _import_from_c_device(in_ptr, type): | ||
| """ | ||
| Import Array from a C ArrowDeviceArray struct, given its pointer | ||
| and the imported array type. | ||
| Parameters | ||
| ---------- | ||
| in_ptr: int | ||
| The raw pointer to a C ArrowDeviceArray struct. | ||
| type: DataType or int | ||
| Either a DataType object, or the raw pointer to a C ArrowSchema | ||
| struct. | ||
| This is a low-level function intended for expert users. | ||
| """ | ||
| cdef: | ||
| void* c_ptr = _as_c_pointer(in_ptr) | ||
| void* c_type_ptr | ||
| shared_ptr[CArray] c_array | ||
| c_type = pyarrow_unwrap_data_type(type) | ||
| if c_type == nullptr: | ||
| # Not a DataType object, perhaps a raw ArrowSchema pointer | ||
| c_type_ptr = _as_c_pointer(type) | ||
| with nogil: | ||
| c_array = GetResultValue( | ||
| ImportDeviceArray(<ArrowDeviceArray*> c_ptr, | ||
| <ArrowSchema*> c_type_ptr) | ||
| ) | ||
Comment on lines
+1834
to
+1837
Member 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. The default mapper is only allowing CPU arrays, but pyarrow does have a cuda lib, shouldn't we allow and enable importing at least CUDA arrays too? MemberAuthor 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. Certainly, but as mentioned earlier (#39980 (comment)), I was planning to tackle CUDA in a follow-up, and this PR indeed only properly supports and tests CPU. | ||
| else: | ||
| with nogil: | ||
| c_array = GetResultValue( | ||
| ImportDeviceArray(<ArrowDeviceArray*> c_ptr, c_type) | ||
| ) | ||
| return pyarrow_wrap_array(c_array) | ||
| def __dlpack__(self, stream=None): | ||
| """Export a primitive array as a DLPack capsule. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -64,6 +64,16 @@ | ||
| // Opaque producer-specific data | ||
| void* private_data; | ||
| }; | ||
| typedef int32_t ArrowDeviceType; | ||
Member 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. should we expose the constants in pyarrow somehow? MemberAuthor 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. If we don't use them ourselves, I don't know if that is needed (although it might still be useful for other users of | ||
| struct ArrowDeviceArray { | ||
| struct ArrowArray array; | ||
| int64_t device_id; | ||
| ArrowDeviceType device_type; | ||
| void* sync_event; | ||
| int64_t reserved[3]; | ||
| }; | ||
| """ | ||
| # TODO use out-of-line mode for faster import and avoid C parsing | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -3145,6 +3145,68 @@ cdef class RecordBatch(_Tabular): | ||
| return pyarrow_wrap_batch(c_batch) | ||
| def _export_to_c_device(self, out_ptr, out_schema_ptr=0): | ||
| """ | ||
| Export to a C ArrowDeviceArray struct, given its pointer. | ||
| If a C ArrowSchema struct pointer is also given, the record batch | ||
| schema is exported to it at the same time. | ||
| Parameters | ||
| ---------- | ||
| out_ptr: int | ||
| The raw pointer to a C ArrowDeviceArray struct. | ||
| out_schema_ptr: int (optional) | ||
| The raw pointer to a C ArrowSchema struct. | ||
| Be careful: if you don't pass the ArrowDeviceArray struct to a consumer, | ||
| array memory will leak. This is a low-level function intended for | ||
| expert users. | ||
| """ | ||
| cdef: | ||
| void* c_ptr = _as_c_pointer(out_ptr) | ||
| void* c_schema_ptr = _as_c_pointer(out_schema_ptr, | ||
| allow_null=True) | ||
| with nogil: | ||
| check_status(ExportDeviceRecordBatch( | ||
| deref(self.sp_batch), <shared_ptr[CSyncEvent]>NULL, | ||
| <ArrowDeviceArray*> c_ptr, <ArrowSchema*> c_schema_ptr) | ||
| ) | ||
| @staticmethod | ||
| def _import_from_c_device(in_ptr, schema): | ||
| """ | ||
| Import RecordBatch from a C ArrowDeviceArray struct, given its pointer | ||
| and the imported schema. | ||
| Parameters | ||
| ---------- | ||
| in_ptr: int | ||
| The raw pointer to a C ArrowDeviceArray struct. | ||
| type: Schema or int | ||
| Either a Schema object, or the raw pointer to a C ArrowSchema | ||
| struct. | ||
| This is a low-level function intended for expert users. | ||
| """ | ||
| cdef: | ||
| void* c_ptr = _as_c_pointer(in_ptr) | ||
| void* c_schema_ptr | ||
| shared_ptr[CRecordBatch] c_batch | ||
| c_schema = pyarrow_unwrap_schema(schema) | ||
| if c_schema == nullptr: | ||
| # Not a Schema object, perhaps a raw ArrowSchema pointer | ||
| c_schema_ptr = _as_c_pointer(schema, allow_null=True) | ||
| with nogil: | ||
| c_batch = GetResultValue(ImportDeviceRecordBatch( | ||
| <ArrowDeviceArray*> c_ptr, <ArrowSchema*> c_schema_ptr)) | ||
Comment on lines
+3202
to
+3203
Member 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. 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? | ||
| else: | ||
| with nogil: | ||
| c_batch = GetResultValue(ImportDeviceRecordBatch( | ||
| <ArrowDeviceArray*> c_ptr, c_schema)) | ||
| return pyarrow_wrap_batch(c_batch) | ||
| def _reconstruct_record_batch(columns, schema): | ||
| """ | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.