Skip to content

Dot fusion and extracting sums #36

@jarlebring

Description

@jarlebring

In julia, the dot should be used extensively to save allocations. Compare:

julia> A=randn(100,100);
julia> B=randn(100,100);
julia> C=randn(100,100);
julia> D1=randn(100,100);
julia> D2=randn(100,100);
julia> @btime begin; D1 .= A + B +C; end;
  9.555 μs (4 allocations: 78.23 KiB)
julia> @btime begin; D2 .= A .+ B  .+ C; end;
  5.514 μs (4 allocations: 128 bytes)

Note that the dot version is both faster and has less allocations. The reason it is faster is that the dot-sum is not computing (A+B)+C but instead summing a_ij+b_ij+c_ij which does not require the storage of an intermediate matrix (and the summing of three numbers is faster than summing two numbers twice because of specialized compiler level properties).

Our graph has quite hard-coded use of sum of two matrices at a time. We can still generate code that exploits this.

  • extract_sums(graph) -> Vector{Tuple{Vector{Float64},Vector{Symbol},Vector{Symbol}}}: Returns a representation of sums which have the potential to be approached with dot-products. Each element of the returned vector is a Tuple of coefficients and Symbols that could be merged. The third element in the tuple is a Vector of Symbol that are intermediate, i.e., freeable. It is something like the inverse of add_sum!().
  • MulitLincombCompgraph representing graph with lincomb of several nodes at once. This can be created with MultiLincombCompgraph(g)
  • Make execute_operation! work for MultiLincombCompgraph by writing julia gen version of :lincomb.
  • Use this in code_gen... to be further specified.

Example:

The last Vector in the output is a list of nodes corresponding to where intermediate variables are stored ending with the node with the output of the sum.

julia> degopt=Degopt([3 3.2 0; 4.1 5.0 6.0], [4 5.0 0 ; 4.4 5.5 6.6],[2; 3; 4.1; 5.0]);
julia> (g,_)= gen_degopt_poly(degopt);
julia> Q=extract_sums(g)
9-element Vector{Any}:
 ([3.0, 3.2], [:I, :A], [:Ba2])
 ([4.0, 5.0], [:I, :A], [:Bb2])
 ([4.1 5.0 6.0], [:I, :A, :B2], [:Ba3_2, :Ba3])
 ([4.4 5.5 6.6], [:I, :A, :B2], [:Bb3_2, :Bb3])
 ([2.0, 3.0, 4.1, 5.0], [:I, :A, :B2, :B3], [:T2k2,:T2k3,:T2k5])
julia> q=Q[end]; # Check it for last row
julia> size(q[1]) == size(q[2]) 
true
julia> size(q[1],1) == size(q[3],1)-1
true

The lengths of each element in the returned vector should always be (n,n,n-1) (n,n,m).

In the code generation we want the sum to be computed directly without the storing the intermediate nodes. The intermediate nodes in the returned sequences should therefore never appear anywhere else in the graph.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy