是否有任何 JVM 的 JIT 编译器生成使用矢量化浮点指令的代码?

2022-01-09 00:00:00 floating-point vectorization jit java sse

假设我的 Java 程序的瓶颈确实是一些紧密循环来计算一堆矢量点积.是的,我已经分析过了,是的,它是瓶颈,是的,它很重要,是的,算法就是这样,是的,我已经运行 Proguard 来优化字节码,等等.

Let's say the bottleneck of my Java program really is some tight loops to compute a bunch of vector dot products. Yes I've profiled, yes it's the bottleneck, yes it's significant, yes that's just how the algorithm is, yes I've run Proguard to optimize the byte code, etc.

这项工作本质上是点积.如,我有两个 float[50] ,我需要计算成对产品的总和.我知道处理器指令集的存在是为了快速批量执行此类操作,例如 SSE 或 MMX.

The work is, essentially, dot products. As in, I have two float[50] and I need to compute the sum of pairwise products. I know processor instruction sets exist to perform these kind of operations quickly and in bulk, like SSE or MMX.

是的,我可以通过在 JNI 中编写一些本机代码来访问这些.事实证明,JNI 调用非常昂贵.

Yes I can probably access these by writing some native code in JNI. The JNI call turns out to be pretty expensive.

我知道你不能保证 JIT 会编译什么,什么不编译.有没有人曾经听说过使用这些指令的 JIT 生成代码?如果是这样,Java 代码有什么东西可以帮助它以这种方式编译吗?

I know you can't guarantee what a JIT will compile or not compile. Has anyone ever heard of a JIT generating code that uses these instructions? and if so, is there anything about the Java code that helps make it compilable this way?

可能是不";值得一问.

Probably a "no"; worth asking.

推荐答案

所以,基本上,你希望你的代码运行得更快.JNI 就是答案.我知道你说它对你不起作用,但让我告诉你你错了.

So, basically, you want your code to run faster. JNI is the answer. I know you said it didn't work for you, but let me show you that you are wrong.

这里是 Dot.java:

import java.nio.FloatBuffer;
import org.bytedeco.javacpp.*;
import org.bytedeco.javacpp.annotation.*;

@Platform(include = "Dot.h", compiler = "fastfpu")
public class Dot {
    static { Loader.load(); }

    static float[] a = new float[50], b = new float[50];
    static float dot() {
        float sum = 0;
        for (int i = 0; i < 50; i++) {
            sum += a[i]*b[i];
        }
        return sum;
    }
    static native @MemberGetter FloatPointer ac();
    static native @MemberGetter FloatPointer bc();
    static native @NoException float dotc();

    public static void main(String[] args) {
        FloatBuffer ab = ac().capacity(50).asBuffer();
        FloatBuffer bb = bc().capacity(50).asBuffer();

        for (int i = 0; i < 10000000; i++) {
            a[i%50] = b[i%50] = dot();
            float sum = dotc();
            ab.put(i%50, sum);
            bb.put(i%50, sum);
        }
        long t1 = System.nanoTime();
        for (int i = 0; i < 10000000; i++) {
            a[i%50] = b[i%50] = dot();
        }
        long t2 = System.nanoTime();
        for (int i = 0; i < 10000000; i++) {
            float sum = dotc();
            ab.put(i%50, sum);
            bb.put(i%50, sum);
        }
        long t3 = System.nanoTime();
        System.out.println("dot(): " + (t2 - t1)/10000000 + " ns");
        System.out.println("dotc(): "  + (t3 - t2)/10000000 + " ns");
    }
}

Dot.h:

float ac[50], bc[50];

inline float dotc() {
    float sum = 0;
    for (int i = 0; i < 50; i++) {
        sum += ac[i]*bc[i];
    }
    return sum;
}

我们可以通过 JavaCPP 使用这个命令来编译和运行它:

We can compile and run that with JavaCPP using this command:

$ java -jar javacpp.jar Dot.java -exec

使用 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz、Fedora 30、GCC 9.1.1 和 OpenJDK 8 或 11,我得到这样的输出:

With an Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz, Fedora 30, GCC 9.1.1, and OpenJDK 8 or 11, I get this kind of output:

dot(): 39 ns
dotc(): 16 ns

或大约快 2.4 倍.我们需要使用直接 NIO 缓冲区而不是数组,但是 HotSpot 可以像访问数组一样快地访问直接 NIO 缓冲区.另一方面,在这种情况下,手动展开循环并不能显着提升性能.

Or roughly 2.4 times faster. We need to use direct NIO buffers instead of arrays, but HotSpot can access direct NIO buffers as fast as arrays. On the other hand, manually unrolling the loop does not provide a measurable boost in performance, in this case.

相关文章