Skip to content

[C++] Random real generator is slow on Arm64 Linux when built with clang #28297

Closed

Description

@asfimport

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.

[1] https://godbolt.org/z/Y3Tc6MTME
[2] https://en.wikipedia.org/wiki/Long_double

Related issues:

PRs and other links:

Note: This issue was originally created as ARROW-12533. Please see the migration documentation for further details.

Activity

  1. asfimport commented on Apr 25, 2021

    @asfimport
    Author

    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.

  2. asfimport commented on Apr 26, 2021

    @asfimport
    Author

    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).

  3. asfimport commented on Apr 27, 2021

    @asfimport
    Author

    yibocai#1:
    -m-long-double-64 is not supported on aarch64 clang, looks it's only for x86.

  4. asfimport commented on Apr 27, 2021

    @asfimport
    Author

    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
  5. asfimport commented on Apr 27, 2021

    @asfimport
    Author

    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.
  6. asfimport commented on May 11, 2021

    @asfimport
    Author

    Antoine Pitrou / @pitrou:
    Issue resolved by pull request 10283
    #10283

  7. added this to the 5.0.0 milestone on Jan 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions