-
Notifications
You must be signed in to change notification settings - Fork 1
Description
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 aTuple
of coefficients andSymbol
s that could be merged. The third element in the tuple is aVector
ofSymbol
that are intermediate, i.e., freeable. It is something like the inverse ofadd_sum!()
. -
MulitLincombCompgraph
representing graph with lincomb of several nodes at once. This can be created withMultiLincombCompgraph(g)
- Make
execute_operation!
work forMultiLincombCompgraph
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.