【问题标题】:Java 8 Stream Matrix Multiplication 10X Slower Than For Loop?Java 8 流矩阵乘法比 For 循环慢 10 倍?
【发布时间】:2016-01-27 12:49:57
【问题描述】:

我创建了一个使用流执行矩阵乘法的模块。在这里能找到它: https://github.com/firefly-math/firefly-math-linear-real/

我尝试编写一个基准测试,以便将流循环实现与 Apache Commons Math 中相应的 for 循环实现进行比较。

基准模块在这里: https://github.com/firefly-math/firefly-math-benchmark

这里的实际基准是: https://github.com/firefly-math/firefly-math-benchmark/blob/master/src/main/java/com/fireflysemantics/benchmark/MultiplyBenchmark.java

当我在大小为 100X100 和 1000X1000 的矩阵上运行基准测试时,结果发现 Apache Commons Math(使用 for 循环)比相应的流实现快 10 倍(大约)。

# Run complete. Total time: 00:14:10

Benchmark                              Mode  Cnt      Score     Error      Units
MultiplyBenchmark.multiplyCM1000_1000  avgt   30   1040.804 ±  11.796  ms/op
MultiplyBenchmark.multiplyCM100_100    avgt   30      0.790 ±   0.010  ms/op
MultiplyBenchmark.multiplyFM1000_1000  avgt   30  11981.228 ± 405.812  ms/op
MultiplyBenchmark.multiplyFM100_100    avgt   30      7.224 ±   0.685  ms/op

我在基准测试中做错了吗(希望 :))?

我正在添加测试的方法,以便每个人都可以看到正在比较的内容。这是 Apache Commons Math Array2DRowRealMatrix.multiply() 方法:

/**
 * Returns the result of postmultiplying {@code this} by {@code m}.
 *
 * @param m matrix to postmultiply by
 * @return {@code this * m}
 * @throws DimensionMismatchException if
 * {@code columnDimension(this) != rowDimension(m)}
 */
public Array2DRowRealMatrix multiply(final Array2DRowRealMatrix m)
    throws DimensionMismatchException {
    MatrixUtils.checkMultiplicationCompatible(this, m);

    final int nRows = this.getRowDimension();
    final int nCols = m.getColumnDimension();
    final int nSum = this.getColumnDimension();

    final double[][] outData = new double[nRows][nCols];
    // Will hold a column of "m".
    final double[] mCol = new double[nSum];
    final double[][] mData = m.data;

    // Multiply.
    for (int col = 0; col < nCols; col++) {
        // Copy all elements of column "col" of "m" so that
        // will be in contiguous memory.
        for (int mRow = 0; mRow < nSum; mRow++) {
            mCol[mRow] = mData[mRow][col];
        }

        for (int row = 0; row < nRows; row++) {
            final double[] dataRow = data[row];
            double sum = 0;
            for (int i = 0; i < nSum; i++) {
                sum += dataRow[i] * mCol[i];
            }
            outData[row][col] = sum;
        }
    }

    return new Array2DRowRealMatrix(outData, false);
}

这是对应的流实现:

/**
 * Returns a {@link BinaryOperator} that multiplies {@link SimpleMatrix}
 * {@code m1} times {@link SimpleMatrix} {@code m2} (m1 X m2).
 * 
 * Example {@code multiply(true).apply(m1, m2);}
 * 
 * @param parallel
 *            Whether to perform the operation concurrently.
 * 
 * @throws MathException
 *             Of type {@code MATRIX_DIMENSION_MISMATCH__MULTIPLICATION} if
 *             {@code m} is not the same size as {@code this}.
 * 
 * @return the {@link BinaryOperator} that performs the operation.
 */
public static BinaryOperator<SimpleMatrix> multiply(boolean parallel) {

    return (m1, m2) -> {
        checkMultiplicationCompatible(m1, m2);

        double[][] a1 = m1.toArray();
        double[][] a2 = m2.toArray();

        Stream<double[]> stream = Arrays.stream(a1);
        stream = parallel ? stream.parallel() : stream;

        final double[][] result =
                stream.map(r -> range(0, a2[0].length)
                        .mapToDouble(i -> range(0, a2.length).mapToDouble(j -> r[j]
                                * a2[j][i]).sum())
                        .toArray()).toArray(double[][]::new);

        return new SimpleMatrix(result);
    };
}

TIA, 奥莱

【问题讨论】:

  • @Holger toArray 是一个简单的字段访问器。我得到similar results after having simplified the test。我的猜测是数据局部性和缓存未命中 - 可能还有更多...
  • 我发现结果仍然很奇怪。我会尽快运行该基准测试。
  • @assylias: DoubleStream.sum() 使用错误补偿算法,它可能比简单的求和循环更昂贵。但是,我不希望因子十。关于局部性,与 Apache 的数学库不同,您的循环变体对改善数据局部性没有任何作用。
  • @Holger 很好发现 - 不使用 DoubleStream::sum 可将性能提高 30% - 现在比率“仅”慢 6 倍,而原始版本慢 8 倍。
  • @assylias:您可以通过像在循环变体中那样将数组长度读入局部变量以及a2 (热点更容易消除不必要的重新读取循环变体中的a2 字段与通过捕获的this 实例重新读取字段的lambda 实例相比)。

标签: java math java-8 benchmarking java-stream


【解决方案1】:

看看DoublePipeline.toArray

public final double[] toArray() {
  return Nodes.flattenDouble((Node.OfDouble) evaluateToArrayNode(Double[]::new))
                    .asPrimitiveArray();
}

似乎首先创建了一个装箱数组,然后将其转换为原始数组。

【讨论】:

  • 这个函数似乎只是为了实现内部接口(或者可能会在调用boxed()时使用)。在进行逐步调试时,您会发现在这种情况下没有使用此生成器。返回的节点将包含一个double[] 数组,并且由于该流具有固定大小,asPrimitiveArray() 将直接返回它。
  • @Holger 感谢您的更新。我会看看它,如果它是错误的,请删除答案。也许速度变慢是由当时创建的大量流引起的。
  • 答案其实是不正确的。之后不再使用此生成器。它已通过 AP::evaluateToArrayNode -> AP::evaluate -> DP::makeNodeBuilder 被忽略。
猜你喜欢
  • 2020-07-13
  • 2013-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-22
  • 1970-01-01
  • 2018-11-29
  • 1970-01-01
相关资源
最近更新 更多