Repository navigation
Speeding up SZ3 Python API Compress and Decompress functions - #99
Conversation
…m C++ in both the compression and decompression functions. This results in a massive speed up of SZ3's Python API.
There was a problem hiding this comment.
Pull Request Overview
This PR replaces element-wise array creation in the Python API with np.frombuffer to eliminate Python-level indexing bottlenecks and reduce memory-copy time for both compression and decompression.
- Use
np.frombufferon the C buffer for fast bulk data transfer. - Preserve original behavior by copying the buffer before freeing it.
- Free the C buffer after converting to a NumPy array.
Comments suppressed due to low confidence (1)
tools/pysz/pysz.py:89
- Add unit tests for the new
frombufferpath to verify correct handling of different dtypes and shapes, ensuring no off-by-one or alignment issues are introduced.
data_dec = np.frombuffer((ctypes.c_ubyte * buffer_size).from_address(buffer_ptr.value),
| # Use np.frombuffer for fast memory copy | ||
| # Cast the ctypes pointer to void pointer, then use addressof to get memory address | ||
| buffer_ptr = ctypes.cast(data_dec_c, ctypes.c_void_p) | ||
| buffer_size = total_elements * np.dtype(original_dtype).itemsize |
There was a problem hiding this comment.
Casting total_elements (a NumPy scalar) to a Python int before using it in the ctypes array declaration will avoid type errors. e.g. buffer_size = int(total_elements) * np.dtype(original_dtype).itemsize.
| buffer_size = total_elements * np.dtype(original_dtype).itemsize | |
| buffer_size = int(total_elements) * np.dtype(original_dtype).itemsize |
| # Use np.frombuffer for fast memory copy | ||
| # Cast the ctypes pointer to void pointer, then use addressof to get memory address | ||
| buffer_ptr = ctypes.cast(data_dec_c, ctypes.c_void_p) | ||
| buffer_size = total_elements * np.dtype(original_dtype).itemsize | ||
|
|
||
| # Create numpy array directly from memory buffer | ||
| data_dec = np.frombuffer((ctypes.c_ubyte * buffer_size).from_address(buffer_ptr.value), | ||
| dtype=original_dtype, | ||
| count=total_elements | ||
| ).reshape(original_shape) | ||
|
|
||
| # Make a copy since we're going to free the original memory | ||
| data_dec = data_dec.copy() | ||
|
|
||
| self.sz.free_buf(data_dec_c) | ||
| return data_dec | ||
|
|
There was a problem hiding this comment.
[nitpick] The buffer-to-NumPy conversion logic is duplicated in both compress and decompress. Consider extracting this into a helper function to reduce code duplication and make future adjustments easier.
| # Use np.frombuffer for fast memory copy | |
| # Cast the ctypes pointer to void pointer, then use addressof to get memory address | |
| buffer_ptr = ctypes.cast(data_dec_c, ctypes.c_void_p) | |
| buffer_size = total_elements * np.dtype(original_dtype).itemsize | |
| # Create numpy array directly from memory buffer | |
| data_dec = np.frombuffer((ctypes.c_ubyte * buffer_size).from_address(buffer_ptr.value), | |
| dtype=original_dtype, | |
| count=total_elements | |
| ).reshape(original_shape) | |
| # Make a copy since we're going to free the original memory | |
| data_dec = data_dec.copy() | |
| self.sz.free_buf(data_dec_c) | |
| return data_dec | |
| # Convert buffer to NumPy array using helper function | |
| data_dec = self._buffer_to_numpy_array(data_dec_c, original_dtype, original_shape) | |
| # Make a copy since we're going to free the original memory | |
| data_dec = data_dec.copy() | |
| self.sz.free_buf(data_dec_c) | |
| return data_dec | |
| def _buffer_to_numpy_array(self, buffer_ptr, dtype, shape): | |
| """ | |
| Convert a memory buffer to a NumPy array. | |
| :param buffer_ptr: ctypes pointer to the buffer | |
| :param dtype: NumPy data type of the array | |
| :param shape: Shape of the resulting NumPy array | |
| :return: NumPy array | |
| """ | |
| total_elements = np.prod(shape) | |
| buffer_size = total_elements * np.dtype(dtype).itemsize | |
| # Create numpy array directly from memory buffer | |
| numpy_array = np.frombuffer((ctypes.c_ubyte * buffer_size).from_address(buffer_ptr.value), | |
| dtype=dtype, | |
| count=total_elements | |
| ).reshape(shape) | |
| return numpy_array |
|
@blt-sl Thanks for this update! |
* Algorithmic updates * replace pointwise iterator by blockwise iterator * replace pointwise iterator by blockwise iterator * Merge test units (#95) * update python support for Windows * update config to support save and load to ini * update error handling * update conf * Speeding up SZ3 Python API Compress and Decompress functions (#99)
The Python API functions currently has a bottleneck in how it transfers data from the SZ3 C buffer to a Python array. Currently in both the compress and decompress functions, the C buffer is indexed and then send into a np.array block. This can lead to some serious bottle necks. For example, I compressed 2 GB of data with SZ3 and the compress step took me 4.5 seconds, with 1 second dedicated to line 106 of the decompress functions below, and the decompress took me 17 seconds, with over 12 of those seconds dedicated to line 80.
This method of transferring data back to Python is inefficient and can be sped up by using Numpy's from buffer function. I have added in the required lines to speed up the compress and decompress Python functions. It now takes me 3.5 seconds to compress and around 2 seconds to compress 2 GB of data.
I recommend integrating the following changes in to ensure the Python API is able to take better advantage of SZ3's speed.