Skip to main content
QUICK REVIEW

[论文解读] Performance comparison between Java and JNI for optimal implementation of computational micro-kernels

Nassim Halli, Henri‐Pierre Charles|arXiv (Cornell University)|Dec 21, 2014
Parallel Computing and Optimization TechniquesComputer Science被引用 5
一句话总结

本文比较了使用 Java 和 JNI 实现计算微内核的性能表现,分析了 JIT 编译与通过 JNI 实现原生代码之间的性能权衡。研究发现,对于计算密集型微内核,JNI 由于具备更优的优化能力(如向量化、乱序执行)而优于 Java;但 JNI 调用开销限制了性能提升,除非每次调用的计算量超过某个阈值。此外,数据对齐方式和原生内存访问对性能有显著影响。

ABSTRACT

General purpose CPUs used in high performance computing (HPC) support a vector instruction set and an out-of-order engine dedicated to increase the instruction level parallelism. Hence, related optimizations are currently critical to improve the performance of applications requiring numerical computation. Moreover, the use of a Java run-time environment such as the HotSpot Java Virtual Machine (JVM) in high performance computing is a promising alternative. It benefits from its programming flexibility, productivity and the performance is ensured by the Just-In-Time (JIT) compiler. Though, the JIT compiler suffers from two main drawbacks. First, the JIT is a black box for developers. We have no control over the generated code nor any feedback from its optimization phases like vectorization. Secondly, the time constraint narrows down the degree of optimization compared to static compilers like GCC or LLVM. So, it is compelling to use statically compiled code since it benefits from additional optimization reducing performance bottlenecks. Java enables to call native code from dynamic libraries through the Java Native Interface (JNI). Nevertheless, JNI methods are not inlined and require an additional cost to be invoked compared to Java ones. Therefore, to benefit from better static optimization, this call overhead must be leveraged by the amount of computation performed at each JNI invocation. In this paper we tackle this problem and we propose to do this analysis for a set of micro-kernels. Our goal is to select the most efficient implementation considering the amount of computation defined by the calling context. We also investigate the impact on performance of several different optimization schemes which are vectorization, out-of-order optimization, data alignment, method inlining and the use of native memory for JNI methods.

研究动机与目标

  • 确定在 HotSpot JVM 环境下,高绩效计算(HPC)中计算微内核的最佳实现方式(纯 Java 与 JNI)。
  • 分析 JIT 编译的局限性(控制不足、优化减少)与 JNI 的较高调用开销但具备更深层次静态优化之间的性能权衡。
  • 量化算术强度、向量化、乱序执行、数据对齐和内存访问方式(堆内存 vs 原生内存)等因素对性能的影响。
  • 确定每次调用的浮点运算量(flop-per-invocation)阈值,使得 JNI 因具备更好的优化机会而比纯 Java 更高效。

提出的方法

  • 在支持 AVX 指令集的 x86-64 Sandy Bridge 系统上,对一组数值微内核(如数组加法、水平求和、Horner 多项式求值)进行基准测试。
  • 测量了在多种配置下的性能表现(以 flop/s 为单位):纯 Java、使用堆内存的 JNI、使用原生内存的 JNI,以及不同优化级别下的表现。
  • 使用算术强度(AI = 每次调用的浮点运算数 / 每次调用的内存访问量)对微内核进行分类,判断其为内存受限或 CPU 受限。
  • 对 JNI 代码采用静态编译(GCC),并分析向量化、乱序执行和内联优化的影响。
  • 通过改变数组对齐方式(8 字节对齐 vs 16 字节对齐)评估数据对齐的影响,并测量性能下降程度。
  • 通过禁用内联优化,比较 JNI 与 Java 方法调用的开销,从而测量调用开销。

实验结果

研究问题

  • RQ1在什么每次调用的浮点运算量(flop-per-invocation)阈值下,JNI 在微内核上优于纯 Java?
  • RQ2与 JIT 编译的 Java 相比,原生代码中的向量化和乱序执行如何提升性能?
  • RQ3在 Java 和 JNI 中,数据未对齐对向量化微内核的性能有何影响?
  • RQ4使用原生内存而非堆内存对 JNI 调用开销和性能有何影响?
  • RQ5方法内联在多大程度上能降低 JNI 方法调用的性能开销?

主要发现

  • 当每次调用的内存访问量低于 L3 缓存大小时,JNI 相较于纯 Java 可实现最高达 2 倍的性能提升,原因在于其能有效进行向量化和乱序执行优化。
  • 在 Horner 数据优先(data-1st)内核中,JNI 通过结合向量化和乱序执行,性能达到了 CPU 的峰值性能,而 JIT 无法对这类模式进行向量化优化。
  • 当一个数组为 8 字节对齐而另一个为 16 字节对齐时,数组加法内核的性能下降了 45%,原因在于未对齐内存访问。
  • JNI 调用开销主要来自堆内存访问:从 Java 堆中获取和释放数组指针的开销约为 40%,而使用原生内存时该开销显著降低。
  • 在 Java 方法中禁用内联后,性能下降约 20%,表明内联能显著缓解方法调用的开销。
  • 对于内存访问量较低的微内核,性能瓶颈在于调用开销,其中循环常量开销占主导地位;该开销在 JNI 中更为显著,因存在回调开销。

阅读本论文

在 Nubint 阅读器中打开原文,边读边就任意段落提问。

在 Nubint 中阅读本论文

本解读由 AI 根据论文原文生成,引用前请核对原文。