|
| 1 | +# Vectorization |
| 2 | + |
| 3 | +This sample shows how to implement matrix multiplication using various |
| 4 | +vectorization approaches. |
| 5 | + |
| 6 | +Note: You should not reuse this matrix library in your application. It was not |
| 7 | +written to be useful beyond the scope of this demo. If you're looking for a |
| 8 | +matrix library, you probably want [GLM] for graphics applications, or a linear |
| 9 | +algebra library for compute applications. |
| 10 | + |
| 11 | +The sample app will benchmark each implementation and display the average run |
| 12 | +time over 1,000,000 runs. The goal of this sample is to illustrate the trade- |
| 13 | +offs of each implementation in terms of flexibility, readability, and |
| 14 | +performance. |
| 15 | + |
| 16 | +Given the relatively small problem size used here (4x4 matrices and vec4s), the |
| 17 | +best performing implementations in this sample are the ones that can best |
| 18 | +improve over the naive implementation without large set up costs. You should not |
| 19 | +take the results of this sample as authoritative: if performance is important to |
| 20 | +you, you **must** benchmark your code for workloads realistic for your app. |
| 21 | + |
| 22 | +If you're not familiar with it [Godbolt] is an invaluable tool for examining |
| 23 | +compiler optimizer behavior. You could also use `llvm-objdump` from the command |
| 24 | +line, but that's just Godbolt with extra steps. |
| 25 | + |
| 26 | +## Implementations |
| 27 | + |
| 28 | +This sample contains the following implementations. Each of their trade-offs are |
| 29 | +discussed briefly, but as mentioned above, you should not rely on the |
| 30 | +performance results measured here to make a decision for your app. |
| 31 | + |
| 32 | +### Auto-vectorization |
| 33 | + |
| 34 | +See [auto_vectorization.h] for the implementation. |
| 35 | + |
| 36 | +This implementation is written in generic C++ and contains no explicit SIMD. The |
| 37 | +only vectorization that will be performed is Clang's auto-vectorization. This |
| 38 | +makes for the most portable code and readable code, but at the cost of |
| 39 | +performance. |
| 40 | + |
| 41 | +See https://llvm.org/docs/Vectorizers.html for Clang's docs about |
| 42 | +auto-vectorization. |
| 43 | + |
| 44 | +### std::simd |
| 45 | + |
| 46 | +This isn't actually available yet. It's an experimental part of the C++ standard |
| 47 | +and is in development in libc++, but NDK r27 happened to catch it right in the |
| 48 | +middle of a rewrite, so it's not currently usable. |
| 49 | + |
| 50 | +See https://en.cppreference.com/w/cpp/experimental/simd/simd. |
| 51 | + |
| 52 | +### Clang vectors |
| 53 | + |
| 54 | +See [clang_vector.h] for the implementation. |
| 55 | + |
| 56 | +This implementation uses Clang's generic vector types. This code is mostly as |
| 57 | +portable as the auto-vectorization implementation, with the only caveat being |
| 58 | +that it is limited by the width of the vector registers for the target hardware. |
| 59 | +To deal with problems that don't fit in the target's vector registers, you would |
| 60 | +need to either alter the algorithm to tile the operations, or use Scalable |
| 61 | +Vector Extensions (AKA [SVE]). |
| 62 | + |
| 63 | +However, the benefit of the portability trade-off is that this does outperform |
| 64 | +the auto-vectorization implementation. |
| 65 | + |
| 66 | +See |
| 67 | +https://clang.llvm.org/docs/LanguageExtensions.html#vectors-and-extended-vectors. |
| 68 | + |
| 69 | +### Neon |
| 70 | + |
| 71 | +See [neon.h] for the implementation. |
| 72 | + |
| 73 | +This implementation uses the Arm-specific Neon vector types. The implementation |
| 74 | +is nearly identical to the one in [clang_vector.h]. The only differences how the |
| 75 | +vector type is specified, and the restriction to only working with matrices of 4 |
| 76 | +rows, which was done for the sake of brevity. It doesn't use the Neon intrinsics |
| 77 | +from `arm_neon.h`. Why not? If you look at how the intrinsics in that file are |
| 78 | +defined, all they actually do (for a little endian system, and Android does not |
| 79 | +support big endian, so we can ignore that caveat) is use the `*` operator and |
| 80 | +leave the correct instruction selection up to Clang. |
| 81 | + |
| 82 | +In other words, you should probably never use this. The generated code should be |
| 83 | +identical to code written with Clang's arch-generic vectors. These benchmarks |
| 84 | +show nearly identical results when run on a Pixel 4A (5G), and the differences |
| 85 | +are actually a problem with the benchmark harness itself: Neon sometimes runs |
| 86 | +marginally faster, unless you swap the order of the benchmarks, then the Clang |
| 87 | +Vector implementation sometimes runs marginally faster. |
| 88 | + |
| 89 | +See |
| 90 | +https://developer.arm.com/documentation/dht0002/a/Introducing-NEON/Developing-for-NEON/Intrinsics |
| 91 | +for more information. |
| 92 | + |
| 93 | +### Clang matrices |
| 94 | + |
| 95 | +See [matrix.h] for the implementation. This is the default implementation for |
| 96 | +`Matrix::operator*`, so unlike the others that file contains the rest of the |
| 97 | +`Matrix` class as well. |
| 98 | + |
| 99 | +This implementation uses Clang's built-in matrix type. This is an experimental |
| 100 | +feature in Clang, but it has the simplest code (because some kind Clang person |
| 101 | +wrote the hard part) and performs the best by a wide margin. There are |
| 102 | +implementation defined limits on the size of the matrix, but within those limits |
| 103 | +the code is as portable as the auto-vectorization implementation. The docs say |
| 104 | +the feature is still under development and subject to change, so be wary of |
| 105 | +using this in production, and definitely don't use these types as part of your |
| 106 | +ABI. |
| 107 | + |
| 108 | +See https://clang.llvm.org/docs/LanguageExtensions.html#matrix-types for more |
| 109 | +details. |
| 110 | + |
| 111 | +### OpenMP SIMD |
| 112 | + |
| 113 | +See [omp_simd.h] for the implementation. |
| 114 | + |
| 115 | +This implementation uses OpenMP's SIMD directive. For some reason this |
| 116 | +under-performs even the auto-vectorized implementation. There are a lot of |
| 117 | +additional specifiers that can be added to the simd directive that would maybe |
| 118 | +improve this implementation. Patches welcome :) |
| 119 | + |
| 120 | +See https://www.openmp.org/spec-html/5.0/openmpsu42.html for more information. |
| 121 | + |
| 122 | +## Alternatives not shown here |
| 123 | + |
| 124 | +There are other approaches that could be used that aren't shown here. |
| 125 | + |
| 126 | +[SVE] scales SIMD to arbitrarily sized vectors, and the C extensions, while |
| 127 | +making for less concise code than is needed for a constrained vector size like |
| 128 | +we have here, handle windowing of data to fit the hardware vector size for you. |
| 129 | +For problems like the small matrix multiply we do here, it's overkill. For |
| 130 | +portability to a wide variety of (Arm) CPUs, it can reduce the difficulty of |
| 131 | +writing SIMD code. |
| 132 | + |
| 133 | +GPU acceleration is a better fit for large data sets. That approach isn't shown |
| 134 | +here because it's substantially more code to set up the GPU for this |
| 135 | +computation, and our data size is so small that the cost of GPU initialization |
| 136 | +and streaming the data to the GPU is likely to make that a net-loss. If you want |
| 137 | +to learn more about GPU compute, see https://vulkan-tutorial.com/Compute_Shader, |
| 138 | +https://www.khronos.org/opengl/wiki/Compute_Shader, and |
| 139 | +https://www.khronos.org/opencl/ (while OpenCL is not guaranteed to be available |
| 140 | +for all Android devices, it is a very common OEM extension). |
| 141 | + |
| 142 | +[auto_vectorization.h]: src/main/cpp/auto_vectorization.h |
| 143 | +[clang_vector.h]: src/main/cpp/clang_vector.h |
| 144 | +[GLM]: https://github.com/g-truc/glm |
| 145 | +[Gobolt]: https://godbolt.org/ |
| 146 | +[matrix.h]: src/main/cpp/matrix.h |
| 147 | +[neon.h]: src/main/cpp/neon.h |
| 148 | +[omp_simd.h]: src/main/cpp/omp_simd.h |
| 149 | +[SVE]: https://developer.arm.com/Architectures/Scalable%20Vector%20Extensions |
0 commit comments