-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
MAINT Refactor vector sentinel into utils #22728
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
97826ff
MAINT Refactor vector sentinel into utils
thomasjpfan 6e9de8c
CLN Consistent naming
thomasjpfan 1fcb04b
Merge remote-tracking branch 'upstream/main' into cln_sentinel
thomasjpfan ba078be
Merge remote-tracking branch 'upstream/main' into cln_sentinel
thomasjpfan b0ae5ed
BLD Add numpy include dir
thomasjpfan 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
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
cimport numpy as np | ||
|
||
from libcpp.vector cimport vector | ||
from ..utils._typedefs cimport ITYPE_t, DTYPE_t | ||
|
||
ctypedef fused vector_typed: | ||
vector[DTYPE_t] | ||
vector[ITYPE_t] | ||
|
||
cdef np.ndarray vector_to_nd_array(vector_typed * vect_ptr) |
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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from cython.operator cimport dereference as deref | ||
from cpython.ref cimport Py_INCREF | ||
cimport numpy as np | ||
|
||
from ._typedefs cimport DTYPECODE, ITYPECODE | ||
|
||
np.import_array() | ||
|
||
|
||
cdef StdVectorSentinel _create_sentinel(vector_typed * vect_ptr): | ||
if vector_typed is vector[DTYPE_t]: | ||
return StdVectorSentinelFloat64.create_for(vect_ptr) | ||
else: | ||
return StdVectorSentinelIntP.create_for(vect_ptr) | ||
|
||
|
||
cdef class StdVectorSentinel: | ||
"""Wraps a reference to a vector which will be deallocated with this object. | ||
|
||
When created, the StdVectorSentinel swaps the reference of its internal | ||
vectors with the provided one (vect_ptr), thus making the StdVectorSentinel | ||
manage the provided one's lifetime. | ||
""" | ||
cdef void* get_data(self): | ||
"""Return pointer to data.""" | ||
|
||
cdef int get_typenum(self): | ||
"""Get typenum for PyArray_SimpleNewFromData.""" | ||
|
||
|
||
cdef class StdVectorSentinelFloat64(StdVectorSentinel): | ||
cdef vector[DTYPE_t] vec | ||
|
||
@staticmethod | ||
cdef StdVectorSentinel create_for(vector[DTYPE_t] * vect_ptr): | ||
# This initializes the object directly without calling __init__ | ||
# See: https://cython.readthedocs.io/en/latest/src/userguide/extension_types.html#instantiation-from-existing-c-c-pointers # noqa | ||
cdef StdVectorSentinelFloat64 sentinel = StdVectorSentinelFloat64.__new__(StdVectorSentinelFloat64) | ||
sentinel.vec.swap(deref(vect_ptr)) | ||
return sentinel | ||
|
||
cdef void* get_data(self): | ||
return self.vec.data() | ||
|
||
cdef int get_typenum(self): | ||
return DTYPECODE | ||
|
||
|
||
cdef class StdVectorSentinelIntP(StdVectorSentinel): | ||
cdef vector[ITYPE_t] vec | ||
|
||
@staticmethod | ||
cdef StdVectorSentinel create_for(vector[ITYPE_t] * vect_ptr): | ||
# This initializes the object directly without calling __init__ | ||
# See: https://cython.readthedocs.io/en/latest/src/userguide/extension_types.html#instantiation-from-existing-c-c-pointers # noqa | ||
cdef StdVectorSentinelIntP sentinel = StdVectorSentinelIntP.__new__(StdVectorSentinelIntP) | ||
sentinel.vec.swap(deref(vect_ptr)) | ||
return sentinel | ||
|
||
cdef void* get_data(self): | ||
return self.vec.data() | ||
|
||
cdef int get_typenum(self): | ||
return ITYPECODE | ||
|
||
|
||
cdef np.ndarray vector_to_nd_array(vector_typed * vect_ptr): | ||
cdef: | ||
np.npy_intp size = deref(vect_ptr).size() | ||
StdVectorSentinel sentinel = _create_sentinel(vect_ptr) | ||
np.ndarray arr = np.PyArray_SimpleNewFromData( | ||
1, &size, sentinel.get_typenum(), sentinel.get_data()) | ||
|
||
# Makes the numpy array responsible of the life-cycle of its buffer. | ||
# A reference to the StdVectorSentinel will be stolen by the call to | ||
# `PyArray_SetBaseObject` below, so we increase its reference counter. | ||
# See: https://docs.python.org/3/c-api/intro.html#reference-count-details | ||
Py_INCREF(sentinel) | ||
np.PyArray_SetBaseObject(arr, sentinel) | ||
return arr |
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 think this is slightly more clear than the current implementation. The
get_data
makes it clear thatarr
points to the data owned by the sentinel.The implementation on
main
definesarr
with the buffer fromvect_ptr
and then the sentinel would set the internal pointer insentinel.vec
tovect_ptr
. Because only the pointers were swapped,arr
is pointing to the correct place in memory.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.
LGTM.