Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of linear algebra operations in Julia

Tags:

julia

If I have a command y = A*B*x where A & B are large matrices and x & y are vectors, will Julia preform y = ((A*B)*x) or y = (A*(B*x))?

The second option should be the best as it only has to allocate an extra vector rather than a large matrix.

like image 295
bfletch Avatar asked Jul 07 '17 03:07

bfletch


1 Answers

The best way to verify this kind of thing is to dump the lowered code via @code_lowered macro:

julia> @code_lowered A * B * x
CodeInfo(:(begin 
        nothing
        return (Core._apply)(Base.afoldl, (Core.tuple)(Base.*, (a * b) * c), xs)
    end))

Like many other languages, Julia does y = (A*B)*x instead of y = A*(B*x), so it's up to you to explicitly use parens to reduce the allocation.

julia> using BenchmarkTools

julia> @btime $A * ($B * $x);
  6.800 μs (2 allocations: 1.75 KiB)

julia> @btime $A * $B * $x;
  45.453 μs (3 allocations: 79.08 KiB)
like image 173
Gnimuc Avatar answered Sep 29 '22 06:09

Gnimuc