c++ - tbb::parallel_for 将 ‘const value_type’ 作为 ‘this’ 参数传递会丢弃限定符

标签 c++ parallel-processing tbb

最近开始学习TBB。我正在尝试使用 TBB 并行实现压缩稀疏行乘法(数据类型 std::complex<int> )。这是我的代码:

struct CSR {
    std::vector<std::complex<int>> values;
    std::vector<int> row_ptr={0};
    std::vector<int> cols_index;
    int rows;
    int cols;
    int NNZ;
};

std::vector<std::complex<int>> tbb_multiply(const CSR& A,
    const CSR& B) { // B here is transpose  
    
    std::vector<std::complex<int>> result(A.rows * B.rows, 0);
    tbb::parallel_for(0,A.rows,[=](int i){
        for (int j = A.row_ptr[i]; j < A.row_ptr[i + 1]; j++) {
            int Ai = A.cols_index[j];
            std::complex<int> Avalue = A.values[j];
            for (int k = 0; k < B.rows; k++) {
                std::complex < int > sum(0, 0);
                for (int l = B.row_ptr[k]; l < B.row_ptr[k + 1]; l++)
                    if (Ai == B.cols_index[l]) {
                        sum += Avalue * B.values[l];
                        break;
                    }
                if (sum != std::complex < int >(0, 0)) {
                    result[i * B.rows + k] = sum;
                }
            }
        }
    });
    return result;
}

编译时出现下一个错误:

error: passing ‘const value_type’ {aka ‘const std::complex’} as ‘this’ argument discards qualifiers [-fpermissive] result[i * B.rows + k] = sum;

最佳答案

您捕获按值,这使得结果 const(因为默认情况下lambda不是可变 )。由于您还希望返回结果,因此我建议您改为通过引用捕获:

tbb::parallel_for(0, A.rows,[&](int i){
//                           ^

关于c++ - tbb::parallel_for 将 ‘const value_type’ 作为 ‘this’ 参数传递会丢弃限定符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72408660/

相关文章:

c++ - 线程构建 block 与 MPI,哪个更适合 mt 需要?

c++ - 在 qt 中使用 intel 的 tbb

c++ - Cocos2d-x runAction 报错

c++ - 无效转换为 C++0x 中的非常量引用...std::pair 中的错误?

Python Joblib 并行 : How to combine results per worker?

algorithm - 我怎样才能使这个矢量枚举代码更快?

python - 为什么pip安装tbb失败

c++ - 我需要实现自己的 QAbstractTableModel 吗?

c++ - 无论我使用什么,我的输入都会被跳过

java - 两个双核处理器系统的并行性