Debian Codegraph @

README.rst

raw
1 ==========
2 Bitshuffle
3 ==========
4
5 Filter for improving compression of typed binary data.
6
7 Bitshuffle is an algorithm that rearranges typed, binary data for improving
8 compression, as well as a python/C package that implements this algorithm
9 within the Numpy framework.
10
11 The library can be used along side HDF5 to compress and decompress datasets and
12 is integrated through the `dynamically loaded filters`_ framework. Bitshuffle
13 is HDF5 filter number ``32008``.
14
15 Algorithmically, Bitshuffle is closely related to HDF5's `Shuffle filter`_
16 except it operates at the bit level instead of the byte level. Arranging a
17 typed data array in to a matrix with the elements as the rows and the bits
18 within the elements as the columns, Bitshuffle "transposes" the matrix,
19 such that all the least-significant-bits are in a row, etc. This transpose
20 is performed within blocks of data roughly 8 kB long [1]_.
21
22 This does not in itself compress data, only rearranges it for more efficient
23 compression. To perform the actual compression you will need a compression
24 library. Bitshuffle has been designed to be well matched to Marc Lehmann's
25 LZF_ as well as LZ4_ and ZSTD_. Note that because Bitshuffle modifies the data at the bit
26 level, sophisticated entropy reducing compression libraries such as GZIP and
27 BZIP are unlikely to achieve significantly better compression than simpler and
28 faster duplicate-string-elimination algorithms such as LZF, LZ4 and ZSTD. Bitshuffle
29 thus includes routines (and HDF5 filter options) to apply LZ4 and ZSTD compression to
30 each block after shuffling [2]_.
31
32 The Bitshuffle algorithm relies on neighbouring elements of a dataset being
33 highly correlated to improve data compression. Any correlations that span at
34 least 24 elements of the dataset may be exploited to improve compression.
35
36 Bitshuffle was designed with performance in mind. On most machines the
37 time required for Bitshuffle+LZ4 is insignificant compared to the time required
38 to read or write the compressed data to disk. Because it is able to exploit the
39 SSE and AVX instruction sets present on modern Intel and AMD processors, on
40 these machines compression is only marginally slower than an out-of-cache
41 memory copy. On modern x86 processors you can expect Bitshuffle to have a
42 throughput of roughly 1 byte per clock cycle, and on the Haswell generation of
43 Intel processors (2013) and later, you can expect up to 2 bytes per clock
44 cycle. In addition, Bitshuffle is parallelized using OpenMP.
45
46 As a bonus, Bitshuffle ships with a dynamically loaded version of
47 `h5py`'s LZF compression filter, such that the filter can be transparently
48 used outside of python and in command line utilities such as ``h5dump``.
49
50 .. [1] Chosen to fit comfortably within L1 cache as well as be well matched
51 window of the LZF compression library.
52
53 .. [2] Over applying bitshuffle to the full dataset then applying LZ4/ZSTD
54 compression, this has the tremendous advantage that the block is
55 already in the L1 cache.
56
57 .. _`dynamically loaded filters`: https://docs.hdfgroup.org/hdf5/rfc/HDF5DynamicallyLoadedFilters.pdf
58
59 .. _`Shuffle filter`: http://www.hdfgroup.org/HDF5/doc_resource/H5Shuffle_Perf.pdf
60
61 .. _LZF: http://oldhome.schmorp.de/marc/liblzf.html
62
63 .. _LZ4: https://code.google.com/p/lz4/
64
65 .. _ZSTD: https://github.com/facebook/zstd
66
67
68 Applications
69 ------------
70
71 Bitshuffle might be right for your application if:
72
73 - You need to compress typed binary data.
74 - Your data is arranged such that adjacent elements over the fastest varying
75 index of your dataset are similar (highly correlated).
76 - A special case of the previous point is if you are only exercising a subset
77 of the bits in your data-type, as is often true of integer data.
78 - You need both high compression ratios and high performance.
79
80
81 Comparing Bitshuffle to other compression algorithms and HDF5 filters:
82
83 - Bitshuffle is less general than many other compression algorithms.
84 To achieve good compression ratios, consecutive elements of your data must
85 be highly correlated.
86 - For the right datasets, Bitshuffle is one of the few compression
87 algorithms that promises both high throughput and high compression ratios.
88 - Bitshuffle should have roughly the same throughput as Shuffle, but
89 may obtain higher compression ratios.
90 - The MAFISC_ filter actually includes something similar to Bitshuffle as one of
91 its prefilters, However, MAFICS's emphasis is on obtaining high compression
92 ratios at all costs, sacrificing throughput.
93
94 .. _MAFISC: https://wr.informatik.uni-hamburg.de/research/projects/icomex/mafisc
95
96
97 Installation for Python
98 -----------------------
99
100
101 In most cases bitshuffle can be installed by `pip`::
102
103 pip install bitshuffle
104
105 On Linux and macOS x86_64 platforms binary wheels are available, on other platforms a
106 source build will be performed. The binary wheels are built with AVX2 support and will
107 only run processors that support these instructions (most processors from 2015 onwards,
108 i.e. Intel Haswell, AMD Excavator and later). On an unsupported processor these builds
109 of bitshuffle will crash with `SIGILL`. To run on unsupported x86_64 processors, or
110 target newer instructions such as AVX512, you should perform a build from source.
111 This can be forced by giving pip the `--no-binary=bitshuffle` option.
112
113 Source installation requires python 2.7+ or 3.3+, HDF5 1.8.4 or later, HDF5 for python
114 (h5py), Numpy and Cython. Bitshuffle is linked against HDF5. To use the dynamically
115 loaded HDF5 filter requires HDF5 1.8.11 or later.
116
117 For total control, bitshuffle can be built using `python setup.py`. If ZSTD support is
118 to be enabled the ZSTD repo needs to pulled into bitshuffle before installation with::
119
120 git submodule update --init
121
122 To build and install bitshuffle::
123
124 python setup.py install [--h5plugin [--h5plugin-dir=spam] --zstd]
125
126 To get finer control of installation options, including whether to compile with OpenMP
127 multi-threading and the target microarchitecture copy the ``setup.cfg.example`` to
128 ``setup.cfg`` and edit the values therein.
129
130 If using the dynamically loaded HDF5 filter (which gives you access to the
131 Bitshuffle and LZF filters outside of python), set the environment variable
132 ``HDF5_PLUGIN_PATH`` to the value of ``--h5plugin-dir`` or use HDF5's default
133 search location of ``/usr/local/hdf5/lib/plugin``.
134
135 ZSTD support is enabled with ``--zstd``.
136
137 If you get an error about missing source files when building the extensions,
138 try upgrading setuptools. There is a weird bug where setuptools prior to 0.7
139 doesn't work properly with Cython in some cases.
140
141 .. _source: https://docs.h5py.org/en/latest/build.html#source-installation
142
143
144 Usage from Python
145 -----------------
146
147 The `bitshuffle` module contains routines for shuffling and unshuffling
148 Numpy arrays.
149
150 If installed with the dynamically loaded filter plugins, Bitshuffle can be used
151 in conjunction with HDF5 both inside and outside of python, in the same way as
152 any other filter; simply by specifying the filter number ``32008``. Otherwise
153 the filter will be available only within python and only after importing
154 `bitshuffle.h5`. Reading Bitshuffle encoded datasets will be transparent.
155 The filter can be added to new datasets either through the `h5py` low level
156 interface or through the convenience functions provided in
157 `bitshuffle.h5`. See the docstrings and unit tests for examples. For `h5py`
158 version 2.5.0 and later Bitshuffle can be added to new datasets through the
159 high level interface, as in the example below.
160
161 The compression algorithm can be configured using the `filter_opts` in
162 `bitshuffle.h5.create_dataset()`. LZ4 is chosen with:
163 `(BLOCK_SIZE, h5.H5_COMPRESS_LZ4)` and ZSTD with:
164 `(BLOCK_SIZE, h5.H5_COMPRESS_ZSTD, COMP_LVL)`. See `test_h5filter.py` for an example.
165
166 Example h5py
167 ------------
168 ::
169
170 import h5py
171 import numpy
172 import bitshuffle.h5
173
174 print(h5py.__version__) # >= '2.5.0'
175
176 f = h5py.File(filename, "w")
177
178 # block_size = 0 let Bitshuffle choose its value
179 block_size = 0
180
181 dataset = f.create_dataset(
182 "data",
183 (100, 100, 100),
184 compression=bitshuffle.h5.H5FILTER,
185 compression_opts=(block_size, bitshuffle.h5.H5_COMPRESS_LZ4),
186 dtype='float32',
187 )
188
189 # create some random data
190 array = numpy.random.rand(100, 100, 100)
191 array = array.astype('float32')
192
193 dataset[:] = array
194
195 f.close()
196
197
198 Usage from C
199 ------------
200
201 If you wish to use Bitshuffle in your C program and would prefer not to use the
202 HDF5 dynamically loaded filter, the C library in the ``src/`` directory is
203 self-contained and complete.
204
205
206 Usage from Java
207 ---------------
208
209 You can use Bitshuffle even in Java and the routines for shuffling and unshuffling
210 are ported into `snappy-java`_. To use the routines, you need to add the following
211 dependency to your pom.xml::
212
213 <dependency>
214 <groupId>org.xerial.snappy</groupId>
215 <artifactId>snappy-java</artifactId>
216 <version>1.1.3-M1</version>
217 </dependency>
218
219 First, import org.xerial.snapy.BitShuffle in your Java code::
220
221 import org.xerial.snappy.BitShuffle;
222
223 Then, you use them like this::
224
225 int[] data = new int[] {1, 3, 34, 43, 34};
226 byte[] shuffledData = BitShuffle.bitShuffle(data);
227 int[] result = BitShuffle.bitUnShuffleIntArray(shuffledData);
228
229 .. _`snappy-java`: https://github.com/xerial/snappy-java
230
231
232 Rust HDF5 plugin
233 ----------------
234
235 If you wish to open HDF5 files compressed with bitshuffle in your Rust program, there is a `Rust binding`_ for it.
236 In your Cargo.toml::
237
238 [dependencies]
239 ...
240 hdf5-bitshuffle = "0.9"
241 ...
242
243 To register the plugin in your code::
244
245 use hdf5_bitshuffle::register_bitshuffle_plugin;
246
247 fn main() {
248 register_bitshuffle_plugin();
249 }
250
251 .. _`Rust binding`: https://docs.rs/hdf5-bitshuffle/latest/hdf5_bitshuffle/
252
253 Anaconda
254 --------
255
256 The conda package can be build via::
257
258 conda build conda-recipe
259
260
261 For Best Results
262 ----------------
263
264 Here are a few tips to help you get the most out of Bitshuffle:
265
266 - For multi-dimensional datasets, order your data such that the fastest varying
267 dimension is the one over which your data is most correlated (have
268 values that change the least), or fake this using chunks.
269 - To achieve the highest throughput, use a data type that is 64 *bytes* or
270 smaller. If you have a very large compound data type, consider adding a
271 dimension to your datasets instead.
272 - To make full use of the SSE2 instruction set, use a data type whose size
273 is a multiple of 2 bytes. For the AVX2 instruction set, use a data type whose
274 size is a multiple of 4 bytes.
275
276
277 Citing Bitshuffle
278 -----------------
279
280 Bitshuffle was initially described in
281 https://doi.org/10.1016/j.ascom.2015.07.002, pre-print available at
282 https://arxiv.org/abs/1503.00638.