我正在转换一些我自己的矢量代数代码,以使用优化的boost uBLAS库。但是,当我尝试执行SymmetricMatrix-SparseVector乘法时,发现它比我自己的实现慢大约4倍。矢量大小通常在0-500左右,大约70-80%的条目为零。uBLAS慢矩阵 - SparseVector乘法
这是我的代码
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存储输入矢量的非零值的索引,vectorLength是矢量的长度,和sparseLength是非零的载体中的数。该矩阵被存储为对称矩阵symmetric_matrix<double, lower>
。
我自己的实现是一个简单的嵌套循环迭代,其中矩阵只是一个2D双阵列:
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;
}
}
为什么是4倍的uBLAS库慢?我不是在正确地写乘法吗?还是有另一个图书馆更适合这个?
编辑:如果我使用一个密集的矢量阵列,而不是那么的uBLAS只有2倍慢...
如果这是在Visual Studio中,你是否检查你是否在调试模式下编译它? – Jacob 2011-06-13 13:34:21
绝对编译为Release,优化全部,并且不在IDE中测试。 – 2011-06-13 13:40:47
请张贴扩展代码 - vectorIn'从哪里来,它的类型是什么?在第二个非uBlas代码中创建了哪些对象副本?请张贴您正在测量的所有代码,以获得4倍速度减慢数字。 – 2011-06-13 13:47:57