numericalEFT / GreenFunc.jl

Toolbox to study quantum many-body problem at the treelevel
https://numericaleft.github.io/GreenFunc.jl/
MIT License
11 stars 1 forks source link

broadcast between MeshArray and Array lead to copies #80

Closed kunyuan closed 1 year ago

kunyuan commented 1 year ago

See the example below, g is a MeshArray of the size 100x60

julia> g
Meshed array with dims = (100, 60) and total length = 6000
- Mesh: Tuple{CompositeGrids.SimpleG.Uniform{Float64}, DLRFreq{Float64}} 

julia> @time g .+= rand(100, 60)
  0.000040 seconds (4 allocations: 46.984 KiB)
Meshed array with dims = (100, 60) and total length = 6000
- Mesh: Tuple{CompositeGrids.SimpleG.Uniform{Float64}, DLRFreq{Float64}} 

julia> @time g .+= g
  0.000032 seconds (2 allocations: 64 bytes)
Meshed array with dims = (100, 60) and total length = 6000
- Mesh: Tuple{CompositeGrids.SimpleG.Uniform{Float64}, DLRFreq{Float64}} 

julia> @time g + rand(100, 60)
  0.000030 seconds (5 allocations: 94.000 KiB)
Meshed array with dims = (100, 60) and total length = 6000
- Mesh: Tuple{CompositeGrids.SimpleG.Uniform{Float64}, DLRFreq{Float64}} 

julia> @time g + g
  0.000029 seconds (3 allocations: 47.078 KiB)
Meshed array with dims = (100, 60) and total length = 6000
- Mesh: Tuple{CompositeGrids.SimpleG.Uniform{Float64}, DLRFreq{Float64}} 
kunyuan commented 1 year ago

This link gives an example of proper broadcast implementation https://github.com/JuliaArrays/BlockArrays.jl/blob/master/src/blockbroadcast.jl

kunyuan commented 1 year ago

There should be no allocation. In the above example, g .+= rand(100, 60) creates allocations because the right hand side creates new array