LoopVectorization.jl icon indicating copy to clipboard operation
LoopVectorization.jl copied to clipboard

LoadError on simple example

Open droodman opened this issue 4 years ago • 1 comments
trafficstars

I'm struggling to understand why this function won't load:

using LoopVectorization
function f(X::Matrix{Float64})
  retval = [1]
  @turbo for i ∈ eachindex(X)
    retval[1] += X[i]
  end
end

When I try to execute this code in Julia 1.6.2--not execute the f() function--I get:

ERROR: LoadError: LoadError: KeyError: key Symbol("###RHS###5###") not found
Stacktrace:
 [1] getindex
   @ .\dict.jl:482 [inlined]
 [2] add_reduction!(ls::LoopVectorization.LoopSet, var::Symbol, reduceddeps::Vector{Symbol}, deps::Vector{Symbol}, vparents::Vector{LoopVectorization.Operation}, reduction_ind::Int64, elementbytes::Int64, instr::LoopVectorization.Instruction)

droodman avatar Aug 28 '21 01:08 droodman

There's another issue like this somewhere, but I couldn't find it with a brief check. You can do some variant of:

function f(X::Matrix{Float64})
  retval = 1
  @turbo for i ∈ eachindex(X)
    retval += X[i]
  end
   [retval]
end

Basically, if you're accumulating into an array, it wants that array to be indexed by one of the loops. I could manually add the transform to go from

function f(X::Matrix{Float64})
  retval = [1]
  @turbo for i ∈ eachindex(X)
    retval[1] += X[i]
  end
end

to

function f(X::Matrix{Float64})
  retval = [1]
  retval_tmp = retval[1]
  @turbo for i ∈ eachindex(X)
    retval_tmp += X[i]
  end
  retval[1] = retval_tmp
end

or some variant of this, but it seems easy enough to do it manually that spending time on that is a very low priority for me.

A PR applying this transform would be welcome, however.

chriselrod avatar Aug 28 '21 02:08 chriselrod