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

Vectorized math functions #106

Open
dubosipsl opened this issue Mar 9, 2023 · 5 comments
Open

Vectorized math functions #106

dubosipsl opened this issue Mar 9, 2023 · 5 comments

Comments

@dubosipsl
Copy link

Thanks for developing SIMD.jl which I use in a number-crunching project of mine for explicit vectorization.

When experimenting with SIMD I realized that many math functions (e.g. exp, log), when given a SIMD.Vec, would simply call Base for each element of the Vec rather than offering a vectorized implementation. This makes Julia look bad compared to, say, Fortran. Please correct me if I am wrong.

After some search I found that SLEEFPirates.jl offers such vectorized implementations, sometimes with slightly degraded accuracy as with @fastmath.

So I developed some code to use SLEEFPirates when calling exp_fast etc. on SIMD.Vec arguments. The code is here :
https://gitlab.in2p3.fr/ipsl/lmd/intro/geofluids/gfpackages/-/blob/master/Computing/GFLoops/julia/src/CPU/vector_math.jl
This works well for my purposes but I feel this would rather be part of SIMD.jl, possibly with a weak dependency on SLEEFPirates ?

I have also some code providing a few non-mathematical "missing" features (mostly array indexing) here :
https://gitlab.in2p3.fr/ipsl/lmd/intro/geofluids/gfpackages/-/blob/master/Computing/GFLoops/julia/src/CPU/simd.jl

Any thoughts, including critics on my code, are welcome.

@eschnett
Copy link
Owner

eschnett commented Mar 9, 2023

Thanks! I'd be very interested in have such code in SIMD.jl. Unfortunately I might not have time to look at that in detail in then near future. Maybe you or someone else has time to turn this into a pull request?

A dependency on SLEEFPirates.jl or a similar package sounds fine to me.

@KristofferC
Copy link
Collaborator

KristofferC commented Mar 9, 2023

A dependency on SLEEFPirates.jl or a similar package sounds fine to me.

To me that really sounds like the reverse of what should be done. The design of this package is very "clean" and limited in scope and if anything, other packages should depend on it. It provides:

  • Direct access to the platform generic vector LLVM intrinsics with the NTuple{N, VecElement{T}} type
  • A Julia interface on top of these LLVM intrinsics with Vec.

Depending on SLEEFPirates would pull in VectorizationBase.jl which is a direct "competitor" to SIMD.jl. Just look at all the code in https://github.com/JuliaSIMD/VectorizationBase.jl/tree/master/src. It's an endless amount of bloat. If you add a dependency on VectorizationBase the whole point of this package falls apart and you might as well just use VectorizationBase in the first place.

@dubosipsl
Copy link
Author

I see you concern. With what I did vectorized functions go to SLEEFPirates then VectorizationBase, and bypass SIMD.jl .

Proceeding the other way round would be to make SLEEFPirates work with SIMD.Vec . I have taken a brief look at the SLEEFPirates code and this looks beyond my capacities. There are a lot of optimized variants depending on architecture and I get lost very quickly.

I have also looked at SLEEF.jl . The code is a lot simpler. After some modifications I have managed to let exp(x) work with SIMD.Vec. It may be possible to do that for the other functions too with some effort. It would require a few extra functions in SIMD.jl too(for instance, unsafe_trunc for SIMD.Vec).

There is no urgency on my side. We can discuss further this topic if you are interested.

@dubosipsl
Copy link
Author

Following up. I have turned the aforementioned vector_math.jl into a standalone package depending on SIMD, VectorizationBase and SLEEFPirates as advocated by @KristofferC .
https://gitlab.in2p3.fr/ipsl/lmd/intro/geofluids/gfpackages/-/tree/master/Computing/SIMDFunctions

Now import SIMDFunctions provides vectorized versions of cos(x::SIMD.Vec) and the like.

Ideally it would be registered with the general registry, but I have zero experience with that. Would you be willing to help ? If so I could improve the doc. Probably tests are needed too.

@dubosipsl
Copy link
Author

dubosipsl commented Oct 23, 2023

Yet another possibility is to let SLEEFPirates be a weak dependency ( Julia 1.9). See #117. Benefits:

  • avoids a strong dependency
  • avoids type piracy : having the functionality in a third package SIMDFunctions means that it must commit type piracy
  • avoid adding another super small package to the general registry, and maintaining it

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

3 participants