-
Notifications
You must be signed in to change notification settings - Fork 8
Immutable Tuples as C Structs, not pointers #248
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
Open
junikimm717
wants to merge
7
commits into
main
Choose a base branch
from
junikimm717/immutabletuple
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
594600c
immutable tuples
junikimm717 072f6c9
ruff reasons
junikimm717 4113469
c.py invocation and serialization errors fixed
junikimm717 7f1431e
ruff format
junikimm717 21f2815
numpy serialization also fixed
junikimm717 8cd031e
introduce immutable and mutable structs
junikimm717 69d89e1
simplify numpy serialization
junikimm717 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -140,7 +140,7 @@ def construct_from_c(fmt, c_obj): | |
| return fmt.construct_from_c(c_obj) | ||
| try: | ||
| return query_property(fmt, "construct_from_c", "__attr__", c_obj) | ||
| except NotImplementedError: | ||
| except AttributeError: | ||
| return fmt(c_obj) | ||
|
|
||
|
|
||
|
|
@@ -204,12 +204,24 @@ def construct_from_c(fmt, c_obj): | |
| register_property(t, "construct_from_c", "__attr__", lambda fmt, c_value: c_value) | ||
| register_property(t, "numba_type", "__attr__", lambda t: t) | ||
|
|
||
|
|
||
| def scalar_to_ctypes_copy(fmt, obj): | ||
| """ | ||
| This hack is required because it turns out that scalars don't own memory or smth | ||
| """ | ||
| arr = np.array([obj], dtype=obj.dtype, copy=True) | ||
| scalar_ctype = np.ctypeslib.as_ctypes_type(obj.dtype) | ||
| ptr_ctype = ctypes.POINTER(scalar_ctype) | ||
| return arr.ctypes.data_as(ptr_ctype).contents | ||
|
|
||
|
|
||
| register_property( | ||
| np.generic, | ||
| "serialize_to_c", | ||
| "__attr__", | ||
| lambda fmt, obj: np.ctypeslib.as_ctypes(obj), | ||
|
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. Can we have it as |
||
| scalar_to_ctypes_copy, | ||
| ) | ||
|
|
||
| # pass by value -> no op | ||
| register_property( | ||
| np.generic, | ||
|
|
@@ -254,11 +266,9 @@ def __call__(self, *args): | |
| self.argtypes, args, serial_args, strict=False | ||
| ): | ||
| deserialize_from_c(type_, arg, serial_arg) | ||
| if hasattr(self.ret_type, "construct_from_c"): | ||
| return construct_from_c(res.ftype, res) | ||
| if self.ret_type is type(None): | ||
| return None | ||
| return self.ret_type(res) | ||
| return construct_from_c(self.ret_type, res) | ||
|
|
||
|
|
||
| class CModule: | ||
|
|
@@ -315,7 +325,7 @@ def __call__(self, prgm): | |
| for func in prgm.funcs: | ||
| match func: | ||
| case asm.Function(asm.Variable(func_name, return_t), args, _): | ||
| return_t = c_type(return_t) | ||
| # return_t = c_type(return_t) | ||
| arg_ts = [arg.result_format for arg in args] | ||
| kern = CKernel(getattr(lib, func_name), return_t, arg_ts) | ||
| kernels[func_name] = kern | ||
|
|
@@ -1035,16 +1045,35 @@ def struct_c_type(fmt: AssemblyStructFType): | |
| return new_struct | ||
|
|
||
|
|
||
| def struct_c_type_wrapper(fmt: AssemblyStructFType): | ||
| """ | ||
| C type decider for struct types. Serialization actually ensures that before | ||
| crossing the FFI boundary, all serialized structs are structs, not | ||
| pointers. | ||
|
|
||
| The reason why we have this method is that ctypes can intelligently infer | ||
| whether we are working with a pointer arg type (pass by reference) or a | ||
| non-pointer type (pass by value) | ||
| """ | ||
| t = struct_c_type(fmt) | ||
| if fmt.is_mutable: | ||
| return ctypes.POINTER(t) | ||
| return t | ||
|
|
||
|
|
||
| register_property( | ||
| AssemblyStructFType, | ||
| "c_type", | ||
| "__attr__", | ||
| lambda fmt: ctypes.POINTER(struct_c_type(fmt)), | ||
| struct_c_type_wrapper, | ||
| ) | ||
|
|
||
|
|
||
| def struct_c_getattr(fmt: AssemblyStructFType, ctx, obj, attr): | ||
| return f"{obj}->{attr}" | ||
| if fmt.is_mutable: | ||
| # we are passing things in as a pointer (reference c_type_wrapper) | ||
| return f"{obj}->{attr}" | ||
| return f"{obj}.{attr}" | ||
|
|
||
|
|
||
| register_property( | ||
|
|
@@ -1056,8 +1085,10 @@ def struct_c_getattr(fmt: AssemblyStructFType, ctx, obj, attr): | |
|
|
||
|
|
||
| def struct_c_setattr(fmt: AssemblyStructFType, ctx, obj, attr, val): | ||
| ctx.emit(f"{ctx.feed}{obj}->{attr} = {val};") | ||
| return | ||
| if fmt.is_mutable: | ||
| ctx.emit(f"{ctx.feed}{obj}->{attr} = {val};") | ||
| else: | ||
| ctx.emit(f"{ctx.feed}{obj}.{attr} = {val};") | ||
|
|
||
|
|
||
| register_property( | ||
|
|
@@ -1092,18 +1123,23 @@ def serialize_tuple_to_c(fmt, obj): | |
| "__attr__", | ||
| serialize_tuple_to_c, | ||
| ) | ||
|
|
||
|
|
||
| def tuple_construct_from_c(fmt: TupleFType, c_struct): | ||
| args = [getattr(c_struct, name) for name in fmt.struct_fieldnames] | ||
| return tuple(args) | ||
|
|
||
|
|
||
| register_property( | ||
| TupleFType, | ||
| "construct_from_c", | ||
| "__attr__", | ||
| lambda fmt, obj, c_tuple: tuple(c_tuple), | ||
| tuple_construct_from_c, | ||
| ) | ||
|
|
||
| register_property( | ||
| TupleFType, | ||
| "c_type", | ||
| "__attr__", | ||
| lambda fmt: ctypes.POINTER( | ||
| struct_c_type(asm.NamedTupleFType("CTuple", fmt.struct_fields)) | ||
| ), | ||
| lambda fmt: struct_c_type_wrapper(asm.NamedTupleFType("CTuple", fmt.struct_fields)), | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I reported it here: numpy/numpy#30354