| 1 |
matrixmultiply |
| 2 |
============== |
| 3 |
|
| 4 |
General matrix multiplication for f32, f64, and complex matrices. Operates on |
| 5 |
matrices with general layout (they can use arbitrary row and column stride). |
| 6 |
|
| 7 |
Please read the `API documentation here`__ |
| 8 |
|
| 9 |
__ https://docs.rs/matrixmultiply/ |
| 10 |
|
| 11 |
|
| 12 |
We presently provide a few good microkernels, portable and for x86-64 and |
| 13 |
AArch64 NEON, and only one operation: the general matrix-matrix multiplication |
| 14 |
(“gemm”). |
| 15 |
|
| 16 |
This crate was inspired by the macro/microkernel approach to matrix |
| 17 |
multiplication that is used by the BLIS_ project. |
| 18 |
|
| 19 |
.. _BLIS: https://github.com/flame/blis |
| 20 |
|
| 21 |
|crates|_ |
| 22 |
|
| 23 |
.. |crates| image:: https://img.shields.io/crates/v/matrixmultiply.svg |
| 24 |
.. _crates: https://crates.io/crates/matrixmultiply |
| 25 |
|
| 26 |
Development Goals |
| 27 |
----------------- |
| 28 |
|
| 29 |
- Code clarity and maintainability |
| 30 |
- Portability and stable Rust |
| 31 |
- Performance: provide target-specific microkernels when it is beneficial |
| 32 |
- Testing: Test diverse inputs and test and benchmark all microkernels |
| 33 |
- Small code footprint and fast compilation |
| 34 |
- We are not reimplementing BLAS. |
| 35 |
|
| 36 |
Benchmarks |
| 37 |
---------- |
| 38 |
|
| 39 |
- ``cargo bench`` is useful for special cases and small matrices |
| 40 |
- The best gemm and threading benchmark is is ``examples/benchmarks.rs`` which supports custom sizes, |
| 41 |
some configuration, and csv output. |
| 42 |
Use the script ``benches/benchloop.py`` to run benchmarks over parameter ranges. |
| 43 |
|
| 44 |
Blog Posts About This Crate |
| 45 |
--------------------------- |
| 46 |
|
| 47 |
+ `gemm: a rabbit hole`__ |
| 48 |
|
| 49 |
__ https://bluss.github.io/rust/2016/03/28/a-gemmed-rabbit-hole/ |
| 50 |
|
| 51 |
Recent Changes |
| 52 |
-------------- |
| 53 |
|
| 54 |
- 0.3.10 |
| 55 |
|
| 56 |
- sgemm: Reduce unnecessary AVX register permutations by `@SongXiaoXi <https://github.com/SongXiaoXi>`_ `#88 <https://github.com/bluss/matrixmultiply/pull/88>`_ |
| 57 |
- Exclude alignment for MaskBuffer for i686-win7-windows-msvc by `@drewkett <https://github.com/drewkett>`_ `#87 <https://github.com/bluss/matrixmultiply/pull/87>`_ |
| 58 |
- Fix cross builder CI and fix a few warnings by `@bluss <https://github.com/bluss>`_ `#89 <https://github.com/bluss/matrixmultiply/pull/89>`_ |
| 59 |
|
| 60 |
- 0.3.9 |
| 61 |
|
| 62 |
- Fix debug assertion for alignment on s390x, involving alignment of the mask buffer; |
| 63 |
the change lowered the static alignment request to (16) in thread local storage |
| 64 |
on all platforms (except macos was unchanged). |
| 65 |
- Test with cargo-careful |
| 66 |
|
| 67 |
- 0.3.8 |
| 68 |
|
| 69 |
- Lower alignment requirement for thread local storage value on macos, since |
| 70 |
it's was not respected and caused a debug assertion. (Previous issue #55) |
| 71 |
|
| 72 |
- 0.3.7 |
| 73 |
|
| 74 |
- Rename a directory, avoiding spaces in filenames, to be compatible with |
| 75 |
Bazel. By @xander-zitara |
| 76 |
|
| 77 |
- 0.3.6 |
| 78 |
|
| 79 |
- Fix the build for the combination of cgemm and no_std (#76) |
| 80 |
|
| 81 |
- 0.3.5 |
| 82 |
|
| 83 |
- Significant improvements to complex matrix packing and kernels (#75) |
| 84 |
|
| 85 |
- Use a specialized AVX2 matrix packing function for sgemm, dgemm when this |
| 86 |
feature is detected on x86-64 |
| 87 |
|
| 88 |
- 0.3.4 |
| 89 |
|
| 90 |
- Sgemm, dgemm microkernel implementations for AArch64 NEON (ARM) |
| 91 |
|
| 92 |
Matrixmultiply now uses autocfg to detect rust version to enable these kernels |
| 93 |
when AArch64 intrinsics are available from Rust 1.61. |
| 94 |
|
| 95 |
- Small change to matrix packing functions so that they in some cases optimize |
| 96 |
better due to improvements to pointer alias information. |
| 97 |
|
| 98 |
- 0.3.3 |
| 99 |
|
| 100 |
- Attempt to fix macos bug #55 again (manifesting as a debug assertion, only |
| 101 |
in debug builds.) |
| 102 |
|
| 103 |
- Updated comments for x86 kernels by @Tastaturtaste |
| 104 |
|
| 105 |
- Updates to MIRI/CI by @jturner314 |
| 106 |
|
| 107 |
- Silenced Send/Sync future compatibility warnings for a raw pointer wrapper |
| 108 |
|
| 109 |
- 0.3.2 |
| 110 |
|
| 111 |
- Add optional feature ``cgemm`` for complex matmult functions ``cgemm`` and |
| 112 |
``zgemm`` |
| 113 |
|
| 114 |
- Add optional feature ``constconf`` for compile-time configuration of matrix |
| 115 |
kernel parameters for chunking. Improved scripts for benchmarking over ranges |
| 116 |
of different settings. With thanks to @DutchGhost for the const-time |
| 117 |
parsing functions. |
| 118 |
|
| 119 |
- Improved benchmarking and testing. |
| 120 |
|
| 121 |
- Threading is now slightly more eager to threads (depending on matrix element count). |
| 122 |
|
| 123 |
- 0.3.1 |
| 124 |
|
| 125 |
- Attempt to fix bug #55 were the mask buffer in TLS did not seem to |
| 126 |
get its requested alignment on macos. The mask buffer pointer is now |
| 127 |
aligned manually (again, like it was in 0.2.x). |
| 128 |
|
| 129 |
- Fix a minor issue where we were passing a buffer pointer as ``&T`` |
| 130 |
when it should have been ``&[T]``. |
| 131 |
|
| 132 |
- 0.3.0 |
| 133 |
|
| 134 |
- Implement initial support for threading using a bespoke thread pool with |
| 135 |
little contention. |
| 136 |
To use, enable feature ``threading`` (and configure number of threads with the |
| 137 |
variable ``MATMUL_NUM_THREADS``). |
| 138 |
|
| 139 |
Initial support is for up to 4 threads - will be updated with more |
| 140 |
experience in coming versions. |
| 141 |
|
| 142 |
- Added a better benchmarking program for arbitrary size and layout, see |
| 143 |
``examples/benchmark.rs`` for this; it supports csv output for better |
| 144 |
recording of measurements |
| 145 |
|
| 146 |
- Minimum supported rust version is 1.41.1 and the version update policy |
| 147 |
has been updated. |
| 148 |
|
| 149 |
- Updated to Rust 2018 edition |
| 150 |
|
| 151 |
- Moved CI to github actions (so long travis and thanks for all the fish). |
| 152 |
|
| 153 |
- 0.2.4 |
| 154 |
|
| 155 |
- Support no-std mode by @vadixidav and @jturner314 |
| 156 |
New (default) feature flag "std"; use default-features = false to disable |
| 157 |
and use no-std. |
| 158 |
Note that runtime CPU feature detection requires std. |
| 159 |
|
| 160 |
- Fix tests so that they build correctly on non-x86 #49 platforms, and manage |
| 161 |
the release by @bluss |
| 162 |
|
| 163 |
- 0.2.3 |
| 164 |
|
| 165 |
- Update rawpointer dependency to 0.2 |
| 166 |
- Minor changes to inlining for ``-Ctarget-cpu=native`` use (not recommended - |
| 167 |
use automatic runtime feature detection. |
| 168 |
- Minor improvements to kernel masking (#42, #41) by @bluss and @SuperFluffy |
| 169 |
|
| 170 |
- 0.2.2 |
| 171 |
|
| 172 |
- New dgemm avx and fma kernels implemented by R. Janis Goldschmidt |
| 173 |
(@SuperFluffy). With fast cases for both row and column major output. |
| 174 |
|
| 175 |
Benchmark improvements: Using fma instructions reduces execution time on |
| 176 |
dgemm benchmarks by 25-35% compared with the avx kernel, see issue `#35`_ |
| 177 |
|
| 178 |
Using the avx dgemm kernel reduces execution time on dgemm benchmarks by |
| 179 |
5-7% compared with the previous version's autovectorized kernel. |
| 180 |
|
| 181 |
- New fma adaption of the sgemm avx kernel by R. Janis Goldschmidt |
| 182 |
(@SuperFluffy). |
| 183 |
|
| 184 |
Benchmark improvement: Using fma instructions reduces execution time on |
| 185 |
sgemm benchmarks by 10-15% compared with the avx kernel, see issue `#35`_ |
| 186 |
|
| 187 |
- More flexible kernel selection allows kernels to individually set all |
| 188 |
their parameters, ensures the fallback (plain Rust) kernels can be tuned |
| 189 |
for performance as well, and moves feature detection out of the gemm loop. |
| 190 |
|
| 191 |
Benchmark improvement: Reduces execution time on various benchmarks |
| 192 |
by 1-2% in the avx kernels, see `#37`_. |
| 193 |
|
| 194 |
- Improved testing to cover input/output strides of more diversity. |
| 195 |
|
| 196 |
.. _#35: https://github.com/bluss/matrixmultiply/issues/35 |
| 197 |
.. _#37: https://github.com/bluss/matrixmultiply/issues/37 |
| 198 |
|
| 199 |
- 0.2.1 |
| 200 |
|
| 201 |
- Improve matrix packing by taking better advantage of contiguous inputs. |
| 202 |
|
| 203 |
Benchmark improvement: execution time for 64×64 problem where inputs are either |
| 204 |
both row major or both column major changed by -5% sgemm and -1% for dgemm. |
| 205 |
(#26) |
| 206 |
|
| 207 |
- In the sgemm avx kernel, handle column major output arrays just like |
| 208 |
it does row major arrays. |
| 209 |
|
| 210 |
Benchmark improvement: execution time for 32×32 problem where output is column |
| 211 |
major changed by -11%. (#27) |
| 212 |
|
| 213 |
- 0.2.0 |
| 214 |
|
| 215 |
- Use runtime feature detection on x86 and x86-64 platforms, to enable |
| 216 |
AVX-specific microkernels at runtime if available on the currently |
| 217 |
executing configuration. |
| 218 |
|
| 219 |
This means no special compiler flags are needed to enable native |
| 220 |
instruction performance! |
| 221 |
|
| 222 |
- Implement a specialized 8×8 sgemm (f32) AVX microkernel, this speeds up |
| 223 |
matrix multiplication by another 25%. |
| 224 |
|
| 225 |
- Use ``std::alloc`` for allocation of aligned packing buffers |
| 226 |
|
| 227 |
- We now require Rust 1.28 as the minimal version |
| 228 |
|
| 229 |
- 0.1.15 |
| 230 |
|
| 231 |
- Fix bug where the result matrix C was not updated in the case of a M × K by |
| 232 |
K × N matrix multiplication where K was zero. (This resulted in the output |
| 233 |
C potentially being left uninitialized or with incorrect values in this |
| 234 |
specific scenario.) By @jturner314 (PR #21) |
| 235 |
|
| 236 |
- 0.1.14 |
| 237 |
|
| 238 |
- Avoid an unused code warning |
| 239 |
|
| 240 |
- 0.1.13 |
| 241 |
|
| 242 |
- Pick 8x8 sgemm (f32) kernel when AVX target feature is enabled |
| 243 |
(with Rust 1.14 or later, no effect otherwise). |
| 244 |
- Use ``rawpointer``, a µcrate with raw pointer methods taken from this |
| 245 |
project. |
| 246 |
|
| 247 |
- 0.1.12 |
| 248 |
|
| 249 |
- Internal cleanup with retained performance |
| 250 |
|
| 251 |
- 0.1.11 |
| 252 |
|
| 253 |
- Adjust sgemm (f32) kernel to optimize better on recent Rust. |
| 254 |
|
| 255 |
- 0.1.10 |
| 256 |
|
| 257 |
- Update doc links to docs.rs |
| 258 |
|
| 259 |
- 0.1.9 |
| 260 |
|
| 261 |
- Workaround optimization regression in rust nightly (1.12-ish) (#9) |
| 262 |
|
| 263 |
- 0.1.8 |
| 264 |
|
| 265 |
- Improved docs |
| 266 |
|
| 267 |
- 0.1.7 |
| 268 |
|
| 269 |
- Reduce overhead slightly for small matrix multiplication problems by using |
| 270 |
only one allocation call for both packing buffers. |
| 271 |
|
| 272 |
- 0.1.6 |
| 273 |
|
| 274 |
- Disable manual loop unrolling in debug mode (quicker debug builds) |
| 275 |
|
| 276 |
- 0.1.5 |
| 277 |
|
| 278 |
- Update sgemm to use a 4x8 microkernel (“still in simplistic rust”), |
| 279 |
which improves throughput by 10%. |
| 280 |
|
| 281 |
- 0.1.4 |
| 282 |
|
| 283 |
- Prepare support for aligned packed buffers |
| 284 |
- Update dgemm to use a 8x4 microkernel, still in simplistic rust, |
| 285 |
which improves throughput by 10-20% when using AVX. |
| 286 |
|
| 287 |
- 0.1.3 |
| 288 |
|
| 289 |
- Silence some debug prints |
| 290 |
|
| 291 |
- 0.1.2 |
| 292 |
|
| 293 |
- Major performance improvement for sgemm and dgemm (20-30% when using AVX). |
| 294 |
Since it all depends on what the optimizer does, I'd love to get |
| 295 |
issue reports that report good or bad performance. |
| 296 |
- Made the kernel masking generic, which is a cleaner design |
| 297 |
|
| 298 |
- 0.1.1 |
| 299 |
|
| 300 |
- Minor improvement in the kernel |