Skip to content

Matrix exponential compiles slowly on Julia 1.0 #631

Open
@sethaxen

Description

@sethaxen

Here's a simple example:

julia> using Pkg, LinearAlgebra, StaticArrays;

julia> v = @MMatrix zeros(Float64, 5, 5);

julia> v[2, 3] = -π;

julia> v[3, 2] = π;

julia> v
5×5 MArray{Tuple{5,5},Float64,2,25}:
0.0  0.0       0.0      0.0  0.0
0.0  0.0      -3.14159  0.0  0.0
0.0  3.14159   0.0      0.0  0.0
0.0  0.0       0.0      0.0  0.0
0.0  0.0       0.0      0.0  0.0

On Julia 1.1, this compiles and runs quickly

julia> @time exp(v)
8.741098 seconds (23.58 M allocations: 983.001 MiB, 8.48% gc time)
5×5 MArray{Tuple{5,5},Float64,2,25}:
1.0   0.0           0.0           0.0   0.0
-0.0  -1.0          -2.35127e-16  -0.0  -0.0
0.0   2.35127e-16  -1.0           0.0   0.0
0.0   0.0           0.0           1.0   0.0
0.0   0.0           0.0           0.0   1.0

julia> versioninfo()
Julia Version 1.1.0
Commit 80516ca202 (2019-01-21 21:24 UTC)
Platform Info:
OS: macOS (x86_64-apple-darwin14.5.0)
CPU: Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-6.0.1 (ORCJIT, haswell)

julia> Pkg.status("StaticArrays")
    Status `~/.julia/environments/v1.1/Project.toml`
  [90137ffa] StaticArrays v0.11.0 #master (https://github.com/JuliaArrays/StaticArrays.jl)

On Julia 1.0, it takes nearly 50x longer:

julia> @time exp(v)
406.894653 seconds (24.31 M allocations: 1.512 GiB, 0.31% gc time)
5×5 MArray{Tuple{5,5},Float64,2,25}:
1.0   0.0           0.0           0.0   0.0
-0.0  -1.0          -2.35127e-16  -0.0  -0.0
0.0   2.35127e-16  -1.0           0.0   0.0
0.0   0.0           0.0           1.0   0.0
0.0   0.0           0.0           0.0   1.0

julia> versioninfo()
Julia Version 1.0.4
Commit 38e9fb7f80 (2019-05-16 03:38 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin15.6.0)
  CPU: Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.0 (ORCJIT, haswell)

julia> Pkg.installed()["StaticArrays"]
v"0.11.0"

The culprit appears to be these lines

StaticArrays.jl/src/expm.jl

Lines 105 to 112 in e77e3ef

U = A6*(S(1)*A6 + S(16380)*A4 + S(40840800)*A2) +
(S(33522128640)*A6 + S(10559470521600)*A4 + S(1187353796428800)*A2) +
S(32382376266240000)*I
U = A*U
V = A6*(S(182)*A6 + S(960960)*A4 + S(1323241920)*A2) +
(S(670442572800)*A6 + S(129060195264000)*A4 + S(7771770303897600)*A2) +
S(64764752532480000)*I
expA = (V - U) \ (V + U)

If I remove them and everything afterward, the function compiles and returns quickly. Also, if I interrupt the stalled command (exp(v)) with Ctrl+C, and then re-run the command, it runs instantly.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions