You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello,
I tried benchmark on 7950x cpu and performance is in some tests up to 2.3x faster but in other tests much slower (like 0.3x) compared to classical sorting. Is amd implementation of avx512 not so powerful (and your code is not suitable for zen4) or is it something else ?
Thankfully, replacing calls to compressstoreu with their emulated versions significantly improves the results.
I'm going to push the fix on a fork and will make a PR.
Reacted by Oscar Barenys, Suresh, duli, ss, Muhammed Fatih BALIN and Agriya Khetarpal
Avoid the memory destination form of COMPRESS instructions. These forms are implemented using microcode and achieve a lower store bandwidth than their register destination forms which use fastpath macro ops.
In short the idea is: if CPU has AVX-512, but is Zen 4, then use AVX2. Otherwise use AVX-512 on Zen 5 and future ones. That would have less negative performance consequences than solutions like openjdk/jdk#16124
Restriction of the AVX512 sort acceleration to only Intel CPUs. A performance regression (due to micro-architectural differences) was reported for AMD Zen4 CPUs in the comments section of PR.
The patch from https://web.archive.org/web/20230319232625/https://github.com/natmaurice/x86-simd-sort/commit/41d03b2d8f3b62a2ee6a3a97a8da7f193a407026 is actually incomplete. It improves things if we only care about Zen 4, but the goal is to have single library that is well optimized for wide range of popular CPUs. If you define SW_VCOMPRESS then you gain performance on Zen 4, but lose performance on Zen 5 and other AVX-512 capable CPUs. If you don't define SW_VCOMPRESS then you get situation from this thread's original post. Therefore, to get the most optimized version in all cases, the code would need to be compiled twice, once with SW_VCOMPRESS defined and once without, and then the dispatch would need to choose between these two copies. That would increase maintenance costs, since there would need to be 2x testing of AVX-512 versions, and only improve performance for Zen 4, which is old generation of CPUs already. I'm not sure if it's worth the effort compared to the (at least simple sounding) fix described in #6 (comment)
@tarsa I am fine with that approach. We could start with disabling avx512 on zen4 and eventually get to enabling it with a SW_VCOMPRESS enabled. I am happy to review to a pull request if someone can work on it and provide benchmarks. I am on travel for the next 6 weeks and so I might not get to it immediately.
I am fine with that approach. We could start with disabling avx512 on zen4
That could be done by checking the CPU family number. By https://en.wikipedia.org/wiki/List_of_AMD_CPU_microarchitectures the CPU family number for Zen 3 to Zen 4 is 25 (decimal) / 19h (hexadecimal). On that CPUs, the AVX2 sort variant is faster than the current AVX512 sort variant.
Activity
gregy4 commentedon Feb 16, 2023
I even tried to use aocc compiler since support for zen4 is limited in gcc-12 but I ended with similar results.
natmaurice commentedon Feb 16, 2023
Zen 4 based CPUs perform poorly because AMD's implementation of
compressstoreuis extremely inefficient (~142 cycles latency / throughput of 54-72 according to uops.info. It is even worse than an 'emulated' version.I've also run the benchmark on my 7700X CPU and also get extremely poor results for Zen 4.
Thankfully, replacing calls to
compressstoreuwith their emulated versions significantly improves the results.I'm going to push the fix on a fork and will make a PR.
gregy4 commentedon Feb 16, 2023
Thanks for the explanation. It is good that the instruction can be emulated and results can be similar to intel speedup.
tarsa commentedon Nov 23, 2024
The answer for slow performance of AVX512 version of x86-simd-sort on Zen 4 is most probably explained in AMD manuals which could be found at: https://www.amd.com/en/search/documentation/hub.html#q=software%20optimization%20guide%20for%20the%20amd%20microarchitecture&f-amd_document_type=Software%20Optimization%20Guides
Software Optimization Guide for the AMD Zen4 Microarchitecture has following remark in "2.11.2 Code recommendations" chapter:
Software Optimization Guide for the AMD Zen5 Microarchitecture doesn't have any remark about COMPRESS instructions.
Could you add some code that disables the AVX512 version on Zen4, but keeps it enabled on Zen5 and future Zen architectures?
ValeZAA commentedon Nov 23, 2024
https://web.archive.org/web/20230319232625/https://github.com/natmaurice/x86-simd-sort/commit/41d03b2d8f3b62a2ee6a3a97a8da7f193a407026
OOPS
r-devulap commentedon Nov 25, 2024
@tarsa Will this fix work for you? You would need to compile this library with a macro enabled.
tarsa commentedon Nov 28, 2024
@r-devulap I'm thinking more about adding Zen 4 AVX-512 exclusion to runtime dispatch, i.e. probably here: https://github.com/intel/x86-simd-sort/blob/59e298d8c9d1bee2cded744b9adbe31107ee220c/lib/x86simdsort.cpp
In short the idea is: if CPU has AVX-512, but is Zen 4, then use AVX2. Otherwise use AVX-512 on Zen 5 and future ones. That would have less negative performance consequences than solutions like openjdk/jdk#16124
ValeZAA commentedon Nov 28, 2024
But AVX512 is faster?? Why would you use AVX2?
tarsa commentedon Nov 28, 2024
The patch from https://web.archive.org/web/20230319232625/https://github.com/natmaurice/x86-simd-sort/commit/41d03b2d8f3b62a2ee6a3a97a8da7f193a407026 is actually incomplete. It improves things if we only care about Zen 4, but the goal is to have single library that is well optimized for wide range of popular CPUs. If you define SW_VCOMPRESS then you gain performance on Zen 4, but lose performance on Zen 5 and other AVX-512 capable CPUs. If you don't define SW_VCOMPRESS then you get situation from this thread's original post. Therefore, to get the most optimized version in all cases, the code would need to be compiled twice, once with SW_VCOMPRESS defined and once without, and then the dispatch would need to choose between these two copies. That would increase maintenance costs, since there would need to be 2x testing of AVX-512 versions, and only improve performance for Zen 4, which is old generation of CPUs already. I'm not sure if it's worth the effort compared to the (at least simple sounding) fix described in #6 (comment)
r-devulap commentedon Dec 5, 2024
@tarsa I am fine with that approach. We could start with disabling avx512 on zen4 and eventually get to enabling it with a SW_VCOMPRESS enabled. I am happy to review to a pull request if someone can work on it and provide benchmarks. I am on travel for the next 6 weeks and so I might not get to it immediately.
tarsa commentedon Apr 19, 2025
That could be done by checking the CPU family number. By https://en.wikipedia.org/wiki/List_of_AMD_CPU_microarchitectures the CPU family number for Zen 3 to Zen 4 is 25 (decimal) / 19h (hexadecimal). On that CPUs, the AVX2 sort variant is faster than the current AVX512 sort variant.