Live data from Hacker News

Vectorized and performance-portable Quicksort

opensource.googleblog.com

141–147 of 147 posts

Re: Vectorized and performance-portable Quicksort

#141
post #113

Earlier quoted context omitted.

Thanks for sharing the pointers. I hadn't come across that, will read soon :)

Interestingly from the portability angle, this was all written in C# but with the SIMD intrinsics there he was still able to obtain ~SOTA performance. The repo I linked is his C++ version of the C# original. It was submitted to the C# standard library but was rejected (as an observer, the rejection was disappointing) – had it been accepted C# would have been in the interesting position of having a faster sort than an…

I've read parts 1-5, there doesn't seem to be a part6. Thanks again for the link, it is regrettable that there is duplication of effort. His approach is quite similar to ours (including HeapSort, sorting network, unrolled in-place partition), and re-invents what he calls "double-pumped partitioning" (introduced by Bramas).

Copying the blog post to arxiv and citing some related work so it comes up in alerts and cited-by searches would [have made/make] this good work visible to a wider circle, including the algorithms community.

It's nice to see a C++ version, this will be easier for me to benchmark, and it also seems to support other built-in types now, as well as AVX2+AVX-512 (though not other platforms). A quick list of differences based on reading the blog:

* Nifty idea for simplifying the branch in partition. (We also found that branchless code is worse)

* Major effort to make all reads vector-aligned. This could make a difference especially on AVX-512. I had tried two variants but it was difficult to make this safe for asan (no out of bounds reads allowed).

* The lookup table is 2KB, which could be halved using 32-bit broadcast + variable shift instead of PDEP.

* The sorting network seems to be half the size and bitonic. I haven't yet compared the number of permute instructions (not obvious from the source because it's generated).

* The pivot is only a median of three. We use a much larger sample.

Re: Vectorized and performance-portable Quicksort

#142
post #128

Earlier quoted context omitted.

Hey mate, can you throw some challenges/problems at me to optimize? I don't know what's worth looking at! Thanks!

Depends on your goals? If you'd like to contribute code under Apache2, I'd be happy to collaborate. Feel free to reach out to the email in the Highway README. Some of the things on my short to medium-term todo list include: - CompressStore for 8-bit lanes (before Icelake, that will likely require splitting up into runs of 8 bytes with one PSHUFB + store each) - also ExpandLoad (analogous to CompressStore) and Leading…

I have no idea what you just wrote. I don't speak C, know basically nothing about it, I'm not in the business and I'm not ever going to do either, because I don't like it.

So I looked that up. You're talking about intrinsics. I have no idea about that, beyond "Why would people rather remember this instead of just learning the proper assembly code, which is far easier to read and remember anyway?"

I don't write code for compilers to do the work I am supposed to be doing, so I have no idea of the world you code in.

Your response doesn't really answer my question, but that's totally my fault, so ...

Hand me a box with a defined problem, some compiled code I can run to benchmark my own solution against, then wait for me to beat your solution within defined parameters.

My goal is to have some challenges that need optimization, but there needs to be a point to it. I can take any problem from the web, rethink the approach and rewrite it in assembly, but in 99.999% of all cases there's no point to it and I have nothing to compare it against on my own machine anyway.

I do not know what's generally useful to optimize and I certainly do not know who actually cares about that anymore.

Like, for example, how the pattern matching/string search function in freepascal is damn slow. I've researched the topic and noticed that all the solutions I've found are basically crap, so I wrote my own, beating it by some wide margin in terms of codesize and performance. I don't even know why these devs accept subpar solutions, but responses I got to that question were, basically, "It's fast enough" ... which is dumb.

Still, I don't actually have a way of fairly comparing my work.

Regardless, apparently the fastest fixed size pattern/string search algorithms I've found are all crap, but I really have no way of creating a fair comparison. Oh, on that note ... you don't, by chance, have a pattern matching benchmark for fixed sized needles I could compare my own against?

Sorry for being a mess. :D I really just wanna work on something that's actually a challenge AND something people actually need to run more efficiently, just like the QuickSort example.

Re: Vectorized and performance-portable Quicksort

#143

Man, I was thinking about this problem and came up with some weird solutions. I've actually thought all this stuff has already been solved? Is there a point in still trying to improve efficiency of sorting? I can do that! I'd love such a challenge if there was a point to it!

Sorting is one of a few things that computers spend so much time doing, even a relatively small improvement over that state of the art can have a big impact. On the one hand this means if you can come up with a better way to do it in some case that's awesome and could be very valuable. On the other hand it means a lot of people have thought pretty hard about it and the next improvement may be hard to come up with unl…

Thanks for your post! Let me explain my problem, which fits right to it. It works as an example, too.

So, in freepascal there's some subpar pattern matching/string search function. It annoyed me so greatly, I've started researching into the "state of the art" and apparently they're all crap. My fixed size pattern matching solution fits into less than 256 bytes, not counting setup code, which is minimal.

And here start the problems for me, because I have no way of actually comparing my solution to "the state of the art", because "the state of the art" doesn't offer me downloadable, compiled benchmarks I can just run to compare stuff to my own and learning a new language (like, i don't speak C and certainly never will), installing a compiler, getting through all the walls that inevitably be in my way to getting it to run, etc ... totally breaks my timebudget and thus I drop the problem.

Basically, what I'm looking for is:

Give me a problem that's worth tackling, with a defined set of parameters and a compiled solution I can compare my own against and I will beat it eventually. I've tried doing this on my own, but that's just a big no-go on many levels, so I'm dependent on someone handing something to me.

Like ... the QuickSort. I have no way of comparing my potential own solution against "the state of the art", which is - as far as I know - pretty much all that's holding me back.

Re: Vectorized and performance-portable Quicksort

#144
post #52

Earlier quoted context omitted.

CPU vendors have recently introduced SVE (Arm) and RVV (RISC-V) and we support those in Highway and thus also vqsort. It's definitely interesting to discuss std::sort benefitting from such optimizations, but "very easy" seems rather optimistic.

Do you expect these optimizations making their way into std::sort eventually?

It depends on the goals of the library maintainers. Users who really care about speed may already be using another library such as PDQsort or ips4o. Given that, the stdlib maintainers may reason that adding code and 'complexity' (not all developers understand or are able to maintain SIMD) is not worthwhile.

Conversely, they may prefer to work towards the standard library being the fastest known way of doing things. This is now much more feasible given the single portable implementation, vs. having to rewrite thousands of lines for six instruction sets.

Not sure which consideration carries more weight.

Re: Vectorized and performance-portable Quicksort

#145

Looks like djb is giving it a spin: https://twitter.com/hashbreaker/status/1533201734369103872

Response here because I'm not on Twitter: https://github.com/google/highway/issues/736

In short, what is being compared is O(1) djbsort sorting network, vs our full quicksort with pivot sampling, partitioning, then sorting network.

This is because our sorting network size is 16 * elements_per_vector i.e. 128 in this configuration.

Re: Vectorized and performance-portable Quicksort

#147
post #36

Author here, happy to discuss.

Is there any benefit to a multipivot sort? The partitioning would be more complex but possibly save memory accesses.

Yes, this is discussed in section 4.2 of our paper: https://arxiv.org/pdf/2205.05982.pdf

In short, it turns out not to help for single core with vectors, but a few initial passes of ips4o (with 64..256-way partitioning) is faster for parallel sorts.

Post reply on HN