鉴于稀疏向量SparseVectorDouble
(基本上是索引和值数组)和向量乘法的一些固定实现,我想实现并行化矩阵向量乘法。我尝试了基于全新流框架的以下方法。
ArrayList<SparseVectorDouble> rows;
//...
public double[] multiply(final double[] vector, final double[] result)
{
final IntToDoubleFunction multiply = (index) -> rows.get(index).multiply(vector);
Arrays.parallelSetAll(result, multiply);
return result;
}
然而,对于巨大的矩阵(16M x 16M矩阵;每行约10个条目),使用具有超线程的双核CPU的加速仅为20%左右。
编辑#1: SparseVectorDouble类中的向量乘法基本上如下实现:
protected double multiplyThisDefaultZero(final double[] other) {
assert defaultValue == 0.0;
if (!(size() <= other.length)) {
throw new IllegalArgumentException("vector of matching size expected");
}
double sumXY = 0;
for (int internalIndex = 0; internalIndex < internalSize; internalIndex ++) {
final int index = indices[internalIndex];
sumXY += values[internalIndex] * other[index];
}
return sumXY;
}