-
Notifications
You must be signed in to change notification settings - Fork 15
Parallel decompression for detector data #593
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
15 commits
Select commit
Hold shift + click to select a range
071c962
Machinery for decompressing chunks
takluyver fef9a2c
Integrate parallel decompression with xtdf detector components
takluyver dc96f46
Add zlib_into to dependencies
takluyver ae91877
Add decompress_threads parameter to .xarray() as well
takluyver f0c6e1d
Make some None returns explicit
takluyver 7014d6e
Use a newer OS & platform on RTD
takluyver 4dd2291
Use parallel decompression in components by default (on suitable data)
takluyver a2e8428
Limit default number of decompression threads
takluyver 7a7b7ef
Handle chunk not allocated in file
takluyver a48da13
Test parallel decompression machinery
takluyver d9423d4
Some fixes from static code checks
takluyver d10806d
Add some tests of compression machinery specifically
takluyver 7d909a7
Use a token for uploading coverage reports to codecov
takluyver 66c5f67
Tell coverage we're using threads
takluyver 2167dcd
Control thread pool with EXTRA_NUM_THREADS environment variable
takluyver 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 |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| [run] | ||
| omit = */tests/* | ||
| concurrency = multiprocessing | ||
| concurrency = multiprocessing,thread | ||
|
|
||
| [paths] | ||
| source = | ||
|
|
||
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 |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| version: 2 # Required | ||
|
|
||
| build: | ||
| os: ubuntu-20.04 | ||
| os: ubuntu-24.04 | ||
| tools: | ||
| python: "3.12" | ||
|
|
||
|
|
||
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,125 @@ | ||
| import threading | ||
| from copy import copy | ||
| from multiprocessing.pool import ThreadPool | ||
|
|
||
| import h5py | ||
| import numpy as np | ||
| from zlib_into import decompress_into | ||
|
|
||
|
|
||
| def filter_ids(dset: h5py.Dataset): | ||
| dcpl = dset.id.get_create_plist() | ||
| return [dcpl.get_filter(i)[0] for i in range(dcpl.get_nfilters())] | ||
|
|
||
|
|
||
| class DeflateDecompressor: | ||
| def __init__(self, deflate_filter_idx=0): | ||
| self.deflate_filter_bit = 1 << deflate_filter_idx | ||
|
|
||
| @classmethod | ||
| def for_dataset(cls, dset: h5py.Dataset): | ||
|
||
| filters = filter_ids(dset) | ||
| if filters == [h5py.h5z.FILTER_DEFLATE]: | ||
| return cls() | ||
| if dset.dtype.itemsize == 1 and filters == [ | ||
| h5py.h5z.FILTER_SHUFFLE, | ||
| h5py.h5z.FILTER_DEFLATE, | ||
| ]: | ||
| # The shuffle filter doesn't change single byte values, so we can | ||
| # skip it. | ||
| return cls(deflate_filter_idx=1) | ||
|
|
||
| return None | ||
|
|
||
| def clone(self): | ||
| return copy(self) | ||
|
|
||
| def apply_filters(self, data, filter_mask, out): | ||
| if filter_mask & self.deflate_filter_bit: | ||
| # The deflate filter is skipped, so just copy the data | ||
| memoryview(out)[:] = data | ||
| else: | ||
| decompress_into(data, out) | ||
|
|
||
|
|
||
| class ShuffleDeflateDecompressor: | ||
| def __init__(self, chunk_shape, dtype): | ||
| self.chunk_shape = chunk_shape | ||
| self.dtype = dtype | ||
| chunk_nbytes = dtype.itemsize | ||
| for l in chunk_shape: | ||
| chunk_nbytes *= l | ||
| # This will hold the decompressed data before shuffling | ||
| self.chunk_buf = np.zeros(chunk_nbytes, dtype=np.uint8) | ||
| self.shuffled_view = ( # E.g. for int32 data with chunks (10, 5): | ||
| self.chunk_buf # (200,) uint8 | ||
| .reshape((dtype.itemsize, -1)) # (4, 50) | ||
| .transpose() # (50, 4) | ||
| ) | ||
| # Check this is still a view on the buffered data | ||
| assert self.shuffled_view.base is self.chunk_buf | ||
|
|
||
| @classmethod | ||
| def for_dataset(cls, dset: h5py.Dataset): | ||
|
||
| if filter_ids(dset) == [h5py.h5z.FILTER_SHUFFLE, h5py.h5z.FILTER_DEFLATE]: | ||
| return cls(dset.chunks, dset.dtype) | ||
|
|
||
| return None | ||
|
|
||
| def clone(self): | ||
| return type(self)(self.chunk_shape, self.dtype) | ||
|
|
||
| def apply_filters(self, data, filter_mask, out): | ||
| if filter_mask & 2: | ||
| # The deflate filter is skipped | ||
| memoryview(self.chunk_buf)[:] = data | ||
| else: | ||
| decompress_into(data, self.chunk_buf) | ||
|
|
||
| if filter_mask & 1: | ||
| # The shuffle filter is skipped | ||
| memoryview(out)[:] = self.chunk_buf | ||
| else: | ||
| # Numpy does the shuffling by copying data between views | ||
| out.reshape((-1, 1)).view(np.uint8)[:] = self.shuffled_view | ||
|
|
||
|
|
||
| def dataset_decompressor(dset): | ||
|
||
| for cls in [DeflateDecompressor, ShuffleDeflateDecompressor]: | ||
| if (inst := cls.for_dataset(dset)) is not None: | ||
| return inst | ||
|
|
||
| return None | ||
|
|
||
|
|
||
| def multi_dataset_decompressor(dsets): | ||
| if not dsets: | ||
| return None | ||
|
|
||
| chunk = dsets[0].chunks | ||
| dtype = dsets[0] | ||
| filters = filter_ids(dsets[0]) | ||
| for d in dsets[1:]: | ||
| if d.chunks != chunk or d.dtype != dtype or filter_ids(d) != filters: | ||
| return None # Datasets are not consistent | ||
|
|
||
| return dataset_decompressor(dsets[0]) | ||
|
|
||
|
|
||
| def parallel_decompress_chunks(tasks, decompressor_proto, threads=16): | ||
| tlocal = threading.local() | ||
|
|
||
| def load_one(dset_id, coord, dest): | ||
| try: | ||
| decomp = tlocal.decompressor | ||
| except AttributeError: | ||
| tlocal.decompressor = decomp = decompressor_proto.clone() | ||
|
|
||
| if dset_id.get_chunk_info_by_coord(coord).byte_offset is None: | ||
| return # Chunk not allocated in file | ||
|
|
||
| filter_mask, compdata = dset_id.read_direct_chunk(coord) | ||
| decomp.apply_filters(compdata, filter_mask, dest) | ||
|
|
||
| with ThreadPool(threads) as pool: | ||
| pool.starmap(load_one, tasks) | ||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.