Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance of @views with reshape #29675

Closed
jkozdon opened this issue Oct 16, 2018 · 2 comments
Closed

Performance of @views with reshape #29675

jkozdon opened this issue Oct 16, 2018 · 2 comments
Labels
arrays [a, r, r, a, y, s] performance Must go faster potential benchmark Could make a good benchmark in BaseBenchmarks

Comments

@jkozdon
Copy link
Contributor

jkozdon commented Oct 16, 2018

When view is used with a reshape in some circumstances there can be a lot of allocation.

Consider the following minimal example:

const N = 1000
function reshapefill(A)
  A = reshape(A, N, N)
  for j = 1:N, i = 1:N
    A[i, j] = 0
  end
end

function noreshapefill(A)
  for j = 1:N, i = 1:N
    A[i, j] = 0
  end
end

let
  A = Array{Float64}(undef, N, N, N)

  B = @views A[1, :, :]
  noreshapefill(B)
  reshapefill(B)

  println("B = @views A[1, :, :]")
  println("fill without reshape")
  @time noreshapefill(B)
  println("fill with reshape")
  @time reshapefill(B)

  C = @views A[:, 1, :]
  noreshapefill(C)
  reshapefill(C)

  println()
  println("C = @views A[:, 1, :]")
  println("fill without reshape")
  @time noreshapefill(C)
  println("fill with reshape")
  @time reshapefill(C)

  D = @views A[:, :, 1]
  noreshapefill(D)
  reshapefill(D)

  println()
  println("D = @views A[:, :, 1]")
  println("fill without reshape")
  @time noreshapefill(D)
  println("fill with reshape")
  @time reshapefill(D)
end

which results in the output

B = @views A[1, :, :]
fill without reshape
  0.022676 seconds (4 allocations: 160 bytes)
fill with reshape
  0.793889 seconds (8.00 M allocations: 320.419 MiB, 3.36% gc time)

C = @views A[:, 1, :]
fill without reshape
  0.000762 seconds (4 allocations: 160 bytes)
fill with reshape
  0.003002 seconds (5 allocations: 224 bytes)

D = @views A[:, :, 1]
fill without reshape
  0.000811 seconds (4 allocations: 160 bytes)
fill with reshape
  0.730238 seconds (8.00 M allocations: 320.419 MiB, 3.70% gc time)

As can be seen when @views is used with a reshape there can be a lot of allocations in some situations but not always.

Here is my versioninfo()

Julia Version 1.0.1
Commit 0d713926f8 (2018-09-29 19:05 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin14.5.0)
  CPU: Intel(R) Xeon(R) CPU E5-1650 v2 @ 3.50GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.0 (ORCJIT, ivybridge)

Not surprisingly, I see similar performance issues when I replace the @views macros with the view function.

@mbauman mbauman added performance Must go faster arrays [a, r, r, a, y, s] labels Oct 16, 2018
@mbauman
Copy link
Member

mbauman commented Oct 16, 2018

Thanks for the report. Looks like we're hitting a splatting penalty here… why isn't SubArray's setindex! inlining here?

julia> @code_warntype reshapefill(B)
%53 = (Base.add_int)(%49, %52)::Int64 # computation of linear index%54 = (Core.tuple)(A, 0)::Tuple{SubArray{Float64,2,Array{Float64,3},Tuple{Int64,Base.Slice{Base.OneTo{Int64}},Base.Slice{Base.OneTo{Int64}}},true},Int64}
  │          (Core._apply)(Base.setindex!, %54, %53)

@vtjnash
Copy link
Member

vtjnash commented Oct 16, 2018

dup #29114

@vtjnash vtjnash closed this as completed Oct 16, 2018
mbauman added a commit that referenced this issue Oct 16, 2018
@KristofferC KristofferC added the potential benchmark Could make a good benchmark in BaseBenchmarks label Oct 16, 2018
mbauman added a commit that referenced this issue Oct 17, 2018
KristofferC pushed a commit that referenced this issue Oct 19, 2018
Work around for #29675.

(cherry picked from commit 2a9271a)
KristofferC pushed a commit that referenced this issue Feb 11, 2019
Work around for #29675.

(cherry picked from commit 2a9271a)
KristofferC pushed a commit that referenced this issue Feb 20, 2020
Work around for #29675.

(cherry picked from commit 2a9271a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrays [a, r, r, a, y, s] performance Must go faster potential benchmark Could make a good benchmark in BaseBenchmarks
Projects
None yet
Development

No branches or pull requests

4 participants