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

Error with ifelse #220

Closed
jebej opened this issue Mar 24, 2021 · 5 comments
Closed

Error with ifelse #220

jebej opened this issue Mar 24, 2021 · 5 comments

Comments

@jebej
Copy link

jebej commented Mar 24, 2021

On LoopVectorization 0.12.9:

using LoopVectorization

function _real_sqrt(x,y)
    t = sqrt(2*(abs(x) + hypot(x, y)))
    return ifelse(x  zero(x), t/2, abs(y)/t)
end

x = (rand(1000).-0.5).*10;
y = (rand(1000).-0.5).*10;

_real_sqrt.(x,y) # works

@avx _real_sqrt.(x,y) # error

The error is:

ERROR: TypeError: non-boolean (VectorizationBase.Mask{4,UInt8}) used in boolean context
Stacktrace:
 [1] _real_sqrt(::VectorizationBase.Vec{4,Float64}, ::VectorizationBase.Vec{4,Float64}) at .\REPL[2]:3
 [2] macro expansion at C:\Users\Jeremy\.julia\packages\LoopVectorization\eSjbW\src\reconstruct_loopset.jl:623 [inlined]
 [3] _avx_! at C:\Users\Jeremy\.julia\packages\LoopVectorization\eSjbW\src\reconstruct_loopset.jl:623 [inlined]
 [4] macro expansion at C:\Users\Jeremy\.julia\packages\LoopVectorization\eSjbW\src\broadcast.jl:393 [inlined]
 [5] vmaterialize! at C:\Users\Jeremy\.julia\packages\LoopVectorization\eSjbW\src\broadcast.jl:393 [inlined]
 [6] vmaterialize(::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Nothing,typeof(_real_sqrt),Tuple{Array{Float64,1},Array{Float64,1}}}, ::Val{:Main}, ::Val{(true, 0, 0, true, 0, 32, 15, 64, 32768, 262144, 12582912, 0x0000000000000001)}) at C:\Users\Jeremy\.julia\packages\LoopVectorization\eSjbW\src\broadcast.jl:467
 [7] top-level scope at REPL[6]:1
@chriselrod
Copy link
Member

chriselrod commented Mar 24, 2021

Use IfElse.ifelse.
Base.ifelse is a builtin function, so I can't overload it.

julia> using LoopVectorization, IfElse

julia> function _real_sqrt(x,y)
           t = sqrt(2*(abs(x) + hypot(x, y)))
           return IfElse.ifelse(x  zero(x), t/2, abs(y)/t)
       end
_real_sqrt (generic function with 1 method)

julia> x = (rand(1000).-0.5).*10;

julia> y = (rand(1000).-0.5).*10;

julia> _real_sqrt.(x,y)  @avx _real_sqrt.(x,y)
true

julia> @btime @. $z = _real_sqrt($x, $y);
  9.486 μs (0 allocations: 0 bytes)

julia> @btime @avx @. $z = _real_sqrt($x, $y);
  2.335 μs (0 allocations: 0 bytes)

@chriselrod
Copy link
Member

Here is the related issue:
JuliaLang/julia#32844

@chriselrod
Copy link
Member

Although I guess this should be solvable via the abstract interpreter. So reopening the issue'd be fine to track that.
That's still a ways away, though.

@jebej
Copy link
Author

jebej commented Mar 24, 2021

Thanks!

@chriselrod
Copy link
Member

For more of an explanation, vectorization/SIMD means doing multiple operations per instruction. LoopVectorization/VectorizationBase represent that with types like:

julia> using VectorizationBase

julia> vx = Vec(ntuple(_ -> 2rand(), VectorizationBase.pick_vector_width(Float64))...)
Vec{8, Float64}<1.5219234039045744, 1.8300372474242277, 1.5738687191737752, 0.18406707750515228, 0.6740398193024504, 0.253508594387144, 1.715660422824369, 1.6706874133577432>

julia> vx > abs2(vx)
Mask{8,Bit}<0, 0, 0, 1, 1, 1, 0, 0>

This relies on overloading a bunch of functions to make them work as expected.
But unfortunately I can't overload branches (meaning if and ? won't work), and I can't overload builtin functions like ifelse either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants