ASCII Validation from 2.5 to 114 GB/s with SIMD
14 points by modulovalue
14 points by modulovalue
Interesting. I haven't tested throughput, but from a very quick skim, this autovectorizes quite well with both gcc and clang on x86 (edit: once the early return is removed):
int
scalar(char *b, int len) {
int acc = 0;
for (int i = 0; i < len; i++)
acc |= b[i] & 0x80;
return acc;
}
I was expecting to see some exploration of what the assembly looks like, and how autovectorization kicks in.
You shouldn't need any vectorization to reach 120+ GB/s on one thread.
; The rsi/rdi registers point to the start/end of buffer.
; Assume for simplicity that rdi - rsi is divisible by 24.
xor rax, rax
xor rcx, rcx
xor rdx, rdx
; The rax/rcx/rdx registers are three phased or-accumulators, which
; is the usual trick for splitting up the loop-carried dependency chain
; for commutative associative aggregations like this or-accumulation.
; A slight variation works for non-commutative, merely associative ops.
L:
or rax, [rsi]
or rcx, [rsi + 8]
or rdx, [rsi + 16]
add rsi, 24
cmp rsi, rdi
jne L
; Fold the phased accumulators into rax.
or rax, rcx,
or rax, rdx
; Test if the non-ASCII bit was set in any of the buffer's bytes.
movabs rcx, 0x8080...
test rax, rcx
This can sustain at least 1 iteration per cycle (24 bytes per cycle, assuming no memory bottleneck) on recent processors. To run in 1 iteration per cycle, the microarchitecture must be able to sustain 3 scalar loads per cycle and 5 instructions per cycle. E.g. the Zen 5 I'm writing this comment with ticks that box. At 5.5 GHz that would top out at 132 GB/s. Using 4 phased accumulators instead of 3 exposes even more ILP. You can also do the usual stuff like unrolling to arbitrarily reduce the administrative overhead from add and cmp/jne; the real work is or reg, [rsi + disp] per 8 bytes.
Note that I moved the 0x80 test to the end of the loop. Hopefully it's clear why that's valid: each byte of the final accumulator is the bitwise-or of all byte positions (mod 8) in the buffer. The 8th bit will be set in one of those accumulator bytes iff it was set in one of the bytes of the original buffer.
Of course this computational structure works just as well with vectorization but the scalar approach is fast enough for this problem that it's a compelling stopping point.
You shouldn't need any vectorization to reach 120+ GB/s on one thread.
Although this doesn't use the SSE/AVX SIMD registers/operations, this is vectorized data processing using general-purpose registers.
We can agree that every bitwise operation on words is a vertical operation on bit-vectors, and indeed this is how I think about such things. For precision I should have said SIMD.
Autovectorization continues not to be a programming model: When I rewrote the ASCII acceleration earlier this year, I learned that small snippets autovectorize nicely when godbolting, but the autovectorization falls apart when they are inlined together.
To be completely honest with you, this use case vectorizes really well where autovectorization is an option, and it's a one-liner in Rust. I wouldn't recommend optimizing it by hand. However, that stops being the case once more complex kernels are involved (e.g., a vectorized BLAKE3), that is, these techniques become relevant for more advanced kernels that can't be autovectorized.
But your point is totally valid, I should probably add a sentence or two about autovectorization. Thanks for the feedback!
I think one of the big parts articles like these have is figuring out how you know you need to use these techniques, and how you figure out if it's having the effects you want on the generated code.
In short -- how do you know you need this? And even more importantly, what's the process you used to figure out if the changes you were making were steps in the right direction?
Unfortunately I don't have a good answer. Luckily we have a lot of people like Daniel Lemire that have figured many of those things out for us. To me it feels like theorem proving because one needs to figure out how to move from A to B, from one computational model (SISD, single instruction single data) to a new one, SIMD. I think it's mostly pattern recognition. For example it's on my todo list to generalize simdjson so I can apply their techniques to the parser generator I'm working on, but I don't think there's a simple general strategy here.
how do you know you need this?
I know for example that ASCII validation can achieve ~160 GB/s when the data fits into L1. I know that it's roughly ~60 GB/s on an M3 when the caches are too small. If I need to process lots of data, and it's taking me a long time, then one can calculate the throughput, model his computation as a flow graph and see where the critical path is. In parsing, for example, it's IO, syscalls, lexing and parsing. If any of those are significantly slower than they should be, then there's an indication that things could be sped up.
Perhaps more than a sentence or two! Why does it stop being the case with more complex kernels? / What are the stumbling blocks?
I plan to write more about this in a future post, however, one of the challenges is that it's difficult for an autovectorizer to know how far to unroll if the loop body is complex. A human can use context sensitive information such as that the routine is marked as "never-inline" to simply unroll more. An autovectorizer is a generic tool that needs to perform well (and terminate fast) across many use cases. A human can take more time and optimize longer for a specific use case.