【发布时间】:2011-06-13 13:17:37
【问题描述】:
我正在转换我自己的一些向量代数代码以使用优化的 boost uBLAS 库。然而,当我尝试做一个 SymmetricMatrix-SparseVector 乘法时,我发现它比我自己的实现慢了大约 4 倍。向量大小通常在 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 存储输入向量的非零值的索引,vectorLength 是向量的长度,sparseLength 是向量中非零的个数。矩阵存储为对称矩阵symmetric_matrix<double, lower>。
我自己的实现是一个简单的嵌套循环迭代,其中矩阵只是一个二维双数组:
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 倍?我没有正确写乘法吗?还是有其他图书馆更适合这个?
编辑:如果我改用密集向量数组,那么 uBLAS 只会慢 2 倍...
【问题讨论】:
-
如果这是在 Visual Studio 中,你是否检查过你是否在调试模式下编译它?
-
肯定会编译到 Release,优化全部开启,而不是在 IDE 中测试。
-
请发布扩展代码 -
vectorIn来自哪里,它的类型是什么?在第二个非 uBlas 代码中创建了哪些对象副本?请发布您正在测量的所有代码以得出 4 倍减速数字。 -
好的,我发布了额外的代码。一些额外的信息:这段代码被编译成一个 dll 并在 C# 中调用,但我认为这根本不会有任何区别。
-
另外,
matrix是triangular_matrix<double, lower>?
标签: c++ boost linear-algebra blas ublas