-
Notifications
You must be signed in to change notification settings - Fork 242
Optimize Buffer.fill() to avoid intermediate object creation #1376
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
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 | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,7 +5,8 @@ | |||||||||||||
| from __future__ import annotations | ||||||||||||||
|
|
||||||||||||||
| cimport cython | ||||||||||||||
| from libc.stdint cimport uintptr_t | ||||||||||||||
| from libc.stdint cimport uint8_t, uint16_t, uint32_t, uintptr_t | ||||||||||||||
| from cpython.buffer cimport PyObject_GetBuffer, PyBuffer_Release, Py_buffer, PyBUF_SIMPLE | ||||||||||||||
|
|
||||||||||||||
| from cuda.bindings cimport cydriver | ||||||||||||||
| from cuda.core.experimental._memory._device_memory_resource cimport DeviceMemoryResource | ||||||||||||||
|
|
@@ -232,65 +233,27 @@ cdef class Buffer: | |||||||||||||
|
|
||||||||||||||
| """ | ||||||||||||||
| cdef Stream s_stream = Stream_accept(stream) | ||||||||||||||
| cdef unsigned char c_value8 | ||||||||||||||
| cdef unsigned short c_value16 | ||||||||||||||
| cdef unsigned int c_value32 | ||||||||||||||
| cdef size_t N | ||||||||||||||
| cdef size_t width | ||||||||||||||
| cdef unsigned int int_value | ||||||||||||||
|
|
||||||||||||||
| # Get fill pattern from value | ||||||||||||||
|
|
||||||||||||||
| # Handle int case: 1-byte fill with automatic overflow checking. | ||||||||||||||
| if isinstance(value, int): | ||||||||||||||
| # We define the int input to mean a 1-byte pattern. | ||||||||||||||
| # Match int.to_bytes(1, "little") behavior: raise OverflowError if not in [0, 256). | ||||||||||||||
| if value < 0 or value >= 256: | ||||||||||||||
| raise OverflowError("int value must be in range [0, 256)") | ||||||||||||||
| width = 1 | ||||||||||||||
| int_value = <unsigned int>value | ||||||||||||||
| else: | ||||||||||||||
| try: | ||||||||||||||
| mv = memoryview(value) | ||||||||||||||
| except TypeError: | ||||||||||||||
| raise TypeError( | ||||||||||||||
| f"value must be an int or support the buffer protocol, got {type(value).__name__}" | ||||||||||||||
| ) from None | ||||||||||||||
| width = mv.nbytes | ||||||||||||||
|
|
||||||||||||||
| # Validate width early to avoid copying/processing large invalid inputs. | ||||||||||||||
| if width not in (1, 2, 4): | ||||||||||||||
| raise ValueError(f"value must be 1, 2, or 4 bytes, got {width}") | ||||||||||||||
|
|
||||||||||||||
| # Convert to a 1-D view of bytes. | ||||||||||||||
| # | ||||||||||||||
| # Note: NumPy scalar memoryviews are 0-D, and int.from_bytes(mv, ...) errors with | ||||||||||||||
| # "0-dim memory has no length". Casting to 'B' gives us a byte-addressable view. | ||||||||||||||
| try: | ||||||||||||||
| int_value = int.from_bytes(mv.cast("B"), "little") | ||||||||||||||
| except TypeError: | ||||||||||||||
| int_value = int.from_bytes(mv.tobytes(), "little") | ||||||||||||||
|
|
||||||||||||||
| # Validate buffer size modulus. | ||||||||||||||
| cdef size_t buffer_size = self._size | ||||||||||||||
| if buffer_size % width != 0: | ||||||||||||||
| raise ValueError(f"buffer size ({buffer_size}) must be divisible by {width}") | ||||||||||||||
|
|
||||||||||||||
| # Perform fill based on width | ||||||||||||||
| cdef cydriver.CUstream s = s_stream._handle | ||||||||||||||
| if width == 1: | ||||||||||||||
| c_value8 = <unsigned char>int_value | ||||||||||||||
| N = buffer_size | ||||||||||||||
| with nogil: | ||||||||||||||
| HANDLE_RETURN(cydriver.cuMemsetD8Async(<cydriver.CUdeviceptr>self._ptr, c_value8, N, s)) | ||||||||||||||
| elif width == 2: | ||||||||||||||
| c_value16 = <unsigned short>int_value | ||||||||||||||
| N = buffer_size // 2 | ||||||||||||||
| with nogil: | ||||||||||||||
| HANDLE_RETURN(cydriver.cuMemsetD16Async(<cydriver.CUdeviceptr>self._ptr, c_value16, N, s)) | ||||||||||||||
| else: # width == 4 | ||||||||||||||
| c_value32 = <unsigned int>int_value | ||||||||||||||
| N = buffer_size // 4 | ||||||||||||||
| with nogil: | ||||||||||||||
| HANDLE_RETURN(cydriver.cuMemsetD32Async(<cydriver.CUdeviceptr>self._ptr, c_value32, N, s)) | ||||||||||||||
| Buffer_fill_uint8(self, value, s_stream._handle) | ||||||||||||||
| return | ||||||||||||||
|
|
||||||||||||||
| # Handle bytes case: direct pointer access without intermediate objects. | ||||||||||||||
| if isinstance(value, bytes): | ||||||||||||||
| Buffer_fill_from_ptr(self, <const char*><bytes>value, len(value), s_stream._handle) | ||||||||||||||
| return | ||||||||||||||
|
|
||||||||||||||
| # General buffer protocol path using C buffer API. | ||||||||||||||
| cdef Py_buffer buf | ||||||||||||||
| if PyObject_GetBuffer(value, &buf, PyBUF_SIMPLE) != 0: | ||||||||||||||
| raise TypeError( | ||||||||||||||
| f"value must be an int or support the buffer protocol, got {type(value).__name__}" | ||||||||||||||
| ) | ||||||||||||||
| try: | ||||||||||||||
| Buffer_fill_from_ptr(self, <const char*>buf.buf, buf.len, s_stream._handle) | ||||||||||||||
| finally: | ||||||||||||||
| PyBuffer_Release(&buf) | ||||||||||||||
|
|
||||||||||||||
| def __dlpack__( | ||||||||||||||
| self, | ||||||||||||||
|
|
@@ -419,6 +382,36 @@ cdef inline void Buffer_close(Buffer self, stream): | |||||||||||||
| self._alloc_stream = None | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| cdef inline void Buffer_fill_uint8(Buffer self, uint8_t value, cydriver.CUstream s): | ||||||||||||||
| with nogil: | ||||||||||||||
| HANDLE_RETURN(cydriver.cuMemsetD8Async(<cydriver.CUdeviceptr>self._ptr, value, self._size, s)) | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| cdef inline void Buffer_fill_from_ptr( | ||||||||||||||
| Buffer self, const char* ptr, size_t width, cydriver.CUstream s | ||||||||||||||
| ) except *: | ||||||||||||||
|
Comment on lines
+390
to
+392
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. ditto
Suggested change
|
||||||||||||||
| cdef size_t buffer_size = self._size | ||||||||||||||
|
|
||||||||||||||
| if width == 1: | ||||||||||||||
| with nogil: | ||||||||||||||
| HANDLE_RETURN(cydriver.cuMemsetD8Async( | ||||||||||||||
| <cydriver.CUdeviceptr>self._ptr, (<uint8_t*>ptr)[0], buffer_size, s)) | ||||||||||||||
| elif width == 2: | ||||||||||||||
| if buffer_size & 0x1: | ||||||||||||||
| raise ValueError(f"buffer size ({buffer_size}) must be divisible by 2") | ||||||||||||||
| with nogil: | ||||||||||||||
| HANDLE_RETURN(cydriver.cuMemsetD16Async( | ||||||||||||||
| <cydriver.CUdeviceptr>self._ptr, (<uint16_t*>ptr)[0], buffer_size // 2, s)) | ||||||||||||||
| elif width == 4: | ||||||||||||||
| if buffer_size & 0x3: | ||||||||||||||
| raise ValueError(f"buffer size ({buffer_size}) must be divisible by 4") | ||||||||||||||
| with nogil: | ||||||||||||||
| HANDLE_RETURN(cydriver.cuMemsetD32Async( | ||||||||||||||
| <cydriver.CUdeviceptr>self._ptr, (<uint32_t*>ptr)[0], buffer_size // 4, s)) | ||||||||||||||
| else: | ||||||||||||||
| raise ValueError(f"value must be 1, 2, or 4 bytes, got {width}") | ||||||||||||||
|
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.
Suggested change
|
||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| cdef Buffer_init_mem_attrs(Buffer self): | ||||||||||||||
| if not self._mem_attrs_inited: | ||||||||||||||
| query_memory_attrs(self._mem_attrs, self._ptr) | ||||||||||||||
|
|
||||||||||||||
Oops, something went wrong.
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.
nit: we want the exception clause
except*sinceHANDLE_RETURNcan raise, but we don't want Cython to warn, so with Cython 3 we general want to avoid usingvoidas the return type and use this instead: