ASCII Validation from 2.5 to 114 GB/s with SIMD

21 points by modulovalue 23 hours ago on lobsters | 12 comments

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.

pervognsen | 20 hours ago

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.

aengelke | 15 hours ago

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.

pervognsen | 14 hours ago

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.

tomsmeding | 8 hours ago

I think that's a little reductive; how I'd understand "vectorised" is handling multiple data units in parallel in a single instruction, where "data unit" refers to the natural unit as would appear in an implementation optimised for clarity instead of performance. Here that would be a byte, so in this sense you're vectorising in vectors of 8 bytes (which do indeed fit in one register on x64).

EDIT: the article's step 2 does precisely this, except without the 3x unrolling. Is Dart's codegen just mediocre here?

pervognsen | 7 hours ago

EDIT: the article's step 2 does precisely this, except without the 3x unrolling. Is Dart's codegen just mediocre here?

A 3x unroll does nothing by itself to alleviate the critical path bottleneck. You also need to split the accumulator into multiple independent accumulators. And when you do that it's important to not leave three interleaved add rsi, 8 but one add rsi, 24 since add and or are tied as critical paths. I only highlight the latter point because it's easy to overlook that the combined add rsi, 24 is both work reducing (the [reg + disp] AGU adds are free in the use-it-or-lose-it sense) and latency reducing. On a wide machine the latency reduction still matters to performance even when the work reduction doesn't.

hsivonen | 18 hours ago

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.

[OP] modulovalue | 21 hours ago

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?

[OP] modulovalue | 19 hours ago

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?

[OP] modulovalue | 19 hours ago

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.