|
| 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 such as BLAS 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 `$NDK_BIN/clang -S -O2 -o -` |
| 24 | +from the command line for a local workflow. |
| 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 | +### Clang matrices |
| 70 | + |
| 71 | +See [matrix.h] for the implementation. This is the default implementation for |
| 72 | +`Matrix::operator*`, so unlike the others that file contains the rest of the |
| 73 | +`Matrix` class as well. |
| 74 | + |
| 75 | +This implementation uses Clang's built-in matrix type. This is an experimental |
| 76 | +feature in Clang, but it has the simplest code (because some kind Clang person |
| 77 | +wrote the hard part) and performs the best by a wide margin. There are |
| 78 | +implementation defined limits on the size of the matrix, but within those limits |
| 79 | +the code is as portable as the auto-vectorization implementation. The docs say |
| 80 | +the feature is still under development and subject to change, so be wary of |
| 81 | +using this in production, and definitely don't use these types as part of your |
| 82 | +ABI. |
| 83 | + |
| 84 | +See https://clang.llvm.org/docs/LanguageExtensions.html#matrix-types for more |
| 85 | +details. |
| 86 | + |
| 87 | +### OpenMP SIMD |
| 88 | + |
| 89 | +See [omp_simd.h] for the implementation. |
| 90 | + |
| 91 | +This implementation uses OpenMP's SIMD directive. For some reason this |
| 92 | +under-performs even the auto-vectorized implementation. There are a lot of |
| 93 | +additional specifiers that can be added to the simd directive that would maybe |
| 94 | +improve this implementation. Patches welcome :) |
| 95 | + |
| 96 | +See https://www.openmp.org/spec-html/5.0/openmpsu42.html for more information. |
| 97 | + |
| 98 | +## Alternatives not shown here |
| 99 | + |
| 100 | +There are other approaches that could be used that aren't shown here. |
| 101 | + |
| 102 | +### Neon |
| 103 | + |
| 104 | +A Neon implementation would be nearly identical to the one in [clang_vector.h]. |
| 105 | +The only difference is how the vector type is specified. A lot of older Neon |
| 106 | +sample code looks substantially different because it uses the Neon intrinsics |
| 107 | +defined in `arm_neon.h`, but if you look at how the intrinsics in that file are |
| 108 | +defined, all they actually do (for a little endian system, and Android does not |
| 109 | +support big endian, so we can ignore that caveat) is use the `*` operator and |
| 110 | +leave the correct instruction selection up to Clang. |
| 111 | + |
| 112 | +In other words, you should probably never use the Neon-specific approach. The |
| 113 | +generated code should be identical to code written with Clang's arch-generic |
| 114 | +vectors. If you rewrite the [clang_vector.h] implementation to use Neon's |
| 115 | +`float32x4_t` instead of the Clang vector, the results are identical. |
| 116 | + |
| 117 | +### SVE |
| 118 | + |
| 119 | +[SVE] scales SIMD to arbitrarily sized vectors, and the C extensions, while |
| 120 | +making for less concise code than is needed for a constrained vector size like |
| 121 | +we have here, handle windowing of data to fit the hardware vector size for you. |
| 122 | +For problems like the small matrix multiply we do here, it's overkill. For |
| 123 | +portability across various vector widths for the Arm CPUs that support SVE, it |
| 124 | +can reduce the difficulty of writing SIMD code. |
| 125 | + |
| 126 | +### GPU acceleration |
| 127 | + |
| 128 | +GPU acceleration is a better fit for large data sets. That approach isn't shown |
| 129 | +here because it's substantially more code to set up the GPU for this |
| 130 | +computation, and our data size is so small that the cost of GPU initialization |
| 131 | +and streaming the data to the GPU is likely to make that a net-loss. If you want |
| 132 | +to learn more about GPU compute, see https://vulkan-tutorial.com/Compute_Shader, |
| 133 | +https://www.khronos.org/opengl/wiki/Compute_Shader, and |
| 134 | +https://www.khronos.org/opencl/ (while OpenCL is not guaranteed to be available |
| 135 | +for all Android devices, it is a very common OEM extension). |
| 136 | + |
| 137 | +## Function multi-versioning |
| 138 | + |
| 139 | +There are two compiler attributes that can be helpful for targeting specific |
| 140 | +hardware features when optimizing hot code paths: [target] and [target_clones], |
| 141 | +both of which may be referred to as "function multiversioning" or "FMV". Each |
| 142 | +solves a slightly different but related problem. |
| 143 | + |
| 144 | +The `target` attribute makes it easier to write multiple implementations for a |
| 145 | +function that should be selected based on the runtime hardware. If benchmarking |
| 146 | +shows that one implementation performs better on armv8.2 and a different |
| 147 | +implementation performs better on armv8 (see the docs for more details on |
| 148 | +specific targeting capabilities), you can write the function twice, annotate |
| 149 | +them with the appropriate `__attribute__((target(...)))` tag, and the compiler |
| 150 | +will auto-generate the code to select the best-fitting implementation at runtime |
| 151 | +(it uses ifuncs under the hood, so the branch is resolved once at library load |
| 152 | +time rather than for each call). |
| 153 | + |
| 154 | +The `target_clones` attribute, on the other hand, allows you to write the |
| 155 | +function once but instruct the compiler to generate multiple variants of the |
| 156 | +function for each requested target. This means that, for example, if you've |
| 157 | +requested both `default` and `armv8.2`, the compiler will generate a default |
| 158 | +implementation compatible with all Android devices, as well as a second |
| 159 | +implementation that uses instructions available in armv8.2 but not available in |
| 160 | +the base armv8 ABI. As with the `target` attribute, Clang will automatically |
| 161 | +select the best-fitting implementation at runtime. Using `target_clones` is the |
| 162 | +same as using `target` with identical function bodies. |
| 163 | + |
| 164 | +Note that with both of these approaches, testing becomes more difficult because |
| 165 | +you will need a greater variety of hardware to test each code path. If you're |
| 166 | +already doing fine grained targeting like this, that isn't a new problem, and |
| 167 | +using one or both of these attributes may help you simplify your implementation. |
| 168 | + |
| 169 | +Neither of these techniques are shown in this sample. We don't have access to |
| 170 | +enough hardware to benchmark or verify multiple implementations, and (as of NDK |
| 171 | +r27, at least), Clang doesn't support `target_clones` on templated functions. |
| 172 | + |
| 173 | +[auto_vectorization.h]: src/main/cpp/auto_vectorization.h |
| 174 | +[clang_vector.h]: src/main/cpp/clang_vector.h |
| 175 | +[GLM]: https://github.com/g-truc/glm |
| 176 | +[Gobolt]: https://godbolt.org/ |
| 177 | +[matrix.h]: src/main/cpp/matrix.h |
| 178 | +[neon.h]: src/main/cpp/neon.h |
| 179 | +[omp_simd.h]: src/main/cpp/omp_simd.h |
| 180 | +[SVE]: https://developer.arm.com/Architectures/Scalable%20Vector%20Extensions |
| 181 | +[target_clones]: https://clang.llvm.org/docs/AttributeReference.html#target-clones |
| 182 | +[target]: https://clang.llvm.org/docs/AttributeReference.html#target |
0 commit comments