by Hamlin Li, Rivos
The Java landscape is constantly evolving, with performance being a key area of focus. One of the most exciting recent developments is the introduction of the Vector API, a feature that allows developers to write highly parallel and performant code by taking advantage of modern SIMD (Single Instruction, Multiple Data) architectures. However, to truly unlock the potential of the Vector API, especially for complex mathematical computations, a powerful and optimized math library is essential. This is where the SLEEF (SIMD Library for Evaluating Elementary Functions) comes into the picture.
This blog post delves into the recent efforts within the OpenJDK community to integrate the SLEEF library, a move that promises to significantly boost the performance of vector math operations in Java, particularly for the RISC-V architectures.
The Need for Speed: Vectorized Math in Java
Modern processors are equipped with vector instructions that can perform the same operation on multiple data points simultaneously. The Java Vector API provides a platform-agnostic way to express vector computations, which the JVM can then translate into these efficient hardware instructions. This is a game-changer for performance-intensive domains like:
- Machine Learning and AI
- Scientific Computing and Simulations
- Data Analysis and Signal Processing
- Graphics and Game Development
While the Vector API provides the framework, the efficient implementation of transcendental math functions (like sin, cos, log, etc.) for vectors is a complex task. This is where a specialized library like SLEEF is invaluable.
SLEEF: A High-Performance Math Engine
SLEEF is an open-source library that provides highly optimized SIMD implementations of a wide range of mathematical functions. By using SLEEF, the OpenJDK can offload the heavy lifting of vector math calculations to a library that is purpose-built for speed and accuracy on various hardware architectures.
The Integration Journey: A Collaborative Effort
The integration of SLEEF into the OpenJDK has been a multi-step process, involving several pull requests and a close collaboration between the OpenJDK and SLEEF communities. Here’s a look at the key milestones in this journey:
- Initial Steps: The effort began with the goal of optimizing vector math operations with a 3rd party library. Initial pull requests focused on integrating SLEEF as an external dependency. However, this approach presented challenges in terms of build and runtime dependencies.
- Bringing SLEEF In-House – PR #20781: To simplify the build process and for legal reasons, the decision was made to integrate the SLEEF generated headers directly into the OpenJDK. This was a significant step that ensured a more seamless and self-contained build process. A dedicated pull request was created to add the SLEEF source files to the JDK. (For some legal reason, this pr has to be integrated by Oracle employees)
- Leverage SLEEF on RISC-V – PR #21083: After bringing the SLEEF source files to the JDK, the corresponding math operations in vector API were intrisified to leverage the SLEEF vectorized implementation for RISC-V. We also built a bridge between the JDK vector API and SLEEF source, the bridge is responsible for wrapping SLEEF interfaces and supplying uniform interfaces to JDK, also doing some sanity checks after calling into SLEEF ( e.g. in JDK we maintain an invariant in java world that default dynamic rounding mode is RNE, so we need to make sure after calling into SLEEF the rounding mode is still RNE before return back to java world).
- Upstream Contributions – SLEEF PR #536, SLEEF PR #537: The collaboration didn’t stop at just consuming the SLEEF library. The OpenJDK developers also contributed back to the SLEEF project. These contributions included performance enhancements like enforcing inlining of critical functions and bug fixes to ensure smooth compilation. This is a testament to the power of open-source collaboration, where both projects benefit from each other’s expertise.
What This Means for Java developers
The integration of SLEEF into the OpenJDK brings several key benefits to Java developers:
- Out-of-the-Box Performance: You can now expect significantly faster vector math operations on RISC-V platforms without any special configuration.
- On average, it brings about 2.38x speedup.
- Enhanced Vector API: The Vector API becomes even more powerful and appealing for a wider range of applications that rely on heavy mathematical computations.
- Future-Proofing: As hardware evolves, the use of libraries like SLEEF ensures that the Java platform can keep pace with the latest advancements in SIMD technology.
The Road Ahead
The integration of the SLEEF library is a major step forward for high-performance computing in Java. It showcases the OpenJDK community’s commitment to pushing the boundaries of performance and embracing the latest hardware capabilities. As the Vector API continues to mature, we can expect to see even more exciting developments in this space, further solidifying Java’s position as a top-tier platform for performance-critical applications.