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
{{ message }}
Repository navigation
[C++] Random real generator is slow on Arm64 Linux when built with clang #28297
Many benchmarks run very slow on Arm64 Linux when built with clang.
Most time is spent in preparing test data, not the test itself.
Per my investigation, it boils down to poor performance of std::uniform_real_distribution, which uses software emulated long double arithmetic on Arm64 [1].
Apple M1 doesn't have this issue. Clang aarch64 sets long double size to 64 bits on MacOS, but 128 on Linux [2].
Gcc aarch64 doesn't have this issue. It doesn't use long double to generate random reals [1].
clang -ffast-math option removes the long double arithmetic (and adds other simplifications to floating point arithmetic), it improves speed 100x on Arm64 in generating random reals.
It may deserve some effort to study if long double is really necessary, and if -ffast-math is acceptable for generating test bits.
Antoine Pitrou / @pitrou:
We don't need excellent statistical properties when generating test data. We can reimplement a faster std::uniform_real_distribution and/or a faster std::bernoulli_distribution. Or we can use -ffast-math, but that adds complication to the build chain.
yibocai#1:
This issue is gone if using clang's own c++ library libc++.
Most Linux distros only installs gnu libstdc+. Clang libc+ is not widely used. I tested clang libc++ on ubuntu-18.04 aarch64. The test binary is 100x faster with clang libc++ than gnu libstdc++.
See https://godbolt.org/z/d79TW53cb, adding '-stdlib=libc++' generates much better code.
$ sudo apt install libc++-dev libc++abi-dev
# test gnu libstdc++
$ clang++-10 -O3 test.cc
$ time ./a.out
real 0m11.002s
user 0m10.998s
sys 0m0.004s
# test clang libc++
$ clang++-10 -stdlib=libc++ -O3 test.cc
$ time ./a.out
real 0m0.099s
user 0m0.095s
sys 0m0.004s
yibocai#1:
As clang libc++ is not adopted widely, I think it makes sense to implement a random real distribution code in Arrow test.
I looked at implementation in gcc and clang. I plan to port clang libc++ code to Arrow. Clang adopts Apache 2.0 license, and the code looks more optimized than gcc.
We need three function/classes.
std::generate_canonical function, produces random reals in [0, 1). This function does the dirty job.
std::uniform_real_distribution class, calls std::generate_canonical to produce reals in any value range.
std::bernoulli_distribution class, a shallow wrap of std::uniform_real_distribution.
Activity
asfimport commentedon Apr 25, 2021
Antoine Pitrou / @pitrou:
We don't need excellent statistical properties when generating test data. We can reimplement a faster
std::uniform_real_distributionand/or a fasterstd::bernoulli_distribution. Or we can use-ffast-math, but that adds complication to the build chain.asfimport commentedon Apr 26, 2021
Eduardo Ponce / @edponce:
We can limit the size of long double in Clang with -m-long-double-64 (refer to https://clang.llvm.org/docs/ClangCommandLineReference.html#long-double-flags).
asfimport commentedon Apr 27, 2021
yibocai#1:
-m-long-double-64 is not supported on aarch64 clang, looks it's only for x86.
asfimport commentedon Apr 27, 2021
yibocai#1:
This issue is gone if using clang's own c++ library libc++.
Most Linux distros only installs gnu libstdc+. Clang libc+ is not widely used. I tested clang libc++ on ubuntu-18.04 aarch64. The test binary is 100x faster with clang libc++ than gnu libstdc++.
See https://godbolt.org/z/d79TW53cb, adding '-stdlib=libc++' generates much better code.
asfimport commentedon Apr 27, 2021
yibocai#1:
As clang libc++ is not adopted widely, I think it makes sense to implement a random real distribution code in Arrow test.
I looked at implementation in gcc and clang. I plan to port clang libc++ code to Arrow. Clang adopts Apache 2.0 license, and the code looks more optimized than gcc.
We need three function/classes.
std::generate_canonicalfunction, produces random reals in[0, 1). This function does the dirty job.std::uniform_real_distributionclass, callsstd::generate_canonicalto produce reals in any value range.std::bernoulli_distributionclass, a shallow wrap ofstd::uniform_real_distribution.asfimport commentedon May 11, 2021
Antoine Pitrou / @pitrou:
Issue resolved by pull request 10283
#10283