Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix pow_fast(x, y) for large integer y #54512

Merged
merged 5 commits into from
May 26, 2024

Conversation

KlausC
Copy link
Contributor

@KlausC KlausC commented May 17, 2024

Fixes #53857

For x::Union{Float32,Float64} and y::Integer with y % Int32 != y a stack trace appears when calling @fastmath x^y.

This fix calls x^y in those cases, while leaving the other cases as is.

@KlausC
Copy link
Contributor Author

KlausC commented May 20, 2024

@oscardssmith , can you have a look on this?

@oscardssmith
Copy link
Member

it looks good to me, but I do wonder whether with that extra branch it is faster than just calling pow.

@KlausC
Copy link
Contributor Author

KlausC commented May 20, 2024

You are partially right:
The following examples show, that on my machine, both 64-bit and 32-bit versions,

  1. Float64: @fastmath a^n is faster than a^n if n = 2^30
  2. Float32: @fastmath a^n is slower than a^n if n = 2^22
    which suggests to use the pow_fast as is only for Float64, and let pow_fast(a::Float32, n::Integer) = a^n
Julia Version 1.12.0-DEV.564
Commit c641bf67d1 (2024-05-20 07:24 UTC)
Platform Info:
  OS: Linux (x86_64-redhat-linux)
  CPU: 12 × Intel(R) Core(TM) i7-10710U CPU @ 1.10GHz
  WORD_SIZE: 64
  LLVM: libLLVM-17.0.6 (ORCJIT, skylake)
Threads: 1 default, 0 interactive, 1 GC (on 1 virtual cores)
Environment:
  JULIA_CPU_THREADS = 1
  LD_LIBRARY_PATH = /usr/local/intel/compilers_and_libraries_2020.1.217/linux/mkl/lib/intel64:/usr/local/lib:
  JULIA_NUM_THREADS = 1
  JULIA_EDITOR = /usr/bin/vim -R -c"set nonumber"

julia> n = 2^30
1073741824

julia> a = 1.0000000001
1.0000000001

julia> using BenchmarkTools
Precompiling BenchmarkTools...
  1 dependency successfully precompiled in 2 seconds. 13 already precompiled.

julia> @btime $b^$n
ERROR: UndefVarError: `b` not defined in `Main`
Suggestion: check for spelling errors or missing imports.
Stacktrace:
 [1] top-level scope
   @ ~/.julia/packages/BenchmarkTools/QNsku/src/execution.jl:496

julia> @btime $a^$n
  23.736 ns (0 allocations: 0 bytes)
1.1133507826969948

julia> @btime @fastmath $a^$n
  21.254 ns (0 allocations: 0 bytes)
1.113350782267788

julia> @btime @fastmath $a^$(Int32(n))
  19.284 ns (0 allocations: 0 bytes)
1.113350782267788

julia> n = 2^22
4194304

julia> b = Float32(1 + 1/n)
1.0000002f0

julia> @btime $b^$n
  9.662 ns (0 allocations: 0 bytes)
3.7182815f0

julia> @btime @fastmath $b^$n
  13.764 ns (0 allocations: 0 bytes)
2.7175977f0

julia> @btime @fastmath $b^$(Int32(n))
  12.772 ns (0 allocations: 0 bytes)
2.7175977f0

Julia Version 1.12.0-DEV.564
Commit c641bf67d1 (2024-05-20 07:24 UTC)
Platform Info:
  OS: Linux (i686-redhat-linux)
  CPU: 12 × Intel(R) Core(TM) i7-10710U CPU @ 1.10GHz
  WORD_SIZE: 32

julia> a = 1.000001
1.000001

julia> n = 2^30
1073741824

julia> @btime $a^$n
  96.309 ns (0 allocations: 0 bytes)
1.1133507826969948

julia> @btime @fastmath $a^$n
  28.971 ns (0 allocations: 0 bytes)
1.1133507826730735

julia> n = 2^22
4194304

julia> b = Float32(1 + 1/n)
1.0000002f0

julia> @btime $b^$n
  14.665 ns (0 allocations: 0 bytes)
2.7182815f0

julia> @btime @fastmath $b^$n
  21.108 ns (0 allocations: 0 bytes)
2.7182815f0

julia> @btime @fastmath $b^$(Int32(n))
  21.111 ns (0 allocations: 0 bytes)
2.7182815f0

@KlausC
Copy link
Contributor Author

KlausC commented May 26, 2024

ping

@oscardssmith oscardssmith merged commit 768921e into JuliaLang:master May 26, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Base.FastMath.pow_fast(a::Float64, i::Int) fails for huge i
2 participants