我正在转换我自己的一些 vector 代数代码以使用优化的 boost uBLAS 库。但是,当我尝试执行 SymmetricMatrix-SparseVector 乘法时,我发现它比我自己的实现慢了大约 4 倍。 vector 大小通常在 0-500 左右,大约 70-80% 的条目为零。
这是我的代码
void CRoutines::GetA(double a[], double vectorIn[], int sparseVectorIndexes[], int vectorLength, int sparseLength)
{
compressed_vector<double> inVec (vectorLength, sparseLength);
for(int i = 0; i < sparseLength; i++)
{
inVec(sparseVectorIndexes[i]) = vectorIn[sparseVectorIndexes[i]];
}
vector<double> test = prod(inVec, matrix);
for(int i = 0; i < vectorLength; i++)
{
a[i] = test(i);
}
}
sparseVectorIndexes 存储输入 vector 非零值的索引,vectorLength 是 vector 的长度,sparseLength 是 vector 中非零值的个数。矩阵存储为对称矩阵 symmetric_matrix<double, lower>
.
我自己的实现是一个简单的嵌套循环迭代,其中矩阵只是一个二维 double 组:
void CRoutines::GetA(double a[], double vectorIn[], int sparseVectorIndexes[], int vectorLength, int sparseLength)
{
for (int i = 0; i < vectorLength; i++)
{
double temp = 0;
for (int j = 0; j < sparseLength; j++)
{
int row = sparseVectorIndexes[j];
if (row <= i) // Handle lower triangular sparseness
temp += matrix[i][row] * vectorIn[row];
else
temp += matrix[row][i] * vectorIn[row];
}
a[i] = temp;
}
为什么 uBLAS 慢了 4 倍?我没有正确编写乘法吗?或者是否有另一个库更适合这个?
编辑:如果我改用密集 vector 数组,uBLAS 只会慢 2 倍...
最佳答案
uBlas 在设计时并未将性能作为第一目标。有些库比 uBlas 快得多。参见例如http://eigen.tuxfamily.org/index.php?title=Benchmark
关于c++ - uBLAS 慢速矩阵-稀疏 vector 乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6330936/