ITensor / NDTensors.jl

A Julia package for n-dimensional sparse tensors.
Apache License 2.0
27 stars 7 forks source link

Microoptimizations of contraction logic code #53

Closed mtfishman closed 3 years ago

mtfishman commented 3 years ago

This adds some microoptimizations to the contraction logic code. The main strategy is minimizing the number of getfield calls to the ContractionProperties mutable struct by avoiding things like props.Acstart in loops. Here is a benchmark:

using BenchmarkTools
using ITensors

N = 6 
d = 1 
i = Index(QN(0, 2) => d, QN(1, 2) => d)
is = IndexSet(n -> settags(i, "i$n"), N)
A = randomITensor(is'..., dag(is)...)
B = randomITensor(is'..., dag(is)...)

@btime $A' * $B samples = 5

on this branch gives:

julia> include("time.jl");
  115.340 ms (868058 allocations: 138.49 MiB)

while on master:

julia> include("time.jl");
  170.253 ms (1261274 allocations: 141.49 MiB)

This case is pretty extreme since it involves contracting many very small blocks, so the contraction logic really dominates.

Most of the rest of the time spent in this example is block sparse logic like creating the block-offset list for the output tensor.

codecov-io commented 3 years ago

Codecov Report

Merging #53 (6170aa5) into master (649930a) will increase coverage by 0.55%. The diff coverage is 50.79%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #53      +/-   ##
==========================================
+ Coverage   34.87%   35.43%   +0.55%     
==========================================
  Files          22       22              
  Lines        2523     2588      +65     
==========================================
+ Hits          880      917      +37     
- Misses       1643     1671      +28     
Impacted Files Coverage Δ
src/blocksparse/blocksparsetensor.jl 47.81% <0.00%> (ø)
src/contraction_logic.jl 51.17% <51.61%> (+1.59%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 649930a...6170aa5. Read the comment docs.