[ANN] SIMDMathFunctions.jl : Fast vectorized mathematical functions for SIMD.jl , using SLEEFPirates.jl

Feedback welcome, especially on the package name which I intend to register in a matter of days. The name could be SIMDFunctions for instance.

From the README (GitHub - ClimFlows/SIMDMathFunctions.jl):

] add https://github.com/ClimFlows/SIMDFastMath


using SIMD: VecRange
using SIMDFastMath: is_supported, is_fast, fast_functions
using BenchmarkTools

function exp!(xs::Vector{T}, ys::Vector{T}) where {T}
    @inbounds for i in eachindex(xs,ys)
        xs[i] = @fastmath exp(ys[i])

function exp!(xs::Vector{T}, ys::Vector{T}, ::Val{N}) where {N, T}
    @assert length(ys) == length(xs)
    @assert length(xs) % N == 0
    @assert is_supported(@fastmath exp)
    @inbounds for istart in 1:N:length(xs)
        i = VecRange{N}(istart)
        xs[i] = @fastmath exp(ys[i])

y=randn(Float32, 1024*1024); x=similar(y);

@benchmark exp!($x, $y)
@benchmark exp!($x, $y, Val(8))
@benchmark exp!($x, $y, Val(16))
@benchmark exp!($x, $y, Val(32))


SIMDMathFunctions is clear.


Your README gives erf as an example for adding support for a function without an optimized implementation.

erf does have one, though:


Sounds like a better name indeed.

Hum, I did not catch that one, thanks. However one difficulty is that erf is not in Base, it is provided by SpecialFunctions.jl . SIMDFastMaths could depend on SpecialFunctions and implement erf for SIMD.Vec arguments by calling VectorizationBase.verf, but it looks backwards to me. Should a vectorized erf not rather be part of SpecialFunctions.jl ?

Anyway I will improve the example in the README.

That’s a usecase for a pkg extension.
See here for more details.
But the short of it is, you can write code in one package that only gets loaded when another package does.

LoopVectorization does this for SpecialFunctions, so that Base.erf(::VectorizatoinBase.AbstractSIMD) gets defined to use VextorizationBase.verf

This really should have been an extension to VectorizationBase instead of LoopVectorization. However, in the pre-pkg extension era, this code was loaded unconditionally, and I didn’t want VB to pay the cost. It should be moved, but that’d make version compatibility a little awkward.


How about making this an extension of SpecialFunctions instead ?

I have no permissions there. You’d have to ask the maintainers.

Happy to give you access to SpecialFunctions.jl. What’s your github ID? Alternately, open an issue on the repo and ping me - ViralBShah.

Thanks for offering. I guess I will first rename and register the package. Then I can open a PR for a weak dep of SpecialFunctions. This would however cover only erf, among the many functions provided. Is it worth it ?

The package is now registered as SIMDMathFunctions

1 Like