JuliaSIMD / JuliaSIMD/LoopVectorization.jl

How to handle user defined functions?

Ouverte
#55 2 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
Langage dominant
Julia
Étoiles
789
Forks
73
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

I get the following error
```julia
ERROR: MethodError: no method matching calc_A(::VectorizationBase.SVec{16,Int16})
```
where `calc_A` is a user defined function. Do I need to implement `calc_A(::VectorizationBase.SVec{16,Int16})` myself? Why isn't there an error for the other user defined functions? My first guess was because they are inlined, but `calc_A` should be inlined, too.

Here is the code for that:
```julia
@inline function calc_A(x::Int16)
p = 15; r = 1; A = Int16(23170); C = Int16(-425)
n = 7
a = 7
x² = (x * x) >> n
rounding = one(x) << (p - a - 1)
(A + rounding + (x² * C) >> r) >> (p - a)
end
@inline function calc_B(x::Int16)
p = 14; r = 3; B = Int16(-17790); D = Int16(351)
n = 7
a = 7
x² = (x * x) >> n
rounding = one(x) << (p - a - 1)
(rounding + x * (B + (x² * D) >> r) >> n) >> (p - a)
end
@inline function get_first_bit_sign(x)
n = 7
mysign(x << (sizeof(x) * 8 - n - 1))
end
@inline function get_second_bit_sign(x)
n = 7
mysign(x << (sizeof(x) * 8 - n - 2))
end
@inline function get_quarter_angle(x)
n = 7
x & (one(x) << n - one(x)) - one(x) << (n - 1)
end
@inline mysign(x) = 2 * signbit(x) - 1

function gen_sincos!(sins, coss, phases)
@avx for i = 1:2500
first_bit_sign = get_first_bit_sign(phases[i])
second_bit_sign = get_second_bit_sign(phases[i])
quarter_angle = get_quarter_angle(phases[i])
A = calc_A(quarter_angle)
B = calc_B(quarter_angle)

coss[i] = second_bit_sign * (first_bit_sign * A + B)
sins[i] = second_bit_sign * (A - first_bit_sign * B)
end
end
sins = Vector{Int16}(undef, 2500)
coss = Vector{Int16}(undef, 2500)
phases = Vector{Int16}(undef, 2500)
gen_sincos!(sins, coss, phases)
```
**EDIT:** Alright I found the solution myself: If I write `function calc_A(x)` instead of `function calc_A(x::Int16)`, it will work. So should I omit the declaration of `x`? What if I have a different function for `x::Int32`?

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Piste de recherche

Start with gen_sincos! and the @avx loop, then compare how calc_A is defined with the other user-defined functions in the example. Check how the vectorized loop dispatches calc_A for quarter_angle and verify the behavior for Int16 and Int32 inputs. Done means documenting the required method definitions or confirming the supported dispatch pattern.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
julia
Domaine
performance
Type d'issue
Bug
Difficulté
3/5
Temps estimé
1-2 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.