【发布时间】: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
当我在大小为 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