为什么Java SIMD(巴拿马)比标量慢?

时间:2018-06-07 11:48:56

标签: java performance simd project-panama

我已经在Java中使用了针对SIMD的intel tutoriel Panama。我想对数组做一些简单的操作:

这里是网站上的标量和矢量循环:

public static void scalarComputation(float[] a, float[] b, float[] c) {
    for (int i = 0; i < a.length; i++) {
        c[i] = (a[i] * a[i] + b[i] * b[i]) * - 1.0f;
    }
}

public static void vectorComputation(float[] a, float[] b, float[] c) {
    int i = 0;
    for (; i < (a.length & ~(species.length() - 1));
         i += species.length()) {
        FloatVector<Shapes.S256Bit> va = speciesFloat.fromArray(a, i);
        FloatVector<Shapes.S256Bit> vb = speciesFloat.fromArray(b, i);
        FloatVector<Shapes.S256Bit> vc = va.mul(va).
                add(vb.mul(vb)).
                neg();
        vc.intoArray(c, i);
    }

    for (; i < a.length; i++) {
        c[i] = (a[i] * a[i] + b[i] * b[i]) * -1.0f;
    }
}

当我测量时间时:

float [] A = new float[N];
float [] B = new float[N];
float [] C = new float[N];

for(int i = 0; i < C.length; i++)
{
    C[i] = 2.0f;
    A[i] = 2.0f;
    B[i] = 2.0f;
}

long start = System.nanoTime();
for(int i = 0; i < 200; i++)
{
    //scalarComputation(C,A,B);
    //vectorComputation(C,A,B);
}
long end = System.nanoTime();        
System.out.println(end - start);

我总是得到更高的矢量时间而不是标量。 你知道为什么吗? 谢谢。

1 个答案:

答案 0 :(得分:2)

您使用了错误的分支:从vectorIntrinsics branch开始构建。您还需要使用JMH进行正确的测量-here是为Vector API编写的一些第三方基准测试。

有关Vector API对点积计算的影响,请参见here

相关问题