我要做重矩阵乘法
由于我的应用程序需要高性能,所以我决定使用JBLAS。
但是,我发现JBLAS在测试中比简单的for循环慢。
double[][] M = new double[3000][3100];
double[] a = new double [3100];
double[] b = new double[3000];
for(double[] row: M){
Arrays.fill(row, 3.343);
}
Arrays.fill(a, 1.324);
DoubleMatrix M1 = new DoubleMatrix(M);
DoubleMatrix a1 = new DoubleMatrix(a);
DoubleMatrix b1= new DoubleMatrix(b);
//1. Simple for loop : 366 ms
for(int i=0; i<3000; i++){
for(int j=0; j<3100; j++){
b[i] = b[i] + a[j]*M[i][j];
}
}
// 2. JBLAS : 1190 ms
b1 = M1.mmul(a1);虽然它们是相同的计算,但JBLAS比简单的for循环慢3倍。
是因为我的错误吗?还是其他原因?
谢谢!
发布于 2015-07-16 08:26:26
我自己找到了解决办法,并分享它!
原因是本机代码涉及复制数据,因此像矩阵向量乘法这样的操作不能从本机代码中受益。
因此,如果mmul函数识别矩阵*向量乘法,则使用Java代码。
您可以在这里获得更多信息,https://groups.google.com/forum/#!topic/jblas-users/HY2acEE3Y10
https://stackoverflow.com/questions/31448782
复制相似问题