Skip to content

function_(gradient/value)_(jump/average) allocates when u is a view #1301

@AbdAlazezAhmed

Description

@AbdAlazezAhmed

It seems like getting a view of a view that's not indexed by a range allocate. I ran into this when doing error estimation for AMR, it had a lot of allocations and I didn't suspect this could be the issue back then.

using Ferrite

using BenchmarkTools

grid = generate_grid(Hexahedron, (1,2,1));

ip = Lagrange{RefHexahedron, 1}();

fqr = FacetQuadratureRule{RefHexahedron}(2);

iv = Ferrite.InterfaceValues(fqr, ip);

reinit!(iv, first(InterfaceIterator(grid)));

u = rand(100);

ui = @view u[rand(1:100, 16)];

@benchmark function_gradient_jump($iv, $1, $ui)
BenchmarkTools.Trial: 10000 samples with 987 evaluations per sample.
 Range (min  max):  49.479 ns   48.324 μs  ┊ GC (min  max):  0.00%  99.78%
 Time  (median):     63.649 ns               ┊ GC (median):     0.00%
 Time  (mean ± σ):   95.356 ns ± 569.371 ns  ┊ GC (mean ± σ):  17.39% ±  4.06%

  ▅█▇▄▃▃▁▁            ▃▄▄▅▅▄▅▅▄▃▂▁▁▁▁▁                         ▂
  █████████▇▇▅▇██▇▇▆▆▆██████████████████▇█▇▇█▇▆▅▄▆▅▄▄▄▅▄▄▄▅▄▄▅ █
  49.5 ns       Histogram: log(frequency) by time       170 ns <

 Memory estimate: 256 bytes, allocs estimate: 4.
ui = @view u[40:56];

@benchmark function_gradient_jump($iv, $1, $ui)
BenchmarkTools.Trial: 10000 samples with 995 evaluations per sample.
 Range (min  max):  26.543 ns  547.550 ns  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     28.528 ns               ┊ GC (median):    0.00%
 Time  (mean ± σ):   28.915 ns ±  10.339 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

   ▁▁  ▂▄▁  ▄▆▂  ▆█▅   ▁                              ▁        ▁
  ▆██▂▃███▃▃███▅████▇▇▆██▆▅▆▆█▆▆▆▆▇▆▅▆▅▆▅▅▄▄▅▅▇█▅▅▃▅▅▇█▄▃▄▅▃▇█ █
  26.5 ns       Histogram: log(frequency) by time      33.9 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.
ui = @view u[collect(40:56)];

@benchmark function_gradient_jump($iv, $1, $ui)
BenchmarkTools.Trial: 10000 samples with 986 evaluations per sample.
 Range (min  max):  49.354 ns   48.101 μs  ┊ GC (min  max):  0.00%  99.72%
 Time  (median):     91.593 ns               ┊ GC (median):     0.00%
 Time  (mean ± σ):   97.412 ns ± 570.019 ns  ┊ GC (mean ± σ):  16.81% ±  3.94%

  ▆█▇▅▃▃▂▁             ▂▅▄▅▆▅▅▆▆▅▃▂▂▂▂▁▁▁                      ▂
  █████████▇▇▆▇███▇▆▇▇▆█████████████████████▇█▇▇▆▇▆▆▆▅▄▆▅▃▅▃▂▄ █
  49.4 ns       Histogram: log(frequency) by time       163 ns <

 Memory estimate: 256 bytes, allocs estimate: 4.

This might not be an issue if one uses a vector instead of views, I just wanted to document this (and maybe disallow views to be passed to a function that creates views).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions