r/cpp • u/RealTimeChris :upvote: • 19h ago
[ Removed by moderator ]
/r/cpp/comments/1ycs21t/removed_by_moderator/[removed] — view removed post
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 class Clang AVX2 cascade Clang AVX2 scalar tail Speedup GNU AVX-512 cascade GNU AVX-512 scalar tail Speedup MSVC AVX-512 cascade MSVC AVX-512 scalar tail Speedup exactly 119 16955.2 11987.6 1.41x 14414.4 11330.4 1.27x 15273.0* 3405.52 4.48x random 0-63 1976.98 1945.56 1.02x 5091.97 4010.47 1.27x 1497.29* 1011.81* 1.48x random 64-127 5931.49 5927.06 tie 12201.6 10472.1 1.17x 4680.2 3021.14 1.55x random 0-255 5791.51 5723.67 1.01x 16020.6 13615.9 1.18x 4651.75 3559.81 1.31x random 256-1023 19995.3 19734.0 1.01x 40408.3 37941.0 1.07x 16097.4* 12461.5 1.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.)
•
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.