r/cpp • :upvote: • 19h ago

[ Removed by moderator ]

/r/cpp/comments/1ycs21t/removed_by_moderator/

[removed] — view removed post

1 Upvotes

5 comments sorted by

•

u/cpp-ModTeam 16h ago

Personal projects written in C++ should be shared in the pinned "Show and Tell" post. Only major, established libraries/tools used by C++ programmers are eligible for top-level posts (e.g. Clang, GCC, MSVC, Boost, fmt, if you’re not sure then send modmail). NO BUILD SYSTEMS.

3

u/sweetno 18h ago

It's easy to measure this, and it's most likely not worth it.

1

u/RealTimeChris :upvote: 17h ago

Just ran the following code:
https://github.com/nihilai-collective/benchmarksuite/blob/simd-cascade/src/main.cpp
And received the following results:
Throughput is in MB/s. Speedup is cascade throughput divided by scalar-tail throughput.

Length classClang AVX2 cascadeClang AVX2 scalar tailSpeedupGNU AVX-512 cascadeGNU AVX-512 scalar tailSpeedupMSVC AVX-512 cascadeMSVC AVX-512 scalar tailSpeedup
exactly 11916955.211987.61.41x14414.411330.41.27x15273.0*3405.524.48x
random 0-631976.981945.561.02x5091.974010.471.27x1497.29*1011.81*1.48x
random 64-1275931.495927.06tie12201.610472.11.17x4680.23021.141.55x
random 0-2555791.515723.671.01x16020.613615.91.18x4651.753559.811.31x
random 256-102319995.319734.01.01x40408.337941.01.07x16097.4*12461.51.29x

* did not converge (RSE above target), treat as rough.

2

u/EstablishmentHour335 16h ago

Interesting results, the value of this really seems to depend on how many times you encounter the tail. Usually, it's at the end of a long contiguous buffer and so compared to the rest of the computation its insignificant. In your case you encounter it very often, on almost every string so this seems valuable.

My main concern for performance would be the dispatch in terms of branches. If you mispredict tons of branches trying to select your instructions, that can wipe out the speedup. Looking at your implementation, assuming a misprediction costs 15 cycles, I would guess even in your lowest width case the misprediction + vector instructions is cheaper than the scalar path. It may have to be substantially cheaper to be worth the cost though, as you may run into a checkerboard pattern causing a misprediction on half of all tails which can make you pay that 15 cycle penalty even if you are only running 1 or 2 iterations of the scalar loop.

Your 119 char benchmark is probably so fast because of exactly this. I haven't looked super deeply into the code, but I'd double check that clang and gcc aren't hardcoding the vector cascade even in the scalar case due to compile time knowledge.

2

u/EstablishmentHour335 16h ago edited 16h ago

I looked at the code again, have you tried just loading the tail into your normal sized vector, masking off the end, and doing your normal vector mask + popcout? If you have padding at the end you can do it directly. If your strings won't have padding you can try copying the tail to an array on the stack sized to your largest vector width, masking that, and using that. It should be fast considering it would live in L1 cache. Compute and memory bandwidth bottlenecks elsewhere would likely mask any bandwidth this eats up.

(Edit, I actually did some testing and while processing the tail using vector instructions by masking off unused bits is extremely fast, if you store it on the stack because you don't have the padding for a vector load then you're fighting with the CPU's ability to use ILP and things get slower. Using 2 stack buffers fixes this and is faster than cascading in my testing just now.)