c++ - uBLAS 慢速矩阵-稀疏 vector 乘法

标签 c++ boost linear-algebra blas ublas

我正在转换我自己的一些 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/

相关文章:

c++ - boost 中 is_same 和 mpl::same_as 的区别

matrix - 减少 Julia 中矩阵乘法的分配数量?

c++ - Qt 顶点数组不适用于 QImage

c++ - WC_NO_BEST_FIT_CHARS 未定义?

python - Python 3 中是否有类似于 C++ 中的 getchar() 的内置函数?

c++ - 将指针容器转换为智能指针?

c++ - VS2013 使用 NuGet boost

java - 在 Java 中使用 Ojalgo 求解线性系统

matlab - MATLAB 是否优化 diag(A*B)?

c++ - 随应用程序一起运送 libstdc++.so.6